...really get rid of link color query spam
authorAaron Schulz <aaron@users.mediawiki.org>
Wed, 9 Apr 2008 05:21:00 +0000 (05:21 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Wed, 9 Apr 2008 05:21:00 +0000 (05:21 +0000)
includes/Article.php
includes/LinkBatch.php
includes/LinkCache.php
includes/Parser.php
includes/Parser_OldPP.php
includes/Title.php
includes/WatchlistEditor.php
includes/filerepo/File.php

index 3637973..c1180b4 100644 (file)
@@ -305,7 +305,7 @@ class Article {
 
                $lc =& LinkCache::singleton();
                if ( $data ) {
-                       $lc->addGoodLinkObj( $data->page_id, $this->mTitle );
+                       $lc->addGoodLinkObj( $data->page_id, $this->mTitle, $data->page_len, $data->page_is_redirect );
 
                        $this->mTitle->mArticleID = $data->page_id;
 
index eb5f8e2..8d86b83 100644 (file)
@@ -96,7 +96,7 @@ class LinkBatch {
                $remaining = $this->data;
                while ( $row = $res->fetchObject() ) {
                        $title = Title::newFromRow( $row );
-                       $cache->addGoodLinkObj( $row->page_id, $title );
+                       $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
                        $ids[$title->getPrefixedDBkey()] = $row->page_id;
                        unset( $remaining[$row->page_namespace][$row->page_title] );
                }
index 1c094f6..55aaae8 100644 (file)
@@ -7,7 +7,7 @@
 class LinkCache {
        // Increment $mClassVer whenever old serialized versions of this class
        // becomes incompatible with the new version.
-       /* private */ var $mClassVer = 3;
+       /* private */ var $mClassVer = 4;
 
        /* private */ var $mPageLinks;
        /* private */ var $mGoodLinks, $mBadLinks;
@@ -28,6 +28,7 @@ class LinkCache {
                $this->mForUpdate = false;
                $this->mPageLinks = array();
                $this->mGoodLinks = array();
+               $this->mGoodLinkFields = array();
                $this->mBadLinks = array();
        }
 
@@ -49,14 +50,38 @@ class LinkCache {
                        return 0;
                }
        }
+       
+       /**
+        * Get a field of a title object from cache. 
+        * If this link is not good, it will return NULL.
+        * @param Title $title
+        * @param string $field ('length','redirect')
+        * @return mixed
+        */
+       function getGoodLinkFieldObj( $title, $field ) {
+               $dbkey = $title->getPrefixedDbKey();
+               if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
+                       return $this->mGoodLinkFields[$dbkey][$field];
+               } else {
+                       return NULL;
+               }
+       }
 
        function isBadLink( $title ) {
                return array_key_exists( $title, $this->mBadLinks );
        }
 
-       function addGoodLinkObj( $id, $title ) {
+       /**
+        * Add a link for the title to the link cache
+        * @param int $id
+        * @param Title $title
+        * @param int $len
+        * @param int $redir
+        */
+       function addGoodLinkObj( $id, $title, $len = -1, $redir = NULL ) {
                $dbkey = $title->getPrefixedDbKey();
                $this->mGoodLinks[$dbkey] = $id;
+               $this->mGoodLinkFields[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
                $this->mPageLinks[$dbkey] = $title;
        }
 
@@ -124,11 +149,15 @@ class LinkCache {
                        wfProfileOut( $fname );
                        return 0;
                }
-
+               # Some fields heavily used for linking...
                $id = NULL;
-               if( $wgLinkCacheMemcached )
+               $len = -1;
+               $redirect = NULL;
+               
+               if( $wgLinkCacheMemcached ) {
                        $id = $wgMemc->get( $key = $this->getKey( $title ) );
-               if( ! is_integer( $id ) ) {
+               }
+               if( !is_integer( $id ) ) {
                        if ( $this->mForUpdate ) {
                                $db = wfGetDB( DB_MASTER );
                                if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
@@ -141,20 +170,24 @@ class LinkCache {
                                $options = array();
                        }
 
-                       $id = $db->selectField( 'page', 'page_id',
-                                       array( 'page_namespace' => $ns, 'page_title' => $t ),
-                                       $fname, $options );
-                       if ( !$id ) {
-                               $id = 0;
-                       }
-                       if( $wgLinkCacheMemcached )
+                       $s = $db->selectRow( 'page', 
+                               array( 'page_id', 'page_len', 'page_is_redirect' ),
+                               array( 'page_namespace' => $ns, 'page_title' => $t ),
+                               $fname, $options );
+                       # Set fields...
+                       $id = $s ? $s->page_id : 0;
+                       $len = $s ? $s->page_len : -1;
+                       $redirect = $s ? $s->page_is_redirect : 0;
+
+                       if( $wgLinkCacheMemcached ) {
                                $wgMemc->add( $key, $id, 3600*24 );
+                       }
                }
 
                if( 0 == $id ) {
                        $this->addBadLinkObj( $nt );
                } else {
-                       $this->addGoodLinkObj( $id, $nt );
+                       $this->addGoodLinkObj( $id, $nt, $len, $redirect );
                }
                wfProfileOut( $fname );
                return $id;
@@ -166,6 +199,7 @@ class LinkCache {
        function clear() {
                $this->mPageLinks = array();
                $this->mGoodLinks = array();
+               $this->mGoodLinkFields = array();
                $this->mBadLinks = array();
        }
 }
index 12e9f82..3e8c9e4 100644 (file)
@@ -4028,7 +4028,7 @@ class Parser
                                while ( $s = $dbr->fetchObject($res) ) {
                                        $title = Title::newFromRow( $s );
                                        $pdbk = $title->getPrefixedDBkey();
-                                       $linkCache->addGoodLinkObj( $s->page_id, $title );
+                                       $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect );
                                        $this->mOutput->addLink( $title, $s->page_id );
                                        $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
                                        //add id to the extension todolist
@@ -4110,7 +4110,7 @@ class Parser
                                                $holderKeys = array();
                                                if(isset($variantMap[$varPdbk])){
                                                        $holderKeys = $variantMap[$varPdbk];
-                                                       $linkCache->addGoodLinkObj( $s->page_id, $variantTitle );
+                                                       $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
                                                        $this->mOutput->addLink( $variantTitle, $s->page_id );
                                                }
 
index 14194e2..852f338 100644 (file)
@@ -4147,7 +4147,7 @@ class Parser_OldPP
                                while ( $s = $dbr->fetchObject($res) ) {
                                        $title = Title::newFromRow( $s );
                                        $pdbk = $title->getPrefixedDBkey();
-                                       $linkCache->addGoodLinkObj( $s->page_id, $title );
+                                       $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect );
                                        $this->mOutput->addLink( $title, $s->page_id );
 
                                        $colours[$pdbk] = ( $threshold == 0 || (
@@ -4230,7 +4230,7 @@ class Parser_OldPP
                                                $holderKeys = array();
                                                if(isset($variantMap[$varPdbk])){
                                                        $holderKeys = $variantMap[$varPdbk];
-                                                       $linkCache->addGoodLinkObj( $s->page_id, $variantTitle );
+                                                       $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
                                                        $this->mOutput->addLink( $variantTitle, $s->page_id );
                                                }
 
index 4038e8a..ba83bd6 100644 (file)
@@ -86,7 +86,7 @@ class Title {
                $this->mLatestID = false;
                $this->mOldRestrictions = false;
                $this->mLength = -1;
-               $this->mRedirect = null;
+               $this->mRedirect = NULL;
        }
 
        /**
@@ -231,12 +231,12 @@ class Title {
         */
        public static function newFromRow( $row ) {
                $t = self::makeTitle( $row->page_namespace, $row->page_title );
-               
-               $t->mArticleID = isset($row->page_id) ? $row->page_id : -1;
-               $t->mLength = isset($row->page_len) ? $row->page_len : -1;
-               $t->mRedirect = isset($row->page_is_redirect) ? (bool)$row->page_is_redirect : null;
-               $t->mLatest = isset($row->page_latest) ? $row->page_latest : 0;
-               
+
+               $t->mArticleID = isset($row->page_id) ? intval($row->page_id) : -1;
+               $t->mLength = isset($row->page_len) ? intval($row->page_len) : -1;
+               $t->mRedirect = isset($row->page_is_redirect) ? intval($row->page_is_redirect) : NULL;
+               $t->mLatestID = isset($row->page_latest) ? $row->page_latest : false;
+
                return $t;
        }
 
@@ -1482,47 +1482,6 @@ class Title {
        public function isTalkPage() {
                return MWNamespace::isTalk( $this->getNamespace() );
        }
-       
-       /**
-        * Is this an article that is a redirect page?
-        * @return bool
-        */
-       public function isRedirect() {
-               if( $this->mRedirect !== null )
-                       return $this->mRedirect;
-               # Zero for special pages
-               if( $this->getNamespace() == NS_SPECIAL )
-                       return 0;
-
-               $dbr = wfGetDB( DB_SLAVE );
-               $redir = $dbr->selectField( 'page', 'page_is_redirect',
-                       array( 'page_id' => $this->getArticleId() ),
-                       __METHOD__ );
-
-               $this->mRedirect = (bool)$redir;
-
-               return $this->mRedirect;
-       }
-       
-       /**
-        * What is the length of this page?
-        * @return bool
-        */
-       public function getLength() {
-               if( $this->mLength != -1 || $this->mArticleID == 0 )
-                       return $this->mLength;
-               # Zero for special pages
-               if( $this->getNamespace() == NS_SPECIAL )
-                       return 0;
-
-               $dbr = wfGetDB( DB_SLAVE );
-               $len = $dbr->selectField( 'page', 'page_len',
-                       array( 'page_id' => $this->getArticleId() ),
-                       __METHOD__ );
-               $this->mLength = intval($len);
-
-               return $this->mLength;
-       }
 
        /**
         * Is this a subpage?
@@ -1889,6 +1848,42 @@ class Title {
                }
                return $this->mArticleID;
        }
+       
+       /**
+        * Is this an article that is a redirect page?
+        * Uses link cache, adding it if necessary
+        * @param int $flags a bit field; may be GAID_FOR_UPDATE to select for update
+        * @return bool
+        */
+       public function isRedirect( $flags = 0 ) {
+               # Zero for special pages. 
+               # Also, calling getArticleID() loads the field from cache!
+               if( !$this->getArticleID($flags) || $this->getNamespace() == NS_SPECIAL ) {
+                       return false;
+               }
+               $linkCache =& LinkCache::singleton();
+               $this->mRedirect = $linkCache->getGoodLinkFieldObj( $this, 'redirect' );
+
+               return (bool)$this->mRedirect;
+       }
+       
+       /**
+        * What is the length of this page?
+        * Uses link cache, adding it if necessary
+        * @param int $flags a bit field; may be GAID_FOR_UPDATE to select for update
+        * @return bool
+        */
+       public function getLength( $flags = 0 ) {
+               # Zero for special pages. 
+               # Also, calling getArticleID() loads the field from cache!
+               if( !$this->getArticleID($flags) || $this->getNamespace() == NS_SPECIAL ) {
+                       return 0;
+               }
+               $linkCache =& LinkCache::singleton();
+               $this->mLength = $linkCache->getGoodLinkFieldObj( $this, 'length' );
+
+               return intval($this->mLength);
+       }
 
        public function getLatestRevID() {
                if ($this->mLatestID !== false)
@@ -2245,7 +2240,7 @@ class Title {
                if ( $db->numRows( $res ) ) {
                        while ( $row = $db->fetchObject( $res ) ) {
                                if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
-                                       $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
+                                       $linkCache->addGoodLinkObj( $row->page_id, $titleObj, $row->page_len, $row->page_is_redirect );
                                        $retVal[] = $titleObj;
                                }
                        }
index 36b3899..9848734 100644 (file)
@@ -217,7 +217,7 @@ class WatchlistEditor {
                                if( $title instanceof Title ) {
                                        // Update the link cache while we're at it
                                        if( $row->page_id ) {
-                                               $cache->addGoodLinkObj( $row->page_id, $title );
+                                               $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
                                        } else {
                                                $cache->addBadLinkObj( $title );
                                        }
index 970a8b0..8f89092 100644 (file)
@@ -852,7 +852,7 @@ abstract class File {
                if ( $db->numRows( $res ) ) {
                        while ( $row = $db->fetchObject( $res ) ) {
                                if ( $titleObj = Title::newFromRow( $row ) ) {
-                                       $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
+                                       $linkCache->addGoodLinkObj( $row->page_id, $titleObj, $row->page_len, $row->page_is_redirect );
                                        $retVal[] = $titleObj;
                                }
                        }