+ foreach (array('status_network', 'status_network_tag', 'unavailable_status_network') as $table) {
+ $config['db']['table_'.$table] = $dbname;
+ }
+
+ if (class_exists('Memcache')) {
+ self::$cache = new Memcache();
+
+ // If we're a parent command-line process we need
+ // to be able to close out the connection after
+ // forking, so disable persistence.
+ //
+ // We'll turn it back on again the second time
+ // through which will either be in a child process,
+ // or a single-process script which is switching
+ // configurations.
+ $persist = php_sapi_name() != 'cli' || self::$cacheInitialized;
+ if (!is_array($servers)) {
+ $servers = array($servers);
+ }
+ foreach($servers as $server) {
+ $parts = explode(':', $server);
+ $server = $parts[0];
+ if (count($parts) > 1) {
+ $port = $parts[1];
+ } else {
+ $port = 11211;
+ }
+ self::$cache->addServer($server, $port, $persist);
+ }
+ self::$cacheInitialized = true;
+ }
+
+ self::$base = $dbname;
+ }
+
+ static function cacheKey($k, $v) {
+ return 'statusnet:' . self::$base . ':status_network:'.$k.':'.$v;
+ }
+
+ static function memGet($k, $v)
+ {
+ if (!self::$cache) {
+ return self::getKV($k, $v);
+ }
+
+ $ck = self::cacheKey($k, $v);
+
+ $sn = self::$cache->get($ck);
+
+ if (empty($sn)) {
+ $sn = self::getKV($k, $v);
+ if (!empty($sn)) {
+ self::$cache->set($ck, clone($sn));
+ }
+ }
+
+ return $sn;
+ }
+
+ function decache()
+ {
+ if (self::$cache) {
+ $keys = array('nickname', 'hostname', 'pathname');
+ foreach ($keys as $k) {
+ $ck = self::cacheKey($k, $this->$k);
+ self::$cache->delete($ck);
+ }
+ }
+ }
+
+ function update($orig=null)
+ {
+ if (is_object($orig)) {
+ $orig->decache(); # might be different keys
+ }
+ return parent::update($orig);
+ }
+
+ /**
+ * DB_DataObject doesn't allow updating keys (even non-primary)
+ */
+ function updateKeys(&$orig)
+ {
+ $this->_connect();
+ foreach (array('hostname', 'pathname') as $k) {
+ if (strcmp($this->$k, $orig->$k) != 0) {
+ $parts[] = $k . ' = ' . $this->_quote($this->$k);
+ }
+ }
+ if (count($parts) == 0) {
+ // No changes
+ return true;
+ }
+
+ $toupdate = implode(', ', $parts);
+
+ $table = common_database_tablename($this->tableName());
+ $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
+ ' WHERE nickname = ' . $this->_quote($this->nickname);
+ $orig->decache();
+ $result = $this->query($qry);
+ $this->decache();
+
+ return $result;
+ }
+
+ function delete()
+ {
+ $this->decache(); # while we still have the values!
+ return parent::delete();
+ }
+
+ /**
+ * @param string $servername hostname
+ * @param string $wildcard hostname suffix to match wildcard config
+ * @return mixed Status_network or null
+ */
+ static function getFromHostname($servername, $wildcard)
+ {
+ $sn = null;
+ if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
+ // special case for exact match
+ if (0 == strcasecmp($servername, $wildcard)) {
+ $sn = self::memGet('nickname', '');
+ } else {
+ $parts = explode('.', $servername);
+ $sn = self::memGet('nickname', strtolower($parts[0]));
+ }
+ } else {
+ $sn = self::memGet('hostname', strtolower($servername));
+
+ if (empty($sn)) {
+ // Try for a no-www address
+ if (0 == strncasecmp($servername, 'www.', 4)) {
+ $sn = self::memGet('hostname', strtolower(substr($servername, 4)));
+ }
+ }
+ }
+ return $sn;