00001 <?php
00015 class Article {
00019 var $mComment = '';
00020 var $mContent;
00021 var $mContentLoaded = false;
00022 var $mCounter = -1;
00023 var $mCurID = -1;
00024 var $mDataLoaded = false;
00025 var $mForUpdate = false;
00026 var $mGoodAdjustment = 0;
00027 var $mIsRedirect = false;
00028 var $mLatest = false;
00029 var $mMinorEdit;
00030 var $mOldId;
00031 var $mPreparedEdit = false;
00032 var $mRedirectedFrom = null;
00033 var $mRedirectTarget = null;
00034 var $mRedirectUrl = false;
00035 var $mRevIdFetched = 0;
00036 var $mRevision;
00037 var $mTimestamp = '';
00038 var $mTitle;
00039 var $mTotalAdjustment = 0;
00040 var $mTouched = '19700101000000';
00041 var $mUser = -1;
00042 var $mUserText = '';
00043
00050 public function __construct( Title $title, $oldId = null ) {
00051 $this->mTitle =& $title;
00052 $this->mOldId = $oldId;
00053 }
00054
00059 public static function newFromID( $id ) {
00060 $t = Title::newFromID( $id );
00061 return $t == null ? null : new Article( $t );
00062 }
00063
00069 public function setRedirectedFrom( $from ) {
00070 $this->mRedirectedFrom = $from;
00071 }
00072
00080 public function getRedirectTarget() {
00081 if( !$this->mTitle || !$this->mTitle->isRedirect() )
00082 return null;
00083 if( !is_null($this->mRedirectTarget) )
00084 return $this->mRedirectTarget;
00085 # Query the redirect table
00086 $dbr = wfGetDB( DB_SLAVE );
00087 $row = $dbr->selectRow( 'redirect',
00088 array('rd_namespace', 'rd_title'),
00089 array('rd_from' => $this->getID() ),
00090 __METHOD__
00091 );
00092 if( $row ) {
00093 return $this->mRedirectTarget = Title::makeTitle($row->rd_namespace, $row->rd_title);
00094 }
00095 # This page doesn't have an entry in the redirect table
00096 return $this->mRedirectTarget = $this->insertRedirect();
00097 }
00098
00105 public function insertRedirect() {
00106 $retval = Title::newFromRedirect( $this->getContent() );
00107 if( !$retval ) {
00108 return null;
00109 }
00110 $dbw = wfGetDB( DB_MASTER );
00111 $dbw->replace( 'redirect', array('rd_from'),
00112 array(
00113 'rd_from' => $this->getID(),
00114 'rd_namespace' => $retval->getNamespace(),
00115 'rd_title' => $retval->getDBKey()
00116 ),
00117 __METHOD__
00118 );
00119 return $retval;
00120 }
00121
00127 public function followRedirect() {
00128 $text = $this->getContent();
00129 return $this->followRedirectText( $text );
00130 }
00131
00137 public function followRedirectText( $text ) {
00138 $rt = Title::newFromRedirectRecurse( $text );
00139 # process if title object is valid and not special:userlogout
00140 if( $rt ) {
00141 if( $rt->getInterwiki() != '' ) {
00142 if( $rt->isLocal() ) {
00143
00144
00145
00146
00147 $source = $this->mTitle->getFullURL( 'redirect=no' );
00148 return $rt->getFullURL( 'rdfrom=' . urlencode( $source ) );
00149 }
00150 } else {
00151 if( $rt->getNamespace() == NS_SPECIAL ) {
00152
00153
00154
00155
00156
00157 if( $rt->isSpecial( 'Userlogout' ) ) {
00158
00159 } else {
00160 return $rt->getFullURL();
00161 }
00162 }
00163 return $rt;
00164 }
00165 }
00166
00167 return false;
00168 }
00169
00173 public function getTitle() {
00174 return $this->mTitle;
00175 }
00176
00181 public function clear() {
00182 $this->mDataLoaded = false;
00183 $this->mContentLoaded = false;
00184
00185 $this->mCurID = $this->mUser = $this->mCounter = -1; # Not loaded
00186 $this->mRedirectedFrom = null; # Title object if set
00187 $this->mRedirectTarget = null; # Title object if set
00188 $this->mUserText =
00189 $this->mTimestamp = $this->mComment = '';
00190 $this->mGoodAdjustment = $this->mTotalAdjustment = 0;
00191 $this->mTouched = '19700101000000';
00192 $this->mForUpdate = false;
00193 $this->mIsRedirect = false;
00194 $this->mRevIdFetched = 0;
00195 $this->mRedirectUrl = false;
00196 $this->mLatest = false;
00197 $this->mPreparedEdit = false;
00198 }
00199
00207 public function getContent() {
00208 global $wgUser, $wgContLang, $wgOut, $wgMessageCache;
00209 wfProfileIn( __METHOD__ );
00210 if( $this->getID() === 0 ) {
00211 # If this is a MediaWiki:x message, then load the messages
00212 # and return the message value for x.
00213 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
00214 # If this is a system message, get the default text.
00215 list( $message, $lang ) = $wgMessageCache->figureMessage( $wgContLang->lcfirst( $this->mTitle->getText() ) );
00216 $wgMessageCache->loadAllMessages( $lang );
00217 $text = wfMsgGetKey( $message, false, $lang, false );
00218 if( wfEmptyMsg( $message, $text ) )
00219 $text = '';
00220 } else {
00221 $text = wfMsgExt( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon', 'parsemag' );
00222 }
00223 wfProfileOut( __METHOD__ );
00224 return $text;
00225 } else {
00226 $this->loadContent();
00227 wfProfileOut( __METHOD__ );
00228 return $this->mContent;
00229 }
00230 }
00231
00237 public function getRawText() {
00238
00239 if( $this->mContentLoaded && $this->mOldId == 0 ) {
00240 return $this->mContent;
00241 }
00242 $rev = Revision::newFromTitle( $this->mTitle );
00243 $text = $rev ? $rev->getRawText() : false;
00244 return $text;
00245 }
00246
00259 public function getSection( $text, $section ) {
00260 global $wgParser;
00261 return $wgParser->getSection( $text, $section );
00262 }
00263
00272 public function getUndoText( Revision $undo, Revision $undoafter = null ) {
00273 $undo_text = $undo->getText();
00274 $undoafter_text = $undoafter->getText();
00275 $cur_text = $this->getContent();
00276 if ( $cur_text == $undo_text ) {
00277 # No use doing a merge if it's just a straight revert.
00278 return $undoafter_text;
00279 }
00280 $undone_text = '';
00281 if ( !wfMerge( $undo_text, $undoafter_text, $cur_text, $undone_text ) )
00282 return false;
00283 return $undone_text;
00284 }
00285
00290 public function getOldID() {
00291 if( is_null( $this->mOldId ) ) {
00292 $this->mOldId = $this->getOldIDFromRequest();
00293 }
00294 return $this->mOldId;
00295 }
00296
00302 public function getOldIDFromRequest() {
00303 global $wgRequest;
00304 $this->mRedirectUrl = false;
00305 $oldid = $wgRequest->getVal( 'oldid' );
00306 if( isset( $oldid ) ) {
00307 $oldid = intval( $oldid );
00308 if( $wgRequest->getVal( 'direction' ) == 'next' ) {
00309 $nextid = $this->mTitle->getNextRevisionID( $oldid );
00310 if( $nextid ) {
00311 $oldid = $nextid;
00312 } else {
00313 $this->mRedirectUrl = $this->mTitle->getFullURL( 'redirect=no' );
00314 }
00315 } elseif( $wgRequest->getVal( 'direction' ) == 'prev' ) {
00316 $previd = $this->mTitle->getPreviousRevisionID( $oldid );
00317 if( $previd ) {
00318 $oldid = $previd;
00319 }
00320 }
00321 }
00322 if( !$oldid ) {
00323 $oldid = 0;
00324 }
00325 return $oldid;
00326 }
00327
00331 function loadContent() {
00332 if( $this->mContentLoaded ) return;
00333 wfProfileIn( __METHOD__ );
00334 # Query variables :P
00335 $oldid = $this->getOldID();
00336 # Pre-fill content with error message so that if something
00337 # fails we'll have something telling us what we intended.
00338 $this->mOldId = $oldid;
00339 $this->fetchContent( $oldid );
00340 wfProfileOut( __METHOD__ );
00341 }
00342
00343
00349 protected function pageData( $dbr, $conditions ) {
00350 $fields = array(
00351 'page_id',
00352 'page_namespace',
00353 'page_title',
00354 'page_restrictions',
00355 'page_counter',
00356 'page_is_redirect',
00357 'page_is_new',
00358 'page_random',
00359 'page_touched',
00360 'page_latest',
00361 'page_len',
00362 );
00363 wfRunHooks( 'ArticlePageDataBefore', array( &$this, &$fields ) );
00364 $row = $dbr->selectRow(
00365 'page',
00366 $fields,
00367 $conditions,
00368 __METHOD__
00369 );
00370 wfRunHooks( 'ArticlePageDataAfter', array( &$this, &$row ) );
00371 return $row ;
00372 }
00373
00378 public function pageDataFromTitle( $dbr, $title ) {
00379 return $this->pageData( $dbr, array(
00380 'page_namespace' => $title->getNamespace(),
00381 'page_title' => $title->getDBkey() ) );
00382 }
00383
00388 protected function pageDataFromId( $dbr, $id ) {
00389 return $this->pageData( $dbr, array( 'page_id' => $id ) );
00390 }
00391
00398 public function loadPageData( $data = 'fromdb' ) {
00399 if( $data === 'fromdb' ) {
00400 $dbr = wfGetDB( DB_MASTER );
00401 $data = $this->pageDataFromId( $dbr, $this->getId() );
00402 }
00403
00404 $lc = LinkCache::singleton();
00405 if( $data ) {
00406 $lc->addGoodLinkObj( $data->page_id, $this->mTitle, $data->page_len, $data->page_is_redirect );
00407
00408 $this->mTitle->mArticleID = $data->page_id;
00409
00410 # Old-fashioned restrictions
00411 $this->mTitle->loadRestrictions( $data->page_restrictions );
00412
00413 $this->mCounter = $data->page_counter;
00414 $this->mTouched = wfTimestamp( TS_MW, $data->page_touched );
00415 $this->mIsRedirect = $data->page_is_redirect;
00416 $this->mLatest = $data->page_latest;
00417 } else {
00418 if( is_object( $this->mTitle ) ) {
00419 $lc->addBadLinkObj( $this->mTitle );
00420 }
00421 $this->mTitle->mArticleID = 0;
00422 }
00423
00424 $this->mDataLoaded = true;
00425 }
00426
00433 function fetchContent( $oldid = 0 ) {
00434 if( $this->mContentLoaded ) {
00435 return $this->mContent;
00436 }
00437
00438 $dbr = wfGetDB( DB_MASTER );
00439
00440 # Pre-fill content with error message so that if something
00441 # fails we'll have something telling us what we intended.
00442 $t = $this->mTitle->getPrefixedText();
00443 $d = $oldid ? wfMsgExt( 'missingarticle-rev', array( 'escape' ), $oldid ) : '';
00444 $this->mContent = wfMsg( 'missing-article', $t, $d ) ;
00445
00446 if( $oldid ) {
00447 $revision = Revision::newFromId( $oldid );
00448 if( is_null( $revision ) ) {
00449 wfDebug( __METHOD__." failed to retrieve specified revision, id $oldid\n" );
00450 return false;
00451 }
00452 $data = $this->pageDataFromId( $dbr, $revision->getPage() );
00453 if( !$data ) {
00454 wfDebug( __METHOD__." failed to get page data linked to revision id $oldid\n" );
00455 return false;
00456 }
00457 $this->mTitle = Title::makeTitle( $data->page_namespace, $data->page_title );
00458 $this->loadPageData( $data );
00459 } else {
00460 if( !$this->mDataLoaded ) {
00461 $data = $this->pageDataFromTitle( $dbr, $this->mTitle );
00462 if( !$data ) {
00463 wfDebug( __METHOD__." failed to find page data for title " . $this->mTitle->getPrefixedText() . "\n" );
00464 return false;
00465 }
00466 $this->loadPageData( $data );
00467 }
00468 $revision = Revision::newFromId( $this->mLatest );
00469 if( is_null( $revision ) ) {
00470 wfDebug( __METHOD__." failed to retrieve current page, rev_id {$this->mLatest}\n" );
00471 return false;
00472 }
00473 }
00474
00475
00476
00477 $this->mContent = $revision->getText( Revision::FOR_THIS_USER );
00478
00479 $this->mUser = $revision->getUser();
00480 $this->mUserText = $revision->getUserText();
00481 $this->mComment = $revision->getComment();
00482 $this->mTimestamp = wfTimestamp( TS_MW, $revision->getTimestamp() );
00483
00484 $this->mRevIdFetched = $revision->getId();
00485 $this->mContentLoaded = true;
00486 $this->mRevision =& $revision;
00487
00488 wfRunHooks( 'ArticleAfterFetchContent', array( &$this, &$this->mContent ) ) ;
00489
00490 return $this->mContent;
00491 }
00492
00498 public function forUpdate( $x = NULL ) {
00499 return wfSetVar( $this->mForUpdate, $x );
00500 }
00501
00508 function getDB() {
00509 wfDeprecated( __METHOD__ );
00510 return wfGetDB( DB_MASTER );
00511 }
00512
00520 protected function getSelectOptions( $options = '' ) {
00521 if( $this->mForUpdate ) {
00522 if( is_array( $options ) ) {
00523 $options[] = 'FOR UPDATE';
00524 } else {
00525 $options = 'FOR UPDATE';
00526 }
00527 }
00528 return $options;
00529 }
00530
00534 public function getID() {
00535 if( $this->mTitle ) {
00536 return $this->mTitle->getArticleID();
00537 } else {
00538 return 0;
00539 }
00540 }
00541
00545 public function exists() {
00546 return $this->getId() > 0;
00547 }
00548
00557 public function hasViewableContent() {
00558 return $this->exists() || $this->mTitle->isAlwaysKnown();
00559 }
00560
00564 public function getCount() {
00565 if( -1 == $this->mCounter ) {
00566 $id = $this->getID();
00567 if( $id == 0 ) {
00568 $this->mCounter = 0;
00569 } else {
00570 $dbr = wfGetDB( DB_SLAVE );
00571 $this->mCounter = $dbr->selectField( 'page',
00572 'page_counter',
00573 array( 'page_id' => $id ),
00574 __METHOD__,
00575 $this->getSelectOptions()
00576 );
00577 }
00578 }
00579 return $this->mCounter;
00580 }
00581
00589 public function isCountable( $text ) {
00590 global $wgUseCommaCount;
00591
00592 $token = $wgUseCommaCount ? ',' : '[[';
00593 return $this->mTitle->isContentPage() && !$this->isRedirect($text) && in_string($token,$text);
00594 }
00595
00602 public function isRedirect( $text = false ) {
00603 if( $text === false ) {
00604 if( $this->mDataLoaded ) {
00605 return $this->mIsRedirect;
00606 }
00607
00608 $this->loadContent();
00609 $titleObj = Title::newFromRedirectRecurse( $this->fetchContent() );
00610 } else {
00611 $titleObj = Title::newFromRedirect( $text );
00612 }
00613 return $titleObj !== NULL;
00614 }
00615
00621 public function isCurrent() {
00622 # If no oldid, this is the current version.
00623 if( $this->getOldID() == 0 ) {
00624 return true;
00625 }
00626 return $this->exists() && isset($this->mRevision) && $this->mRevision->isCurrent();
00627 }
00628
00633 protected function loadLastEdit() {
00634 if( -1 != $this->mUser )
00635 return;
00636
00637 # New or non-existent articles have no user information
00638 $id = $this->getID();
00639 if( 0 == $id ) return;
00640
00641 $this->mLastRevision = Revision::loadFromPageId( wfGetDB( DB_MASTER ), $id );
00642 if( !is_null( $this->mLastRevision ) ) {
00643 $this->mUser = $this->mLastRevision->getUser();
00644 $this->mUserText = $this->mLastRevision->getUserText();
00645 $this->mTimestamp = $this->mLastRevision->getTimestamp();
00646 $this->mComment = $this->mLastRevision->getComment();
00647 $this->mMinorEdit = $this->mLastRevision->isMinor();
00648 $this->mRevIdFetched = $this->mLastRevision->getId();
00649 }
00650 }
00651
00652 public function getTimestamp() {
00653
00654 if( !$this->mTimestamp ) {
00655 $this->loadLastEdit();
00656 }
00657 return wfTimestamp(TS_MW, $this->mTimestamp);
00658 }
00659
00660 public function getUser() {
00661 $this->loadLastEdit();
00662 return $this->mUser;
00663 }
00664
00665 public function getUserText() {
00666 $this->loadLastEdit();
00667 return $this->mUserText;
00668 }
00669
00670 public function getComment() {
00671 $this->loadLastEdit();
00672 return $this->mComment;
00673 }
00674
00675 public function getMinorEdit() {
00676 $this->loadLastEdit();
00677 return $this->mMinorEdit;
00678 }
00679
00680
00681 public function getRevIdFetched() {
00682 $this->loadLastEdit();
00683 return $this->mRevIdFetched;
00684 }
00685
00690 public function getContributors($limit = 0, $offset = 0) {
00691 # XXX: this is expensive; cache this info somewhere.
00692
00693 $contribs = array();
00694 $dbr = wfGetDB( DB_SLAVE );
00695 $revTable = $dbr->tableName( 'revision' );
00696 $userTable = $dbr->tableName( 'user' );
00697 $user = $this->getUser();
00698 $pageId = $this->getId();
00699
00700 $hideBit = Revision::DELETED_USER;
00701
00702 $sql = "SELECT {$userTable}.*, MAX(rev_timestamp) as timestamp
00703 FROM $revTable LEFT JOIN $userTable ON rev_user = user_id
00704 WHERE rev_page = $pageId
00705 AND rev_user != $user
00706 AND rev_deleted & $hideBit = 0
00707 GROUP BY rev_user, rev_user_text, user_real_name
00708 ORDER BY timestamp DESC";
00709
00710 if($limit > 0) { $sql .= ' LIMIT '.$limit; }
00711 if($offset > 0) { $sql .= ' OFFSET '.$offset; }
00712
00713 $sql .= ' '. $this->getSelectOptions();
00714
00715 $res = $dbr->query($sql, __METHOD__ );
00716
00717 return new UserArrayFromResult( $res );
00718 }
00719
00724 public function view() {
00725 global $wgUser, $wgOut, $wgRequest, $wgContLang;
00726 global $wgEnableParserCache, $wgStylePath, $wgParser;
00727 global $wgUseTrackbacks, $wgNamespaceRobotPolicies, $wgArticleRobotPolicies;
00728 global $wgDefaultRobotPolicy;
00729
00730 # Let the parser know if this is the printable version
00731 if( $wgOut->isPrintable() ) {
00732 $wgOut->parserOptions()->setIsPrintable( true );
00733 }
00734
00735 wfProfileIn( __METHOD__ );
00736
00737 # Get variables from query string
00738 $oldid = $this->getOldID();
00739
00740 # Try client and file cache
00741 if( $oldid === 0 && $this->checkTouched() ) {
00742 global $wgUseETag;
00743 if( $wgUseETag ) {
00744 $parserCache = ParserCache::singleton();
00745 $wgOut->setETag( $parserCache->getETag($this, $wgOut->parserOptions()) );
00746 }
00747 # Is is client cached?
00748 if( $wgOut->checkLastModified( $this->getTouched() ) ) {
00749 wfProfileOut( __METHOD__ );
00750 return;
00751 # Try file cache
00752 } else if( $this->tryFileCache() ) {
00753 # tell wgOut that output is taken care of
00754 $wgOut->disable();
00755 $this->viewUpdates();
00756 wfProfileOut( __METHOD__ );
00757 return;
00758 }
00759 }
00760
00761 $ns = $this->mTitle->getNamespace(); # shortcut
00762 $sk = $wgUser->getSkin();
00763
00764 # getOldID may want us to redirect somewhere else
00765 if( $this->mRedirectUrl ) {
00766 $wgOut->redirect( $this->mRedirectUrl );
00767 wfProfileOut( __METHOD__ );
00768 return;
00769 }
00770
00771 $diff = $wgRequest->getVal( 'diff' );
00772 $rcid = $wgRequest->getVal( 'rcid' );
00773 $rdfrom = $wgRequest->getVal( 'rdfrom' );
00774 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
00775 $purge = $wgRequest->getVal( 'action' ) == 'purge';
00776 $return404 = false;
00777
00778 $wgOut->setArticleFlag( true );
00779
00780 # Discourage indexing of printable versions, but encourage following
00781 if( $wgOut->isPrintable() ) {
00782 $policy = 'noindex,follow';
00783 } elseif( isset( $wgArticleRobotPolicies[$this->mTitle->getPrefixedText()] ) ) {
00784 $policy = $wgArticleRobotPolicies[$this->mTitle->getPrefixedText()];
00785 } elseif( isset( $wgNamespaceRobotPolicies[$ns] ) ) {
00786 # Honour customised robot policies for this namespace
00787 $policy = $wgNamespaceRobotPolicies[$ns];
00788 } else {
00789 $policy = $wgDefaultRobotPolicy;
00790 }
00791 $wgOut->setRobotPolicy( $policy );
00792
00793 # Allow admins to see deleted content if explicitly requested
00794 $delId = $diff ? $diff : $oldid;
00795 $unhide = $wgRequest->getInt('unhide') == 1
00796 && $wgUser->matchEditToken( $wgRequest->getVal('token'), $delId );
00797 # If we got diff and oldid in the query, we want to see a
00798 # diff page instead of the article.
00799 if( !is_null( $diff ) ) {
00800 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
00801
00802 $htmldiff = $wgRequest->getVal( 'htmldiff' , false);
00803 $de = new DifferenceEngine( $this->mTitle, $oldid, $diff, $rcid, $purge, $htmldiff, $unhide );
00804
00805 $this->mRevIdFetched = $de->mNewid;
00806 $de->showDiffPage( $diffOnly );
00807
00808
00809 $this->loadPageData();
00810 if( $diff == 0 || $diff == $this->mLatest ) {
00811 # Run view updates for current revision only
00812 $this->viewUpdates();
00813 }
00814 wfProfileOut( __METHOD__ );
00815 return;
00816 }
00817
00818 if( $ns == NS_USER || $ns == NS_USER_TALK ) {
00819 # User/User_talk subpages are not modified. (bug 11443)
00820 if( !$this->mTitle->isSubpage() ) {
00821 $block = new Block();
00822 if( $block->load( $this->mTitle->getBaseText() ) ) {
00823 $wgOut->setRobotpolicy( 'noindex,nofollow' );
00824 }
00825 }
00826 }
00827
00828 # Should the parser cache be used?
00829 $pcache = $this->useParserCache( $oldid );
00830 wfDebug( 'Article::view using parser cache: ' . ($pcache ? 'yes' : 'no' ) . "\n" );
00831 if( $wgUser->getOption( 'stubthreshold' ) ) {
00832 wfIncrStats( 'pcache_miss_stub' );
00833 }
00834
00835 $wasRedirected = false;
00836 if( isset( $this->mRedirectedFrom ) ) {
00837
00838
00839 if( wfRunHooks( 'ArticleViewRedirect', array( &$this ) ) ) {
00840 $redir = $sk->makeKnownLinkObj( $this->mRedirectedFrom, '', 'redirect=no' );
00841 $s = wfMsgExt( 'redirectedfrom', array( 'parseinline', 'replaceafter' ), $redir );
00842 $wgOut->setSubtitle( $s );
00843
00844
00845 if( strval( $this->mTitle->getFragment() ) != '' ) {
00846 $fragment = Xml::escapeJsString( $this->mTitle->getFragmentForURL() );
00847 $wgOut->addInlineScript( "redirectToFragment(\"$fragment\");" );
00848 }
00849
00850
00851 $wgOut->addLink( array( 'rel' => 'canonical',
00852 'href' => $this->mTitle->getLocalURL() )
00853 );
00854 $wasRedirected = true;
00855 }
00856 } elseif( !empty( $rdfrom ) ) {
00857
00858
00859 global $wgRedirectSources;
00860 if( $wgRedirectSources && preg_match( $wgRedirectSources, $rdfrom ) ) {
00861 $redir = $sk->makeExternalLink( $rdfrom, $rdfrom );
00862 $s = wfMsgExt( 'redirectedfrom', array( 'parseinline', 'replaceafter' ), $redir );
00863 $wgOut->setSubtitle( $s );
00864 $wasRedirected = true;
00865 }
00866 }
00867
00868 $outputDone = false;
00869 wfRunHooks( 'ArticleViewHeader', array( &$this, &$outputDone, &$pcache ) );
00870 if( $pcache && $wgOut->tryParserCache( $this ) ) {
00871
00872
00873 $wgOut->setRevisionId( $this->mLatest );
00874 $outputDone = true;
00875 }
00876 # Fetch content and check for errors
00877 if( !$outputDone ) {
00878 # If the article does not exist and was deleted, show the log
00879 if( $this->getID() == 0 ) {
00880 $this->showDeletionLog();
00881 }
00882 $text = $this->getContent();
00883
00884
00885 if( $text === false || $this->getID() === 0 ) {
00886 # Failed to load, replace text with error message
00887 $t = $this->mTitle->getPrefixedText();
00888 if( $oldid ) {
00889 $d = wfMsgExt( 'missingarticle-rev', 'escape', $oldid );
00890 $text = wfMsgExt( 'missing-article', 'parsemag', $t, $d );
00891
00892
00893 } elseif ( $this->mTitle->getNamespace() != NS_MEDIAWIKI ) {
00894 $text = wfMsgExt( 'noarticletext', 'parsemag' );
00895 }
00896 }
00897
00898 # Non-existent pages
00899 if( $this->getID() === 0 ) {
00900 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00901 $text = "<div class='noarticletext'>\n$text\n</div>";
00902 if( !$this->hasViewableContent() ) {
00903
00904
00905 $return404 = true;
00906 }
00907 }
00908
00909 if( $return404 ) {
00910 $wgRequest->response()->header( "HTTP/1.x 404 Not Found" );
00911 }
00912
00913 # Another whitelist check in case oldid is altering the title
00914 if( !$this->mTitle->userCanRead() ) {
00915 $wgOut->loginToUse();
00916 $wgOut->output();
00917 $wgOut->disable();
00918 wfProfileOut( __METHOD__ );
00919 return;
00920 }
00921
00922 # For ?curid=x urls, disallow indexing
00923 if( $wgRequest->getInt('curid') )
00924 $wgOut->setRobotPolicy( 'noindex,follow' );
00925
00926 # We're looking at an old revision
00927 if( !empty( $oldid ) ) {
00928 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00929 if( is_null( $this->mRevision ) ) {
00930
00931 } else {
00932 $this->setOldSubtitle( isset($this->mOldId) ? $this->mOldId : $oldid );
00933 # Allow admins to see deleted content if explicitly requested
00934 if( $this->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
00935 if( !$unhide || !$this->mRevision->userCan(Revision::DELETED_TEXT) ) {
00936 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
00937 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
00938 wfProfileOut( __METHOD__ );
00939 return;
00940 } else {
00941 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
00942
00943 }
00944 }
00945
00946 if( $oldid === $this->getLatest() && $this->useParserCache( false )
00947 && $wgOut->tryParserCache( $this ) )
00948 {
00949 $outputDone = true;
00950 }
00951 }
00952 }
00953
00954
00955
00956 $wgOut->setRevisionId( $this->getRevIdFetched() );
00957
00958 if( $outputDone ) {
00959
00960
00961 } else if( $this->mTitle->isCssOrJsPage() || $this->mTitle->isCssJsSubpage() ) {
00962 $wgOut->addHTML( wfMsgExt( 'clearyourcache', 'parse' ) );
00963
00964 if( wfRunHooks( 'ShowRawCssJs', array( $this->mContent, $this->mTitle, $wgOut ) ) ) {
00965
00966 $m = array();
00967 preg_match( '!\.(css|js)$!u', $this->mTitle->getText(), $m );
00968 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
00969 $wgOut->addHTML( htmlspecialchars( $this->mContent ) );
00970 $wgOut->addHTML( "\n</pre>\n" );
00971 }
00972 } else if( $rt = Title::newFromRedirectArray( $text ) ) { # get an array of redirect targets
00973 # Don't append the subtitle if this was an old revision
00974 $wgOut->addHTML( $this->viewRedirect( $rt, !$wasRedirected && $this->isCurrent() ) );
00975 $parseout = $wgParser->parse($text, $this->mTitle, ParserOptions::newFromUser($wgUser));
00976 $wgOut->addParserOutputNoText( $parseout );
00977 } else if( $pcache ) {
00978 # Display content and save to parser cache
00979 $this->outputWikiText( $text );
00980 } else {
00981 # Display content, don't attempt to save to parser cache
00982 # Don't show section-edit links on old revisions... this way lies madness.
00983 if( !$this->isCurrent() ) {
00984 $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
00985 }
00986 # Display content and don't save to parser cache
00987 # With timing hack -- TS 2006-07-26
00988 $time = -wfTime();
00989 $this->outputWikiText( $text, false );
00990 $time += wfTime();
00991
00992 # Timing hack
00993 if( $time > 3 ) {
00994 wfDebugLog( 'slow-parse', sprintf( "%-5.2f %s", $time,
00995 $this->mTitle->getPrefixedDBkey()));
00996 }
00997
00998 if( !$this->isCurrent() ) {
00999 $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
01000 }
01001 }
01002 }
01003
01004 $t = $wgOut->getPageTitle();
01005 if( empty( $t ) ) {
01006 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
01007
01008 # For the main page, overwrite the <title> element with the con-
01009 # tents of 'pagetitle-view-mainpage' instead of the default (if
01010 # that's not empty).
01011 if( $this->mTitle->equals( Title::newMainPage() ) &&
01012 wfMsgForContent( 'pagetitle-view-mainpage' ) !== '' ) {
01013 $wgOut->setHTMLTitle( wfMsgForContent( 'pagetitle-view-mainpage' ) );
01014 }
01015 }
01016
01017 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
01018 if( $ns == NS_USER_TALK && IP::isValid( $this->mTitle->getText() ) ) {
01019 $wgOut->addWikiMsg('anontalkpagetext');
01020 }
01021
01022 # If we have been passed an &rcid= parameter, we want to give the user a
01023 # chance to mark this new article as patrolled.
01024 if( !empty($rcid) && $this->mTitle->exists() && $this->mTitle->quickUserCan('patrol') ) {
01025 $wgOut->addHTML(
01026 "<div class='patrollink'>" .
01027 wfMsgHtml( 'markaspatrolledlink',
01028 $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml('markaspatrolledtext'),
01029 "action=markpatrolled&rcid=$rcid" )
01030 ) .
01031 '</div>'
01032 );
01033 }
01034
01035 # Trackbacks
01036 if( $wgUseTrackbacks ) {
01037 $this->addTrackbacks();
01038 }
01039
01040 $this->viewUpdates();
01041 wfProfileOut( __METHOD__ );
01042 }
01043
01044 protected function showDeletionLog() {
01045 global $wgUser, $wgOut;
01046 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut );
01047 $pager = new LogPager( $loglist, 'delete', false, $this->mTitle->getPrefixedText() );
01048 if( $pager->getNumRows() > 0 ) {
01049 $pager->mLimit = 10;
01050 $wgOut->addHTML( '<div class="mw-warning-with-logexcerpt">' );
01051 $wgOut->addWikiMsg( 'deleted-notice' );
01052 $wgOut->addHTML(
01053 $loglist->beginLogEventsList() .
01054 $pager->getBody() .
01055 $loglist->endLogEventsList()
01056 );
01057 if( $pager->getNumRows() > 10 ) {
01058 $wgOut->addHTML( $wgUser->getSkin()->link(
01059 SpecialPage::getTitleFor( 'Log' ),
01060 wfMsgHtml( 'deletelog-fulllog' ),
01061 array(),
01062 array( 'type' => 'delete', 'page' => $this->mTitle->getPrefixedText() )
01063 ) );
01064 }
01065 $wgOut->addHTML( '</div>' );
01066 }
01067 }
01068
01069
01070
01071
01072 protected function useParserCache( $oldid ) {
01073 global $wgUser, $wgEnableParserCache;
01074
01075 return $wgEnableParserCache
01076 && intval( $wgUser->getOption( 'stubthreshold' ) ) == 0
01077 && $this->exists()
01078 && empty( $oldid )
01079 && !$this->mTitle->isCssOrJsPage()
01080 && !$this->mTitle->isCssJsSubpage();
01081 }
01082
01089 public function viewRedirect( $target, $appendSubtitle = true, $forceKnown = false ) {
01090 global $wgParser, $wgOut, $wgContLang, $wgStylePath, $wgUser;
01091 # Display redirect
01092 if( !is_array( $target ) ) {
01093 $target = array( $target );
01094 }
01095 $imageDir = $wgContLang->isRTL() ? 'rtl' : 'ltr';
01096 $imageUrl = $wgStylePath . '/common/images/redirect' . $imageDir . '.png';
01097 $imageUrl2 = $wgStylePath . '/common/images/nextredirect' . $imageDir . '.png';
01098 $alt2 = $wgContLang->isRTL() ? '←' : '→';
01099
01100 if( $appendSubtitle ) {
01101 $wgOut->appendSubtitle( wfMsgHtml( 'redirectpagesub' ) );
01102 }
01103 $sk = $wgUser->getSkin();
01104
01105 $title = array_shift( $target );
01106 if( $forceKnown ) {
01107 $link = $sk->makeKnownLinkObj( $title, htmlspecialchars( $title->getFullText() ) );
01108 } else {
01109 $link = $sk->makeLinkObj( $title, htmlspecialchars( $title->getFullText() ) );
01110 }
01111
01112 foreach( $target as $rt ) {
01113 if( $forceKnown ) {
01114 $link .= '<img src="'.$imageUrl2.'" alt="'.$alt2.' " />'
01115 . $sk->makeKnownLinkObj( $rt, htmlspecialchars( $rt->getFullText() ) );
01116 } else {
01117 $link .= '<img src="'.$imageUrl2.'" alt="'.$alt2.' " />'
01118 . $sk->makeLinkObj( $rt, htmlspecialchars( $rt->getFullText() ) );
01119 }
01120 }
01121 return '<img src="'.$imageUrl.'" alt="#REDIRECT " />' .
01122 '<span class="redirectText">'.$link.'</span>';
01123
01124 }
01125
01126 public function addTrackbacks() {
01127 global $wgOut, $wgUser;
01128 $dbr = wfGetDB( DB_SLAVE );
01129 $tbs = $dbr->select( 'trackbacks',
01130 array('tb_id', 'tb_title', 'tb_url', 'tb_ex', 'tb_name'),
01131 array('tb_page' => $this->getID() )
01132 );
01133 if( !$dbr->numRows($tbs) ) return;
01134
01135 $tbtext = "";
01136 while( $o = $dbr->fetchObject($tbs) ) {
01137 $rmvtxt = "";
01138 if( $wgUser->isAllowed( 'trackback' ) ) {
01139 $delurl = $this->mTitle->getFullURL("action=deletetrackback&tbid=" .
01140 $o->tb_id . "&token=" . urlencode( $wgUser->editToken() ) );
01141 $rmvtxt = wfMsg( 'trackbackremove', htmlspecialchars( $delurl ) );
01142 }
01143 $tbtext .= "\n";
01144 $tbtext .= wfMsg(strlen($o->tb_ex) ? 'trackbackexcerpt' : 'trackback',
01145 $o->tb_title,
01146 $o->tb_url,
01147 $o->tb_ex,
01148 $o->tb_name,
01149 $rmvtxt);
01150 }
01151 $wgOut->wrapWikiMsg( "<div id='mw_trackbacks'>$1</div>\n", array( 'trackbackbox', $tbtext ) );
01152 $this->mTitle->invalidateCache();
01153 }
01154
01155 public function deletetrackback() {
01156 global $wgUser, $wgRequest, $wgOut, $wgTitle;
01157 if( !$wgUser->matchEditToken($wgRequest->getVal('token')) ) {
01158 $wgOut->addWikiMsg( 'sessionfailure' );
01159 return;
01160 }
01161
01162 $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser );
01163 if( count($permission_errors) ) {
01164 $wgOut->showPermissionsErrorPage( $permission_errors );
01165 return;
01166 }
01167
01168 $db = wfGetDB( DB_MASTER );
01169 $db->delete( 'trackbacks', array('tb_id' => $wgRequest->getInt('tbid')) );
01170
01171 $wgOut->addWikiMsg( 'trackbackdeleteok' );
01172 $this->mTitle->invalidateCache();
01173 }
01174
01175 public function render() {
01176 global $wgOut;
01177 $wgOut->setArticleBodyOnly(true);
01178 $this->view();
01179 }
01180
01184 public function purge() {
01185 global $wgUser, $wgRequest, $wgOut;
01186 if( $wgUser->isAllowed( 'purge' ) || $wgRequest->wasPosted() ) {
01187 if( wfRunHooks( 'ArticlePurge', array( &$this ) ) ) {
01188 $this->doPurge();
01189 $this->view();
01190 }
01191 } else {
01192 $action = htmlspecialchars( $wgRequest->getRequestURL() );
01193 $button = wfMsgExt( 'confirm_purge_button', array('escapenoentities') );
01194 $form = "<form method=\"post\" action=\"$action\">\n" .
01195 "<input type=\"submit\" name=\"submit\" value=\"$button\" />\n" .
01196 "</form>\n";
01197 $top = wfMsgExt( 'confirm-purge-top', array('parse') );
01198 $bottom = wfMsgExt( 'confirm-purge-bottom', array('parse') );
01199 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
01200 $wgOut->setRobotPolicy( 'noindex,nofollow' );
01201 $wgOut->addHTML( $top . $form . $bottom );
01202 }
01203 }
01204
01208 public function doPurge() {
01209 global $wgUseSquid;
01210
01211 $this->mTitle->invalidateCache();
01212
01213 if( $wgUseSquid ) {
01214
01215 $dbw = wfGetDB( DB_MASTER );
01216 $dbw->immediateCommit();
01217
01218
01219 $update = SquidUpdate::newSimplePurge( $this->mTitle );
01220 $update->doUpdate();
01221 }
01222 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
01223 global $wgMessageCache;
01224 if( $this->getID() == 0 ) {
01225 $text = false;
01226 } else {
01227 $text = $this->getRawText();
01228 }
01229 $wgMessageCache->replace( $this->mTitle->getDBkey(), $text );
01230 }
01231 }
01232
01244 public function insertOn( $dbw ) {
01245 wfProfileIn( __METHOD__ );
01246
01247 $page_id = $dbw->nextSequenceValue( 'page_page_id_seq' );
01248 $dbw->insert( 'page', array(
01249 'page_id' => $page_id,
01250 'page_namespace' => $this->mTitle->getNamespace(),
01251 'page_title' => $this->mTitle->getDBkey(),
01252 'page_counter' => 0,
01253 'page_restrictions' => '',
01254 'page_is_redirect' => 0, # Will set this shortly...
01255 'page_is_new' => 1,
01256 'page_random' => wfRandom(),
01257 'page_touched' => $dbw->timestamp(),
01258 'page_latest' => 0, # Fill this in shortly...
01259 'page_len' => 0, # Fill this in shortly...
01260 ), __METHOD__, 'IGNORE' );
01261
01262 $affected = $dbw->affectedRows();
01263 if( $affected ) {
01264 $newid = $dbw->insertId();
01265 $this->mTitle->resetArticleId( $newid );
01266 }
01267 wfProfileOut( __METHOD__ );
01268 return $affected ? $newid : false;
01269 }
01270
01286 public function updateRevisionOn( &$dbw, $revision, $lastRevision = null, $lastRevIsRedirect = null ) {
01287 wfProfileIn( __METHOD__ );
01288
01289 $text = $revision->getText();
01290 $rt = Title::newFromRedirect( $text );
01291
01292 $conditions = array( 'page_id' => $this->getId() );
01293 if( !is_null( $lastRevision ) ) {
01294 # An extra check against threads stepping on each other
01295 $conditions['page_latest'] = $lastRevision;
01296 }
01297
01298 $dbw->update( 'page',
01299 array(
01300 'page_latest' => $revision->getId(),
01301 'page_touched' => $dbw->timestamp(),
01302 'page_is_new' => ($lastRevision === 0) ? 1 : 0,
01303 'page_is_redirect' => $rt !== NULL ? 1 : 0,
01304 'page_len' => strlen( $text ),
01305 ),
01306 $conditions,
01307 __METHOD__ );
01308
01309 $result = $dbw->affectedRows() != 0;
01310 if( $result ) {
01311 $this->updateRedirectOn( $dbw, $rt, $lastRevIsRedirect );
01312 }
01313
01314 wfProfileOut( __METHOD__ );
01315 return $result;
01316 }
01317
01329 public function updateRedirectOn( &$dbw, $redirectTitle, $lastRevIsRedirect = null ) {
01330
01331
01332
01333 $isRedirect = !is_null($redirectTitle);
01334 if($isRedirect || is_null($lastRevIsRedirect) || $lastRevIsRedirect !== $isRedirect) {
01335 wfProfileIn( __METHOD__ );
01336 if( $isRedirect ) {
01337
01338 $set = array(
01339 'rd_namespace' => $redirectTitle->getNamespace(),
01340 'rd_title' => $redirectTitle->getDBkey(),
01341 'rd_from' => $this->getId(),
01342 );
01343 $dbw->replace( 'redirect', array( 'rd_from' ), $set, __METHOD__ );
01344 } else {
01345
01346 $where = array( 'rd_from' => $this->getId() );
01347 $dbw->delete( 'redirect', $where, __METHOD__);
01348 }
01349 if( $this->getTitle()->getNamespace() == NS_FILE ) {
01350 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $this->getTitle() );
01351 }
01352 wfProfileOut( __METHOD__ );
01353 return ( $dbw->affectedRows() != 0 );
01354 }
01355 return true;
01356 }
01357
01365 public function updateIfNewerOn( &$dbw, $revision ) {
01366 wfProfileIn( __METHOD__ );
01367 $row = $dbw->selectRow(
01368 array( 'revision', 'page' ),
01369 array( 'rev_id', 'rev_timestamp', 'page_is_redirect' ),
01370 array(
01371 'page_id' => $this->getId(),
01372 'page_latest=rev_id' ),
01373 __METHOD__ );
01374 if( $row ) {
01375 if( wfTimestamp(TS_MW, $row->rev_timestamp) >= $revision->getTimestamp() ) {
01376 wfProfileOut( __METHOD__ );
01377 return false;
01378 }
01379 $prev = $row->rev_id;
01380 $lastRevIsRedirect = (bool)$row->page_is_redirect;
01381 } else {
01382 # No or missing previous revision; mark the page as new
01383 $prev = 0;
01384 $lastRevIsRedirect = null;
01385 }
01386 $ret = $this->updateRevisionOn( $dbw, $revision, $prev, $lastRevIsRedirect );
01387 wfProfileOut( __METHOD__ );
01388 return $ret;
01389 }
01390
01395 public function replaceSection( $section, $text, $summary = '', $edittime = NULL ) {
01396 wfProfileIn( __METHOD__ );
01397 if( strval( $section ) == '' ) {
01398
01399 } else {
01400 if( is_null($edittime) ) {
01401 $rev = Revision::newFromTitle( $this->mTitle );
01402 } else {
01403 $dbw = wfGetDB( DB_MASTER );
01404 $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
01405 }
01406 if( !$rev ) {
01407 wfDebug( "Article::replaceSection asked for bogus section (page: " .
01408 $this->getId() . "; section: $section; edittime: $edittime)\n" );
01409 return null;
01410 }
01411 $oldtext = $rev->getText();
01412
01413 if( $section == 'new' ) {
01414 # Inserting a new section
01415 $subject = $summary ? wfMsgForContent('newsectionheaderdefaultlevel',$summary) . "\n\n" : '';
01416 $text = strlen( trim( $oldtext ) ) > 0
01417 ? "{$oldtext}\n\n{$subject}{$text}"
01418 : "{$subject}{$text}";
01419 } else {
01420 # Replacing an existing section; roll out the big guns
01421 global $wgParser;
01422 $text = $wgParser->replaceSection( $oldtext, $section, $text );
01423 }
01424 }
01425 wfProfileOut( __METHOD__ );
01426 return $text;
01427 }
01428
01432 function insertNewArticle( $text, $summary, $isminor, $watchthis, $suppressRC=false, $comment=false, $bot=false ) {
01433 wfDeprecated( __METHOD__ );
01434 $flags = EDIT_NEW | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
01435 ( $isminor ? EDIT_MINOR : 0 ) |
01436 ( $suppressRC ? EDIT_SUPPRESS_RC : 0 ) |
01437 ( $bot ? EDIT_FORCE_BOT : 0 );
01438
01439 # If this is a comment, add the summary as headline
01440 if( $comment && $summary != "" ) {
01441 $text = wfMsgForContent('newsectionheaderdefaultlevel',$summary) . "\n\n".$text;
01442 }
01443
01444 $this->doEdit( $text, $summary, $flags );
01445
01446 $dbw = wfGetDB( DB_MASTER );
01447 if($watchthis) {
01448 if(!$this->mTitle->userIsWatching()) {
01449 $dbw->begin();
01450 $this->doWatch();
01451 $dbw->commit();
01452 }
01453 } else {
01454 if( $this->mTitle->userIsWatching() ) {
01455 $dbw->begin();
01456 $this->doUnwatch();
01457 $dbw->commit();
01458 }
01459 }
01460 $this->doRedirect( $this->isRedirect( $text ) );
01461 }
01462
01466 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' ) {
01467 wfDeprecated( __METHOD__ );
01468 $flags = EDIT_UPDATE | EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
01469 ( $minor ? EDIT_MINOR : 0 ) |
01470 ( $forceBot ? EDIT_FORCE_BOT : 0 );
01471
01472 $status = $this->doEdit( $text, $summary, $flags );
01473 if( !$status->isOK() ) {
01474 return false;
01475 }
01476
01477 $dbw = wfGetDB( DB_MASTER );
01478 if( $watchthis ) {
01479 if(!$this->mTitle->userIsWatching()) {
01480 $dbw->begin();
01481 $this->doWatch();
01482 $dbw->commit();
01483 }
01484 } else {
01485 if( $this->mTitle->userIsWatching() ) {
01486 $dbw->begin();
01487 $this->doUnwatch();
01488 $dbw->commit();
01489 }
01490 }
01491
01492 $extraQuery = '';
01493 wfRunHooks( 'ArticleUpdateBeforeRedirect', array( $this, &$sectionanchor, &$extraQuery ) );
01494
01495 $this->doRedirect( $this->isRedirect( $text ), $sectionanchor, $extraQuery );
01496 return true;
01497 }
01498
01549 public function doEdit( $text, $summary, $flags = 0, $baseRevId = false, $user = null ) {
01550 global $wgUser, $wgDBtransactions, $wgUseAutomaticEditSummaries;
01551
01552 # Low-level sanity check
01553 if( $this->mTitle->getText() == '' ) {
01554 throw new MWException( 'Something is trying to edit an article with an empty title' );
01555 }
01556
01557 wfProfileIn( __METHOD__ );
01558
01559 $user = is_null($user) ? $wgUser : $user;
01560 $status = Status::newGood( array() );
01561
01562 # Load $this->mTitle->getArticleID() and $this->mLatest if it's not already
01563 $this->loadPageData();
01564
01565 if( !($flags & EDIT_NEW) && !($flags & EDIT_UPDATE) ) {
01566 $aid = $this->mTitle->getArticleID();
01567 if( $aid ) {
01568 $flags |= EDIT_UPDATE;
01569 } else {
01570 $flags |= EDIT_NEW;
01571 }
01572 }
01573
01574 if( !wfRunHooks( 'ArticleSave', array( &$this, &$user, &$text, &$summary,
01575 $flags & EDIT_MINOR, null, null, &$flags, &$status ) ) )
01576 {
01577 wfDebug( __METHOD__ . ": ArticleSave hook aborted save!\n" );
01578 wfProfileOut( __METHOD__ );
01579 if( $status->isOK() ) {
01580 $status->fatal( 'edit-hook-aborted');
01581 }
01582 return $status;
01583 }
01584
01585 # Silently ignore EDIT_MINOR if not allowed
01586 $isminor = ( $flags & EDIT_MINOR ) && $user->isAllowed('minoredit');
01587 $bot = $flags & EDIT_FORCE_BOT;
01588
01589 $oldtext = $this->getRawText();
01590 $oldsize = strlen( $oldtext );
01591
01592 # Provide autosummaries if one is not provided and autosummaries are enabled.
01593 if( $wgUseAutomaticEditSummaries && $flags & EDIT_AUTOSUMMARY && $summary == '' ) {
01594 $summary = $this->getAutosummary( $oldtext, $text, $flags );
01595 }
01596
01597 $editInfo = $this->prepareTextForEdit( $text );
01598 $text = $editInfo->pst;
01599 $newsize = strlen( $text );
01600
01601 $dbw = wfGetDB( DB_MASTER );
01602 $now = wfTimestampNow();
01603
01604 if( $flags & EDIT_UPDATE ) {
01605 # Update article, but only if changed.
01606 $status->value['new'] = false;
01607 # Make sure the revision is either completely inserted or not inserted at all
01608 if( !$wgDBtransactions ) {
01609 $userAbort = ignore_user_abort( true );
01610 }
01611
01612 $revisionId = 0;
01613
01614 $changed = ( strcmp( $text, $oldtext ) != 0 );
01615
01616 if( $changed ) {
01617 $this->mGoodAdjustment = (int)$this->isCountable( $text )
01618 - (int)$this->isCountable( $oldtext );
01619 $this->mTotalAdjustment = 0;
01620
01621 if( !$this->mLatest ) {
01622 # Article gone missing
01623 wfDebug( __METHOD__.": EDIT_UPDATE specified but article doesn't exist\n" );
01624 $status->fatal( 'edit-gone-missing' );
01625 wfProfileOut( __METHOD__ );
01626 return $status;
01627 }
01628
01629 $revision = new Revision( array(
01630 'page' => $this->getId(),
01631 'comment' => $summary,
01632 'minor_edit' => $isminor,
01633 'text' => $text,
01634 'parent_id' => $this->mLatest,
01635 'user' => $user->getId(),
01636 'user_text' => $user->getName(),
01637 ) );
01638
01639 $dbw->begin();
01640 $revisionId = $revision->insertOn( $dbw );
01641
01642 # Update page
01643 #
01644 # Note that we use $this->mLatest instead of fetching a value from the master DB
01645 # during the course of this function. This makes sure that EditPage can detect
01646 # edit conflicts reliably, either by $ok here, or by $article->getTimestamp()
01647 # before this function is called. A previous function used a separate query, this
01648 # creates a window where concurrent edits can cause an ignored edit conflict.
01649 $ok = $this->updateRevisionOn( $dbw, $revision, $this->mLatest );
01650
01651 if( !$ok ) {
01652
01653 $status->fatal( 'edit-conflict' );
01654 # Delete the invalid revision if the DB is not transactional
01655 if( !$wgDBtransactions ) {
01656 $dbw->delete( 'revision', array( 'rev_id' => $revisionId ), __METHOD__ );
01657 }
01658 $revisionId = 0;
01659 $dbw->rollback();
01660 } else {
01661 global $wgUseRCPatrol;
01662 wfRunHooks( 'NewRevisionFromEditComplete', array($this, $revision, $baseRevId, $user) );
01663 # Update recentchanges
01664 if( !( $flags & EDIT_SUPPRESS_RC ) ) {
01665 # Mark as patrolled if the user can do so
01666 $patrolled = $wgUseRCPatrol && $this->mTitle->userCan('autopatrol');
01667 # Add RC row to the DB
01668 $rc = RecentChange::notifyEdit( $now, $this->mTitle, $isminor, $user, $summary,
01669 $this->mLatest, $this->getTimestamp(), $bot, '', $oldsize, $newsize,
01670 $revisionId, $patrolled
01671 );
01672 # Log auto-patrolled edits
01673 if( $patrolled ) {
01674 PatrolLog::record( $rc, true );
01675 }
01676 }
01677 $user->incEditCount();
01678 $dbw->commit();
01679 }
01680 } else {
01681 $status->warning( 'edit-no-change' );
01682 $revision = null;
01683
01684 $revisionId = $this->getRevIdFetched();
01685
01686
01687 $this->mTitle->invalidateCache();
01688 }
01689
01690 if( !$wgDBtransactions ) {
01691 ignore_user_abort( $userAbort );
01692 }
01693
01694 if( !$status->isOK() ) {
01695 wfProfileOut( __METHOD__ );
01696 return $status;
01697 }
01698
01699 # Invalidate cache of this article and all pages using this article
01700 # as a template. Partly deferred.
01701 Article::onArticleEdit( $this->mTitle );
01702 # Update links tables, site stats, etc.
01703 $this->editUpdates( $text, $summary, $isminor, $now, $revisionId, $changed );
01704 } else {
01705 # Create new article
01706 $status->value['new'] = true;
01707
01708 # Set statistics members
01709 # We work out if it's countable after PST to avoid counter drift
01710 # when articles are created with {{subst:}}
01711 $this->mGoodAdjustment = (int)$this->isCountable( $text );
01712 $this->mTotalAdjustment = 1;
01713
01714 $dbw->begin();
01715
01716 # Add the page record; stake our claim on this title!
01717 # This will return false if the article already exists
01718 $newid = $this->insertOn( $dbw );
01719
01720 if( $newid === false ) {
01721 $dbw->rollback();
01722 $status->fatal( 'edit-already-exists' );
01723 wfProfileOut( __METHOD__ );
01724 return $status;
01725 }
01726
01727 # Save the revision text...
01728 $revision = new Revision( array(
01729 'page' => $newid,
01730 'comment' => $summary,
01731 'minor_edit' => $isminor,
01732 'text' => $text,
01733 'user' => $user->getId(),
01734 'user_text' => $user->getName(),
01735 ) );
01736 $revisionId = $revision->insertOn( $dbw );
01737
01738 $this->mTitle->resetArticleID( $newid );
01739
01740 # Update the page record with revision data
01741 $this->updateRevisionOn( $dbw, $revision, 0 );
01742
01743 wfRunHooks( 'NewRevisionFromEditComplete', array($this, $revision, false, $user) );
01744 # Update recentchanges
01745 if( !( $flags & EDIT_SUPPRESS_RC ) ) {
01746 global $wgUseRCPatrol, $wgUseNPPatrol;
01747 # Mark as patrolled if the user can do so
01748 $patrolled = ($wgUseRCPatrol || $wgUseNPPatrol) && $this->mTitle->userCan('autopatrol');
01749 # Add RC row to the DB
01750 $rc = RecentChange::notifyNew( $now, $this->mTitle, $isminor, $user, $summary, $bot,
01751 '', strlen($text), $revisionId, $patrolled );
01752 # Log auto-patrolled edits
01753 if( $patrolled ) {
01754 PatrolLog::record( $rc, true );
01755 }
01756 }
01757 $user->incEditCount();
01758 $dbw->commit();
01759
01760 # Update links, etc.
01761 $this->editUpdates( $text, $summary, $isminor, $now, $revisionId, true );
01762
01763 # Clear caches
01764 Article::onArticleCreate( $this->mTitle );
01765
01766 wfRunHooks( 'ArticleInsertComplete', array( &$this, &$user, $text, $summary,
01767 $flags & EDIT_MINOR, null, null, &$flags, $revision ) );
01768 }
01769
01770 # Do updates right now unless deferral was requested
01771 if( !( $flags & EDIT_DEFER_UPDATES ) ) {
01772 wfDoUpdates();
01773 }
01774
01775
01776 $status->value['revision'] = $revision;
01777
01778 wfRunHooks( 'ArticleSaveComplete', array( &$this, &$user, $text, $summary,
01779 $flags & EDIT_MINOR, null, null, &$flags, $revision, &$status, $baseRevId ) );
01780
01781 wfProfileOut( __METHOD__ );
01782 return $status;
01783 }
01784
01788 public function showArticle( $text, $subtitle , $sectionanchor = '', $me2, $now, $summary, $oldid ) {
01789 wfDeprecated( __METHOD__ );
01790 $this->doRedirect( $this->isRedirect( $text ), $sectionanchor );
01791 }
01792
01801 public function doRedirect( $noRedir = false, $sectionAnchor = '', $extraQuery = '' ) {
01802 global $wgOut;
01803 if( $noRedir ) {
01804 $query = 'redirect=no';
01805 if( $extraQuery )
01806 $query .= "&$query";
01807 } else {
01808 $query = $extraQuery;
01809 }
01810 $wgOut->redirect( $this->mTitle->getFullURL( $query ) . $sectionAnchor );
01811 }
01812
01816 public function markpatrolled() {
01817 global $wgOut, $wgRequest, $wgUseRCPatrol, $wgUseNPPatrol, $wgUser;
01818 $wgOut->setRobotPolicy( 'noindex,nofollow' );
01819
01820 # If we haven't been given an rc_id value, we can't do anything
01821 $rcid = (int) $wgRequest->getVal('rcid');
01822 $rc = RecentChange::newFromId($rcid);
01823 if( is_null($rc) ) {
01824 $wgOut->showErrorPage( 'markedaspatrollederror', 'markedaspatrollederrortext' );
01825 return;
01826 }
01827
01828 #It would be nice to see where the user had actually come from, but for now just guess
01829 $returnto = $rc->getAttribute( 'rc_type' ) == RC_NEW ? 'Newpages' : 'Recentchanges';
01830 $return = SpecialPage::getTitleFor( $returnto );
01831
01832 $dbw = wfGetDB( DB_MASTER );
01833 $errors = $rc->doMarkPatrolled();
01834
01835 if( in_array(array('rcpatroldisabled'), $errors) ) {
01836 $wgOut->showErrorPage( 'rcpatroldisabled', 'rcpatroldisabledtext' );
01837 return;
01838 }
01839
01840 if( in_array(array('hookaborted'), $errors) ) {
01841
01842 return;
01843 }
01844
01845 if( in_array(array('markedaspatrollederror-noautopatrol'), $errors) ) {
01846 $wgOut->setPageTitle( wfMsg( 'markedaspatrollederror' ) );
01847 $wgOut->addWikiMsg( 'markedaspatrollederror-noautopatrol' );
01848 $wgOut->returnToMain( false, $return );
01849 return;
01850 }
01851
01852 if( !empty($errors) ) {
01853 $wgOut->showPermissionsErrorPage( $errors );
01854 return;
01855 }
01856
01857 # Inform the user
01858 $wgOut->setPageTitle( wfMsg( 'markedaspatrolled' ) );
01859 $wgOut->addWikiMsg( 'markedaspatrolledtext' );
01860 $wgOut->returnToMain( false, $return );
01861 }
01862
01867 public function watch() {
01868 global $wgUser, $wgOut;
01869 if( $wgUser->isAnon() ) {
01870 $wgOut->showErrorPage( 'watchnologin', 'watchnologintext' );
01871 return;
01872 }
01873 if( wfReadOnly() ) {
01874 $wgOut->readOnlyPage();
01875 return;
01876 }
01877 if( $this->doWatch() ) {
01878 $wgOut->setPagetitle( wfMsg( 'addedwatch' ) );
01879 $wgOut->setRobotPolicy( 'noindex,nofollow' );
01880 $wgOut->addWikiMsg( 'addedwatchtext', $this->mTitle->getPrefixedText() );
01881 }
01882 $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
01883 }
01884
01889 public function doWatch() {
01890 global $wgUser;
01891 if( $wgUser->isAnon() ) {
01892 return false;
01893 }
01894 if( wfRunHooks('WatchArticle', array(&$wgUser, &$this)) ) {
01895 $wgUser->addWatch( $this->mTitle );
01896 return wfRunHooks('WatchArticleComplete', array(&$wgUser, &$this));
01897 }
01898 return false;
01899 }
01900
01904 public function unwatch() {
01905 global $wgUser, $wgOut;
01906 if( $wgUser->isAnon() ) {
01907 $wgOut->showErrorPage( 'watchnologin', 'watchnologintext' );
01908 return;
01909 }
01910 if( wfReadOnly() ) {
01911 $wgOut->readOnlyPage();
01912 return;
01913 }
01914 if( $this->doUnwatch() ) {
01915 $wgOut->setPagetitle( wfMsg( 'removedwatch' ) );
01916 $wgOut->setRobotPolicy( 'noindex,nofollow' );
01917 $wgOut->addWikiMsg( 'removedwatchtext', $this->mTitle->getPrefixedText() );
01918 }
01919 $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
01920 }
01921
01926 public function doUnwatch() {
01927 global $wgUser;
01928 if( $wgUser->isAnon() ) {
01929 return false;
01930 }
01931 if( wfRunHooks('UnwatchArticle', array(&$wgUser, &$this)) ) {
01932 $wgUser->removeWatch( $this->mTitle );
01933 return wfRunHooks('UnwatchArticleComplete', array(&$wgUser, &$this));
01934 }
01935 return false;
01936 }
01937
01941 public function protect() {
01942 $form = new ProtectionForm( $this );
01943 $form->execute();
01944 }
01945
01949 public function unprotect() {
01950 $this->protect();
01951 }
01952
01962 public function updateRestrictions( $limit = array(), $reason = '', &$cascade = 0, $expiry = array() ) {
01963 global $wgUser, $wgRestrictionTypes, $wgContLang;
01964
01965 $id = $this->mTitle->getArticleID();
01966 if ( $id <= 0 ) {
01967 wfDebug( "updateRestrictions failed: $id <= 0\n" );
01968 return false;
01969 }
01970
01971 if ( wfReadOnly() ) {
01972 wfDebug( "updateRestrictions failed: read-only\n" );
01973 return false;
01974 }
01975
01976 if ( !$this->mTitle->userCan( 'protect' ) ) {
01977 wfDebug( "updateRestrictions failed: insufficient permissions\n" );
01978 return false;
01979 }
01980
01981 if( !$cascade ) {
01982 $cascade = false;
01983 }
01984
01985
01986 Title::purgeExpiredRestrictions();
01987
01988 # FIXME: Same limitations as described in ProtectionForm.php (line 37);
01989 # we expect a single selection, but the schema allows otherwise.
01990 $current = array();
01991 $updated = Article::flattenRestrictions( $limit );
01992 $changed = false;
01993 foreach( $wgRestrictionTypes as $action ) {
01994 if( isset( $expiry[$action] ) ) {
01995 # Get current restrictions on $action
01996 $aLimits = $this->mTitle->getRestrictions( $action );
01997 $current[$action] = implode( '', $aLimits );
01998 # Are any actual restrictions being dealt with here?
01999 $aRChanged = count($aLimits) || !empty($limit[$action]);
02000 # If something changed, we need to log it. Checking $aRChanged
02001 # assures that "unprotecting" a page that is not protected does
02002 # not log just because the expiry was "changed".
02003 if( $aRChanged && $this->mTitle->mRestrictionsExpiry[$action] != $expiry[$action] ) {
02004 $changed = true;
02005 }
02006 }
02007 }
02008
02009 $current = Article::flattenRestrictions( $current );
02010
02011 $changed = ($changed || $current != $updated );
02012 $changed = $changed || ($updated && $this->mTitle->areRestrictionsCascading() != $cascade);
02013 $protect = ( $updated != '' );
02014
02015 # If nothing's changed, do nothing
02016 if( $changed ) {
02017 if( wfRunHooks( 'ArticleProtect', array( &$this, &$wgUser, $limit, $reason ) ) ) {
02018
02019 $dbw = wfGetDB( DB_MASTER );
02020
02021 # Prepare a null revision to be added to the history
02022 $modified = $current != '' && $protect;
02023 if( $protect ) {
02024 $comment_type = $modified ? 'modifiedarticleprotection' : 'protectedarticle';
02025 } else {
02026 $comment_type = 'unprotectedarticle';
02027 }
02028 $comment = $wgContLang->ucfirst( wfMsgForContent( $comment_type, $this->mTitle->getPrefixedText() ) );
02029
02030 # Only restrictions with the 'protect' right can cascade...
02031 # Otherwise, people who cannot normally protect can "protect" pages via transclusion
02032 $editrestriction = isset( $limit['edit'] ) ? array( $limit['edit'] ) : $this->mTitle->getRestrictions( 'edit' );
02033 # The schema allows multiple restrictions
02034 if(!in_array('protect', $editrestriction) && !in_array('sysop', $editrestriction))
02035 $cascade = false;
02036 $cascade_description = '';
02037 if( $cascade ) {
02038 $cascade_description = ' ['.wfMsgForContent('protect-summary-cascade').']';
02039 }
02040
02041 if( $reason )
02042 $comment .= ": $reason";
02043
02044 $editComment = $comment;
02045 $encodedExpiry = array();
02046 $protect_description = '';
02047 foreach( $limit as $action => $restrictions ) {
02048 if ( !isset($expiry[$action]) )
02049 $expiry[$action] = 'infinite';
02050
02051 $encodedExpiry[$action] = Block::encodeExpiry($expiry[$action], $dbw );
02052 if( $restrictions != '' ) {
02053 $protect_description .= "[$action=$restrictions] (";
02054 if( $encodedExpiry[$action] != 'infinity' ) {
02055 $protect_description .= wfMsgForContent( 'protect-expiring',
02056 $wgContLang->timeanddate( $expiry[$action], false, false ) ,
02057 $wgContLang->date( $expiry[$action], false, false ) ,
02058 $wgContLang->time( $expiry[$action], false, false ) );
02059 } else {
02060 $protect_description .= wfMsgForContent( 'protect-expiry-indefinite' );
02061 }
02062 $protect_description .= ') ';
02063 }
02064 }
02065 $protect_description = trim($protect_description);
02066
02067 if( $protect_description && $protect )
02068 $editComment .= " ($protect_description)";
02069 if( $cascade )
02070 $editComment .= "$cascade_description";
02071 # Update restrictions table
02072 foreach( $limit as $action => $restrictions ) {
02073 if($restrictions != '' ) {
02074 $dbw->replace( 'page_restrictions', array(array('pr_page', 'pr_type')),
02075 array( 'pr_page' => $id,
02076 'pr_type' => $action,
02077 'pr_level' => $restrictions,
02078 'pr_cascade' => ($cascade && $action == 'edit') ? 1 : 0,
02079 'pr_expiry' => $encodedExpiry[$action] ), __METHOD__ );
02080 } else {
02081 $dbw->delete( 'page_restrictions', array( 'pr_page' => $id,
02082 'pr_type' => $action ), __METHOD__ );
02083 }
02084 }
02085
02086 # Insert a null revision
02087 $nullRevision = Revision::newNullRevision( $dbw, $id, $editComment, true );
02088 $nullRevId = $nullRevision->insertOn( $dbw );
02089
02090 $latest = $this->getLatest();
02091 # Update page record
02092 $dbw->update( 'page',
02093 array(
02094 'page_touched' => $dbw->timestamp(),
02095 'page_restrictions' => '',
02096 'page_latest' => $nullRevId
02097 ), array(
02098 'page_id' => $id
02099 ), 'Article::protect'
02100 );
02101
02102 wfRunHooks( 'NewRevisionFromEditComplete', array($this, $nullRevision, $latest, $wgUser) );
02103 wfRunHooks( 'ArticleProtectComplete', array( &$this, &$wgUser, $limit, $reason ) );
02104
02105 # Update the protection log
02106 $log = new LogPage( 'protect' );
02107 if( $protect ) {
02108 $params = array($protect_description,$cascade ? 'cascade' : '');
02109 $log->addEntry( $modified ? 'modify' : 'protect', $this->mTitle, trim( $reason), $params );
02110 } else {
02111 $log->addEntry( 'unprotect', $this->mTitle, $reason );
02112 }
02113
02114 } # End hook
02115 } # End "changed" check
02116
02117 return true;
02118 }
02119
02126 protected static function flattenRestrictions( $limit ) {
02127 if( !is_array( $limit ) ) {
02128 throw new MWException( 'Article::flattenRestrictions given non-array restriction set' );
02129 }
02130 $bits = array();
02131 ksort( $limit );
02132 foreach( $limit as $action => $restrictions ) {
02133 if( $restrictions != '' ) {
02134 $bits[] = "$action=$restrictions";
02135 }
02136 }
02137 return implode( ':', $bits );
02138 }
02139
02144 public function generateReason( &$hasHistory ) {
02145 global $wgContLang;
02146 $dbw = wfGetDB( DB_MASTER );
02147
02148 $rev = Revision::newFromTitle( $this->mTitle );
02149 if( is_null( $rev ) )
02150 return false;
02151
02152
02153 $contents = $rev->getText();
02154 $blank = false;
02155
02156
02157 if( $contents == '' ) {
02158 $prev = $rev->getPrevious();
02159 if( $prev ) {
02160 $contents = $prev->getText();
02161 $blank = true;
02162 }
02163 }
02164
02165
02166
02167 $limit = 20;
02168 $res = $dbw->select( 'revision', 'rev_user_text',
02169 array( 'rev_page' => $this->getID() ), __METHOD__,
02170 array( 'LIMIT' => $limit )
02171 );
02172 if( $res === false )
02173
02174 return false;
02175 if( $res->numRows() > 1 )
02176 $hasHistory = true;
02177 else
02178 $hasHistory = false;
02179 $row = $dbw->fetchObject( $res );
02180 $onlyAuthor = $row->rev_user_text;
02181
02182 foreach( $res as $row ) {
02183 if( $row->rev_user_text != $onlyAuthor ) {
02184 $onlyAuthor = false;
02185 break;
02186 }
02187 }
02188 $dbw->freeResult( $res );
02189
02190
02191 if( $blank ) {
02192
02193
02194 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
02195 } else {
02196 if( $onlyAuthor )
02197 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
02198 else
02199 $reason = wfMsgForContent( 'excontent', '$1' );
02200 }
02201
02202 if( $reason == '-' ) {
02203
02204 return '';
02205 }
02206
02207
02208 $contents = preg_replace( "/[\n\r]/", ' ', $contents );
02209
02210
02211 $maxLength = 255 - (strlen( $reason ) - 2) - 3;
02212 $contents = $wgContLang->truncate( $contents, $maxLength );
02213
02214 $contents = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $contents );
02215
02216 $reason = str_replace( '$1', $contents, $reason );
02217 return $reason;
02218 }
02219
02220
02221
02222
02223
02224 public function delete() {
02225 global $wgUser, $wgOut, $wgRequest;
02226
02227 $confirm = $wgRequest->wasPosted() &&
02228 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
02229
02230 $this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList', 'other' );
02231 $this->DeleteReason = $wgRequest->getText( 'wpReason' );
02232
02233 $reason = $this->DeleteReasonList;
02234
02235 if( $reason != 'other' && $this->DeleteReason != '' ) {
02236
02237 $reason .= wfMsgForContent( 'colon-separator' ) . $this->DeleteReason;
02238 } elseif( $reason == 'other' ) {
02239 $reason = $this->DeleteReason;
02240 }
02241 # Flag to hide all contents of the archived revisions
02242 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
02243
02244 # This code desperately needs to be totally rewritten
02245
02246 # Read-only check...
02247 if( wfReadOnly() ) {
02248 $wgOut->readOnlyPage();
02249 return;
02250 }
02251
02252 # Check permissions
02253 $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser );
02254
02255 if( count( $permission_errors ) > 0 ) {
02256 $wgOut->showPermissionsErrorPage( $permission_errors );
02257 return;
02258 }
02259
02260 $wgOut->setPagetitle( wfMsg( 'delete-confirm', $this->mTitle->getPrefixedText() ) );
02261
02262 # Better double-check that it hasn't been deleted yet!
02263 $dbw = wfGetDB( DB_MASTER );
02264 $conds = $this->mTitle->pageCond();
02265 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
02266 if( $latest === false ) {
02267 $wgOut->showFatalError( wfMsgExt( 'cannotdelete', array( 'parse' ) ) );
02268 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
02269 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTitle->getPrefixedText() );
02270 return;
02271 }
02272
02273 # Hack for big sites
02274 $bigHistory = $this->isBigDeletion();
02275 if( $bigHistory && !$this->mTitle->userCan( 'bigdelete' ) ) {
02276 global $wgLang, $wgDeleteRevisionsLimit;
02277 $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>\n",
02278 array( 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
02279 return;
02280 }
02281
02282 if( $confirm ) {
02283 $this->doDelete( $reason, $suppress );
02284 if( $wgRequest->getCheck( 'wpWatch' ) ) {
02285 $this->doWatch();
02286 } elseif( $this->mTitle->userIsWatching() ) {
02287 $this->doUnwatch();
02288 }
02289 return;
02290 }
02291
02292
02293 $hasHistory = false;
02294 if( !$reason ) $reason = $this->generateReason($hasHistory);
02295
02296
02297 if( $hasHistory && !$confirm ) {
02298 $skin = $wgUser->getSkin();
02299 $wgOut->addHTML( '<strong>' . wfMsgExt( 'historywarning', array( 'parseinline' ) ) . ' ' . $skin->historyLink() . '</strong>' );
02300 if( $bigHistory ) {
02301 global $wgLang, $wgDeleteRevisionsLimit;
02302 $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>\n",
02303 array( 'delete-warning-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
02304 }
02305 }
02306
02307 return $this->confirmDelete( $reason );
02308 }
02309
02313 public function isBigDeletion() {
02314 global $wgDeleteRevisionsLimit;
02315 if( $wgDeleteRevisionsLimit ) {
02316 $revCount = $this->estimateRevisionCount();
02317 return $revCount > $wgDeleteRevisionsLimit;
02318 }
02319 return false;
02320 }
02321
02325 public function estimateRevisionCount() {
02326 $dbr = wfGetDB( DB_SLAVE );
02327
02328
02329
02330 return $dbr->estimateRowCount( 'revision', '*',
02331 array( 'rev_page' => $this->getId() ), __METHOD__ );
02332 }
02333
02340 public function getLastNAuthors( $num, $revLatest = 0 ) {
02341 wfProfileIn( __METHOD__ );
02342
02343
02344 $continue = 2;
02345 $db = wfGetDB( DB_SLAVE );
02346 do {
02347 $res = $db->select( array( 'page', 'revision' ),
02348 array( 'rev_id', 'rev_user_text' ),
02349 array(
02350 'page_namespace' => $this->mTitle->getNamespace(),
02351 'page_title' => $this->mTitle->getDBkey(),
02352 'rev_page = page_id'
02353 ), __METHOD__, $this->getSelectOptions( array(
02354 'ORDER BY' => 'rev_timestamp DESC',
02355 'LIMIT' => $num
02356 ) )
02357 );
02358 if( !$res ) {
02359 wfProfileOut( __METHOD__ );
02360 return array();
02361 }
02362 $row = $db->fetchObject( $res );
02363 if( $continue == 2 && $revLatest && $row->rev_id != $revLatest ) {
02364 $db = wfGetDB( DB_MASTER );
02365 $continue--;
02366 } else {
02367 $continue = 0;
02368 }
02369 } while ( $continue );
02370
02371 $authors = array( $row->rev_user_text );
02372 while ( $row = $db->fetchObject( $res ) ) {
02373 $authors[] = $row->rev_user_text;
02374 }
02375 wfProfileOut( __METHOD__ );
02376 return $authors;
02377 }
02378
02383 public function confirmDelete( $reason ) {
02384 global $wgOut, $wgUser;
02385
02386 wfDebug( "Article::confirmDelete\n" );
02387
02388 $wgOut->setSubtitle( wfMsgHtml( 'delete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->mTitle ) ) );
02389 $wgOut->setRobotPolicy( 'noindex,nofollow' );
02390 $wgOut->addWikiMsg( 'confirmdeletetext' );
02391
02392 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
02393 $suppress = "<tr id=\"wpDeleteSuppressRow\" name=\"wpDeleteSuppressRow\">
02394 <td></td>
02395 <td class='mw-input'>" .
02396 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
02397 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '4' ) ) .
02398 "</td>
02399 </tr>";
02400 } else {
02401 $suppress = '';
02402 }
02403 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->mTitle->userIsWatching();
02404
02405 $form = Xml::openElement( 'form', array( 'method' => 'post',
02406 'action' => $this->mTitle->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) .
02407 Xml::openElement( 'fieldset', array( 'id' => 'mw-delete-table' ) ) .
02408 Xml::tags( 'legend', null, wfMsgExt( 'delete-legend', array( 'parsemag', 'escapenoentities' ) ) ) .
02409 Xml::openElement( 'table', array( 'id' => 'mw-deleteconfirm-table' ) ) .
02410 "<tr id=\"wpDeleteReasonListRow\">
02411 <td class='mw-label'>" .
02412 Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' ) .
02413 "</td>
02414 <td class='mw-input'>" .
02415 Xml::listDropDown( 'wpDeleteReasonList',
02416 wfMsgForContent( 'deletereason-dropdown' ),
02417 wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
02418 "</td>
02419 </tr>
02420 <tr id=\"wpDeleteReasonRow\">
02421 <td class='mw-label'>" .
02422 Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' ) .
02423 "</td>
02424 <td class='mw-input'>" .
02425 Xml::input( 'wpReason', 60, $reason, array( 'type' => 'text', 'maxlength' => '255',
02426 'tabindex' => '2', 'id' => 'wpReason' ) ) .
02427 "</td>
02428 </tr>
02429 <tr>
02430 <td></td>
02431 <td class='mw-input'>" .
02432 Xml::checkLabel( wfMsg( 'watchthis' ),
02433 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
02434 "</td>
02435 </tr>
02436 $suppress
02437 <tr>
02438 <td></td>
02439 <td class='mw-submit'>" .
02440 Xml::submitButton( wfMsg( 'deletepage' ),
02441 array( 'name' => 'wpConfirmB', 'id' => 'wpConfirmB', 'tabindex' => '5' ) ) .
02442 "</td>
02443 </tr>" .
02444 Xml::closeElement( 'table' ) .
02445 Xml::closeElement( 'fieldset' ) .
02446 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
02447 Xml::closeElement( 'form' );
02448
02449 if( $wgUser->isAllowed( 'editinterface' ) ) {
02450 $skin = $wgUser->getSkin();
02451 $link = $skin->makeLink ( 'MediaWiki:Deletereason-dropdown', wfMsgHtml( 'delete-edit-reasonlist' ) );
02452 $form .= '<p class="mw-delete-editreasons">' . $link . '</p>';
02453 }
02454
02455 $wgOut->addHTML( $form );
02456 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTitle->getPrefixedText() );
02457 }
02458
02462 public function doDelete( $reason, $suppress = false ) {
02463 global $wgOut, $wgUser;
02464 $id = $this->mTitle->getArticleID( GAID_FOR_UPDATE );
02465
02466 $error = '';
02467 if( wfRunHooks('ArticleDelete', array(&$this, &$wgUser, &$reason, &$error)) ) {
02468 if( $this->doDeleteArticle( $reason, $suppress, $id ) ) {
02469 $deleted = $this->mTitle->getPrefixedText();
02470
02471 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
02472 $wgOut->setRobotPolicy( 'noindex,nofollow' );
02473
02474 $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
02475
02476 $wgOut->addWikiMsg( 'deletedtext', $deleted, $loglink );
02477 $wgOut->returnToMain( false );
02478 wfRunHooks('ArticleDeleteComplete', array(&$this, &$wgUser, $reason, $id));
02479 } else {
02480 if( $error == '' ) {
02481 $wgOut->showFatalError( wfMsgExt( 'cannotdelete', array( 'parse' ) ) );
02482 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
02483 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTitle->getPrefixedText() );
02484 } else {
02485 $wgOut->showFatalError( $error );
02486 }
02487 }
02488 }
02489 }
02490
02496 public function doDeleteArticle( $reason, $suppress = false, $id = 0 ) {
02497 global $wgUseSquid, $wgDeferredUpdateList;
02498 global $wgUseTrackbacks;
02499
02500 wfDebug( __METHOD__."\n" );
02501
02502 $dbw = wfGetDB( DB_MASTER );
02503 $ns = $this->mTitle->getNamespace();
02504 $t = $this->mTitle->getDBkey();
02505 $id = $id ? $id : $this->mTitle->getArticleID( GAID_FOR_UPDATE );
02506
02507 if( $t == '' || $id == 0 ) {
02508 return false;
02509 }
02510
02511 $u = new SiteStatsUpdate( 0, 1, -(int)$this->isCountable( $this->getRawText() ), -1 );
02512 array_push( $wgDeferredUpdateList, $u );
02513
02514
02515 if( $suppress ) {
02516 $bitfield = 0;
02517
02518 $bitfield |= Revision::DELETED_TEXT;
02519 $bitfield |= Revision::DELETED_COMMENT;
02520 $bitfield |= Revision::DELETED_USER;
02521 $bitfield |= Revision::DELETED_RESTRICTED;
02522 } else {
02523 $bitfield = 'rev_deleted';
02524 }
02525
02526 $dbw->begin();
02527
02528
02529
02530
02531
02532
02533
02534
02535
02536
02537 $dbw->insertSelect( 'archive', array( 'page', 'revision' ),
02538 array(
02539 'ar_namespace' => 'page_namespace',
02540 'ar_title' => 'page_title',
02541 'ar_comment' => 'rev_comment',
02542 'ar_user' => 'rev_user',
02543 'ar_user_text' => 'rev_user_text',
02544 'ar_timestamp' => 'rev_timestamp',
02545 'ar_minor_edit' => 'rev_minor_edit',
02546 'ar_rev_id' => 'rev_id',
02547 'ar_text_id' => 'rev_text_id',
02548 'ar_text' => '\'\'',
02549 'ar_flags' => '\'\'',
02550 'ar_len' => 'rev_len',
02551 'ar_page_id' => 'page_id',
02552 'ar_deleted' => $bitfield
02553 ), array(
02554 'page_id' => $id,
02555 'page_id = rev_page'
02556 ), __METHOD__
02557 );
02558
02559 # Delete restrictions for it
02560 $dbw->delete( 'page_restrictions', array ( 'pr_page' => $id ), __METHOD__ );
02561
02562 # Now that it's safely backed up, delete it
02563 $dbw->delete( 'page', array( 'page_id' => $id ), __METHOD__);
02564 $ok = ( $dbw->affectedRows() > 0 );
02565 if( !$ok ) {
02566 $dbw->rollback();
02567 return false;
02568 }
02569
02570 # Fix category table counts
02571 $cats = array();
02572 $res = $dbw->select( 'categorylinks', 'cl_to', array( 'cl_from' => $id ), __METHOD__ );
02573 foreach( $res as $row ) {
02574 $cats []= $row->cl_to;
02575 }
02576 $this->updateCategoryCounts( array(), $cats );
02577
02578 # If using cascading deletes, we can skip some explicit deletes
02579 if( !$dbw->cascadingDeletes() ) {
02580 $dbw->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ );
02581
02582 if($wgUseTrackbacks)
02583 $dbw->delete( 'trackbacks', array( 'tb_page' => $id ), __METHOD__ );
02584
02585 # Delete outgoing links
02586 $dbw->delete( 'pagelinks', array( 'pl_from' => $id ) );
02587 $dbw->delete( 'imagelinks', array( 'il_from' => $id ) );
02588 $dbw->delete( 'categorylinks', array( 'cl_from' => $id ) );
02589 $dbw->delete( 'templatelinks', array( 'tl_from' => $id ) );
02590 $dbw->delete( 'externallinks', array( 'el_from' => $id ) );
02591 $dbw->delete( 'langlinks', array( 'll_from' => $id ) );
02592 $dbw->delete( 'redirect', array( 'rd_from' => $id ) );
02593 }
02594
02595 # If using cleanup triggers, we can skip some manual deletes
02596 if( !$dbw->cleanupTriggers() ) {
02597 # Clean up recentchanges entries...
02598 $dbw->delete( 'recentchanges',
02599 array( 'rc_type != '.RC_LOG,
02600 'rc_namespace' => $this->mTitle->getNamespace(),
02601 'rc_title' => $this->mTitle->getDBKey() ),
02602 __METHOD__ );
02603 $dbw->delete( 'recentchanges',
02604 array( 'rc_type != '.RC_LOG, 'rc_cur_id' => $id ),
02605 __METHOD__ );
02606 }
02607
02608 # Clear caches
02609 Article::onArticleDelete( $this->mTitle );
02610
02611 # Clear the cached article id so the interface doesn't act like we exist
02612 $this->mTitle->resetArticleID( 0 );
02613
02614 # Log the deletion, if the page was suppressed, log it at Oversight instead
02615 $logtype = $suppress ? 'suppress' : 'delete';
02616 $log = new LogPage( $logtype );
02617
02618 # Make sure logging got through
02619 $log->addEntry( 'delete', $this->mTitle, $reason, array() );
02620
02621 $dbw->commit();
02622
02623 return true;
02624 }
02625
02647 public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails ) {
02648 global $wgUser;
02649 $resultDetails = null;
02650
02651 # Check permissions
02652 $editErrors = $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser );
02653 $rollbackErrors = $this->mTitle->getUserPermissionsErrors( 'rollback', $wgUser );
02654 $errors = array_merge( $editErrors, wfArrayDiff2( $rollbackErrors, $editErrors ) );
02655
02656 if( !$wgUser->matchEditToken( $token, array( $this->mTitle->getPrefixedText(), $fromP ) ) )
02657 $errors[] = array( 'sessionfailure' );
02658
02659 if( $wgUser->pingLimiter( 'rollback' ) || $wgUser->pingLimiter() ) {
02660 $errors[] = array( 'actionthrottledtext' );
02661 }
02662 # If there were errors, bail out now
02663 if( !empty( $errors ) )
02664 return $errors;
02665
02666 return $this->commitRollback($fromP, $summary, $bot, $resultDetails);
02667 }
02668
02678 public function commitRollback($fromP, $summary, $bot, &$resultDetails) {
02679 global $wgUseRCPatrol, $wgUser, $wgLang;
02680 $dbw = wfGetDB( DB_MASTER );
02681
02682 if( wfReadOnly() ) {
02683 return array( array( 'readonlytext' ) );
02684 }
02685
02686 # Get the last editor
02687 $current = Revision::newFromTitle( $this->mTitle );
02688 if( is_null( $current ) ) {
02689 # Something wrong... no page?
02690 return array(array('notanarticle'));
02691 }
02692
02693 $from = str_replace( '_', ' ', $fromP );
02694 if( $from != $current->getUserText() ) {
02695 $resultDetails = array( 'current' => $current );
02696 return array(array('alreadyrolled',
02697 htmlspecialchars($this->mTitle->getPrefixedText()),
02698 htmlspecialchars($fromP),
02699 htmlspecialchars($current->getUserText())
02700 ));
02701 }
02702
02703 # Get the last edit not by this guy
02704 $user = intval( $current->getUser() );
02705 $user_text = $dbw->addQuotes( $current->getUserText() );
02706 $s = $dbw->selectRow( 'revision',
02707 array( 'rev_id', 'rev_timestamp', 'rev_deleted' ),
02708 array( 'rev_page' => $current->getPage(),
02709 "rev_user != {$user} OR rev_user_text != {$user_text}"
02710 ), __METHOD__,
02711 array( 'USE INDEX' => 'page_timestamp',
02712 'ORDER BY' => 'rev_timestamp DESC' )
02713 );
02714 if( $s === false ) {
02715 # No one else ever edited this page
02716 return array(array('cantrollback'));
02717 } else if( $s->rev_deleted & REVISION::DELETED_TEXT || $s->rev_deleted & REVISION::DELETED_USER ) {
02718 # Only admins can see this text
02719 return array(array('notvisiblerev'));
02720 }
02721
02722 $set = array();
02723 if( $bot && $wgUser->isAllowed('markbotedits') ) {
02724 # Mark all reverted edits as bot
02725 $set['rc_bot'] = 1;
02726 }
02727 if( $wgUseRCPatrol ) {
02728 # Mark all reverted edits as patrolled
02729 $set['rc_patrolled'] = 1;
02730 }
02731
02732 if( $set ) {
02733 $dbw->update( 'recentchanges', $set,
02734 array(
02735 'rc_cur_id' => $current->getPage(),
02736 'rc_user_text' => $current->getUserText(),
02737 "rc_timestamp > '{$s->rev_timestamp}'",
02738 ), __METHOD__
02739 );
02740 }
02741
02742 # Generate the edit summary if necessary
02743 $target = Revision::newFromId( $s->rev_id );
02744 if( empty( $summary ) ){
02745 $summary = wfMsgForContent( 'revertpage' );
02746 }
02747
02748 # Allow the custom summary to use the same args as the default message
02749 $args = array(
02750 $target->getUserText(), $from, $s->rev_id,
02751 $wgLang->timeanddate(wfTimestamp(TS_MW, $s->rev_timestamp), true),
02752 $current->getId(), $wgLang->timeanddate($current->getTimestamp())
02753 );
02754 $summary = wfMsgReplaceArgs( $summary, $args );
02755
02756 # Save
02757 $flags = EDIT_UPDATE;
02758
02759 if( $wgUser->isAllowed('minoredit') )
02760 $flags |= EDIT_MINOR;
02761
02762 if( $bot && ($wgUser->isAllowed('markbotedits') || $wgUser->isAllowed('bot')) )
02763 $flags |= EDIT_FORCE_BOT;
02764 # Actually store the edit
02765 $status = $this->doEdit( $target->getText(), $summary, $flags, $target->getId() );
02766 if( !empty( $status->value['revision'] ) ) {
02767 $revId = $status->value['revision']->getId();
02768 } else {
02769 $revId = false;
02770 }
02771
02772 wfRunHooks( 'ArticleRollbackComplete', array( $this, $wgUser, $target, $current ) );
02773
02774 $resultDetails = array(
02775 'summary' => $summary,
02776 'current' => $current,
02777 'target' => $target,
02778 'newid' => $revId
02779 );
02780 return array();
02781 }
02782
02786 public function rollback() {
02787 global $wgUser, $wgOut, $wgRequest, $wgUseRCPatrol;
02788 $details = null;
02789
02790 $result = $this->doRollback(
02791 $wgRequest->getVal( 'from' ),
02792 $wgRequest->getText( 'summary' ),
02793 $wgRequest->getVal( 'token' ),
02794 $wgRequest->getBool( 'bot' ),
02795 $details
02796 );
02797
02798 if( in_array( array( 'actionthrottledtext' ), $result ) ) {
02799 $wgOut->rateLimited();
02800 return;
02801 }
02802 if( isset( $result[0][0] ) && ( $result[0][0] == 'alreadyrolled' || $result[0][0] == 'cantrollback' ) ) {
02803 $wgOut->setPageTitle( wfMsg( 'rollbackfailed' ) );
02804 $errArray = $result[0];
02805 $errMsg = array_shift( $errArray );
02806 $wgOut->addWikiMsgArray( $errMsg, $errArray );
02807 if( isset( $details['current'] ) ){
02808 $current = $details['current'];
02809 if( $current->getComment() != '' ) {
02810 $wgOut->addWikiMsgArray( 'editcomment', array(
02811 $wgUser->getSkin()->formatComment( $current->getComment() ) ), array( 'replaceafter' ) );
02812 }
02813 }
02814 return;
02815 }
02816 # Display permissions errors before read-only message -- there's no
02817 # point in misleading the user into thinking the inability to rollback
02818 # is only temporary.
02819 if( !empty( $result ) && $result !== array( array( 'readonlytext' ) ) ) {
02820 # array_diff is completely broken for arrays of arrays, sigh. Re-
02821 # move any 'readonlytext' error manually.
02822 $out = array();
02823 foreach( $result as $error ) {
02824 if( $error != array( 'readonlytext' ) ) {
02825 $out []= $error;
02826 }
02827 }
02828 $wgOut->showPermissionsErrorPage( $out );
02829 return;
02830 }
02831 if( $result == array( array( 'readonlytext' ) ) ) {
02832 $wgOut->readOnlyPage();
02833 return;
02834 }
02835
02836 $current = $details['current'];
02837 $target = $details['target'];
02838 $newId = $details['newid'];
02839 $wgOut->setPageTitle( wfMsg( 'actioncomplete' ) );
02840 $wgOut->setRobotPolicy( 'noindex,nofollow' );
02841 $old = $wgUser->getSkin()->userLink( $current->getUser(), $current->getUserText() )
02842 . $wgUser->getSkin()->userToolLinks( $current->getUser(), $current->getUserText() );
02843 $new = $wgUser->getSkin()->userLink( $target->getUser(), $target->getUserText() )
02844 . $wgUser->getSkin()->userToolLinks( $target->getUser(), $target->getUserText() );
02845 $wgOut->addHTML( wfMsgExt( 'rollback-success', array( 'parse', 'replaceafter' ), $old, $new ) );
02846 $wgOut->returnToMain( false, $this->mTitle );
02847
02848 if( !$wgRequest->getBool( 'hidediff', false ) && !$wgUser->getBoolOption( 'norollbackdiff', false ) ) {
02849 $de = new DifferenceEngine( $this->mTitle, $current->getId(), $newId, false, true );
02850 $de->showDiff( '', '' );
02851 }
02852 }
02853
02854
02858 public function viewUpdates() {
02859 global $wgDeferredUpdateList, $wgDisableCounters, $wgUser;
02860 # Don't update page view counters on views from bot users (bug 14044)
02861 if( !$wgDisableCounters && !$wgUser->isAllowed('bot') && $this->getID() ) {
02862 Article::incViewCount( $this->getID() );
02863 $u = new SiteStatsUpdate( 1, 0, 0 );
02864 array_push( $wgDeferredUpdateList, $u );
02865 }
02866 # Update newtalk / watchlist notification status
02867 $wgUser->clearNotification( $this->mTitle );
02868 }
02869
02874 public function prepareTextForEdit( $text, $revid=null ) {
02875 if( $this->mPreparedEdit && $this->mPreparedEdit->newText == $text && $this->mPreparedEdit->revid == $revid) {
02876
02877 return $this->mPreparedEdit;
02878 }
02879 global $wgParser;
02880 $edit = (object)array();
02881 $edit->revid = $revid;
02882 $edit->newText = $text;
02883 $edit->pst = $this->preSaveTransform( $text );
02884 $options = new ParserOptions;
02885 $options->setTidy( true );
02886 $options->enableLimitReport();
02887 $edit->output = $wgParser->parse( $edit->pst, $this->mTitle, $options, true, true, $revid );
02888 $edit->oldText = $this->getContent();
02889 $this->mPreparedEdit = $edit;
02890 return $edit;
02891 }
02892
02907 public function editUpdates( $text, $summary, $minoredit, $timestamp_of_pagechange, $newid, $changed = true ) {
02908 global $wgDeferredUpdateList, $wgMessageCache, $wgUser, $wgParser, $wgEnableParserCache;
02909
02910 wfProfileIn( __METHOD__ );
02911
02912 # Parse the text
02913 # Be careful not to double-PST: $text is usually already PST-ed once
02914 if( !$this->mPreparedEdit || $this->mPreparedEdit->output->getFlag( 'vary-revision' ) ) {
02915 wfDebug( __METHOD__ . ": No prepared edit or vary-revision is set...\n" );
02916 $editInfo = $this->prepareTextForEdit( $text, $newid );
02917 } else {
02918 wfDebug( __METHOD__ . ": No vary-revision, using prepared edit...\n" );
02919 $editInfo = $this->mPreparedEdit;
02920 }
02921
02922 # Save it to the parser cache
02923 if( $wgEnableParserCache ) {
02924 $popts = new ParserOptions;
02925 $popts->setTidy( true );
02926 $popts->enableLimitReport();
02927 $parserCache = ParserCache::singleton();
02928 $parserCache->save( $editInfo->output, $this, $popts );
02929 }
02930
02931 # Update the links tables
02932 $u = new LinksUpdate( $this->mTitle, $editInfo->output );
02933 $u->doUpdate();
02934
02935 wfRunHooks( 'ArticleEditUpdates', array( &$this, &$editInfo, $changed ) );
02936
02937 if( wfRunHooks( 'ArticleEditUpdatesDeleteFromRecentchanges', array( &$this ) ) ) {
02938 if( 0 == mt_rand( 0, 99 ) ) {
02939
02940
02941 global $wgRCMaxAge;
02942 $dbw = wfGetDB( DB_MASTER );
02943 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
02944 $recentchanges = $dbw->tableName( 'recentchanges' );
02945 $sql = "DELETE FROM $recentchanges WHERE rc_timestamp < '{$cutoff}'";
02946 $dbw->query( $sql );
02947 }
02948 }
02949
02950 $id = $this->getID();
02951 $title = $this->mTitle->getPrefixedDBkey();
02952 $shortTitle = $this->mTitle->getDBkey();
02953
02954 if( 0 == $id ) {
02955 wfProfileOut( __METHOD__ );
02956 return;
02957 }
02958
02959 $u = new SiteStatsUpdate( 0, 1, $this->mGoodAdjustment, $this->mTotalAdjustment );
02960 array_push( $wgDeferredUpdateList, $u );
02961 $u = new SearchUpdate( $id, $title, $text );
02962 array_push( $wgDeferredUpdateList, $u );
02963
02964 # If this is another user's talk page, update newtalk
02965 # Don't do this if $changed = false otherwise some idiot can null-edit a
02966 # load of user talk pages and piss people off, nor if it's a minor edit
02967 # by a properly-flagged bot.
02968 if( $this->mTitle->getNamespace() == NS_USER_TALK && $shortTitle != $wgUser->getTitleKey() && $changed
02969 && !( $minoredit && $wgUser->isAllowed( 'nominornewtalk' ) ) ) {
02970 if( wfRunHooks('ArticleEditUpdateNewTalk', array( &$this ) ) ) {
02971 $other = User::newFromName( $shortTitle, false );
02972 if( !$other ) {
02973 wfDebug( __METHOD__.": invalid username\n" );
02974 } elseif( User::isIP( $shortTitle ) ) {
02975
02976 $other->setNewtalk( true );
02977 } elseif( $other->isLoggedIn() ) {
02978 $other->setNewtalk( true );
02979 } else {
02980 wfDebug( __METHOD__. ": don't need to notify a nonexistent user\n" );
02981 }
02982 }
02983 }
02984
02985 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
02986 $wgMessageCache->replace( $shortTitle, $text );
02987 }
02988
02989 wfProfileOut( __METHOD__ );
02990 }
02991
03001 public function createUpdates( $rev ) {
03002 $this->mGoodAdjustment = $this->isCountable( $rev->getText() );
03003 $this->mTotalAdjustment = 1;
03004 $this->editUpdates( $rev->getText(), $rev->getComment(),
03005 $rev->isMinor(), wfTimestamp(), $rev->getId(), true );
03006 }
03007
03016 public function setOldSubtitle( $oldid = 0 ) {
03017 global $wgLang, $wgOut, $wgUser, $wgRequest;
03018
03019 if( !wfRunHooks( 'DisplayOldSubtitle', array( &$this, &$oldid ) ) ) {
03020 return;
03021 }
03022
03023 $revision = Revision::newFromId( $oldid );
03024
03025 $current = ( $oldid == $this->mLatest );
03026 $td = $wgLang->timeanddate( $this->mTimestamp, true );
03027 $sk = $wgUser->getSkin();
03028 $lnk = $current
03029 ? wfMsgHtml( 'currentrevisionlink' )
03030 : $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'currentrevisionlink' ) );
03031 $curdiff = $current
03032 ? wfMsgHtml( 'diff' )
03033 : $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'diff' ), 'diff=cur&oldid='.$oldid );
03034 $prev = $this->mTitle->getPreviousRevisionID( $oldid ) ;
03035 $prevlink = $prev
03036 ? $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'previousrevision' ), 'direction=prev&oldid='.$oldid )
03037 : wfMsgHtml( 'previousrevision' );
03038 $prevdiff = $prev
03039 ? $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'diff' ), 'diff=prev&oldid='.$oldid )
03040 : wfMsgHtml( 'diff' );
03041 $nextlink = $current
03042 ? wfMsgHtml( 'nextrevision' )
03043 : $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'nextrevision' ), 'direction=next&oldid='.$oldid );
03044 $nextdiff = $current
03045 ? wfMsgHtml( 'diff' )
03046 : $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'diff' ), 'diff=next&oldid='.$oldid );
03047
03048 $cdel='';
03049 if( $wgUser->isAllowed( 'deleterevision' ) ) {
03050 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
03051 if( $revision->isCurrent() ) {
03052
03053 $cdel = wfMsgHtml( 'rev-delundel' );
03054 } else if( !$revision->userCan( Revision::DELETED_RESTRICTED ) ) {
03055
03056 $cdel = wfMsgHtml( 'rev-delundel' );
03057 } else {
03058 $cdel = $sk->makeKnownLinkObj( $revdel,
03059 wfMsgHtml('rev-delundel'),
03060 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
03061 '&oldid=' . urlencode( $oldid ) );
03062
03063 if( $revision->isDeleted( Revision::DELETED_RESTRICTED ) )
03064 $cdel = "<strong>$cdel</strong>";
03065 }
03066 $cdel = "(<small>$cdel</small>) ";
03067 }
03068 $unhide = $wgRequest->getInt('unhide') == 1 && $wgUser->matchEditToken( $wgRequest->getVal('token'), $oldid );
03069 # Show user links if allowed to see them. If hidden, then show them only if requested...
03070 $userlinks = $sk->revUserTools( $revision, !$unhide );
03071
03072 $m = wfMsg( 'revision-info-current' );
03073 $infomsg = $current && !wfEmptyMsg( 'revision-info-current', $m ) && $m != '-'
03074 ? 'revision-info-current'
03075 : 'revision-info';
03076
03077 $r = "\n\t\t\t\t<div id=\"mw-{$infomsg}\">" . wfMsgExt( $infomsg, array( 'parseinline', 'replaceafter' ),
03078 $td, $userlinks, $revision->getID() ) . "</div>\n" .
03079
03080 "\n\t\t\t\t<div id=\"mw-revision-nav\">" . $cdel . wfMsgExt( 'revision-nav', array( 'escapenoentities', 'parsemag', 'replaceafter' ),
03081 $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>\n\t\t\t";
03082 $wgOut->setSubtitle( $r );
03083 }
03084
03091 public function preSaveTransform( $text ) {
03092 global $wgParser, $wgUser;
03093 return $wgParser->preSaveTransform( $text, $this->mTitle, $wgUser, ParserOptions::newFromUser( $wgUser ) );
03094 }
03095
03096
03097
03103 protected function tryFileCache() {
03104 static $called = false;
03105 if( $called ) {
03106 wfDebug( "Article::tryFileCache(): called twice!?\n" );
03107 return false;
03108 }
03109 $called = true;
03110 if( $this->isFileCacheable() ) {
03111 $cache = new HTMLFileCache( $this->mTitle );
03112 if( $cache->isFileCacheGood( $this->mTouched ) ) {
03113 wfDebug( "Article::tryFileCache(): about to load file\n" );
03114 $cache->loadFromFileCache();
03115 return true;
03116 } else {
03117 wfDebug( "Article::tryFileCache(): starting buffer\n" );
03118 ob_start( array(&$cache, 'saveToFileCache' ) );
03119 }
03120 } else {
03121 wfDebug( "Article::tryFileCache(): not cacheable\n" );
03122 }
03123 return false;
03124 }
03125
03130 public function isFileCacheable() {
03131 $cacheable = false;
03132 if( HTMLFileCache::useFileCache() ) {
03133 $cacheable = $this->getID() && !$this->mRedirectedFrom;
03134
03135 if( $cacheable ) {
03136 $cacheable = wfRunHooks( 'IsFileCacheable', array( &$this ) );
03137 }
03138 }
03139 return $cacheable;
03140 }
03141
03146 public function checkTouched() {
03147 if( !$this->mDataLoaded ) {
03148 $this->loadPageData();
03149 }
03150 return !$this->mIsRedirect;
03151 }
03152
03156 public function getTouched() {
03157 # Ensure that page data has been loaded
03158 if( !$this->mDataLoaded ) {
03159 $this->loadPageData();
03160 }
03161 return $this->mTouched;
03162 }
03163
03167 public function getLatest() {
03168 if( !$this->mDataLoaded ) {
03169 $this->loadPageData();
03170 }
03171 return (int)$this->mLatest;
03172 }
03173
03183 public function quickEdit( $text, $comment = '', $minor = 0 ) {
03184 wfProfileIn( __METHOD__ );
03185
03186 $dbw = wfGetDB( DB_MASTER );
03187 $revision = new Revision( array(
03188 'page' => $this->getId(),
03189 'text' => $text,
03190 'comment' => $comment,
03191 'minor_edit' => $minor ? 1 : 0,
03192 ) );
03193 $revision->insertOn( $dbw );
03194 $this->updateRevisionOn( $dbw, $revision );
03195
03196 wfRunHooks( 'NewRevisionFromEditComplete', array($this, $revision, false, $wgUser) );
03197
03198 wfProfileOut( __METHOD__ );
03199 }
03200
03206 public static function incViewCount( $id ) {
03207 $id = intval( $id );
03208 global $wgHitcounterUpdateFreq, $wgDBtype;
03209
03210 $dbw = wfGetDB( DB_MASTER );
03211 $pageTable = $dbw->tableName( 'page' );
03212 $hitcounterTable = $dbw->tableName( 'hitcounter' );
03213 $acchitsTable = $dbw->tableName( 'acchits' );
03214
03215 if( $wgHitcounterUpdateFreq <= 1 ) {
03216 $dbw->query( "UPDATE $pageTable SET page_counter = page_counter + 1 WHERE page_id = $id" );
03217 return;
03218 }
03219
03220 # Not important enough to warrant an error page in case of failure
03221 $oldignore = $dbw->ignoreErrors( true );
03222
03223 $dbw->query( "INSERT INTO $hitcounterTable (hc_id) VALUES ({$id})" );
03224
03225 $checkfreq = intval( $wgHitcounterUpdateFreq/25 + 1 );
03226 if( (rand() % $checkfreq != 0) or ($dbw->lastErrno() != 0) ){
03227 # Most of the time (or on SQL errors), skip row count check
03228 $dbw->ignoreErrors( $oldignore );
03229 return;
03230 }
03231
03232 $res = $dbw->query("SELECT COUNT(*) as n FROM $hitcounterTable");
03233 $row = $dbw->fetchObject( $res );
03234 $rown = intval( $row->n );
03235 if( $rown >= $wgHitcounterUpdateFreq ){
03236 wfProfileIn( 'Article::incViewCount-collect' );
03237 $old_user_abort = ignore_user_abort( true );
03238
03239 if($wgDBtype == 'mysql')
03240 $dbw->query("LOCK TABLES $hitcounterTable WRITE");
03241 $tabletype = $wgDBtype == 'mysql' ? "ENGINE=HEAP " : '';
03242 $dbw->query("CREATE TEMPORARY TABLE $acchitsTable $tabletype AS ".
03243 "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable ".
03244 'GROUP BY hc_id');
03245 $dbw->query("DELETE FROM $hitcounterTable");
03246 if($wgDBtype == 'mysql') {
03247 $dbw->query('UNLOCK TABLES');
03248 $dbw->query("UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n ".
03249 'WHERE page_id = hc_id');
03250 }
03251 else {
03252 $dbw->query("UPDATE $pageTable SET page_counter=page_counter + hc_n ".
03253 "FROM $acchitsTable WHERE page_id = hc_id");
03254 }
03255 $dbw->query("DROP TABLE $acchitsTable");
03256
03257 ignore_user_abort( $old_user_abort );
03258 wfProfileOut( 'Article::incViewCount-collect' );
03259 }
03260 $dbw->ignoreErrors( $oldignore );
03261 }
03262
03275 public static function onArticleCreate( $title ) {
03276 # Update existence markers on article/talk tabs...
03277 if( $title->isTalkPage() ) {
03278 $other = $title->getSubjectPage();
03279 } else {
03280 $other = $title->getTalkPage();
03281 }
03282 $other->invalidateCache();
03283 $other->purgeSquid();
03284
03285 $title->touchLinks();
03286 $title->purgeSquid();
03287 $title->deleteTitleProtection();
03288 }
03289
03290 public static function onArticleDelete( $title ) {
03291 global $wgMessageCache;
03292 # Update existence markers on article/talk tabs...
03293 if( $title->isTalkPage() ) {
03294 $other = $title->getSubjectPage();
03295 } else {
03296 $other = $title->getTalkPage();
03297 }
03298 $other->invalidateCache();
03299 $other->purgeSquid();
03300
03301 $title->touchLinks();
03302 $title->purgeSquid();
03303
03304 # File cache
03305 HTMLFileCache::clearFileCache( $title );
03306
03307 # Messages
03308 if( $title->getNamespace() == NS_MEDIAWIKI ) {
03309 $wgMessageCache->replace( $title->getDBkey(), false );
03310 }
03311 # Images
03312 if( $title->getNamespace() == NS_FILE ) {
03313 $update = new HTMLCacheUpdate( $title, 'imagelinks' );
03314 $update->doUpdate();
03315 }
03316 # User talk pages
03317 if( $title->getNamespace() == NS_USER_TALK ) {
03318 $user = User::newFromName( $title->getText(), false );
03319 $user->setNewtalk( false );
03320 }
03321 # Image redirects
03322 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $title );
03323 }
03324
03328 public static function onArticleEdit( $title, $flags = '' ) {
03329 global $wgDeferredUpdateList;
03330
03331
03332 $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'templatelinks' );
03333
03334
03335 $wgDeferredUpdateList[] = new HTMLCacheUpdate( $title, 'redirect' );
03336
03337 # Purge squid for this page only
03338 $title->purgeSquid();
03339
03340 # Clear file cache for this page only
03341 HTMLFileCache::clearFileCache( $title );
03342 }
03343
03350 public function revert() {
03351 global $wgOut;
03352 $wgOut->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
03353 }
03354
03359 public function info() {
03360 global $wgLang, $wgOut, $wgAllowPageInfo, $wgUser;
03361
03362 if( !$wgAllowPageInfo ) {
03363 $wgOut->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
03364 return;
03365 }
03366
03367 $page = $this->mTitle->getSubjectPage();
03368
03369 $wgOut->setPagetitle( $page->getPrefixedText() );
03370 $wgOut->setPageTitleActionText( wfMsg( 'info_short' ) );
03371 $wgOut->setSubtitle( wfMsgHtml( 'infosubtitle' ) );
03372
03373 if( !$this->mTitle->exists() ) {
03374 $wgOut->addHTML( '<div class="noarticletext">' );
03375 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
03376
03377
03378 $wgOut->addHTML( htmlspecialchars( wfMsgWeirdKey( $this->mTitle->getText() ) ) );
03379 } else {
03380 $msg = $wgUser->isLoggedIn()
03381 ? 'noarticletext'
03382 : 'noarticletextanon';
03383 $wgOut->addHTML( wfMsgExt( $msg, 'parse' ) );
03384 }
03385 $wgOut->addHTML( '</div>' );
03386 } else {
03387 $dbr = wfGetDB( DB_SLAVE );
03388 $wl_clause = array(
03389 'wl_title' => $page->getDBkey(),
03390 'wl_namespace' => $page->getNamespace() );
03391 $numwatchers = $dbr->selectField(
03392 'watchlist',
03393 'COUNT(*)',
03394 $wl_clause,
03395 __METHOD__,
03396 $this->getSelectOptions() );
03397
03398 $pageInfo = $this->pageCountInfo( $page );
03399 $talkInfo = $this->pageCountInfo( $page->getTalkPage() );
03400
03401 $wgOut->addHTML( "<ul><li>" . wfMsg("numwatchers", $wgLang->formatNum( $numwatchers ) ) . '</li>' );
03402 $wgOut->addHTML( "<li>" . wfMsg('numedits', $wgLang->formatNum( $pageInfo['edits'] ) ) . '</li>');
03403 if( $talkInfo ) {
03404 $wgOut->addHTML( '<li>' . wfMsg("numtalkedits", $wgLang->formatNum( $talkInfo['edits'] ) ) . '</li>');
03405 }
03406 $wgOut->addHTML( '<li>' . wfMsg("numauthors", $wgLang->formatNum( $pageInfo['authors'] ) ) . '</li>' );
03407 if( $talkInfo ) {
03408 $wgOut->addHTML( '<li>' . wfMsg('numtalkauthors', $wgLang->formatNum( $talkInfo['authors'] ) ) . '</li>' );
03409 }
03410 $wgOut->addHTML( '</ul>' );
03411 }
03412 }
03413
03421 protected function pageCountInfo( $title ) {
03422 $id = $title->getArticleId();
03423 if( $id == 0 ) {
03424 return false;
03425 }
03426 $dbr = wfGetDB( DB_SLAVE );
03427 $rev_clause = array( 'rev_page' => $id );
03428 $edits = $dbr->selectField(
03429 'revision',
03430 'COUNT(rev_page)',
03431 $rev_clause,
03432 __METHOD__,
03433 $this->getSelectOptions()
03434 );
03435 $authors = $dbr->selectField(
03436 'revision',
03437 'COUNT(DISTINCT rev_user_text)',
03438 $rev_clause,
03439 __METHOD__,
03440 $this->getSelectOptions()
03441 );
03442 return array( 'edits' => $edits, 'authors' => $authors );
03443 }
03444
03451 public function getUsedTemplates() {
03452 $result = array();
03453 $id = $this->mTitle->getArticleID();
03454 if( $id == 0 ) {
03455 return array();
03456 }
03457 $dbr = wfGetDB( DB_SLAVE );
03458 $res = $dbr->select( array( 'templatelinks' ),
03459 array( 'tl_namespace', 'tl_title' ),
03460 array( 'tl_from' => $id ),
03461 __METHOD__ );
03462 if( $res !== false ) {
03463 foreach( $res as $row ) {
03464 $result[] = Title::makeTitle( $row->tl_namespace, $row->tl_title );
03465 }
03466 }
03467 $dbr->freeResult( $res );
03468 return $result;
03469 }
03470
03477 public function getHiddenCategories() {
03478 $result = array();
03479 $id = $this->mTitle->getArticleID();
03480 if( $id == 0 ) {
03481 return array();
03482 }
03483 $dbr = wfGetDB( DB_SLAVE );
03484 $res = $dbr->select( array( 'categorylinks', 'page_props', 'page' ),
03485 array( 'cl_to' ),
03486 array( 'cl_from' => $id, 'pp_page=page_id', 'pp_propname' => 'hiddencat',
03487 'page_namespace' => NS_CATEGORY, 'page_title=cl_to'),
03488 __METHOD__ );
03489 if( $res !== false ) {
03490 foreach( $res as $row ) {
03491 $result[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
03492 }
03493 }
03494 $dbr->freeResult( $res );
03495 return $result;
03496 }
03497
03505 public static function getAutosummary( $oldtext, $newtext, $flags ) {
03506 # Decide what kind of autosummary is needed.
03507
03508 # Redirect autosummaries
03509 $ot = Title::newFromRedirect( $oldtext );
03510 $rt = Title::newFromRedirect( $newtext );
03511 if( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
03512 return wfMsgForContent( 'autoredircomment', $rt->getFullText() );
03513 }
03514
03515 # New page autosummaries
03516 if( $flags & EDIT_NEW && strlen( $newtext ) ) {
03517 # If they're making a new article, give its text, truncated, in the summary.
03518 global $wgContLang;
03519 $truncatedtext = $wgContLang->truncate(
03520 str_replace("\n", ' ', $newtext),
03521 max( 0, 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) ) );
03522 return wfMsgForContent( 'autosumm-new', $truncatedtext );
03523 }
03524
03525 # Blanking autosummaries
03526 if( $oldtext != '' && $newtext == '' ) {
03527 return wfMsgForContent( 'autosumm-blank' );
03528 } elseif( strlen( $oldtext ) > 10 * strlen( $newtext ) && strlen( $newtext ) < 500) {
03529 # Removing more than 90% of the article
03530 global $wgContLang;
03531 $truncatedtext = $wgContLang->truncate(
03532 $newtext,
03533 max( 0, 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) ) );
03534 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
03535 }
03536
03537 # If we reach this point, there's no applicable autosummary for our case, so our
03538 # autosummary is empty.
03539 return '';
03540 }
03541
03550 public function outputWikiText( $text, $cache = true ) {
03551 global $wgParser, $wgUser, $wgOut, $wgEnableParserCache, $wgUseFileCache;
03552
03553 $popts = $wgOut->parserOptions();
03554 $popts->setTidy(true);
03555 $popts->enableLimitReport();
03556 $parserOutput = $wgParser->parse( $text, $this->mTitle,
03557 $popts, true, true, $this->getRevIdFetched() );
03558 $popts->setTidy(false);
03559 $popts->enableLimitReport( false );
03560 if( $wgEnableParserCache && $cache && $this && $parserOutput->getCacheTime() != -1 ) {
03561 $parserCache = ParserCache::singleton();
03562 $parserCache->save( $parserOutput, $this, $popts );
03563 }
03564
03565
03566
03567 if( $parserOutput->getCacheTime() == -1 || $parserOutput->containsOldMagic() ) {
03568 $wgUseFileCache = false;
03569 }
03570
03571 if( $this->isCurrent() && !wfReadOnly() && $this->mTitle->areRestrictionsCascading() ) {
03572
03573
03574
03575
03576
03577
03578
03579 # Get templates from templatelinks
03580 $id = $this->mTitle->getArticleID();
03581
03582 $tlTemplates = array();
03583
03584 $dbr = wfGetDB( DB_SLAVE );
03585 $res = $dbr->select( array( 'templatelinks' ),
03586 array( 'tl_namespace', 'tl_title' ),
03587 array( 'tl_from' => $id ),
03588 __METHOD__ );
03589
03590 global $wgContLang;
03591 foreach( $res as $row ) {
03592 $tlTemplates["{$row->tl_namespace}:{$row->tl_title}"] = true;
03593 }
03594
03595 # Get templates from parser output.
03596 $poTemplates = array();
03597 foreach ( $parserOutput->getTemplates() as $ns => $templates ) {
03598 foreach ( $templates as $dbk => $id ) {
03599 $key = $row->tl_namespace . ':'. $row->tl_title;
03600 $poTemplates["$ns:$dbk"] = true;
03601 }
03602 }
03603
03604 # Get the diff
03605 # Note that we simulate array_diff_key in PHP <5.0.x
03606 $templates_diff = array_diff_key( $poTemplates, $tlTemplates );
03607
03608 if( count( $templates_diff ) > 0 ) {
03609 # Whee, link updates time.
03610 $u = new LinksUpdate( $this->mTitle, $parserOutput, false );
03611 $u->doUpdate();
03612 }
03613 }
03614
03615 $wgOut->addParserOutput( $parserOutput );
03616 }
03617
03626 public function updateCategoryCounts( $added, $deleted ) {
03627 $ns = $this->mTitle->getNamespace();
03628 $dbw = wfGetDB( DB_MASTER );
03629
03630 # First make sure the rows exist. If one of the "deleted" ones didn't
03631 # exist, we might legitimately not create it, but it's simpler to just
03632 # create it and then give it a negative value, since the value is bogus
03633 # anyway.
03634 #
03635 # Sometimes I wish we had INSERT ... ON DUPLICATE KEY UPDATE.
03636 $insertCats = array_merge( $added, $deleted );
03637 if( !$insertCats ) {
03638 # Okay, nothing to do
03639 return;
03640 }
03641 $insertRows = array();
03642 foreach( $insertCats as $cat ) {
03643 $insertRows[] = array( 'cat_title' => $cat );
03644 }
03645 $dbw->insert( 'category', $insertRows, __METHOD__, 'IGNORE' );
03646
03647 $addFields = array( 'cat_pages = cat_pages + 1' );
03648 $removeFields = array( 'cat_pages = cat_pages - 1' );
03649 if( $ns == NS_CATEGORY ) {
03650 $addFields[] = 'cat_subcats = cat_subcats + 1';
03651 $removeFields[] = 'cat_subcats = cat_subcats - 1';
03652 } elseif( $ns == NS_FILE ) {
03653 $addFields[] = 'cat_files = cat_files + 1';
03654 $removeFields[] = 'cat_files = cat_files - 1';
03655 }
03656
03657 if( $added ) {
03658 $dbw->update(
03659 'category',
03660 $addFields,
03661 array( 'cat_title' => $added ),
03662 __METHOD__
03663 );
03664 }
03665 if( $deleted ) {
03666 $dbw->update(
03667 'category',
03668 $removeFields,
03669 array( 'cat_title' => $deleted ),
03670 __METHOD__
03671 );
03672 }
03673 }
03674 }