Lazy initialisation of wgProxyList, no flip required
[lhc/web/wiklou.git] / includes / LinkCache.php
index 9d86a24..dd87fce 100644 (file)
@@ -18,7 +18,7 @@ define ('LINKCACHE_PAGE', 3);
  * @package MediaWiki
  * @subpackage Cache
  */
-class LinkCache {      
+class LinkCache {
        // Increment $mClassVer whenever old serialized versions of this class
        // becomes incompatible with the new version.
        /* private */ var $mClassVer = 3;
@@ -33,7 +33,7 @@ class LinkCache {
                global $wgDBname;
                return $wgDBname.':lc:title:'.$title;
        }
-       
+
        function LinkCache() {
                $this->mActive = true;
                $this->mPreFilled = false;
@@ -45,15 +45,16 @@ class LinkCache {
                $this->mCategoryLinks = array();
                $this->mOldGoodLinks = array();
                $this->mOldBadLinks = array();
+               $this->mOldPageLinks = array();
        }
 
        /**
         * General accessor to get/set whether SELECT FOR UPDATE should be used
         */
-       function forUpdate( $update = NULL ) { 
+       function forUpdate( $update = NULL ) {
                return wfSetVar( $this->mForUpdate, $update );
        }
-       
+
        function getGoodLinkID( $title ) {
                if ( array_key_exists( $title, $this->mGoodLinks ) ) {
                        return $this->mGoodLinks[$title];
@@ -63,7 +64,7 @@ class LinkCache {
        }
 
        function isBadLink( $title ) {
-               return array_key_exists( $title, $this->mBadLinks ); 
+               return array_key_exists( $title, $this->mBadLinks );
        }
 
        function addGoodLinkObj( $id, $title ) {
@@ -89,11 +90,11 @@ class LinkCache {
        function addImageLinkObj( $nt ) {
                if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
        }
-       
+
        function addCategoryLink( $title, $sortkey ) {
                if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
        }
-       
+
        function addCategoryLinkObj( &$nt, $sortkey ) {
                $this->addCategoryLink( $nt->getDBkey(), $sortkey );
        }
@@ -102,7 +103,7 @@ class LinkCache {
                unset( $this->mBadLinks[$title] );
                $this->clearLink( $title );
        }
-       
+
        function clearLink( $title ) {
                global $wgMemc, $wgLinkCacheMemcached;
                if( $wgLinkCacheMemcached )
@@ -125,25 +126,30 @@ class LinkCache {
                        return 0;
                }
        }
-       
+
        function addLinkObj( &$nt ) {
                global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
                $title = $nt->getPrefixedDBkey();
-               if ( $this->isBadLink( $title ) ) { return 0; }         
+               if ( $this->isBadLink( $title ) ) { return 0; }
                $id = $this->getGoodLinkID( $title );
                if ( 0 != $id ) { return $id; }
 
                $fname = 'LinkCache::addLinkObj';
+               global $wgProfiler;
+               if ( isset( $wgProfiler ) ) {
+                       $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
+               }
+
                wfProfileIn( $fname );
 
                $ns = $nt->getNamespace();
                $t = $nt->getDBkey();
 
-               if ( '' == $title ) { 
+               if ( '' == $title ) {
                        wfProfileOut( $fname );
-                       return 0; 
+                       return 0;
                }
-               
+
                $id = NULL;
                if( $wgLinkCacheMemcached )
                        $id = $wgMemc->get( $key = $this->getKey( $title ) );
@@ -152,20 +158,24 @@ class LinkCache {
                                $db =& wfGetDB( DB_MASTER );
                                if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
                                        $options = array( 'FOR UPDATE' );
+                               } else {
+                                       $options = array();
                                }
                        } else {
                                $db =& wfGetDB( DB_SLAVE );
                                $options = array();
                        }
 
-                       $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
+                       $id = $db->selectField( 'page', 'page_id',
+                                       array( 'page_namespace' => $ns, 'page_title' => $t ),
+                                       $fname, $options );
                        if ( !$id ) {
                                $id = 0;
                        }
                        if( $wgLinkCacheMemcached )
                                $wgMemc->add( $key, $id, 3600*24 );
                }
-               
+
                if( 0 == $id ) {
                        $this->addBadLinkObj( $nt );
                } else {
@@ -180,30 +190,27 @@ class LinkCache {
         * @param Title $fromtitle
         */
        function preFill( &$fromtitle ) {
-               global $wgEnablePersistentLC;
-
+               global $wgAntiLockFlags;
                $fname = 'LinkCache::preFill';
                wfProfileIn( $fname );
 
                $this->suspend();
                $id = $fromtitle->getArticleID();
                $this->resume();
-               
+
                if( $id == 0 ) {
                        wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
                        wfProfileOut( $fname );
                        return;
                }
-               
-               if ( $wgEnablePersistentLC ) {
-                       if( $this->fillFromLinkscc( $id ) ){
-                               wfProfileOut( $fname );
-                               return;
-                       }
-               }
+
                if ( $this->mForUpdate ) {
                        $db =& wfGetDB( DB_MASTER );
-                       $options = 'FOR UPDATE';
+                       if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
+                               $options = 'FOR UPDATE';
+                       } else {
+                               $options = '';
+                       }
                } else {
                        $db =& wfGetDB( DB_SLAVE );
                        $options = '';
@@ -226,14 +233,8 @@ class LinkCache {
                                $this->addBadLinkObj( $title );
                        }
                }
-               $this->mOldPageLinks = $this->mPageLinks;
-               $this->mOldBadLinks  = $this->mBadLinks;
-               $this->mOldGoodLinks = $this->mGoodLinks;
                $this->mPreFilled = true;
 
-               if ( $wgEnablePersistentLC ) {
-                       $this->saveToLinkscc( $id );
-               }
                wfProfileOut( $fname );
        }
 
@@ -262,7 +263,7 @@ class LinkCache {
        function getImageDeletions() {
                return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
        }
-       
+
        function getPageAdditions() {
                $set = array_diff( array_keys( $this->mPageLinks ), array_keys( $this->mOldPageLinks ) );
                $out = array();
@@ -271,7 +272,7 @@ class LinkCache {
                }
                return $out;
        }
-       
+
        function getPageDeletions() {
                $set = array_diff( array_keys( $this->mOldPageLinks ), array_keys( $this->mPageLinks ) );
                $out = array();
@@ -316,109 +317,34 @@ class LinkCache {
                                $add = $this->getPageAdditions();
                                break;
                        default: # LINKCACHE_IMAGE
-                               return false;           
+                               return false;
                }
-               
+
                return true;
        }
 
        /**
-        * Clears cache but leaves old preFill copies alone
+        * Clears cache
         */
        function clear() {
                $this->mPageLinks = array();
                $this->mGoodLinks = array();
                $this->mBadLinks = array();
                $this->mImageLinks = array();
+               $this->mCategoryLinks = array();
+               $this->mOldGoodLinks = array();
+               $this->mOldBadLinks = array();
+               $this->mOldPageLinks = array();
        }
 
        /**
-        * @access private
-        */
-       function fillFromLinkscc( $id ){ 
-               $fname = 'LinkCache::fillFromLinkscc';
-
-               $id = IntVal( $id );
-               if ( $this->mForUpdate ) {
-                       $db =& wfGetDB( DB_MASTER );
-                       $options = 'FOR UPDATE';
-               } else {
-                       $db =& wfGetDB( DB_SLAVE );
-                       $options = '';
-               }
-               $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
-               if ( $raw === false ) {
-                       return false;
-               }
-               
-               $cacheobj = false;
-               if( function_exists( 'gzuncompress' ) )
-                       $cacheobj = @gzuncompress( $raw );
-
-               if($cacheobj == FALSE){
-                       $cacheobj = $raw;
-               }
-               $cc = @unserialize( $cacheobj );
-               if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
-                       $this->mOldPageLinks = $this->mPageLinks = $cc->mPageLinks;
-                       $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
-                       $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
-                       $this->mPreFilled = true;
-                       return TRUE;
-               } else {
-                       return FALSE;
-               }
-
-       }
-
-       /**
-        * @access private
-        */
-       function saveToLinkscc( $pid ){
-               global $wgCompressedPersistentLC;
-               if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
-                       $ser = gzcompress( serialize( $this ), 3 );
-               } else {
-                       $ser = serialize( $this );
-               }
-               $db =& wfGetDB( DB_MASTER );
-               $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
-       }
-
-       /**
-        * Delete linkscc rows which link to here
-        * @param $title The page linked to
-        * @static
-        */
-       function linksccClearLinksTo( $title ){
-               global $wgEnablePersistentLC;
-               if ( $wgEnablePersistentLC ) {
-                       $fname = 'LinkCache::linksccClearLinksTo';
-                       $pid = intval( $pid );
-                       $dbw =& wfGetDB( DB_MASTER );
-                       # Delete linkscc rows which link to here
-                       $dbw->deleteJoin( 'linkscc', 'pagelinks', 'lcc_pageid', 'pl_from',
-                               array(
-                                       'pl_namespace' => $title->getNamespace(),
-                                       'pl-title'     => $title->getDbKey() ),
-                               $fname );
-                       # Delete linkscc row representing this page
-                       $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
-               }
-
-       }
-
-       /**
-        * @param $pid is a page id
-        * @static
+        * Swaps old and current link registers
         */
-       function linksccClearPage( $pid ){
-               global $wgEnablePersistentLC;
-               if ( $wgEnablePersistentLC ) {
-                       $pid = intval( $pid );
-                       $dbw =& wfGetDB( DB_MASTER );
-                       $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );
-               }
+       function swapRegisters() {
+               swap( $this->mGoodLinks, $this->mOldGoodLinks );
+               swap( $this->mBadLinks, $this->mOldBadLinks );
+               swap( $this->mImageLinks, $this->mOldImageLinks );
+               swap( $this->mPageLinks, $this->mOldPageLinks );
        }
 }
 
@@ -426,11 +352,11 @@ class LinkCache {
  * Class representing a list of titles
  * The execute() method checks them all for existence and adds them to a LinkCache object
  +
- * @package MediaWikki
+ * @package MediaWiki
  * @subpackage Cache
  */
 class LinkBatch {
-       /** 
+       /**
         * 2-d array, first index namespace, second index dbkey, value arbitrary
         */
        var $data = array();
@@ -440,9 +366,13 @@ class LinkBatch {
                        $this->addObj( $item );
                }
        }
-       
+
        function addObj( $title ) {
-               $this->add( $title->getNamespace(), $title->getDBkey() );
+               if ( is_object( $title ) ) {
+                       $this->add( $title->getNamespace(), $title->getDBkey() );
+               } else {
+                       wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
+               }
        }
 
        function add( $ns, $dbkey ) {
@@ -468,11 +398,11 @@ class LinkBatch {
 
                // Construct query
                // This is very similar to Parser::replaceLinkHolders
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr =& wfGetDB( DB_SLAVE );
                $page = $dbr->tableName( 'page' );
                $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE "
                        . $this->constructSet( 'page', $dbr );
-               
+
                // Do query
                $res = $dbr->query( $sql, $fname );
 
@@ -497,7 +427,7 @@ class LinkBatch {
 
                wfProfileOut( $fname );
        }
-       
+
        /**
         * Construct a WHERE clause which will match all the given titles.
         * Give the appropriate table's field name prefix ('page', 'pl', etc).