From: Aaron Schulz Date: Tue, 8 Apr 2008 20:34:09 +0000 (+0000) Subject: * Add redirect and size fields to title. Add accessors. X-Git-Tag: 1.31.0-rc.0~48518 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=a17762266f67e86fd10ab7a8456c82170a2c5333;p=lhc%2Fweb%2Fwiklou.git * Add redirect and size fields to title. Add accessors. * Add newFromRow() function for Titles to optionally avoid some queries on accessor use * Make linkbatch and some things that directly use linkcache select len/redirect fields along with the rest all in one query. This avoids query spam for link coloring. --- diff --git a/includes/LinkBatch.php b/includes/LinkBatch.php index c5c5721eaf..eb5f8e2604 100644 --- a/includes/LinkBatch.php +++ b/includes/LinkBatch.php @@ -82,6 +82,8 @@ class LinkBatch { /** * Add a ResultWrapper containing IDs and titles to a LinkCache object + * Title are initialized here and they will go to the static title cache + * field of the Title class. */ function addResultToCache( $cache, $res ) { if ( !$res ) { @@ -93,7 +95,7 @@ class LinkBatch { $ids = array(); $remaining = $this->data; while ( $row = $res->fetchObject() ) { - $title = Title::makeTitle( $row->page_namespace, $row->page_title ); + $title = Title::newFromRow( $row ); $cache->addGoodLinkObj( $row->page_id, $title ); $ids[$title->getPrefixedDBkey()] = $row->page_id; unset( $remaining[$row->page_namespace][$row->page_title] ); @@ -128,7 +130,7 @@ class LinkBatch { wfProfileOut( __METHOD__ ); return false; } - $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set"; + $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect FROM $page WHERE $set"; // Do query $res = new ResultWrapper( $dbr, $dbr->query( $sql, __METHOD__ ) ); diff --git a/includes/Linker.php b/includes/Linker.php index f7688dd521..1cab7f7a43 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -79,20 +79,16 @@ class Linker { /** * Return the CSS colour of a known link * - * @param mixed $s + * @param Title $t * @param integer $threshold user defined threshold * @return string CSS class */ - function getLinkColour( $s, $threshold ) { - if( $s === false ) { - return ''; - } - + function getLinkColour( $t, $threshold ) { $colour = ''; - if ( !empty( $s->page_is_redirect ) ) { + if ( $t->isRedirect() ) { # Page is a redirect $colour = 'mw-redirect'; - } elseif ( $threshold > 0 && $s->page_len < $threshold && MWNamespace::isContent( $s->page_namespace ) ) { + } elseif ( $threshold > 0 && $t->getLength() < $threshold && MWNamespace::isContent( $t->getNamespace() ) ) { # Page is a stub $colour = 'stub'; } @@ -256,15 +252,8 @@ class Linker { } else { $colour = ''; if ( $nt->isContentPage() ) { - # FIXME: This is stupid, we should combine this query with - # the Title::getArticleID() query above. $threshold = $wgUser->getOption('stubthreshold'); - $dbr = wfGetDB( DB_SLAVE ); - $s = $dbr->selectRow( - array( 'page' ), - array( 'page_len', 'page_is_redirect', 'page_namespace' ), - array( 'page_id' => $aid ), __METHOD__ ) ; - $colour = $this->getLinkColour( $s, $threshold ); + $colour = $this->getLinkColour( $nt, $threshold ); } $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix ); } diff --git a/includes/Parser.php b/includes/Parser.php index 536b88cd8c..11e4542c3f 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -4003,10 +4003,7 @@ class Parser # Not in the link cache, add it to the query if ( !isset( $current ) ) { $current = $ns; - $query = "SELECT page_id, page_namespace, page_title, page_is_redirect"; - if ( $threshold > 0 ) { - $query .= ', page_len'; - } + $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len"; $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN("; } elseif ( $current != $ns ) { $current = $ns; @@ -4029,7 +4026,7 @@ class Parser # Fetch data and form into an associative array # non-existent = broken while ( $s = $dbr->fetchObject($res) ) { - $title = Title::makeTitle( $s->page_namespace, $s->page_title ); + $title = Title::newFromRow( $s ); $pdbk = $title->getPrefixedDBkey(); $linkCache->addGoodLinkObj( $s->page_id, $title ); $this->mOutput->addLink( $title, $s->page_id ); @@ -4094,10 +4091,7 @@ class Parser // construct query $titleClause = $linkBatch->constructSet('page', $dbr); - $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect"; - if ( $threshold > 0 ) { - $variantQuery .= ', page_len'; - } + $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len"; $variantQuery .= " FROM $page WHERE $titleClause"; if ( $options & RLH_FOR_UPDATE ) { @@ -4109,7 +4103,7 @@ class Parser // for each found variants, figure out link holders and replace while ( $s = $dbr->fetchObject($varRes) ) { - $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title ); + $variantTitle = Title::newFromRow( $s ); $varPdbk = $variantTitle->getPrefixedDBkey(); $vardbk = $variantTitle->getDBkey(); diff --git a/includes/Parser_OldPP.php b/includes/Parser_OldPP.php index 7462397bb1..805c7fa60f 100644 --- a/includes/Parser_OldPP.php +++ b/includes/Parser_OldPP.php @@ -4120,9 +4120,7 @@ class Parser_OldPP # Not in the link cache, add it to the query if ( !isset( $current ) ) { $current = $ns; - $query = "SELECT page_id, page_namespace, page_title"; - if ( $threshold > 0 ) { - $query .= ', page_len, page_is_redirect'; + $query = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect"; } $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN("; } elseif ( $current != $ns ) { @@ -4148,7 +4146,7 @@ class Parser_OldPP # 1 = known # 2 = stub while ( $s = $dbr->fetchObject($res) ) { - $title = Title::makeTitle( $s->page_namespace, $s->page_title ); + $title = Title::newFromRow( $s ); $pdbk = $title->getPrefixedDBkey(); $linkCache->addGoodLinkObj( $s->page_id, $title ); $this->mOutput->addLink( $title, $s->page_id ); @@ -4163,7 +4161,7 @@ class Parser_OldPP wfProfileOut( $fname.'-check' ); # Do a second query for different language variants of links and categories - if($wgContLang->hasVariants()){ + if( $wgContLang->hasVariants() ){ $linkBatch = new LinkBatch(); $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders) $categoryMap = array(); // maps $category_variant => $category (dbkeys) @@ -4210,13 +4208,11 @@ class Parser_OldPP } - if(!$linkBatch->isEmpty()){ + if ( !$linkBatch->isEmpty() ){ // construct query $titleClause = $linkBatch->constructSet('page', $dbr); - $variantQuery = "SELECT page_id, page_namespace, page_title"; - if ( $threshold > 0 ) { - $variantQuery .= ', page_len, page_is_redirect'; + $variantQuery = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect"; } $variantQuery .= " FROM $page WHERE $titleClause"; @@ -4229,7 +4225,7 @@ class Parser_OldPP // for each found variants, figure out link holders and replace while ( $s = $dbr->fetchObject($varRes) ) { - $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title ); + $variantTitle = Title::newFromRow( $s ); $varPdbk = $variantTitle->getPrefixedDBkey(); $vardbk = $variantTitle->getDBkey(); diff --git a/includes/SpecialContributions.php b/includes/SpecialContributions.php index 59bd77c1d4..63e14c509d 100644 --- a/includes/SpecialContributions.php +++ b/includes/SpecialContributions.php @@ -11,7 +11,7 @@ class ContribsPager extends ReverseChronologicalPager { function __construct( $target, $namespace = false, $year = false, $month = false ) { parent::__construct(); - foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) { + foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) { $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') ); } $this->target = $target; @@ -42,7 +42,7 @@ class ContribsPager extends ReverseChronologicalPager { 'fields' => array( 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user', - 'rev_user_text', 'rev_deleted' + 'rev_user_text', 'rev_parent_id', 'rev_deleted' ), 'conds' => $conds, 'options' => array( 'USE INDEX' => $index ) @@ -165,6 +165,12 @@ class ContribsPager extends ReverseChronologicalPager { if( $rev->isDeleted( Revision::DELETED_TEXT ) ) { $d = '' . $d . ''; } + + if( $rev->getParentId() === 0 ) { + $nflag = '' . $this->messages['newpageletter'] . ''; + } else { + $nflag = ''; + } if( $row->rev_minor_edit ) { $mflag = '' . $this->messages['minoreditletter'] . ' '; @@ -172,7 +178,7 @@ class ContribsPager extends ReverseChronologicalPager { $mflag = ''; } - $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link}{$userlink}{$comment} {$topmarktext}"; + $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink}{$comment} {$topmarktext}"; if( $rev->isDeleted( Revision::DELETED_TEXT ) ) { $ret .= ' ' . wfMsgHtml( 'deletedrev' ); } diff --git a/includes/Title.php b/includes/Title.php index 0c91eab141..e23387e2db 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -63,6 +63,8 @@ class Title { var $mDefaultNamespace; # Namespace index when there is no namespace # Zero except in {{transclusion}} tags var $mWatched; # Is $wgUser watching this page? NULL if unfilled, accessed through userIsWatching() + var $mLength; # The page length, 0 for special pages + var $mRedirect; # Is the article at this title a redirect? /**#@-*/ @@ -83,6 +85,8 @@ class Title { $this->mWatched = NULL; $this->mLatestID = false; $this->mOldRestrictions = false; + $this->mLength = -1; + $this->mRedirect = null; } /** @@ -220,6 +224,20 @@ class Title { } return $titles; } + + /** + * Make a Title object from a DB row + * @param Row $row (needs at least page_title,page_namespace) + */ + 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) ? $row->page_is_redirect : -1; + $t->mLatest = isset($row->page_latest) ? $row->page_latest : 0; + + return $t; + } /** * Create a new Title from a namespace index and a DB key. @@ -1463,6 +1481,47 @@ 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->mArticleID <= 0 ) + return 0; + + $dbr = wfGetDB( DB_SLAVE ); + $redir = $dbr->selectField( 'page', 'page_is_redirect', + array( 'page_id' => $this->mArticleID ), + __METHOD__ ); + + $this->mRedirect = $redir ? true : false; + + return $this->mRedirect; + } + + /** + * What is the length of this page (-1 for special pages)? + * @return bool + */ + public function getLength() { + if( $this->mLength != -1 || $this->mArticleID == 0 ) + return $this->mLength; + # Zero for special pages + if( $this->mArticleID <= 0 ) + return -1; + + $dbr = wfGetDB( DB_SLAVE ); + $len = $dbr->selectField( 'page', 'page_len', + array( 'page_id' => $this->mArticleID ), + __METHOD__ ); + $this->mLength = intval($len); + + return $this->mLength; + } /** * Is this a subpage? @@ -2173,7 +2232,7 @@ class Title { } $res = $db->select( array( 'page', $table ), - array( 'page_namespace', 'page_title', 'page_id' ), + array( 'page_namespace', 'page_title', 'page_id', 'page_len', 'page_is_redirect' ), array( "{$prefix}_from=page_id", "{$prefix}_namespace" => $this->getNamespace(), @@ -2185,7 +2244,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_redir ); $retVal[] = $titleObj; } } diff --git a/includes/WatchlistEditor.php b/includes/WatchlistEditor.php index 7e37dca78e..36b3899f5b 100644 --- a/includes/WatchlistEditor.php +++ b/includes/WatchlistEditor.php @@ -205,14 +205,15 @@ class WatchlistEditor { $dbr = wfGetDB( DB_MASTER ); $uid = intval( $user->getId() ); list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' ); - $sql = "SELECT wl_namespace, wl_title, page_id, page_is_redirect + $sql = "SELECT wl_namespace, wl_title, page_id, page_len, page_is_redirect, + page_namespace, page_title FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace AND wl_title = page_title ) WHERE wl_user = {$uid}"; $res = $dbr->query( $sql, __METHOD__ ); if( $res && $dbr->numRows( $res ) > 0 ) { $cache = LinkCache::singleton(); while( $row = $dbr->fetchObject( $res ) ) { - $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ); + $title = Title::newFromRow( $row ); if( $title instanceof Title ) { // Update the link cache while we're at it if( $row->page_id ) { diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index b617e3e435..8f890928b7 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -844,14 +844,15 @@ abstract class File { list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' ); $encName = $db->addQuotes( $this->getName() ); - $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options"; + $sql = "SELECT page_namespace,page_title,page_id,page_len,page_is_redirect, + FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options"; $res = $db->query( $sql, __METHOD__ ); $retVal = array(); 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 ); + if ( $titleObj = Title::newFromRow( $row ) ) { + $linkCache->addGoodLinkObj( $row->page_id, $titleObj, $row->page_len, $row->page_is_redirect ); $retVal[] = $titleObj; } }