Merge "Improve the API help for format=xmlfm"
[lhc/web/wiklou.git] / includes / cache / LinkCache.php
index e80dfb3..3db84a6 100644 (file)
  * @ingroup Cache
  */
 class LinkCache {
-       // Increment $mClassVer whenever old serialized versions of this class
-       // becomes incompatible with the new version.
-       private $mClassVer = 4;
-
-       private $mGoodLinks = array();
-       private $mGoodLinkFields = array();
-       private $mBadLinks = array();
+       /**
+        * @var MapCacheLRU
+        */
+       private $mGoodLinks;
+       /**
+        * @var MapCacheLRU
+        */
+       private $mBadLinks;
        private $mForUpdate = false;
 
+       /**
+        * How many Titles to store. There are two caches, so the amount actually
+        * stored in memory can be up to twice this.
+        */
+       const MAX_SIZE = 10000;
+
        /**
         * @var LinkCache
         */
        protected static $instance;
 
+       public function __construct() {
+               $this->mGoodLinks = new MapCacheLRU( self::MAX_SIZE );
+               $this->mBadLinks = new MapCacheLRU( self::MAX_SIZE );
+       }
+
        /**
         * Get an instance of this class.
         *
@@ -90,8 +102,9 @@ class LinkCache {
         * @return int
         */
        public function getGoodLinkID( $title ) {
-               if ( array_key_exists( $title, $this->mGoodLinks ) ) {
-                       return $this->mGoodLinks[$title];
+               if ( $this->mGoodLinks->has( $title ) ) {
+                       $info = $this->mGoodLinks->get( $title );
+                       return $info['id'];
                } else {
                        return 0;
                }
@@ -106,8 +119,9 @@ class LinkCache {
         */
        public function getGoodLinkFieldObj( $title, $field ) {
                $dbkey = $title->getPrefixedDBkey();
-               if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
-                       return $this->mGoodLinkFields[$dbkey][$field];
+               if ( $this->mGoodLinks->has( $dbkey ) ) {
+                       $info = $this->mGoodLinks->get( $dbkey );
+                       return $info[$field];
                } else {
                        return null;
                }
@@ -118,7 +132,8 @@ class LinkCache {
         * @return bool
         */
        public function isBadLink( $title ) {
-               return array_key_exists( $title, $this->mBadLinks );
+               // We need to use get here since has will not call ping.
+               return $this->mBadLinks->get( $title ) !== null;
        }
 
        /**
@@ -129,19 +144,19 @@ class LinkCache {
         * @param int $len Text's length
         * @param int $redir Whether the page is a redirect
         * @param int $revision Latest revision's ID
-        * @param int $model Latest revision's content model ID
+        * @param string|null $model Latest revision's content model ID
         */
        public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
-               $revision = 0, $model = 0
+               $revision = 0, $model = null
        ) {
                $dbkey = $title->getPrefixedDBkey();
-               $this->mGoodLinks[$dbkey] = (int)$id;
-               $this->mGoodLinkFields[$dbkey] = array(
+               $this->mGoodLinks->set( $dbkey, array(
+                       'id' => (int)$id,
                        'length' => (int)$len,
                        'redirect' => (int)$redir,
                        'revision' => (int)$revision,
-                       'model' => (int)$model
-               );
+                       'model' => $model ? (string)$model : null,
+               ) );
        }
 
        /**
@@ -153,13 +168,13 @@ class LinkCache {
         */
        public function addGoodLinkObjFromRow( $title, $row ) {
                $dbkey = $title->getPrefixedDBkey();
-               $this->mGoodLinks[$dbkey] = intval( $row->page_id );
-               $this->mGoodLinkFields[$dbkey] = array(
+               $this->mGoodLinks->set( $dbkey, array(
+                       'id' => intval( $row->page_id ),
                        'length' => intval( $row->page_len ),
                        'redirect' => intval( $row->page_is_redirect ),
                        'revision' => intval( $row->page_latest ),
                        'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
-               );
+               ) );
        }
 
        /**
@@ -168,12 +183,12 @@ class LinkCache {
        public function addBadLinkObj( $title ) {
                $dbkey = $title->getPrefixedDBkey();
                if ( !$this->isBadLink( $dbkey ) ) {
-                       $this->mBadLinks[$dbkey] = 1;
+                       $this->mBadLinks->set( $dbkey, 1 );
                }
        }
 
        public function clearBadLink( $title ) {
-               unset( $this->mBadLinks[$title] );
+               $this->mBadLinks->clear( array( $title ) );
        }
 
        /**
@@ -181,17 +196,33 @@ class LinkCache {
         */
        public function clearLink( $title ) {
                $dbkey = $title->getPrefixedDBkey();
-               unset( $this->mBadLinks[$dbkey] );
-               unset( $this->mGoodLinks[$dbkey] );
-               unset( $this->mGoodLinkFields[$dbkey] );
+               $this->mBadLinks->clear( array( $dbkey ) );
+               $this->mGoodLinks->clear( array( $dbkey ) );
        }
 
+
+       /**
+        * @deprecated since 1.26
+        * @return array
+        */
        public function getGoodLinks() {
-               return $this->mGoodLinks;
+               wfDeprecated( __METHOD__, '1.26' );
+               $links = array();
+               foreach ( $this->mGoodLinks->getAllKeys() as $key ) {
+                       $info = $this->mGoodLinks->get( $key );
+                       $links[$key] = $info['id'];
+               }
+
+               return $links;
        }
 
+       /**
+        * @deprecated since 1.26
+        * @return array
+        */
        public function getBadLinks() {
-               return array_keys( $this->mBadLinks );
+               wfDeprecated( __METHOD__, '1.26' );
+               return $this->mBadLinks->getAllKeys();
        }
 
        /**
@@ -216,40 +247,26 @@ class LinkCache {
         * @return int
         */
        public function addLinkObj( $nt ) {
-               global $wgAntiLockFlags, $wgContentHandlerUseDB;
-
-               wfProfileIn( __METHOD__ );
+               global $wgContentHandlerUseDB;
 
                $key = $nt->getPrefixedDBkey();
                if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
-                       wfProfileOut( __METHOD__ );
-
                        return 0;
                }
                $id = $this->getGoodLinkID( $key );
                if ( $id != 0 ) {
-                       wfProfileOut( __METHOD__ );
-
                        return $id;
                }
 
                if ( $key === '' ) {
-                       wfProfileOut( __METHOD__ );
-
                        return 0;
                }
 
                # Some fields heavily used for linking...
                if ( $this->mForUpdate ) {
                        $db = wfGetDB( DB_MASTER );
-                       if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
-                               $options = array( 'FOR UPDATE' );
-                       } else {
-                               $options = array();
-                       }
                } else {
                        $db = wfGetDB( DB_SLAVE );
-                       $options = array();
                }
 
                $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
@@ -259,7 +276,7 @@ class LinkCache {
 
                $s = $db->selectRow( 'page', $f,
                        array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
-                       __METHOD__, $options );
+                       __METHOD__ );
                # Set fields...
                if ( $s !== false ) {
                        $this->addGoodLinkObjFromRow( $nt, $s );
@@ -269,8 +286,6 @@ class LinkCache {
                        $id = 0;
                }
 
-               wfProfileOut( __METHOD__ );
-
                return $id;
        }
 
@@ -278,8 +293,7 @@ class LinkCache {
         * Clears cache
         */
        public function clear() {
-               $this->mGoodLinks = array();
-               $this->mGoodLinkFields = array();
-               $this->mBadLinks = array();
+               $this->mGoodLinks->clear();
+               $this->mBadLinks->clear();
        }
 }