From f2b218381bfa0d8fc18df5578fa29e269e8b6ede Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 11 Jun 2012 15:36:52 +0200 Subject: [PATCH] make sure page_content_model gets loaded where appropriate in Title and LinkCache --- includes/Title.php | 72 +++++++++++++++++++++++++++--------- includes/WikiPage.php | 2 +- includes/cache/LinkCache.php | 20 ++++++---- 3 files changed, 68 insertions(+), 26 deletions(-) diff --git a/includes/Title.php b/includes/Title.php index 62c604b367..9255cb301c 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -201,6 +201,27 @@ class Title { } } + /** + * Returns a list of fields that are to be selected for initializing Title objects or LinkCache entries. + * Uses $wgContentHandlerUseDB to determine whether to include page_content_model. + * + * @return array + */ + protected static function getSelectFields() { + global $wgContentHandlerUseDB; + + $fields = array( + 'page_namespace', 'page_title', 'page_id', + 'page_len', 'page_is_redirect', 'page_latest', + ); + + if ( $wgContentHandlerUseDB ) { + $fields[] = 'page_content_model'; + } + + return $fields; + } + /** * Create a new Title from an article ID * @@ -212,10 +233,7 @@ class Title { $db = ( $flags & self::GAID_FOR_UPDATE ) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE ); $row = $db->selectRow( 'page', - array( - 'page_namespace', 'page_title', 'page_id', - 'page_len', 'page_is_redirect', 'page_latest', - ), + self::getSelectFields(), array( 'page_id' => $id ), __METHOD__ ); @@ -241,10 +259,7 @@ class Title { $res = $dbr->select( 'page', - array( - 'page_namespace', 'page_title', 'page_id', - 'page_len', 'page_is_redirect', 'page_latest', - ), + self::getSelectFields(), array( 'page_id' => $ids ), __METHOD__ ); @@ -283,17 +298,17 @@ class Title { if ( isset( $row->page_is_redirect ) ) $this->mRedirect = (bool)$row->page_is_redirect; if ( isset( $row->page_latest ) ) - $this->mLatestID = (int)$row->page_latest; # FIXME: whene3ver page_latest is updated, also update page_content_model + $this->mLatestID = (int)$row->page_latest; if ( isset( $row->page_content_model ) ) $this->mContentModel = intval( $row->page_content_model ); else - $this->mContentModel = null; # initialized lazily in getContentModel() + $this->mContentModel = false; # initialized lazily in getContentModel() } else { // page not found $this->mArticleID = 0; $this->mLength = 0; $this->mRedirect = false; $this->mLatestID = 0; - $this->mContentModel = null; # initialized lazily in getContentModel() + $this->mContentModel = false; # initialized lazily in getContentModel() } } @@ -319,7 +334,7 @@ class Title { $t->mArticleID = ( $ns >= 0 ) ? -1 : 0; $t->mUrlform = wfUrlencode( $t->mDbkeyform ); $t->mTextform = str_replace( '_', ' ', $title ); - $t->mContentModel = null; # initialized lazily in getContentModel() + $t->mContentModel = false; # initialized lazily in getContentModel() return $t; } @@ -659,11 +674,19 @@ class Title { * @return Integer: Content model id */ public function getContentModel() { - if ( empty( $this->mContentModel ) ) { + if ( !$this->mContentModel ) { + $linkCache = LinkCache::singleton(); + $this->mContentModel = $linkCache->getGoodLinkFieldObj( $this, 'model' ); + } + + if ( !$this->mContentModel ) { $this->mContentModel = ContentHandler::getDefaultModelFor( $this ); } - assert( !empty( $this->mContentModel ) ); + if( !$this->mContentModel ) { + throw new MWException( "failed to determin content model!" ); + } + return $this->mContentModel; } @@ -671,7 +694,7 @@ class Title { * Convenience method for checking a title's content model name * * @param int $id - * @return true if $this->getContentModel() == $id + * @return Boolean true if $this->getContentModel() == $id */ public function hasContentModel( $id ) { return $this->getContentModel() == $id; @@ -2893,6 +2916,7 @@ class Title { $this->mRedirect = null; $this->mLength = -1; $this->mLatestID = false; + $this->mContentModel = false; $this->mEstimateRevisions = null; } @@ -3131,7 +3155,7 @@ class Title { $res = $db->select( array( 'page', $table ), - array( 'page_namespace', 'page_title', 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ), + self::getSelectFields(), array( "{$prefix}_from=page_id", "{$prefix}_namespace" => $this->getNamespace(), @@ -3181,6 +3205,8 @@ class Title { * @return Array of Title objects linking here */ public function getLinksFrom( $options = array(), $table = 'pagelinks', $prefix = 'pl' ) { + global $wgContentHandlerUseDB; + $id = $this->getArticleID(); # If the page doesn't exist; there can't be any link from this page @@ -3197,9 +3223,12 @@ class Title { $namespaceFiled = "{$prefix}_namespace"; $titleField = "{$prefix}_title"; + $fields = array( $namespaceFiled, $titleField, 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ); + if ( $wgContentHandlerUseDB ) $fields[] = 'page_content_model'; + $res = $db->select( array( $table, 'page' ), - array( $namespaceFiled, $titleField, 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ), + $fields, array( "{$prefix}_from" => $id ), __METHOD__, $options, @@ -3761,10 +3790,16 @@ class Title { * @return Bool */ public function isSingleRevRedirect() { + global $wgContentHandlerUseDB; + $dbw = wfGetDB( DB_MASTER ); + # Is it a redirect? + $fields = array( 'page_is_redirect', 'page_latest', 'page_id' ); + if ( $wgContentHandlerUseDB ) $fields[] = 'page_content_model'; + $row = $dbw->selectRow( 'page', - array( 'page_is_redirect', 'page_latest', 'page_id' ), + $fields, $this->pageCond(), __METHOD__, array( 'FOR UPDATE' ) @@ -3773,6 +3808,7 @@ class Title { $this->mArticleID = $row ? intval( $row->page_id ) : 0; $this->mRedirect = $row ? (bool)$row->page_is_redirect : false; $this->mLatestID = $row ? intval( $row->page_latest ) : false; + $this->mContentModel = $row && isset( $row->page_content_model ) ? intval( $row->page_content_model ) : false; if ( !$this->mRedirect ) { return false; } diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 64cc51a626..5919c41cc0 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -1279,7 +1279,7 @@ class WikiPage extends Page { $this->mLatest = $revision->getId(); $this->mIsRedirect = (bool)$rt; # Update the LinkCache. - LinkCache::singleton()->addGoodLinkObj( $this->getId(), $this->mTitle, $len, $this->mIsRedirect, $this->mLatest ); + LinkCache::singleton()->addGoodLinkObj( $this->getId(), $this->mTitle, $len, $this->mIsRedirect, $this->mLatest, $revision->getContentModel() ); } wfProfileOut( __METHOD__ ); diff --git a/includes/cache/LinkCache.php b/includes/cache/LinkCache.php index f759c0206d..a48d007e51 100644 --- a/includes/cache/LinkCache.php +++ b/includes/cache/LinkCache.php @@ -74,7 +74,7 @@ class LinkCache { * Get a field of a title object from cache. * If this link is not good, it will return NULL. * @param $title Title - * @param $field String: ('length','redirect','revision') + * @param $field String: ('length','redirect','revision','model') * @return mixed */ public function getGoodLinkFieldObj( $title, $field ) { @@ -102,14 +102,16 @@ class LinkCache { * @param $len Integer: text's length * @param $redir Integer: whether the page is a redirect * @param $revision Integer: latest revision's ID + * @param $model Integer: latest revision's content model ID */ - public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) { + public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false, $model = false ) { $dbkey = $title->getPrefixedDbKey(); $this->mGoodLinks[$dbkey] = intval( $id ); $this->mGoodLinkFields[$dbkey] = array( 'length' => intval( $len ), 'redirect' => intval( $redir ), - 'revision' => intval( $revision ) ); + 'revision' => intval( $revision ), + 'model' => intval( $model ) ); } /** @@ -117,7 +119,7 @@ class LinkCache { * @since 1.19 * @param $title Title * @param $row object which has the fields page_id, page_is_redirect, - * page_latest + * page_latest and page_content_model */ public function addGoodLinkObjFromRow( $title, $row ) { $dbkey = $title->getPrefixedDbKey(); @@ -126,6 +128,7 @@ class LinkCache { 'length' => intval( $row->page_len ), 'redirect' => intval( $row->page_is_redirect ), 'revision' => intval( $row->page_latest ), + 'model' => !empty( $row->page_content_model ) ? intval( $row->page_content_model ) : null, ); } @@ -178,7 +181,8 @@ class LinkCache { * @return Integer */ public function addLinkObj( $nt ) { - global $wgAntiLockFlags; + global $wgAntiLockFlags, $wgContentHandlerUseDB; + wfProfileIn( __METHOD__ ); $key = $nt->getPrefixedDBkey(); @@ -210,8 +214,10 @@ class LinkCache { $options = array(); } - $s = $db->selectRow( 'page', - array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ), + $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ); + if ( $wgContentHandlerUseDB ) $f[] = 'page_content_model'; + + $s = $db->selectRow( 'page', $f, array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ), __METHOD__, $options ); # Set fields... -- 2.20.1