X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FWikiPage.php;h=8bfc4e19fa1002230f7cc73e701977b2b8de88e6;hb=a82a7940f667d4eccdc0197d7ded786dc5efe474;hp=8a823cde0ea915f4d351caefe7dc17b25534c594;hpb=1fa782d4fdaedefc123324fc54c5015aac5faed6;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 8a823cde0e..8bfc4e19fa 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -15,7 +15,6 @@ abstract class Page {} class WikiPage extends Page { /** * @var Title - * @protected */ public $mTitle = null; @@ -25,19 +24,22 @@ class WikiPage extends Page { public $mCounter = -1; // !< Integer (-1 means "not loaded") public $mDataLoaded = false; // !< Boolean public $mIsRedirect = false; // !< Boolean - public $mLatest = false; // !< Boolean + public $mLatest = false; // !< Integer (false means "not loaded") public $mPreparedEdit = false; // !< Array - public $mRedirectTarget = null; // !< Title object - public $mLastRevision = null; // !< Revision object - public $mTimestamp = ''; // !< String - public $mTouched = '19700101000000'; // !< String - /**@}}*/ /** - * @protected - * @var ParserOptions: ParserOptions object for $wgUser articles + * @var Title + */ + protected $mRedirectTarget = null; + + /** + * @var Revision */ - public $mParserOptions; + protected $mLastRevision = null; + + protected $mTimestamp = ''; // !< String + protected $mTouched = '19700101000000'; // !< String + /**@}}*/ /** * Constructor and clear the article @@ -48,17 +50,47 @@ class WikiPage extends Page { } /** - * Constructor from a page id + * Create a WikiPage object of the appropriate class for the given title. * - * Always override this for all subclasses (until we use PHP with LSB) + * @param $title Title + * @return WikiPage object of the appropriate type + */ + public static function factory( Title $title ) { + $ns = $title->getNamespace(); + + if ( $ns == NS_MEDIA ) { + throw new MWException( "NS_MEDIA is a virtual namespace; use NS_FILE." ); + } elseif ( $ns < 0 ) { + throw new MWException( "Invalid or virtual namespace $ns given." ); + } + + switch ( $ns ) { + case NS_FILE: + $page = new WikiFilePage( $title ); + break; + case NS_CATEGORY: + $page = new WikiCategoryPage( $title ); + break; + default: + $page = new WikiPage( $title ); + } + + return $page; + } + + /** + * Constructor from a page id * * @param $id Int article ID to load + * + * @return WikiPage */ public static function newFromID( $id ) { $t = Title::newFromID( $id ); - # @todo FIXME: Doesn't inherit right - return $t == null ? null : new self( $t ); - # return $t == null ? null : new static( $t ); // PHP 5.3 + if ( $t ) { + return self::factory( $t ); + } + return null; } /** @@ -67,6 +99,8 @@ class WikiPage extends Page { * (and only when) $wgActions[$action] === true. This allows subclasses * to override the default behavior. * + * @todo: move this UI stuff somewhere else + * * @return Array */ public function getActionOverrides() { @@ -133,11 +167,11 @@ class WikiPage extends Page { $dbw = wfGetDB( DB_MASTER ); $dbw->replace( 'redirect', array( 'rd_from' ), array( - 'rd_from' => $this->getId(), - 'rd_namespace' => $rt->getNamespace(), - 'rd_title' => $rt->getDBkey(), - 'rd_fragment' => $rt->getFragment(), - 'rd_interwiki' => $rt->getInterwiki(), + 'rd_from' => $this->getId(), + 'rd_namespace' => $rt->getNamespace(), + 'rd_title' => $rt->getDBkey(), + 'rd_fragment' => $rt->getFragment(), + 'rd_interwiki' => $rt->getInterwiki(), ), __METHOD__ ); @@ -171,7 +205,7 @@ class WikiPage extends Page { return $rt->getFullURL( 'rdfrom=' . urlencode( $source ) ); } } else { - if ( $rt->getNamespace() == NS_SPECIAL ) { + if ( $rt->isSpecialPage() ) { // Gotta handle redirects to special pages differently: // Fill the HTTP response "Location" header and ignore // the rest of the page we're on. @@ -249,6 +283,8 @@ class WikiPage extends Page { /** * Return the list of revision fields that should be selected to create * a new page. + * + * @return array */ public static function selectFields() { return array( @@ -313,18 +349,31 @@ class WikiPage extends Page { * Set the general counter, title etc data loaded from * some source. * - * @param $data Object|String $res->fetchObject() object or the string "fromdb" to reload + * @param $data Object|String One of the following: + * A DB query result object or... + * "fromdb" to get from a slave DB or... + * "fromdbmaster" to get from the master DB + * @return void */ public function loadPageData( $data = 'fromdb' ) { - if ( $data === 'fromdb' ) { - $dbr = wfGetDB( DB_SLAVE ); - $data = $this->pageDataFromTitle( $dbr, $this->mTitle ); + if ( $data === 'fromdbmaster' ) { + $data = $this->pageDataFromTitle( wfGetDB( DB_MASTER ), $this->mTitle ); + } elseif ( $data === 'fromdb' ) { // slave + $data = $this->pageDataFromTitle( wfGetDB( DB_SLAVE ), $this->mTitle ); + # Use a "last rev inserted" timestamp key to dimish the issue of slave lag. + # Note that DB also stores the master position in the session and checks it. + $touched = $this->getCachedLastEditTime(); + if ( $touched ) { // key set + if ( !$data || $touched > wfTimestamp( TS_MW, $data->page_touched ) ) { + $data = $this->pageDataFromTitle( wfGetDB( DB_MASTER ), $this->mTitle ); + } + } } $lc = LinkCache::singleton(); if ( $data ) { - $lc->addGoodLinkObj( $data->page_id, $this->mTitle, $data->page_len, $data->page_is_redirect, $data->page_latest ); + $lc->addGoodLinkObjFromRow( $this->mTitle, $data ); $this->mTitle->loadFromRow( $data ); @@ -501,7 +550,7 @@ class WikiPage extends Page { * Revision::FOR_THIS_USER to be displayed to $wgUser * Revision::RAW get the text regardless of permissions * @return String|false The text of the current revision - */ + */ public function getText( $audience = Revision::FOR_PUBLIC ) { $this->loadLastEdit(); if ( $this->mLastRevision ) { @@ -527,7 +576,7 @@ class WikiPage extends Page { * @return string MW timestamp of last article revision */ public function getTimestamp() { - // Check if the field has been filled by ParserCache::get() + // Check if the field has been filled by WikiPage::setTimestamp() if ( !$this->mTimestamp ) { $this->loadLastEdit(); } @@ -618,7 +667,7 @@ class WikiPage extends Page { if ( $dbr->implicitGroupby() ) { $realNameField = 'user_real_name'; } else { - $realNameField = 'FIRST(user_real_name) AS user_real_name'; + $realNameField = 'MIN(user_real_name) AS user_real_name'; } $tables = array( 'revision', 'user' ); @@ -659,18 +708,59 @@ class WikiPage extends Page { /** * Should the parser cache be used? * - * @param $user User The relevant user + * @param $parserOptions ParserOptions to check + * @param $oldid int * @return boolean */ - public function isParserCacheUsed( User $user, $oldid ) { + public function isParserCacheUsed( ParserOptions $parserOptions, $oldid ) { global $wgEnableParserCache; return $wgEnableParserCache - && $user->getStubThreshold() == 0 + && $parserOptions->getStubThreshold() == 0 && $this->exists() - && empty( $oldid ) - && !$this->mTitle->isCssOrJsPage() - && !$this->mTitle->isCssJsSubpage(); + && ( $oldid === null || $oldid === 0 || $oldid === $this->getLatest() ) + && $this->mTitle->isWikitextPage(); + } + + /** + * Get a ParserOutput for the given ParserOptions and revision ID. + * The the parser cache will be used if possible. + * + * @since 1.19 + * @param $parserOptions ParserOptions to use for the parse operation + * @param $oldid Revision ID to get the text from, passing null or 0 will + * get the current revision (default value) + * @return ParserOutput or false if the revision was not found + */ + public function getParserOutput( ParserOptions $parserOptions, $oldid = null ) { + global $wgParser; + + wfProfileIn( __METHOD__ ); + + $useParserCache = $this->isParserCacheUsed( $parserOptions, $oldid ); + wfDebug( __METHOD__ . ': using parser cache: ' . ( $useParserCache ? 'yes' : 'no' ) . "\n" ); + if ( $parserOptions->getStubThreshold() ) { + wfIncrStats( 'pcache_miss_stub' ); + } + + if ( $useParserCache ) { + $parserOutput = ParserCache::singleton()->get( $this, $parserOptions ); + if ( $parserOutput !== false ) { + wfProfileOut( __METHOD__ ); + return $parserOutput; + } + } + + if ( $oldid === null || $oldid === 0 ) { + $oldid = $this->getLatest(); + } + + $pool = new PoolWorkArticleView( $this, $parserOptions, $oldid, $useParserCache ); + $pool->execute(); + + wfProfileOut( __METHOD__ ); + + return $pool->getParserOutput(); } /** @@ -706,6 +796,7 @@ class WikiPage extends Page { MessageCache::singleton()->replace( $this->mTitle->getDBkey(), $text ); } + return true; } /** @@ -717,7 +808,6 @@ class WikiPage extends Page { * * @param $dbw DatabaseBase * @return int The newly created page_id key, or false if the title already existed - * @private */ public function insertOn( $dbw ) { wfProfileIn( __METHOD__ ); @@ -753,7 +843,7 @@ class WikiPage extends Page { * * @param $dbw DatabaseBase: object * @param $revision Revision: For ID number, and text used to set - length and redirect status fields + * length and redirect status fields * @param $lastRevision Integer: if given, will not overwrite the page field * when different from the currently set value. * Giving 0 indicates the new page flag should be set @@ -767,6 +857,7 @@ class WikiPage extends Page { wfProfileIn( __METHOD__ ); $text = $revision->getText(); + $len = strlen( $text ); $rt = Title::newFromRedirectRecurse( $text ); $conditions = array( 'page_id' => $this->getId() ); @@ -776,13 +867,14 @@ class WikiPage extends Page { $conditions['page_latest'] = $lastRevision; } + $now = wfTimestampNow(); $dbw->update( 'page', array( /* SET */ 'page_latest' => $revision->getId(), - 'page_touched' => $dbw->timestamp(), + 'page_touched' => $dbw->timestamp( $now ), 'page_is_new' => ( $lastRevision === 0 ) ? 1 : 0, 'page_is_redirect' => $rt !== null ? 1 : 0, - 'page_len' => strlen( $text ), + 'page_len' => $len, ), $conditions, __METHOD__ ); @@ -790,12 +882,41 @@ class WikiPage extends Page { $result = $dbw->affectedRows() != 0; if ( $result ) { $this->updateRedirectOn( $dbw, $rt, $lastRevIsRedirect ); + $this->setLastEdit( $revision ); + $this->setCachedLastEditTime( $now ); + $this->mLatest = $revision->getId(); + $this->mIsRedirect = (bool)$rt; + # Update the LinkCache. + LinkCache::singleton()->addGoodLinkObj( $this->getId(), $this->mTitle, $len, $this->mIsRedirect, $this->mLatest ); } wfProfileOut( __METHOD__ ); return $result; } + /** + * Get the cached timestamp for the last time the page changed. + * This is only used to help handle slave lag by comparing to page_touched. + * @return string MW timestamp + */ + protected function getCachedLastEditTime() { + global $wgMemc; + $key = wfMemcKey( 'page-lastedit', md5( $this->mTitle->getPrefixedDBkey() ) ); + return $wgMemc->get( $key ); + } + + /** + * Set the cached timestamp for the last time the page changed. + * This is only used to help handle slave lag by comparing to page_touched. + * @param $timestamp string + * @return void + */ + public function setCachedLastEditTime( $timestamp ) { + global $wgMemc; + $key = wfMemcKey( 'page-lastedit', md5( $this->mTitle->getPrefixedDBkey() ) ); + $wgMemc->set( $key, wfTimestamp( TS_MW, $timestamp ), 60*15 ); + } + /** * Add row to the redirect table if this is a redirect, remove otherwise. * @@ -904,9 +1025,11 @@ class WikiPage extends Page { if ( $section == 'new' ) { # Inserting a new section $subject = $summary ? wfMsgForContent( 'newsectionheaderdefaultlevel', $summary ) . "\n\n" : ''; - $text = strlen( trim( $oldtext ) ) > 0 + if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) { + $text = strlen( trim( $oldtext ) ) > 0 ? "{$oldtext}\n\n{$subject}{$text}" : "{$subject}{$text}"; + } } else { # Replacing an existing section; roll out the big guns global $wgParser; @@ -959,7 +1082,7 @@ class WikiPage extends Page { * Fill in blank summaries with generated text where possible * * If neither EDIT_NEW nor EDIT_UPDATE is specified, the status of the article will be detected. - * If EDIT_UPDATE is specified and the article doesn't exist, the function will an + * If EDIT_UPDATE is specified and the article doesn't exist, the function will return an * edit-gone-missing error. If EDIT_NEW is specified and the article does exist, an * edit-already-exists error will be returned. These two conditions are also possible with * auto-detection due to MediaWiki's performance-optimised locking strategy. @@ -996,7 +1119,7 @@ class WikiPage extends Page { $status = Status::newGood( array() ); # Load $this->mTitle->getArticleID() and $this->mLatest if it's not already - $this->loadPageData(); + $this->loadPageData( 'fromdbmaster' ); $flags = $this->checkFlags( $flags ); @@ -1019,11 +1142,13 @@ class WikiPage extends Page { $oldtext = $this->getRawText(); // current revision $oldsize = strlen( $oldtext ); + $oldid = $this->getLatest(); + $oldIsRedirect = $this->isRedirect(); $oldcountable = $this->isCountable(); # Provide autosummaries if one is not provided and autosummaries are enabled. if ( $wgUseAutomaticEditSummaries && $flags & EDIT_AUTOSUMMARY && $summary == '' ) { - $summary = $this->getAutosummary( $oldtext, $text, $flags ); + $summary = self::getAutosummary( $oldtext, $text, $flags ); } $editInfo = $this->prepareTextForEdit( $text, null, $user ); @@ -1048,7 +1173,7 @@ class WikiPage extends Page { 'comment' => $summary, 'minor_edit' => $isminor, 'text' => $text, - 'parent_id' => $this->mLatest, + 'parent_id' => $oldid, 'user' => $user->getId(), 'user_text' => $user->getName(), 'timestamp' => $now @@ -1076,7 +1201,7 @@ class WikiPage extends Page { # edit conflicts reliably, either by $ok here, or by $article->getTimestamp() # before this function is called. A previous function used a separate query, this # creates a window where concurrent edits can cause an ignored edit conflict. - $ok = $this->updateRevisionOn( $dbw, $revision, $this->mLatest ); + $ok = $this->updateRevisionOn( $dbw, $revision, $oldid, $oldIsRedirect ); if ( !$ok ) { /* Belated edit conflict! Run away!! */ @@ -1099,7 +1224,7 @@ class WikiPage extends Page { $this->mTitle->getUserPermissionsErrors( 'autopatrol', $user ) ); # Add RC row to the DB $rc = RecentChange::notifyEdit( $now, $this->mTitle, $isminor, $user, $summary, - $this->mLatest, $this->getTimestamp(), $bot, '', $oldsize, $newsize, + $oldid, $this->getTimestamp(), $bot, '', $oldsize, $newsize, $revisionId, $patrolled ); @@ -1166,11 +1291,6 @@ class WikiPage extends Page { ) ); $revisionId = $revision->insertOn( $dbw ); - $this->mTitle->resetArticleID( $newid ); - # Update the LinkCache. Resetting the Title ArticleID means it will rely on having that already cached - # @todo FIXME? - LinkCache::singleton()->addGoodLinkObj( $newid, $this->mTitle, strlen( $text ), (bool)Title::newFromRedirect( $text ), $revisionId ); - # Update the page record with revision data $this->updateRevisionOn( $dbw, $revision, 0 ); @@ -1204,7 +1324,7 @@ class WikiPage extends Page { # Do updates right now unless deferral was requested if ( !( $flags & EDIT_DEFER_UPDATES ) ) { - wfDoUpdates(); + DeferredUpdates::doUpdates(); } // Return the new revision (or null) to the caller @@ -1223,6 +1343,8 @@ class WikiPage extends Page { /** * Update the article's restriction field, and leave a log entry. * + * @todo: seperate the business/permission stuff out from backend code + * * @param $limit Array: set of restriction keys * @param $reason String * @param &$cascade Integer. Set to false if cascading protection isn't allowed. @@ -1337,7 +1459,7 @@ class WikiPage extends Page { $encodedExpiry[$action] = $dbw->encodeExpiry( $expiry[$action] ); if ( $restrictions != '' ) { - $protect_description .= "[$action=$restrictions] ("; + $protect_description .= $wgContLang->getDirMark() . "[$action=$restrictions] ("; if ( $encodedExpiry[$action] != 'infinity' ) { $protect_description .= wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry[$action], false, false ) , @@ -1454,10 +1576,6 @@ class WikiPage extends Page { */ public function estimateRevisionCount() { $dbr = wfGetDB( DB_SLAVE ); - - // For an exact count... - // return $dbr->selectField( 'revision', 'COUNT(*)', - // array( 'rev_page' => $this->getId() ), __METHOD__ ); return $dbr->estimateRowCount( 'revision', '*', array( 'rev_page' => $this->getId() ), __METHOD__ ); } @@ -1519,7 +1637,7 @@ class WikiPage extends Page { * Deletes the article with database consistency, writes logs, purges caches * * @param $reason string delete reason for deletion log - * @param suppress bitfield + * @param $suppress bitfield * Revision::DELETED_TEXT * Revision::DELETED_COMMENT * Revision::DELETED_USER @@ -1533,7 +1651,7 @@ class WikiPage extends Page { public function doDeleteArticle( $reason, $suppress = false, $id = 0, $commit = true, &$error = '', User $user = null ) { - global $wgDeferredUpdateList, $wgUseTrackbacks, $wgUser; + global $wgUseTrackbacks, $wgUser; $user = is_null( $user ) ? $wgUser : $user; wfDebug( __METHOD__ . "\n" ); @@ -1549,8 +1667,9 @@ class WikiPage extends Page { return false; } - $u = new SiteStatsUpdate( 0, 1, - (int)$this->isCountable(), -1 ); - array_push( $wgDeferredUpdateList, $u ); + DeferredUpdates::addUpdate( + new SiteStatsUpdate( 0, 1, - (int)$this->isCountable(), -1 ) + ); // Bitfields to further suppress the content if ( $suppress ) { @@ -1585,12 +1704,14 @@ class WikiPage extends Page { 'ar_timestamp' => 'rev_timestamp', 'ar_minor_edit' => 'rev_minor_edit', 'ar_rev_id' => 'rev_id', + 'ar_parent_id' => 'rev_parent_id', 'ar_text_id' => 'rev_text_id', 'ar_text' => '\'\'', // Be explicit to appease 'ar_flags' => '\'\'', // MySQL's "strict mode"... 'ar_len' => 'rev_len', 'ar_page_id' => 'page_id', - 'ar_deleted' => $bitfield + 'ar_deleted' => $bitfield, + 'ar_sha1' => 'rev_sha1' ), array( 'page_id' => $id, 'page_id = rev_page' @@ -1623,18 +1744,20 @@ class WikiPage extends Page { if ( !$dbw->cascadingDeletes() ) { $dbw->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ ); - if ( $wgUseTrackbacks ) + if ( $wgUseTrackbacks ) { $dbw->delete( 'trackbacks', array( 'tb_page' => $id ), __METHOD__ ); + } # Delete outgoing links - $dbw->delete( 'pagelinks', array( 'pl_from' => $id ) ); - $dbw->delete( 'imagelinks', array( 'il_from' => $id ) ); - $dbw->delete( 'categorylinks', array( 'cl_from' => $id ) ); - $dbw->delete( 'templatelinks', array( 'tl_from' => $id ) ); - $dbw->delete( 'externallinks', array( 'el_from' => $id ) ); - $dbw->delete( 'langlinks', array( 'll_from' => $id ) ); - $dbw->delete( 'iwlinks', array( 'iwl_from' => $id ) ); - $dbw->delete( 'redirect', array( 'rd_from' => $id ) ); + $dbw->delete( 'pagelinks', array( 'pl_from' => $id ), __METHOD__ ); + $dbw->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ ); + $dbw->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ ); + $dbw->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ ); + $dbw->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ ); + $dbw->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ ); + $dbw->delete( 'iwlinks', array( 'iwl_from' => $id ), __METHOD__ ); + $dbw->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ ); + $dbw->delete( 'page_props', array( 'pp_page' => $id ), __METHOD__ ); } # If using cleanup triggers, we can skip some manual deletes @@ -1658,10 +1781,13 @@ class WikiPage extends Page { # Log the deletion, if the page was suppressed, log it at Oversight instead $logtype = $suppress ? 'suppress' : 'delete'; - $log = new LogPage( $logtype ); - # Make sure logging got through - $log->addEntry( 'delete', $this->mTitle, $reason, array() ); + $logEntry = new ManualLogEntry( $logtype, 'delete' ); + $logEntry->setPerformer( $user ); + $logEntry->setTarget( $this->mTitle ); + $logEntry->setComment( $reason ); + $logid = $logEntry->insert(); + $logEntry->publish( $logid ); if ( $commit ) { $dbw->commit(); @@ -1677,6 +1803,8 @@ class WikiPage extends Page { * roll back to, e.g. user is the sole contributor. This function * performs permissions checks on $user, then calls commitRollback() * to do the dirty work + * + * @todo: seperate the business/permission stuff out from backend code * * @param $fromP String: Name of the user whose edits to rollback. * @param $summary String: Custom summary. Set to default summary if empty. @@ -1724,7 +1852,7 @@ class WikiPage extends Page { * and return value documentation * * NOTE: This function does NOT check ANY permissions, it just commits the - * rollback to the DB Therefore, you should only call this function direct- + * rollback to the DB. Therefore, you should only call this function direct- * ly if you want to use custom permissions checks. If you don't, use * doRollback() instead. * @param $fromP String: Name of the user whose edits to rollback. @@ -1857,15 +1985,15 @@ class WikiPage extends Page { * @param $user User The relevant user */ public function doViewUpdates( User $user ) { - global $wgDeferredUpdateList, $wgDisableCounters; + global $wgDisableCounters; if ( wfReadOnly() ) { return; } # Don't update page view counters on views from bot users (bug 14044) if ( !$wgDisableCounters && !$user->isAllowed( 'bot' ) && $this->getId() ) { - $wgDeferredUpdateList[] = new ViewCountUpdate( $this->getId() ); - $wgDeferredUpdateList[] = new SiteStatsUpdate( 1, 0, 0 ); + DeferredUpdates::addUpdate( new ViewCountUpdate( $this->getId() ) ); + DeferredUpdates::addUpdate( new SiteStatsUpdate( 1, 0, 0 ) ); } # Update newtalk / watchlist notification status @@ -1877,7 +2005,7 @@ class WikiPage extends Page { * Returns a stdclass with source, pst and output members */ public function prepareTextForEdit( $text, $revid = null, User $user = null ) { - global $wgParser, $wgUser; + global $wgParser, $wgContLang, $wgUser; $user = is_null( $user ) ? $wgUser : $user; // @TODO fixme: check $user->getId() here??? if ( $this->mPreparedEdit @@ -1888,14 +2016,14 @@ class WikiPage extends Page { return $this->mPreparedEdit; } - $popts = ParserOptions::newFromUser( $user ); + $popts = ParserOptions::newFromUserAndLang( $user, $wgContLang ); wfRunHooks( 'ArticlePrepareTextForEdit', array( $this, $popts ) ); $edit = (object)array(); $edit->revid = $revid; $edit->newText = $text; - $edit->pst = $this->preSaveTransform( $text, $user, $popts ); - $edit->popts = $this->getParserOptions( true ); + $edit->pst = $wgParser->preSaveTransform( $text, $this->mTitle, $user, $popts ); + $edit->popts = $this->makeParserOptions( 'canonical' ); $edit->output = $wgParser->parse( $edit->pst, $this->mTitle, $edit->popts, true, true, $revid ); $edit->oldText = $this->getRawText(); @@ -1922,7 +2050,7 @@ class WikiPage extends Page { * - null: don't change the article count */ public function doEditUpdates( Revision $revision, User $user, array $options = array() ) { - global $wgDeferredUpdateList, $wgEnableParserCache; + global $wgEnableParserCache; wfProfileIn( __METHOD__ ); @@ -1990,8 +2118,8 @@ class WikiPage extends Page { $total = 0; } - $wgDeferredUpdateList[] = new SiteStatsUpdate( 0, 1, $good, $total ); - $wgDeferredUpdateList[] = new SearchUpdate( $id, $title, $text ); + DeferredUpdates::addUpdate( new SiteStatsUpdate( 0, 1, $good, $total ) ); + DeferredUpdates::addUpdate( new SearchUpdate( $id, $title, $text ) ); # If this is another user's talk page, update newtalk. # Don't do this if $options['changed'] = false (null-edits) nor if @@ -2037,7 +2165,7 @@ class WikiPage extends Page { * @todo This is a shitty interface function. Kill it and replace the * other shitty functions like doEditUpdates and such so it's not needed * anymore. - * @deprecated since 1.19, use doEditUpdates() + * @deprecated since 1.18, use doEditUpdates() */ public function createUpdates( $rev ) { global $wgUser; @@ -2048,6 +2176,7 @@ class WikiPage extends Page { * This function is called right before saving the wikitext, * so we can do things like signatures and links-in-context. * + * @deprecated in 1.19; use Parser::preSaveTransform() instead * @param $text String article contents * @param $user User object: user doing the edit * @param $popts ParserOptions object: parser options, default options for @@ -2057,6 +2186,9 @@ class WikiPage extends Page { */ public function preSaveTransform( $text, User $user = null, ParserOptions $popts = null ) { global $wgParser, $wgUser; + + wfDeprecated( __METHOD__ ); + $user = is_null( $user ) ? $wgUser : $user; if ( $popts === null ) { @@ -2190,7 +2322,9 @@ class WikiPage extends Page { # User talk pages if ( $title->getNamespace() == NS_USER_TALK ) { $user = User::newFromName( $title->getText(), false ); - $user->setNewtalk( false ); + if ( $user ) { + $user->setNewtalk( false ); + } } # Image redirects @@ -2204,13 +2338,12 @@ class WikiPage extends Page { * @todo: verify that $title is always a Title object (and never false or null), add Title hint to parameter $title */ public static function onArticleEdit( $title ) { - global $wgDeferredUpdateList; - // Invalidate caches of articles which include this page - $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'templatelinks' ); + DeferredUpdates::addHTMLCacheUpdate( $title, 'templatelinks' ); + // Invalidate the caches of all pages which redirect here - $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'redirect' ); + DeferredUpdates::addHTMLCacheUpdate( $title, 'redirect' ); # Purge squid for this page only $title->purgeSquid(); @@ -2299,7 +2432,10 @@ class WikiPage extends Page { if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) { $truncatedtext = $wgContLang->truncate( str_replace( "\n", ' ', $newtext ), - max( 0, 200 - strlen( wfMsgForContent( 'autoredircomment' ) ) ) ); + max( 0, 250 + - strlen( wfMsgForContent( 'autoredircomment' ) ) + - strlen( $rt->getFullText() ) + ) ); return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext ); } @@ -2333,36 +2469,111 @@ class WikiPage extends Page { } /** - * Get parser options suitable for rendering the primary article wikitext - * @param $canonical boolean Determines that the generated options must not depend on user preferences (see bug 14404) - * @return mixed ParserOptions object or boolean false + * Auto-generates a deletion reason + * + * @param &$hasHistory Boolean: whether the page has a history + * @return mixed String containing deletion reason or empty string, or boolean false + * if no revision occurred */ - public function getParserOptions( $canonical = false ) { - global $wgUser, $wgLanguageCode; + public function getAutoDeleteReason( &$hasHistory ) { + global $wgContLang; - if ( !$this->mParserOptions || $canonical ) { - $user = !$canonical ? $wgUser : new User; - $parserOptions = new ParserOptions( $user ); - $parserOptions->setTidy( true ); - $parserOptions->enableLimitReport(); + $dbw = wfGetDB( DB_MASTER ); + // Get the last revision + $rev = Revision::newFromTitle( $this->getTitle() ); - if ( $canonical ) { - $parserOptions->setUserLang( $wgLanguageCode ); # Must be set explicitely - return $parserOptions; + if ( is_null( $rev ) ) { + return false; + } + + // Get the article's contents + $contents = $rev->getText(); + $blank = false; + + // If the page is blank, use the text from the previous revision, + // which can only be blank if there's a move/import/protect dummy revision involved + if ( $contents == '' ) { + $prev = $rev->getPrevious(); + + if ( $prev ) { + $contents = $prev->getText(); + $blank = true; + } + } + + // Find out if there was only one contributor + // Only scan the last 20 revisions + $res = $dbw->select( 'revision', 'rev_user_text', + array( 'rev_page' => $this->getID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ), + __METHOD__, + array( 'LIMIT' => 20 ) + ); + + if ( $res === false ) { + // This page has no revisions, which is very weird + return false; + } + + $hasHistory = ( $res->numRows() > 1 ); + $row = $dbw->fetchObject( $res ); + + if ( $row ) { // $row is false if the only contributor is hidden + $onlyAuthor = $row->rev_user_text; + // Try to find a second contributor + foreach ( $res as $row ) { + if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999 + $onlyAuthor = false; + break; + } } - $this->mParserOptions = $parserOptions; + } else { + $onlyAuthor = false; } - // Clone to allow modifications of the return value without affecting cache - return clone $this->mParserOptions; + + // Generate the summary with a '$1' placeholder + if ( $blank ) { + // The current revision is blank and the one before is also + // blank. It's just not our lucky day + $reason = wfMsgForContent( 'exbeforeblank', '$1' ); + } else { + if ( $onlyAuthor ) { + $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor ); + } else { + $reason = wfMsgForContent( 'excontent', '$1' ); + } + } + + if ( $reason == '-' ) { + // Allow these UI messages to be blanked out cleanly + return ''; + } + + // Replace newlines with spaces to prevent uglyness + $contents = preg_replace( "/[\n\r]/", ' ', $contents ); + // Calculate the maximum amount of chars to get + // Max content length = max comment length - length of the comment (excl. $1) + $maxLength = 255 - ( strlen( $reason ) - 2 ); + $contents = $wgContLang->truncate( $contents, $maxLength ); + // Remove possible unfinished links + $contents = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $contents ); + // Now replace the '$1' placeholder + $reason = str_replace( '$1', $contents, $reason ); + + return $reason; } /** * Get parser options suitable for rendering the primary article wikitext - * @param User $user + * @param User|string $user User object or 'canonical' * @return ParserOptions */ - public function makeParserOptions( User $user ) { - $options = ParserOptions::newFromUser( $user ); + public function makeParserOptions( $user ) { + global $wgContLang; + if ( $user instanceof User ) { // settings per user (even anons) + $options = ParserOptions::newFromUser( $user ); + } else { // canonical settings + $options = ParserOptions::newFromUserAndLang( new User, $wgContLang ); + } $options->enableLimitReport(); // show inclusion/loop reports $options->setTidy( true ); // fix bad HTML return $options; @@ -2435,7 +2646,7 @@ class WikiPage extends Page { * Updates cascading protections * * @param $parserOutput ParserOutput object for the current version - **/ + */ public function doCascadeProtectionUpdates( ParserOutput $parserOutput ) { if ( wfReadOnly() || !$this->mTitle->areRestrictionsCascading() ) { return; @@ -2482,27 +2693,206 @@ class WikiPage extends Page { } } - /* - * @deprecated since 1.19 - */ + /** + * @deprecated since 1.18 + */ public function quickEdit( $text, $comment = '', $minor = 0 ) { global $wgUser; return $this->doQuickEdit( $text, $wgUser, $comment, $minor ); } - /* - * @deprecated since 1.19 - */ + /** + * @deprecated since 1.18 + */ public function viewUpdates() { global $wgUser; return $this->doViewUpdates( $wgUser ); } - /* - * @deprecated since 1.19 - */ + /** + * @deprecated since 1.18 + */ public function useParserCache( $oldid ) { global $wgUser; - return $this->isParserCacheUsed( $wgUser, $oldid ); + return $this->isParserCacheUsed( ParserOptions::newFromUser( $wgUser ), $oldid ); + } +} + +class PoolWorkArticleView extends PoolCounterWork { + + /** + * @var Page + */ + private $page; + + /** + * @var string + */ + private $cacheKey; + + /** + * @var integer + */ + private $revid; + + /** + * @var ParserOptions + */ + private $parserOptions; + + /** + * @var string|null + */ + private $text; + + /** + * @var ParserOutput|false + */ + private $parserOutput = false; + + /** + * @var bool + */ + private $isDirty = false; + + /** + * @var Status|false + */ + private $error = false; + + /** + * Constructor + * + * @param $page Page + * @param $revid Integer: ID of the revision being parsed + * @param $useParserCache Boolean: whether to use the parser cache + * @param $parserOptions parserOptions to use for the parse operation + * @param $text String: text to parse or null to load it + */ + function __construct( Page $page, ParserOptions $parserOptions, $revid, $useParserCache, $text = null ) { + $this->page = $page; + $this->revid = $revid; + $this->cacheable = $useParserCache; + $this->parserOptions = $parserOptions; + $this->text = $text; + $this->cacheKey = ParserCache::singleton()->getKey( $page, $parserOptions ); + parent::__construct( 'ArticleView', $this->cacheKey . ':revid:' . $revid ); + } + + /** + * Get the ParserOutput from this object, or false in case of failure + * + * @return ParserOutput + */ + public function getParserOutput() { + return $this->parserOutput; + } + + /** + * Get whether the ParserOutput is a dirty one (i.e. expired) + * + * @return bool + */ + public function getIsDirty() { + return $this->isDirty; + } + + /** + * Get a Status object in case of error or false otherwise + * + * @return Status|false + */ + public function getError() { + return $this->error; + } + + /** + * @return bool + */ + function doWork() { + global $wgParser, $wgUseFileCache; + + $isCurrent = $this->revid === $this->page->getLatest(); + + if ( $this->text !== null ) { + $text = $this->text; + } elseif ( $isCurrent ) { + $text = $this->page->getRawText(); + } else { + $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid ); + if ( $rev === null ) { + return false; + } + $text = $rev->getText(); + } + + $time = - wfTime(); + $this->parserOutput = $wgParser->parse( $text, $this->page->getTitle(), + $this->parserOptions, true, true, $this->revid ); + $time += wfTime(); + + # Timing hack + if ( $time > 3 ) { + wfDebugLog( 'slow-parse', sprintf( "%-5.2f %s", $time, + $this->page->getTitle()->getPrefixedDBkey() ) ); + } + + if ( $this->cacheable && $this->parserOutput->isCacheable() ) { + ParserCache::singleton()->save( $this->parserOutput, $this->page, $this->parserOptions ); + } + + // Make sure file cache is not used on uncacheable content. + // Output that has magic words in it can still use the parser cache + // (if enabled), though it will generally expire sooner. + if ( !$this->parserOutput->isCacheable() || $this->parserOutput->containsOldMagic() ) { + $wgUseFileCache = false; + } + + if ( $isCurrent ) { + $this->page->doCascadeProtectionUpdates( $this->parserOutput ); + } + + return true; + } + + /** + * @return bool + */ + function getCachedWork() { + $this->parserOutput = ParserCache::singleton()->get( $this->page, $this->parserOptions ); + + if ( $this->parserOutput === false ) { + wfDebug( __METHOD__ . ": parser cache miss\n" ); + return false; + } else { + wfDebug( __METHOD__ . ": parser cache hit\n" ); + return true; + } + } + + /** + * @return bool + */ + function fallback() { + $this->parserOutput = ParserCache::singleton()->getDirty( $this->page, $this->parserOptions ); + + if ( $this->parserOutput === false ) { + wfDebugLog( 'dirty', "dirty missing\n" ); + wfDebug( __METHOD__ . ": no dirty cache\n" ); + return false; + } else { + wfDebug( __METHOD__ . ": sending dirty output\n" ); + wfDebugLog( 'dirty', "dirty output {$this->cacheKey}\n" ); + $this->isDirty = true; + return true; + } + } + + /** + * @param $status Status + */ + function error( $status ) { + $this->error = $status; + return false; } }