Allow to disable specific groups in $wgDebugLogGroups
[lhc/web/wiklou.git] / includes / site / SiteSQLStore.php
index 46ce91d..11141e0 100644 (file)
@@ -47,6 +47,11 @@ class SiteSQLStore implements SiteStore {
         */
        private $cacheKey = null;
 
+       /**
+        * @var int
+        */
+       private $cacheTimeout = 3600;
+
        /**
         * @since 1.21
         *
@@ -88,6 +93,8 @@ class SiteSQLStore implements SiteStore {
         * @return String The cache key.
         */
        protected function getCacheKey() {
+               wfProfileIn( __METHOD__ );
+
                if ( $this->cacheKey === null ) {
                        $type = 'SiteList#' . SiteList::getSerialVersionId();
                        $source = $this->sitesTable->getName();
@@ -99,6 +106,7 @@ class SiteSQLStore implements SiteStore {
                        $this->cacheKey = wfMemcKey( "$source/$type" );
                }
 
+               wfProfileOut( __METHOD__ );
                return $this->cacheKey;
        }
 
@@ -112,6 +120,8 @@ class SiteSQLStore implements SiteStore {
         * @return SiteList
         */
        public function getSites( $source = 'cache' ) {
+               wfProfileIn( __METHOD__ );
+
                if ( $source === 'cache' ) {
                        if ( $this->sites === null ) {
                                $cache = wfGetMainCache();
@@ -128,6 +138,7 @@ class SiteSQLStore implements SiteStore {
                        $this->loadSites();
                }
 
+               wfProfileOut( __METHOD__ );
                return $this->sites;
        }
 
@@ -141,6 +152,8 @@ class SiteSQLStore implements SiteStore {
         * @return Site
         */
        protected function siteFromRow( ORMRow $siteRow ) {
+               wfProfileIn( __METHOD__ );
+
                $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
 
                $site->setGlobalId( $siteRow->getField( 'global_key' ) );
@@ -171,15 +184,51 @@ class SiteSQLStore implements SiteStore {
                        $site->setExtraConfig( $siteRow->getField( 'config' ) );
                }
 
+               wfProfileOut( __METHOD__ );
                return $site;
        }
 
+       /**
+        * Get a new ORMRow from a Site object
+        *
+        * @since 1.22
+        *
+        * @param Site
+        *
+        * @return ORMRow
+        */
+       protected function getRowFromSite( Site $site ) {
+               $fields = array(
+                       // Site data
+                       'global_key' => $site->getGlobalId(), // TODO: check not null
+                       'type' => $site->getType(),
+                       'group' => $site->getGroup(),
+                       'source' => $site->getSource(),
+                       'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
+                       'protocol' => $site->getProtocol(),
+                       'domain' => strrev( $site->getDomain() ) . '.',
+                       'data' => $site->getExtraData(),
+
+                       // Site config
+                       'forward' => $site->shouldForward(),
+                       'config' => $site->getExtraConfig(),
+               );
+
+               if ( $site->getInternalId() !== null ) {
+                       $fields['id'] = $site->getInternalId();
+               }
+
+               return new ORMRow( $this->sitesTable, $fields );
+       }
+
        /**
         * Fetches the site from the database and loads them into the sites field.
         *
         * @since 1.21
         */
        protected function loadSites() {
+               wfProfileIn( __METHOD__ );
+
                $this->sites = new SiteList();
 
                foreach ( $this->sitesTable->select() as $siteRow ) {
@@ -207,7 +256,9 @@ class SiteSQLStore implements SiteStore {
                }
 
                $cache = wfGetMainCache();
-               $cache->set( $this->getCacheKey(), $this->sites );
+               $cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
+
+               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -221,8 +272,11 @@ class SiteSQLStore implements SiteStore {
         * @return Site|null
         */
        public function getSite( $globalId, $source = 'cache' ) {
+               wfProfileIn( __METHOD__ );
+
                $sites = $this->getSites( $source );
 
+               wfProfileOut( __METHOD__ );
                return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
        }
 
@@ -249,7 +303,10 @@ class SiteSQLStore implements SiteStore {
         * @return boolean Success indicator
         */
        public function saveSites( array $sites ) {
+               wfProfileIn( __METHOD__ );
+
                if ( empty( $sites ) ) {
+                       wfProfileOut( __METHOD__ );
                        return true;
                }
 
@@ -267,28 +324,11 @@ class SiteSQLStore implements SiteStore {
                $localIds = array();
 
                foreach ( $sites as $site ) {
-                       $fields = array(
-                               // Site data
-                               'global_key' => $site->getGlobalId(), // TODO: check not null
-                               'type' => $site->getType(),
-                               'group' => $site->getGroup(),
-                               'source' => $site->getSource(),
-                               'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
-                               'protocol' => $site->getProtocol(),
-                               'domain' => strrev( $site->getDomain() ) . '.',
-                               'data' => $site->getExtraData(),
-
-                               // Site config
-                               'forward' => $site->shouldForward(),
-                               'config' => $site->getExtraConfig(),
-                       );
-
                        if ( $site->getInternalId() !== null ) {
-                               $fields['id'] = $site->getInternalId();
                                $internalIds[] = $site->getInternalId();
                        }
 
-                       $siteRow = new ORMRow( $this->sitesTable, $fields );
+                       $siteRow = $this->getRowFromSite( $site );
                        $success = $siteRow->save( __METHOD__ ) && $success;
 
                        foreach ( $site->getLocalIds() as $idType => $ids ) {
@@ -322,9 +362,59 @@ class SiteSQLStore implements SiteStore {
                        $dbw->commit( __METHOD__ );
                }
 
+               // purge cache
+               $this->reset();
+
+               wfProfileOut( __METHOD__ );
                return $success;
        }
 
+       /**
+        * Purges the internal and external cache of the site list, forcing the list
+        * of sites to be re-read from the database.
+        *
+        * @since 1.21
+        */
+       public function reset() {
+               wfProfileIn( __METHOD__ );
+               // purge cache
+               $cache = wfGetMainCache();
+               $cache->delete( $this->getCacheKey() );
+               $this->sites = null;
+
+               wfProfileOut( __METHOD__ );
+       }
+
+       /**
+        * Clears the list of sites stored in the database.
+        *
+        * @see SiteStore::clear()
+        *
+        * @return bool success
+        */
+       public function clear() {
+               wfProfileIn( __METHOD__ );
+               $dbw = $this->sitesTable->getWriteDbConnection();
+
+               $trx = $dbw->trxLevel();
+
+               if ( $trx == 0 ) {
+                       $dbw->begin( __METHOD__ );
+               }
+
+               $ok = $dbw->delete( 'sites', '*', __METHOD__ );
+               $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
+
+               if ( $trx == 0 ) {
+                       $dbw->commit( __METHOD__ );
+               }
+
+               $this->reset();
+
+               wfProfileOut( __METHOD__ );
+               return $ok;
+       }
+
        /**
         * @since 1.21
         *
@@ -397,7 +487,13 @@ class Sites extends SiteSQLStore {
         * @return SiteStore
         */
        public static function singleton() {
-               return new static();
+               static $singleton;
+
+               if ( $singleton === null ) {
+                       $singleton = new static();
+               }
+
+               return $singleton;
        }
 
        /**