From: Aaron Schulz Date: Wed, 9 Apr 2008 05:21:00 +0000 (+0000) Subject: ...really get rid of link color query spam X-Git-Tag: 1.31.0-rc.0~48499 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=aed9d4b91218e361e9f6ff72886cd172147aadd3;p=lhc%2Fweb%2Fwiklou.git ...really get rid of link color query spam --- diff --git a/includes/Article.php b/includes/Article.php index 3637973015..c1180b441e 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -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; diff --git a/includes/LinkBatch.php b/includes/LinkBatch.php index eb5f8e2604..8d86b83922 100644 --- a/includes/LinkBatch.php +++ b/includes/LinkBatch.php @@ -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] ); } diff --git a/includes/LinkCache.php b/includes/LinkCache.php index 1c094f66c1..55aaae8805 100644 --- a/includes/LinkCache.php +++ b/includes/LinkCache.php @@ -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(); } } diff --git a/includes/Parser.php b/includes/Parser.php index 12e9f82c24..3e8c9e4248 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -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 ); } diff --git a/includes/Parser_OldPP.php b/includes/Parser_OldPP.php index 14194e2e6b..852f338646 100644 --- a/includes/Parser_OldPP.php +++ b/includes/Parser_OldPP.php @@ -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 ); } diff --git a/includes/Title.php b/includes/Title.php index 4038e8a0b7..ba83bd6c38 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -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; } } diff --git a/includes/WatchlistEditor.php b/includes/WatchlistEditor.php index 36b3899f5b..984873496b 100644 --- a/includes/WatchlistEditor.php +++ b/includes/WatchlistEditor.php @@ -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 ); } diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index 970a8b0d8b..8f890928b7 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -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; } }