pass context instead of title to getParserOutput
[mediawiki.git] / includes / Article.php
blobdbdae4c8b166bf1a020f2eedd880cd3fb11209a2
1 <?php
2 /**
3 * File for articles
4 * @file
5 */
7 /**
8 * Class for viewing MediaWiki article and history.
10 * This maintains WikiPage functions for backwards compatibility.
12 * @TODO: move and rewrite code to an Action class
14 * See design.txt for an overview.
15 * Note: edit user interface and cache support functions have been
16 * moved to separate EditPage and HTMLFileCache classes.
18 * @internal documentation reviewed 15 Mar 2010
20 class Article extends Page {
21 /**@{{
22 * @private
25 /**
26 * @var IContextSource
28 protected $mContext;
30 /**
31 * @var WikiPage
33 protected $mPage;
35 /**
36 * @var ParserOptions: ParserOptions object for $wgUser articles
38 public $mParserOptions;
40 var $mContent; // !< #BC cruft
42 /**
43 * @var Content
45 var $mContentObject;
47 var $mContentLoaded = false; // !<
48 var $mOldId; // !<
50 /**
51 * @var Title
53 var $mRedirectedFrom = null;
55 /**
56 * @var mixed: boolean false or URL string
58 var $mRedirectUrl = false; // !<
59 var $mRevIdFetched = 0; // !<
61 /**
62 * @var Revision
64 var $mRevision = null;
66 /**
67 * @var ParserOutput
69 var $mParserOutput;
71 /**@}}*/
73 /**
74 * Constructor and clear the article
75 * @param $title Title Reference to a Title object.
76 * @param $oldId Integer revision ID, null to fetch from request, zero for current
78 public function __construct( Title $title, $oldId = null ) {
79 $this->mOldId = $oldId;
80 $this->mPage = $this->newPage( $title );
83 /**
84 * @param $title Title
85 * @return WikiPage
87 protected function newPage( Title $title ) {
88 return new WikiPage( $title );
91 /**
92 * Constructor from a page id
93 * @param $id Int article ID to load
94 * @return Article|null
96 public static function newFromID( $id ) {
97 $t = Title::newFromID( $id );
98 # @todo FIXME: Doesn't inherit right
99 return $t == null ? null : new self( $t );
100 # return $t == null ? null : new static( $t ); // PHP 5.3
104 * Create an Article object of the appropriate class for the given page.
106 * @param $title Title
107 * @param $context IContextSource
108 * @return Article object
110 public static function newFromTitle( $title, IContextSource $context ) {
111 if ( NS_MEDIA == $title->getNamespace() ) {
112 // FIXME: where should this go?
113 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
116 $page = null;
117 wfRunHooks( 'ArticleFromTitle', array( &$title, &$page ) );
118 if ( !$page ) {
119 switch( $title->getNamespace() ) {
120 case NS_FILE:
121 $page = new ImagePage( $title ); #FIXME: teach ImagePage to use ContentHandler
122 break;
123 case NS_CATEGORY:
124 $page = new CategoryPage( $title ); #FIXME: teach ImagePage to use ContentHandler
125 break;
126 default:
127 $handler = ContentHandler::getForTitle( $title );
128 $page = $handler->createArticle( $title );
131 $page->setContext( $context );
133 return $page;
137 * Create an Article object of the appropriate class for the given page.
139 * @param $page WikiPage
140 * @param $context IContextSource
141 * @return Article object
143 public static function newFromWikiPage( WikiPage $page, IContextSource $context ) {
144 $article = self::newFromTitle( $page->getTitle(), $context );
145 $article->mPage = $page; // override to keep process cached vars
146 return $article;
150 * Tell the page view functions that this view was redirected
151 * from another page on the wiki.
152 * @param $from Title object.
154 public function setRedirectedFrom( Title $from ) {
155 $this->mRedirectedFrom = $from;
159 * Get the title object of the article
161 * @return Title object of this page
163 public function getTitle() {
164 return $this->mPage->getTitle();
168 * Get the WikiPage object of this instance
170 * @since 1.19
171 * @return WikiPage
173 public function getPage() {
174 return $this->mPage;
178 * Clear the object
180 public function clear() {
181 $this->mContentLoaded = false;
183 $this->mRedirectedFrom = null; # Title object if set
184 $this->mRevIdFetched = 0;
185 $this->mRedirectUrl = false;
187 $this->mPage->clear();
191 * Note that getContent/loadContent do not follow redirects anymore.
192 * If you need to fetch redirectable content easily, try
193 * the shortcut in WikiPage::getRedirectTarget()
195 * This function has side effects! Do not use this function if you
196 * only want the real revision text if any.
198 * @deprecated in 1.20; use getContentObject() instead
200 * @return string The text of this revision
202 public function getContent() {
203 wfDeprecated( __METHOD__, '1.20' );
204 $content = $this->getContentObject();
205 return ContentHandler::getContentText( $content );
209 * Returns a Content object representing the pages effective display content,
210 * not necessarily the revision's content!
212 * Note that getContent/loadContent do not follow redirects anymore.
213 * If you need to fetch redirectable content easily, try
214 * the shortcut in WikiPage::getRedirectTarget()
216 * This function has side effects! Do not use this function if you
217 * only want the real revision text if any.
219 * @return Content
221 protected function getContentObject() {
222 global $wgUser;
224 wfProfileIn( __METHOD__ );
226 if ( $this->mPage->getID() === 0 ) {
227 # If this is a MediaWiki:x message, then load the messages
228 # and return the message value for x.
229 if ( $this->getTitle()->getNamespace() == NS_MEDIAWIKI ) {
230 $text = $this->getTitle()->getDefaultMessageText();
231 if ( $text === false ) {
232 $text = '';
235 $content = ContentHandler::makeContent( $text, $this->getTitle() );
236 } else {
237 $content = new MessageContent( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon', null, 'parsemag' );
239 wfProfileOut( __METHOD__ );
241 return $content;
242 } else {
243 $this->fetchContentObject();
244 wfProfileOut( __METHOD__ );
246 return $this->mContentObject;
251 * @return int The oldid of the article that is to be shown, 0 for the
252 * current revision
254 public function getOldID() {
255 if ( is_null( $this->mOldId ) ) {
256 $this->mOldId = $this->getOldIDFromRequest();
259 return $this->mOldId;
263 * Sets $this->mRedirectUrl to a correct URL if the query parameters are incorrect
265 * @return int The old id for the request
267 public function getOldIDFromRequest() {
268 global $wgRequest;
270 $this->mRedirectUrl = false;
272 $oldid = $wgRequest->getIntOrNull( 'oldid' );
274 if ( $oldid === null ) {
275 return 0;
278 if ( $oldid !== 0 ) {
279 # Load the given revision and check whether the page is another one.
280 # In that case, update this instance to reflect the change.
281 if ( $oldid === $this->mPage->getLatest() ) {
282 $this->mRevision = $this->mPage->getRevision();
283 } else {
284 $this->mRevision = Revision::newFromId( $oldid );
285 if ( $this->mRevision !== null ) {
286 // Revision title doesn't match the page title given?
287 if ( $this->mPage->getID() != $this->mRevision->getPage() ) {
288 $function = array( get_class( $this->mPage ), 'newFromID' );
289 $this->mPage = call_user_func( $function, $this->mRevision->getPage() );
295 if ( $wgRequest->getVal( 'direction' ) == 'next' ) {
296 $nextid = $this->getTitle()->getNextRevisionID( $oldid );
297 if ( $nextid ) {
298 $oldid = $nextid;
299 $this->mRevision = null;
300 } else {
301 $this->mRedirectUrl = $this->getTitle()->getFullURL( 'redirect=no' );
303 } elseif ( $wgRequest->getVal( 'direction' ) == 'prev' ) {
304 $previd = $this->getTitle()->getPreviousRevisionID( $oldid );
305 if ( $previd ) {
306 $oldid = $previd;
307 $this->mRevision = null;
311 return $oldid;
315 * Load the revision (including text) into this object
317 * @deprecated in 1.19; use fetchContent()
319 function loadContent() {
320 wfDeprecated( __METHOD__, '1.19' );
321 $this->fetchContent();
325 * Get text of an article from database
326 * Does *NOT* follow redirects.
328 * @return mixed string containing article contents, or false if null
329 * @deprecated in 1.20, use getContentObject() instead
331 protected function fetchContent() { #BC cruft!
332 wfDeprecated( __METHOD__, '1.20' );
334 if ( $this->mContentLoaded && $this->mContent ) {
335 return $this->mContent;
338 wfProfileIn( __METHOD__ );
340 $content = $this->fetchContentObject();
342 $this->mContent = ContentHandler::getContentText( $content ); #FIXME: get rid of mContent everywhere!
343 wfRunHooks( 'ArticleAfterFetchContent', array( &$this, &$this->mContent ) ); #BC cruft!
345 wfProfileOut( __METHOD__ );
347 return $this->mContent;
352 * Get text content object
353 * Does *NOT* follow redirects.
354 * TODO: when is this null?
356 * @return Content|null
358 protected function fetchContentObject() {
359 if ( $this->mContentLoaded ) {
360 return $this->mContentObject;
363 wfProfileIn( __METHOD__ );
365 $this->mContentLoaded = true;
366 $this->mContent = null;
368 $oldid = $this->getOldID();
370 # Pre-fill content with error message so that if something
371 # fails we'll have something telling us what we intended.
372 $t = $this->getTitle()->getPrefixedText();
373 $d = $oldid ? wfMsgExt( 'missingarticle-rev', array( 'escape' ), $oldid ) : '';
374 $this->mContentObject = new MessageContent( 'missing-article', array($t, $d), array() ) ;
376 if ( $oldid ) {
377 # $this->mRevision might already be fetched by getOldIDFromRequest()
378 if ( !$this->mRevision ) {
379 $this->mRevision = Revision::newFromId( $oldid );
380 if ( !$this->mRevision ) {
381 wfDebug( __METHOD__ . " failed to retrieve specified revision, id $oldid\n" );
382 wfProfileOut( __METHOD__ );
383 return false;
386 } else {
387 if ( !$this->mPage->getLatest() ) {
388 wfDebug( __METHOD__ . " failed to find page data for title " . $this->getTitle()->getPrefixedText() . "\n" );
389 wfProfileOut( __METHOD__ );
390 return false;
393 $this->mRevision = $this->mPage->getRevision();
395 if ( !$this->mRevision ) {
396 wfDebug( __METHOD__ . " failed to retrieve current page, rev_id " . $this->mPage->getLatest() . "\n" );
397 wfProfileOut( __METHOD__ );
398 return false;
402 // @todo FIXME: Horrible, horrible! This content-loading interface just plain sucks.
403 // We should instead work with the Revision object when we need it...
404 $this->mContentObject = $this->mRevision->getContent( Revision::FOR_THIS_USER ); // Loads if user is allowed
405 $this->mRevIdFetched = $this->mRevision->getId();
407 wfRunHooks( 'ArticleAfterFetchContentObject', array( &$this, &$this->mContentObject ) ); #FIXME: register new hook
409 wfProfileOut( __METHOD__ );
411 return $this->mContentObject;
415 * No-op
416 * @deprecated since 1.18
418 public function forUpdate() {
419 wfDeprecated( __METHOD__, '1.18' );
423 * Returns true if the currently-referenced revision is the current edit
424 * to this page (and it exists).
425 * @return bool
427 public function isCurrent() {
428 # If no oldid, this is the current version.
429 if ( $this->getOldID() == 0 ) {
430 return true;
433 return $this->mPage->exists() && $this->mRevision && $this->mRevision->isCurrent();
437 * Get the fetched Revision object depending on request parameters or null
438 * on failure.
440 * @since 1.19
441 * @return Revision|null
443 public function getRevisionFetched() {
444 $this->fetchContentObject();
446 return $this->mRevision;
450 * Use this to fetch the rev ID used on page views
452 * @return int revision ID of last article revision
454 public function getRevIdFetched() {
455 if ( $this->mRevIdFetched ) {
456 return $this->mRevIdFetched;
457 } else {
458 return $this->mPage->getLatest();
463 * This is the default action of the index.php entry point: just view the
464 * page of the given title.
466 public function view() {
467 global $wgUser, $wgOut, $wgRequest, $wgParser;
468 global $wgUseFileCache, $wgUseETag, $wgDebugToolbar;
470 wfProfileIn( __METHOD__ );
472 # Get variables from query string
473 # As side effect this will load the revision and update the title
474 # in a revision ID is passed in the request, so this should remain
475 # the first call of this method even if $oldid is used way below.
476 $oldid = $this->getOldID();
478 # Another whitelist check in case getOldID() is altering the title
479 $permErrors = $this->getTitle()->getUserPermissionsErrors( 'read', $wgUser );
480 if ( count( $permErrors ) ) {
481 wfDebug( __METHOD__ . ": denied on secondary read check\n" );
482 wfProfileOut( __METHOD__ );
483 throw new PermissionsError( 'read', $permErrors );
486 # getOldID() may as well want us to redirect somewhere else
487 if ( $this->mRedirectUrl ) {
488 $wgOut->redirect( $this->mRedirectUrl );
489 wfDebug( __METHOD__ . ": redirecting due to oldid\n" );
490 wfProfileOut( __METHOD__ );
492 return;
495 # If we got diff in the query, we want to see a diff page instead of the article.
496 if ( $wgRequest->getCheck( 'diff' ) ) {
497 wfDebug( __METHOD__ . ": showing diff page\n" );
498 $this->showDiffPage();
499 wfProfileOut( __METHOD__ );
501 return;
504 # Set page title (may be overridden by DISPLAYTITLE)
505 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
507 $wgOut->setArticleFlag( true );
508 # Allow frames by default
509 $wgOut->allowClickjacking();
511 $parserCache = ParserCache::singleton();
513 $parserOptions = $this->getParserOptions();
514 # Render printable version, use printable version cache
515 if ( $wgOut->isPrintable() ) {
516 $parserOptions->setIsPrintable( true );
517 $parserOptions->setEditSection( false );
518 } elseif ( !$this->isCurrent() || !$this->getTitle()->quickUserCan( 'edit' ) ) {
519 $parserOptions->setEditSection( false );
522 # Try client and file cache
523 if ( !$wgDebugToolbar && $oldid === 0 && $this->mPage->checkTouched() ) {
524 if ( $wgUseETag ) {
525 $wgOut->setETag( $parserCache->getETag( $this, $parserOptions ) );
528 # Is it client cached?
529 if ( $wgOut->checkLastModified( $this->mPage->getTouched() ) ) {
530 wfDebug( __METHOD__ . ": done 304\n" );
531 wfProfileOut( __METHOD__ );
533 return;
534 # Try file cache
535 } elseif ( $wgUseFileCache && $this->tryFileCache() ) {
536 wfDebug( __METHOD__ . ": done file cache\n" );
537 # tell wgOut that output is taken care of
538 $wgOut->disable();
539 $this->mPage->doViewUpdates( $wgUser );
540 wfProfileOut( __METHOD__ );
542 return;
546 # Should the parser cache be used?
547 $useParserCache = $this->mPage->isParserCacheUsed( $parserOptions, $oldid );
548 wfDebug( 'Article::view using parser cache: ' . ( $useParserCache ? 'yes' : 'no' ) . "\n" );
549 if ( $wgUser->getStubThreshold() ) {
550 wfIncrStats( 'pcache_miss_stub' );
553 $this->showRedirectedFromHeader();
554 $this->showNamespaceHeader();
556 # Iterate through the possible ways of constructing the output text.
557 # Keep going until $outputDone is set, or we run out of things to do.
558 $pass = 0;
559 $outputDone = false;
560 $this->mParserOutput = false;
562 while ( !$outputDone && ++$pass ) {
563 switch( $pass ) {
564 case 1:
565 wfRunHooks( 'ArticleViewHeader', array( &$this, &$outputDone, &$useParserCache ) );
566 break;
567 case 2:
568 # Early abort if the page doesn't exist
569 if ( !$this->mPage->exists() ) {
570 wfDebug( __METHOD__ . ": showing missing article\n" );
571 $this->showMissingArticle();
572 wfProfileOut( __METHOD__ );
573 return;
576 # Try the parser cache
577 if ( $useParserCache ) {
578 $this->mParserOutput = $parserCache->get( $this, $parserOptions );
580 if ( $this->mParserOutput !== false ) {
581 if ( $oldid ) {
582 wfDebug( __METHOD__ . ": showing parser cache contents for current rev permalink\n" );
583 $this->setOldSubtitle( $oldid );
584 } else {
585 wfDebug( __METHOD__ . ": showing parser cache contents\n" );
587 $wgOut->addParserOutput( $this->mParserOutput );
588 # Ensure that UI elements requiring revision ID have
589 # the correct version information.
590 $wgOut->setRevisionId( $this->mPage->getLatest() );
591 # Preload timestamp to avoid a DB hit
592 $cachedTimestamp = $this->mParserOutput->getTimestamp();
593 if ( $cachedTimestamp !== null ) {
594 $wgOut->setRevisionTimestamp( $cachedTimestamp );
595 $this->mPage->setTimestamp( $cachedTimestamp );
597 $outputDone = true;
600 break;
601 case 3:
602 # This will set $this->mRevision if needed
603 $this->fetchContentObject();
605 # Are we looking at an old revision
606 if ( $oldid && $this->mRevision ) {
607 $this->setOldSubtitle( $oldid );
609 if ( !$this->showDeletedRevisionHeader() ) {
610 wfDebug( __METHOD__ . ": cannot view deleted revision\n" );
611 wfProfileOut( __METHOD__ );
612 return;
616 # Ensure that UI elements requiring revision ID have
617 # the correct version information.
618 $wgOut->setRevisionId( $this->getRevIdFetched() );
619 # Preload timestamp to avoid a DB hit
620 $wgOut->setRevisionTimestamp( $this->getTimestamp() );
622 # Pages containing custom CSS or JavaScript get special treatment
623 if ( $this->getTitle()->isCssOrJsPage() || $this->getTitle()->isCssJsSubpage() ) {
624 wfDebug( __METHOD__ . ": showing CSS/JS source\n" );
625 $this->showCssOrJsPage();
626 $outputDone = true;
627 } elseif( !wfRunHooks( 'ArticleContentViewCustom', array( $this->fetchContentObject(), $this->getTitle(), $wgOut ) ) ) { #FIXME: document new hook!
628 # Allow extensions do their own custom view for certain pages
629 $outputDone = true;
630 } elseif( Hooks::isRegistered( 'ArticleViewCustom' ) && !wfRunHooks( 'ArticleViewCustom', array( $this->fetchContent(), $this->getTitle(), $wgOut ) ) ) { #FIXME: fetchContent() is deprecated! #FIXME: deprecate hook!
631 # Allow extensions do their own custom view for certain pages
632 $outputDone = true;
633 } else {
634 $content = $this->getContentObject();
635 $rt = $content->getRedirectChain();
636 if ( $rt ) {
637 wfDebug( __METHOD__ . ": showing redirect=no page\n" );
638 # Viewing a redirect page (e.g. with parameter redirect=no)
639 $wgOut->addHTML( $this->viewRedirect( $rt ) );
640 # Parse just to get categories, displaytitle, etc.
641 $this->mParserOutput = $content->getParserOutput( $this->getContext(), $oldid, $parserOptions, false );
642 $wgOut->addParserOutputNoText( $this->mParserOutput );
643 $outputDone = true;
646 break;
647 case 4:
648 # Run the parse, protected by a pool counter
649 wfDebug( __METHOD__ . ": doing uncached parse\n" );
651 $poolArticleView = new PoolWorkArticleView( $this, $parserOptions,
652 $this->getRevIdFetched(), $useParserCache, $this->getContentObject() );
654 if ( !$poolArticleView->execute() ) {
655 $error = $poolArticleView->getError();
656 if ( $error ) {
657 $wgOut->clearHTML(); // for release() errors
658 $wgOut->enableClientCache( false );
659 $wgOut->setRobotPolicy( 'noindex,nofollow' );
661 $errortext = $error->getWikiText( false, 'view-pool-error' );
662 $wgOut->addWikiText( '<div class="errorbox">' . $errortext . '</div>' );
664 # Connection or timeout error
665 wfProfileOut( __METHOD__ );
666 return;
669 $this->mParserOutput = $poolArticleView->getParserOutput();
670 $wgOut->addParserOutput( $this->mParserOutput );
672 # Don't cache a dirty ParserOutput object
673 if ( $poolArticleView->getIsDirty() ) {
674 $wgOut->setSquidMaxage( 0 );
675 $wgOut->addHTML( "<!-- parser cache is expired, sending anyway due to pool overload-->\n" );
678 $outputDone = true;
679 break;
680 # Should be unreachable, but just in case...
681 default:
682 break 2;
686 # Get the ParserOutput actually *displayed* here.
687 # Note that $this->mParserOutput is the *current* version output.
688 $pOutput = ( $outputDone instanceof ParserOutput )
689 ? $outputDone // object fetched by hook
690 : $this->mParserOutput;
692 # Adjust title for main page & pages with displaytitle
693 if ( $pOutput ) {
694 $this->adjustDisplayTitle( $pOutput );
697 # For the main page, overwrite the <title> element with the con-
698 # tents of 'pagetitle-view-mainpage' instead of the default (if
699 # that's not empty).
700 # This message always exists because it is in the i18n files
701 if ( $this->getTitle()->isMainPage() ) {
702 $msg = wfMessage( 'pagetitle-view-mainpage' )->inContentLanguage();
703 if ( !$msg->isDisabled() ) {
704 $wgOut->setHTMLTitle( $msg->title( $this->getTitle() )->text() );
708 # Check for any __NOINDEX__ tags on the page using $pOutput
709 $policy = $this->getRobotPolicy( 'view', $pOutput );
710 $wgOut->setIndexPolicy( $policy['index'] );
711 $wgOut->setFollowPolicy( $policy['follow'] );
713 $this->showViewFooter();
714 $this->mPage->doViewUpdates( $wgUser );
716 wfProfileOut( __METHOD__ );
720 * Adjust title for pages with displaytitle, -{T|}- or language conversion
721 * @param $pOutput ParserOutput
723 public function adjustDisplayTitle( ParserOutput $pOutput ) {
724 global $wgOut;
725 # Adjust the title if it was set by displaytitle, -{T|}- or language conversion
726 $titleText = $pOutput->getTitleText();
727 if ( strval( $titleText ) !== '' ) {
728 $wgOut->setPageTitle( $titleText );
733 * Show a diff page according to current request variables. For use within
734 * Article::view() only, other callers should use the DifferenceEngine class.
736 public function showDiffPage() {
737 global $wgRequest, $wgUser;
739 $diff = $wgRequest->getVal( 'diff' );
740 $rcid = $wgRequest->getVal( 'rcid' );
741 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
742 $purge = $wgRequest->getVal( 'action' ) == 'purge';
743 $unhide = $wgRequest->getInt( 'unhide' ) == 1;
744 $oldid = $this->getOldID();
746 $contentHandler = ContentHandler::getForTitle( $this->getTitle() );
747 $de = $contentHandler->getDifferenceEngine( $this->getContext(), $oldid, $diff, $rcid, $purge, $unhide );
749 // DifferenceEngine directly fetched the revision:
750 $this->mRevIdFetched = $de->mNewid;
751 $de->showDiffPage( $diffOnly );
753 if ( $diff == 0 || $diff == $this->mPage->getLatest() ) {
754 # Run view updates for current revision only
755 $this->mPage->doViewUpdates( $wgUser );
760 * Show a page view for a page formatted as CSS or JavaScript. To be called by
761 * Article::view() only.
763 * This is hooked by SyntaxHighlight_GeSHi to do syntax highlighting of these
764 * page views.
766 protected function showCssOrJsPage( $showCacheHint = true ) {
767 global $wgOut;
769 if ( $showCacheHint ) {
770 $dir = $this->getContext()->getLanguage()->getDir();
771 $lang = $this->getContext()->getLanguage()->getCode();
773 $wgOut->wrapWikiMsg( "<div id='mw-clearyourcache' lang='$lang' dir='$dir' class='mw-content-$dir'>\n$1\n</div>",
774 'clearyourcache' );
777 // Give hooks a chance to customise the output
778 if ( !Hooks::isRegistered('ShowRawCssJs') || wfRunHooks( 'ShowRawCssJs', array( $this->fetchContent(), $this->getTitle(), $wgOut ) ) ) { #FIXME: fetchContent() is deprecated #FIXME: hook is deprecated
779 $po = $this->mContentObject->getParserOutput();
780 $wgOut->addHTML( $po->getText() );
785 * Get the robot policy to be used for the current view
786 * @param $action String the action= GET parameter
787 * @param $pOutput ParserOutput
788 * @return Array the policy that should be set
789 * TODO: actions other than 'view'
791 public function getRobotPolicy( $action, $pOutput ) {
792 global $wgOut, $wgArticleRobotPolicies, $wgNamespaceRobotPolicies;
793 global $wgDefaultRobotPolicy, $wgRequest;
795 $ns = $this->getTitle()->getNamespace();
797 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
798 # Don't index user and user talk pages for blocked users (bug 11443)
799 if ( !$this->getTitle()->isSubpage() ) {
800 if ( Block::newFromTarget( null, $this->getTitle()->getText() ) instanceof Block ) {
801 return array(
802 'index' => 'noindex',
803 'follow' => 'nofollow'
809 if ( $this->mPage->getID() === 0 || $this->getOldID() ) {
810 # Non-articles (special pages etc), and old revisions
811 return array(
812 'index' => 'noindex',
813 'follow' => 'nofollow'
815 } elseif ( $wgOut->isPrintable() ) {
816 # Discourage indexing of printable versions, but encourage following
817 return array(
818 'index' => 'noindex',
819 'follow' => 'follow'
821 } elseif ( $wgRequest->getInt( 'curid' ) ) {
822 # For ?curid=x urls, disallow indexing
823 return array(
824 'index' => 'noindex',
825 'follow' => 'follow'
829 # Otherwise, construct the policy based on the various config variables.
830 $policy = self::formatRobotPolicy( $wgDefaultRobotPolicy );
832 if ( isset( $wgNamespaceRobotPolicies[$ns] ) ) {
833 # Honour customised robot policies for this namespace
834 $policy = array_merge(
835 $policy,
836 self::formatRobotPolicy( $wgNamespaceRobotPolicies[$ns] )
839 if ( $this->getTitle()->canUseNoindex() && is_object( $pOutput ) && $pOutput->getIndexPolicy() ) {
840 # __INDEX__ and __NOINDEX__ magic words, if allowed. Incorporates
841 # a final sanity check that we have really got the parser output.
842 $policy = array_merge(
843 $policy,
844 array( 'index' => $pOutput->getIndexPolicy() )
848 if ( isset( $wgArticleRobotPolicies[$this->getTitle()->getPrefixedText()] ) ) {
849 # (bug 14900) site config can override user-defined __INDEX__ or __NOINDEX__
850 $policy = array_merge(
851 $policy,
852 self::formatRobotPolicy( $wgArticleRobotPolicies[$this->getTitle()->getPrefixedText()] )
856 return $policy;
860 * Converts a String robot policy into an associative array, to allow
861 * merging of several policies using array_merge().
862 * @param $policy Mixed, returns empty array on null/false/'', transparent
863 * to already-converted arrays, converts String.
864 * @return Array: 'index' => <indexpolicy>, 'follow' => <followpolicy>
866 public static function formatRobotPolicy( $policy ) {
867 if ( is_array( $policy ) ) {
868 return $policy;
869 } elseif ( !$policy ) {
870 return array();
873 $policy = explode( ',', $policy );
874 $policy = array_map( 'trim', $policy );
876 $arr = array();
877 foreach ( $policy as $var ) {
878 if ( in_array( $var, array( 'index', 'noindex' ) ) ) {
879 $arr['index'] = $var;
880 } elseif ( in_array( $var, array( 'follow', 'nofollow' ) ) ) {
881 $arr['follow'] = $var;
885 return $arr;
889 * If this request is a redirect view, send "redirected from" subtitle to
890 * $wgOut. Returns true if the header was needed, false if this is not a
891 * redirect view. Handles both local and remote redirects.
893 * @return boolean
895 public function showRedirectedFromHeader() {
896 global $wgOut, $wgRequest, $wgRedirectSources;
898 $rdfrom = $wgRequest->getVal( 'rdfrom' );
900 if ( isset( $this->mRedirectedFrom ) ) {
901 // This is an internally redirected page view.
902 // We'll need a backlink to the source page for navigation.
903 if ( wfRunHooks( 'ArticleViewRedirect', array( &$this ) ) ) {
904 $redir = Linker::linkKnown(
905 $this->mRedirectedFrom,
906 null,
907 array(),
908 array( 'redirect' => 'no' )
911 $wgOut->addSubtitle( wfMessage( 'redirectedfrom' )->rawParams( $redir ) );
913 // Set the fragment if one was specified in the redirect
914 if ( strval( $this->getTitle()->getFragment() ) != '' ) {
915 $fragment = Xml::escapeJsString( $this->getTitle()->getFragmentForURL() );
916 $wgOut->addInlineScript( "redirectToFragment(\"$fragment\");" );
919 // Add a <link rel="canonical"> tag
920 $wgOut->addLink( array( 'rel' => 'canonical',
921 'href' => $this->getTitle()->getLocalURL() )
924 // Tell $wgOut the user arrived at this article through a redirect
925 $wgOut->setRedirectedFrom( $this->mRedirectedFrom );
927 return true;
929 } elseif ( $rdfrom ) {
930 // This is an externally redirected view, from some other wiki.
931 // If it was reported from a trusted site, supply a backlink.
932 if ( $wgRedirectSources && preg_match( $wgRedirectSources, $rdfrom ) ) {
933 $redir = Linker::makeExternalLink( $rdfrom, $rdfrom );
934 $wgOut->addSubtitle( wfMessage( 'redirectedfrom' )->rawParams( $redir ) );
936 return true;
940 return false;
944 * Show a header specific to the namespace currently being viewed, like
945 * [[MediaWiki:Talkpagetext]]. For Article::view().
947 public function showNamespaceHeader() {
948 global $wgOut;
950 if ( $this->getTitle()->isTalkPage() ) {
951 if ( !wfMessage( 'talkpageheader' )->isDisabled() ) {
952 $wgOut->wrapWikiMsg( "<div class=\"mw-talkpageheader\">\n$1\n</div>", array( 'talkpageheader' ) );
958 * Show the footer section of an ordinary page view
960 public function showViewFooter() {
961 global $wgOut;
963 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
964 if ( $this->getTitle()->getNamespace() == NS_USER_TALK && IP::isValid( $this->getTitle()->getText() ) ) {
965 $wgOut->addWikiMsg( 'anontalkpagetext' );
968 # If we have been passed an &rcid= parameter, we want to give the user a
969 # chance to mark this new article as patrolled.
970 $this->showPatrolFooter();
972 wfRunHooks( 'ArticleViewFooter', array( $this ) );
977 * If patrol is possible, output a patrol UI box. This is called from the
978 * footer section of ordinary page views. If patrol is not possible or not
979 * desired, does nothing.
981 public function showPatrolFooter() {
982 global $wgOut, $wgRequest, $wgUser;
984 $rcid = $wgRequest->getVal( 'rcid' );
986 if ( !$rcid || !$this->getTitle()->quickUserCan( 'patrol' ) ) {
987 return;
990 $token = $wgUser->getEditToken( $rcid );
991 $wgOut->preventClickjacking();
993 $wgOut->addHTML(
994 "<div class='patrollink'>" .
995 wfMsgHtml(
996 'markaspatrolledlink',
997 Linker::link(
998 $this->getTitle(),
999 wfMsgHtml( 'markaspatrolledtext' ),
1000 array(),
1001 array(
1002 'action' => 'markpatrolled',
1003 'rcid' => $rcid,
1004 'token' => $token,
1006 array( 'known', 'noclasses' )
1009 '</div>'
1014 * Show the error text for a missing article. For articles in the MediaWiki
1015 * namespace, show the default message text. To be called from Article::view().
1017 public function showMissingArticle() {
1018 global $wgOut, $wgRequest, $wgUser, $wgSend404Code;
1020 # Show info in user (talk) namespace. Does the user exist? Is he blocked?
1021 if ( $this->getTitle()->getNamespace() == NS_USER || $this->getTitle()->getNamespace() == NS_USER_TALK ) {
1022 $parts = explode( '/', $this->getTitle()->getText() );
1023 $rootPart = $parts[0];
1024 $user = User::newFromName( $rootPart, false /* allow IP users*/ );
1025 $ip = User::isIP( $rootPart );
1027 if ( !($user && $user->isLoggedIn()) && !$ip ) { # User does not exist
1028 $wgOut->wrapWikiMsg( "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
1029 array( 'userpage-userdoesnotexist-view', wfEscapeWikiText( $rootPart ) ) );
1030 } elseif ( $user->isBlocked() ) { # Show log extract if the user is currently blocked
1031 LogEventsList::showLogExtract(
1032 $wgOut,
1033 'block',
1034 $user->getUserPage()->getPrefixedText(),
1036 array(
1037 'lim' => 1,
1038 'showIfEmpty' => false,
1039 'msgKey' => array(
1040 'blocked-notice-logextract',
1041 $user->getName() # Support GENDER in notice
1048 wfRunHooks( 'ShowMissingArticle', array( $this ) );
1050 # Show delete and move logs
1051 LogEventsList::showLogExtract( $wgOut, array( 'delete', 'move' ), $this->getTitle()->getPrefixedText(), '',
1052 array( 'lim' => 10,
1053 'conds' => array( "log_action != 'revision'" ),
1054 'showIfEmpty' => false,
1055 'msgKey' => array( 'moveddeleted-notice' ) )
1058 if ( !$this->mPage->hasViewableContent() && $wgSend404Code ) {
1059 // If there's no backing content, send a 404 Not Found
1060 // for better machine handling of broken links.
1061 $wgRequest->response()->header( "HTTP/1.1 404 Not Found" );
1064 $hookResult = wfRunHooks( 'BeforeDisplayNoArticleText', array( $this ) );
1066 if ( ! $hookResult ) {
1067 return;
1070 # Show error message
1071 $oldid = $this->getOldID();
1072 if ( $oldid ) {
1073 $text = wfMsgNoTrans( 'missing-article',
1074 $this->getTitle()->getPrefixedText(),
1075 wfMsgNoTrans( 'missingarticle-rev', $oldid ) );
1076 } elseif ( $this->getTitle()->getNamespace() === NS_MEDIAWIKI ) {
1077 // Use the default message text
1078 $text = $this->getTitle()->getDefaultMessageText();
1079 } else {
1080 $createErrors = $this->getTitle()->getUserPermissionsErrors( 'create', $wgUser );
1081 $editErrors = $this->getTitle()->getUserPermissionsErrors( 'edit', $wgUser );
1082 $errors = array_merge( $createErrors, $editErrors );
1084 if ( !count( $errors ) ) {
1085 $text = wfMsgNoTrans( 'noarticletext' );
1086 } else {
1087 $text = wfMsgNoTrans( 'noarticletext-nopermission' );
1090 $text = "<div class='noarticletext'>\n$text\n</div>";
1092 $wgOut->addWikiText( $text );
1096 * If the revision requested for view is deleted, check permissions.
1097 * Send either an error message or a warning header to $wgOut.
1099 * @return boolean true if the view is allowed, false if not.
1101 public function showDeletedRevisionHeader() {
1102 global $wgOut, $wgRequest;
1104 if ( !$this->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
1105 // Not deleted
1106 return true;
1109 // If the user is not allowed to see it...
1110 if ( !$this->mRevision->userCan( Revision::DELETED_TEXT ) ) {
1111 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1112 'rev-deleted-text-permission' );
1114 return false;
1115 // If the user needs to confirm that they want to see it...
1116 } elseif ( $wgRequest->getInt( 'unhide' ) != 1 ) {
1117 # Give explanation and add a link to view the revision...
1118 $oldid = intval( $this->getOldID() );
1119 $link = $this->getTitle()->getFullUrl( "oldid={$oldid}&unhide=1" );
1120 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1121 'rev-suppressed-text-unhide' : 'rev-deleted-text-unhide';
1122 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1123 array( $msg, $link ) );
1125 return false;
1126 // We are allowed to see...
1127 } else {
1128 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1129 'rev-suppressed-text-view' : 'rev-deleted-text-view';
1130 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", $msg );
1132 return true;
1137 * Generate the navigation links when browsing through an article revisions
1138 * It shows the information as:
1139 * Revision as of \<date\>; view current revision
1140 * \<- Previous version | Next Version -\>
1142 * @param $oldid String: revision ID of this article revision
1144 public function setOldSubtitle( $oldid = 0 ) {
1145 global $wgLang, $wgOut, $wgUser, $wgRequest;
1147 if ( !wfRunHooks( 'DisplayOldSubtitle', array( &$this, &$oldid ) ) ) {
1148 return;
1151 $unhide = $wgRequest->getInt( 'unhide' ) == 1;
1153 # Cascade unhide param in links for easy deletion browsing
1154 $extraParams = array();
1155 if ( $unhide ) {
1156 $extraParams['unhide'] = 1;
1159 if ( $this->mRevision && $this->mRevision->getId() === $oldid ) {
1160 $revision = $this->mRevision;
1161 } else {
1162 $revision = Revision::newFromId( $oldid );
1165 $timestamp = $revision->getTimestamp();
1167 $current = ( $oldid == $this->mPage->getLatest() );
1168 $td = $wgLang->timeanddate( $timestamp, true );
1169 $tddate = $wgLang->date( $timestamp, true );
1170 $tdtime = $wgLang->time( $timestamp, true );
1172 # Show user links if allowed to see them. If hidden, then show them only if requested...
1173 $userlinks = Linker::revUserTools( $revision, !$unhide );
1175 $infomsg = $current && !wfMessage( 'revision-info-current' )->isDisabled()
1176 ? 'revision-info-current'
1177 : 'revision-info';
1179 $wgOut->addSubtitle( "<div id=\"mw-{$infomsg}\">" . wfMessage( $infomsg,
1180 $td )->rawParams( $userlinks )->params( $revision->getID(), $tddate,
1181 $tdtime, $revision->getUser() )->parse() . "</div>" );
1183 $lnk = $current
1184 ? wfMsgHtml( 'currentrevisionlink' )
1185 : Linker::link(
1186 $this->getTitle(),
1187 wfMsgHtml( 'currentrevisionlink' ),
1188 array(),
1189 $extraParams,
1190 array( 'known', 'noclasses' )
1192 $curdiff = $current
1193 ? wfMsgHtml( 'diff' )
1194 : Linker::link(
1195 $this->getTitle(),
1196 wfMsgHtml( 'diff' ),
1197 array(),
1198 array(
1199 'diff' => 'cur',
1200 'oldid' => $oldid
1201 ) + $extraParams,
1202 array( 'known', 'noclasses' )
1204 $prev = $this->getTitle()->getPreviousRevisionID( $oldid ) ;
1205 $prevlink = $prev
1206 ? Linker::link(
1207 $this->getTitle(),
1208 wfMsgHtml( 'previousrevision' ),
1209 array(),
1210 array(
1211 'direction' => 'prev',
1212 'oldid' => $oldid
1213 ) + $extraParams,
1214 array( 'known', 'noclasses' )
1216 : wfMsgHtml( 'previousrevision' );
1217 $prevdiff = $prev
1218 ? Linker::link(
1219 $this->getTitle(),
1220 wfMsgHtml( 'diff' ),
1221 array(),
1222 array(
1223 'diff' => 'prev',
1224 'oldid' => $oldid
1225 ) + $extraParams,
1226 array( 'known', 'noclasses' )
1228 : wfMsgHtml( 'diff' );
1229 $nextlink = $current
1230 ? wfMsgHtml( 'nextrevision' )
1231 : Linker::link(
1232 $this->getTitle(),
1233 wfMsgHtml( 'nextrevision' ),
1234 array(),
1235 array(
1236 'direction' => 'next',
1237 'oldid' => $oldid
1238 ) + $extraParams,
1239 array( 'known', 'noclasses' )
1241 $nextdiff = $current
1242 ? wfMsgHtml( 'diff' )
1243 : Linker::link(
1244 $this->getTitle(),
1245 wfMsgHtml( 'diff' ),
1246 array(),
1247 array(
1248 'diff' => 'next',
1249 'oldid' => $oldid
1250 ) + $extraParams,
1251 array( 'known', 'noclasses' )
1254 $cdel = Linker::getRevDeleteLink( $wgUser, $revision, $this->getTitle() );
1255 if ( $cdel !== '' ) {
1256 $cdel .= ' ';
1259 $wgOut->addSubtitle( "<div id=\"mw-revision-nav\">" . $cdel .
1260 wfMsgExt( 'revision-nav', array( 'escapenoentities', 'parsemag', 'replaceafter' ),
1261 $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>" );
1265 * View redirect
1267 * @param $target Title|Array of destination(s) to redirect
1268 * @param $appendSubtitle Boolean [optional]
1269 * @param $forceKnown Boolean: should the image be shown as a bluelink regardless of existence?
1270 * @return string containing HMTL with redirect link
1272 public function viewRedirect( $target, $appendSubtitle = true, $forceKnown = false ) {
1273 global $wgOut, $wgStylePath;
1275 if ( !is_array( $target ) ) {
1276 $target = array( $target );
1279 $lang = $this->getTitle()->getPageLanguage();
1280 $imageDir = $lang->getDir();
1282 if ( $appendSubtitle ) {
1283 $wgOut->appendSubtitle( wfMsgHtml( 'redirectpagesub' ) );
1286 // the loop prepends the arrow image before the link, so the first case needs to be outside
1289 * @var $title Title
1291 $title = array_shift( $target );
1293 if ( $forceKnown ) {
1294 $link = Linker::linkKnown( $title, htmlspecialchars( $title->getFullText() ) );
1295 } else {
1296 $link = Linker::link( $title, htmlspecialchars( $title->getFullText() ) );
1299 $nextRedirect = $wgStylePath . '/common/images/nextredirect' . $imageDir . '.png';
1300 $alt = $lang->isRTL() ? '←' : '→';
1301 // Automatically append redirect=no to each link, since most of them are redirect pages themselves.
1302 foreach ( $target as $rt ) {
1303 $link .= Html::element( 'img', array( 'src' => $nextRedirect, 'alt' => $alt ) );
1304 if ( $forceKnown ) {
1305 $link .= Linker::linkKnown( $rt, htmlspecialchars( $rt->getFullText(), array(), array( 'redirect' => 'no' ) ) );
1306 } else {
1307 $link .= Linker::link( $rt, htmlspecialchars( $rt->getFullText() ), array(), array( 'redirect' => 'no' ) );
1311 $imageUrl = $wgStylePath . '/common/images/redirect' . $imageDir . '.png';
1312 return '<div class="redirectMsg">' .
1313 Html::element( 'img', array( 'src' => $imageUrl, 'alt' => '#REDIRECT' ) ) .
1314 '<span class="redirectText">' . $link . '</span></div>';
1318 * Handle action=render
1320 public function render() {
1321 global $wgOut;
1323 $wgOut->setArticleBodyOnly( true );
1324 $this->view();
1328 * action=protect handler
1330 public function protect() {
1331 $form = new ProtectionForm( $this );
1332 $form->execute();
1336 * action=unprotect handler (alias)
1338 public function unprotect() {
1339 $this->protect();
1343 * UI entry point for page deletion
1345 public function delete() {
1346 global $wgOut, $wgRequest, $wgLang;
1348 # This code desperately needs to be totally rewritten
1350 $title = $this->getTitle();
1351 $user = $this->getContext()->getUser();
1353 # Check permissions
1354 $permission_errors = $title->getUserPermissionsErrors( 'delete', $user );
1355 if ( count( $permission_errors ) ) {
1356 throw new PermissionsError( 'delete', $permission_errors );
1359 # Read-only check...
1360 if ( wfReadOnly() ) {
1361 throw new ReadOnlyError;
1364 # Better double-check that it hasn't been deleted yet!
1365 $dbw = wfGetDB( DB_MASTER );
1366 $conds = $title->pageCond();
1367 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
1368 if ( $latest === false ) {
1369 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $title->getPrefixedText() ) );
1370 $wgOut->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1371 array( 'cannotdelete', wfEscapeWikiText( $title->getPrefixedText() ) )
1373 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1374 LogEventsList::showLogExtract(
1375 $wgOut,
1376 'delete',
1377 $title->getPrefixedText()
1380 return;
1383 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList', 'other' );
1384 $deleteReason = $wgRequest->getText( 'wpReason' );
1386 if ( $deleteReasonList == 'other' ) {
1387 $reason = $deleteReason;
1388 } elseif ( $deleteReason != '' ) {
1389 // Entry from drop down menu + additional comment
1390 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
1391 } else {
1392 $reason = $deleteReasonList;
1395 if ( $wgRequest->wasPosted() && $user->matchEditToken( $wgRequest->getVal( 'wpEditToken' ),
1396 array( 'delete', $this->getTitle()->getPrefixedText() ) ) )
1398 # Flag to hide all contents of the archived revisions
1399 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $user->isAllowed( 'suppressrevision' );
1401 $this->doDelete( $reason, $suppress );
1403 if ( $wgRequest->getCheck( 'wpWatch' ) && $user->isLoggedIn() ) {
1404 $this->doWatch();
1405 } elseif ( $title->userIsWatching() ) {
1406 $this->doUnwatch();
1409 return;
1412 // Generate deletion reason
1413 $hasHistory = false;
1414 if ( !$reason ) {
1415 try {
1416 $reason = $this->generateReason( $hasHistory );
1417 } catch (MWException $e) {
1418 # if a page is horribly broken, we still want to be able to delete it. so be lenient about errors here.
1419 wfDebug("Error while building auto delete summary: $e");
1420 $reason = '';
1424 // If the page has a history, insert a warning
1425 if ( $hasHistory ) {
1426 $revisions = $this->mTitle->estimateRevisionCount();
1427 // @todo FIXME: i18n issue/patchwork message
1428 $wgOut->addHTML( '<strong class="mw-delete-warning-revisions">' .
1429 wfMsgExt( 'historywarning', array( 'parseinline' ), $wgLang->formatNum( $revisions ) ) .
1430 wfMsgHtml( 'word-separator' ) . Linker::link( $title,
1431 wfMsgHtml( 'history' ),
1432 array( 'rel' => 'archives' ),
1433 array( 'action' => 'history' ) ) .
1434 '</strong>'
1437 if ( $this->mTitle->isBigDeletion() ) {
1438 global $wgDeleteRevisionsLimit;
1439 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n",
1440 array( 'delete-warning-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
1444 return $this->confirmDelete( $reason );
1448 * Output deletion confirmation dialog
1449 * @todo FIXME: Move to another file?
1450 * @param $reason String: prefilled reason
1452 public function confirmDelete( $reason ) {
1453 global $wgOut;
1455 wfDebug( "Article::confirmDelete\n" );
1457 $wgOut->setPageTitle( wfMessage( 'delete-confirm', $this->getTitle()->getPrefixedText() ) );
1458 $wgOut->addBacklinkSubtitle( $this->getTitle() );
1459 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1460 $wgOut->addWikiMsg( 'confirmdeletetext' );
1462 wfRunHooks( 'ArticleConfirmDelete', array( $this, $wgOut, &$reason ) );
1464 $user = $this->getContext()->getUser();
1466 if ( $user->isAllowed( 'suppressrevision' ) ) {
1467 $suppress = "<tr id=\"wpDeleteSuppressRow\">
1468 <td></td>
1469 <td class='mw-input'><strong>" .
1470 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
1471 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '4' ) ) .
1472 "</strong></td>
1473 </tr>";
1474 } else {
1475 $suppress = '';
1477 $checkWatch = $user->getBoolOption( 'watchdeletion' ) || $this->getTitle()->userIsWatching();
1479 $form = Xml::openElement( 'form', array( 'method' => 'post',
1480 'action' => $this->getTitle()->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) .
1481 Xml::openElement( 'fieldset', array( 'id' => 'mw-delete-table' ) ) .
1482 Xml::tags( 'legend', null, wfMsgExt( 'delete-legend', array( 'parsemag', 'escapenoentities' ) ) ) .
1483 Xml::openElement( 'table', array( 'id' => 'mw-deleteconfirm-table' ) ) .
1484 "<tr id=\"wpDeleteReasonListRow\">
1485 <td class='mw-label'>" .
1486 Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' ) .
1487 "</td>
1488 <td class='mw-input'>" .
1489 Xml::listDropDown( 'wpDeleteReasonList',
1490 wfMsgForContent( 'deletereason-dropdown' ),
1491 wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
1492 "</td>
1493 </tr>
1494 <tr id=\"wpDeleteReasonRow\">
1495 <td class='mw-label'>" .
1496 Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' ) .
1497 "</td>
1498 <td class='mw-input'>" .
1499 Html::input( 'wpReason', $reason, 'text', array(
1500 'size' => '60',
1501 'maxlength' => '255',
1502 'tabindex' => '2',
1503 'id' => 'wpReason',
1504 'autofocus'
1505 ) ) .
1506 "</td>
1507 </tr>";
1509 # Disallow watching if user is not logged in
1510 if ( $user->isLoggedIn() ) {
1511 $form .= "
1512 <tr>
1513 <td></td>
1514 <td class='mw-input'>" .
1515 Xml::checkLabel( wfMsg( 'watchthis' ),
1516 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
1517 "</td>
1518 </tr>";
1521 $form .= "
1522 $suppress
1523 <tr>
1524 <td></td>
1525 <td class='mw-submit'>" .
1526 Xml::submitButton( wfMsg( 'deletepage' ),
1527 array( 'name' => 'wpConfirmB', 'id' => 'wpConfirmB', 'tabindex' => '5' ) ) .
1528 "</td>
1529 </tr>" .
1530 Xml::closeElement( 'table' ) .
1531 Xml::closeElement( 'fieldset' ) .
1532 Html::hidden( 'wpEditToken', $user->getEditToken( array( 'delete', $this->getTitle()->getPrefixedText() ) ) ) .
1533 Xml::closeElement( 'form' );
1535 if ( $user->isAllowed( 'editinterface' ) ) {
1536 $title = Title::makeTitle( NS_MEDIAWIKI, 'Deletereason-dropdown' );
1537 $link = Linker::link(
1538 $title,
1539 wfMsgHtml( 'delete-edit-reasonlist' ),
1540 array(),
1541 array( 'action' => 'edit' )
1543 $form .= '<p class="mw-delete-editreasons">' . $link . '</p>';
1546 $wgOut->addHTML( $form );
1547 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1548 LogEventsList::showLogExtract( $wgOut, 'delete',
1549 $this->getTitle()->getPrefixedText()
1554 * Perform a deletion and output success or failure messages
1555 * @param $reason
1556 * @param $suppress bool
1558 public function doDelete( $reason, $suppress = false ) {
1559 global $wgOut;
1561 $error = '';
1562 if ( $this->mPage->doDeleteArticle( $reason, $suppress, 0, true, $error ) ) {
1563 $deleted = $this->getTitle()->getPrefixedText();
1565 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
1566 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1568 $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
1570 $wgOut->addWikiMsg( 'deletedtext', wfEscapeWikiText( $deleted ), $loglink );
1571 $wgOut->returnToMain( false );
1572 } else {
1573 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $this->getTitle()->getPrefixedText() ) );
1574 if ( $error == '' ) {
1575 $wgOut->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1576 array( 'cannotdelete', wfEscapeWikiText( $this->getTitle()->getPrefixedText() ) )
1578 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1580 LogEventsList::showLogExtract(
1581 $wgOut,
1582 'delete',
1583 $this->getTitle()->getPrefixedText()
1585 } else {
1586 $wgOut->addHTML( $error );
1591 /* Caching functions */
1594 * checkLastModified returns true if it has taken care of all
1595 * output to the client that is necessary for this request.
1596 * (that is, it has sent a cached version of the page)
1598 * @return boolean true if cached version send, false otherwise
1600 protected function tryFileCache() {
1601 static $called = false;
1603 if ( $called ) {
1604 wfDebug( "Article::tryFileCache(): called twice!?\n" );
1605 return false;
1608 $called = true;
1609 if ( $this->isFileCacheable() ) {
1610 $cache = HTMLFileCache::newFromTitle( $this->getTitle(), 'view' );
1611 if ( $cache->isCacheGood( $this->mPage->getTouched() ) ) {
1612 wfDebug( "Article::tryFileCache(): about to load file\n" );
1613 $cache->loadFromFileCache( $this->getContext() );
1614 return true;
1615 } else {
1616 wfDebug( "Article::tryFileCache(): starting buffer\n" );
1617 ob_start( array( &$cache, 'saveToFileCache' ) );
1619 } else {
1620 wfDebug( "Article::tryFileCache(): not cacheable\n" );
1623 return false;
1627 * Check if the page can be cached
1628 * @return bool
1630 public function isFileCacheable() {
1631 $cacheable = false;
1633 if ( HTMLFileCache::useFileCache( $this->getContext() ) ) {
1634 $cacheable = $this->mPage->getID()
1635 && !$this->mRedirectedFrom && !$this->getTitle()->isRedirect();
1636 // Extension may have reason to disable file caching on some pages.
1637 if ( $cacheable ) {
1638 $cacheable = wfRunHooks( 'IsFileCacheable', array( &$this ) );
1642 return $cacheable;
1645 /**#@-*/
1648 * Lightweight method to get the parser output for a page, checking the parser cache
1649 * and so on. Doesn't consider most of the stuff that WikiPage::view is forced to
1650 * consider, so it's not appropriate to use there.
1652 * @since 1.16 (r52326) for LiquidThreads
1654 * @param $oldid mixed integer Revision ID or null
1655 * @param $user User The relevant user
1656 * @return ParserOutput or false if the given revsion ID is not found
1658 public function getParserOutput( $oldid = null, User $user = null ) {
1659 global $wgUser;
1661 $user = is_null( $user ) ? $wgUser : $user;
1662 $parserOptions = $this->mPage->makeParserOptions( $user );
1664 return $this->mPage->getParserOutput( $parserOptions, $oldid );
1668 * Get parser options suitable for rendering the primary article wikitext
1669 * @return ParserOptions
1671 public function getParserOptions() {
1672 global $wgUser;
1673 if ( !$this->mParserOptions ) {
1674 $this->mParserOptions = $this->mPage->makeParserOptions( $wgUser );
1676 // Clone to allow modifications of the return value without affecting cache
1677 return clone $this->mParserOptions;
1681 * Sets the context this Article is executed in
1683 * @param $context IContextSource
1684 * @since 1.18
1686 public function setContext( $context ) {
1687 $this->mContext = $context;
1691 * Gets the context this Article is executed in
1693 * @return IContextSource
1694 * @since 1.18
1696 public function getContext() {
1697 if ( $this->mContext instanceof IContextSource ) {
1698 return $this->mContext;
1699 } else {
1700 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
1701 return RequestContext::getMain();
1706 * Info about this page
1707 * @deprecated since 1.19
1709 public function info() {
1710 wfDeprecated( __METHOD__, '1.19' );
1711 Action::factory( 'info', $this )->show();
1715 * Mark this particular edit/page as patrolled
1716 * @deprecated since 1.18
1718 public function markpatrolled() {
1719 wfDeprecated( __METHOD__, '1.18' );
1720 Action::factory( 'markpatrolled', $this )->show();
1724 * Handle action=purge
1725 * @deprecated since 1.19
1727 public function purge() {
1728 return Action::factory( 'purge', $this )->show();
1732 * Handle action=revert
1733 * @deprecated since 1.19
1735 public function revert() {
1736 wfDeprecated( __METHOD__, '1.19' );
1737 Action::factory( 'revert', $this )->show();
1741 * Handle action=rollback
1742 * @deprecated since 1.19
1744 public function rollback() {
1745 wfDeprecated( __METHOD__, '1.19' );
1746 Action::factory( 'rollback', $this )->show();
1750 * User-interface handler for the "watch" action.
1751 * Requires Request to pass a token as of 1.18.
1752 * @deprecated since 1.18
1754 public function watch() {
1755 wfDeprecated( __METHOD__, '1.18' );
1756 Action::factory( 'watch', $this )->show();
1760 * Add this page to $wgUser's watchlist
1762 * This is safe to be called multiple times
1764 * @return bool true on successful watch operation
1765 * @deprecated since 1.18
1767 public function doWatch() {
1768 global $wgUser;
1769 wfDeprecated( __METHOD__, '1.18' );
1770 return WatchAction::doWatch( $this->getTitle(), $wgUser );
1774 * User interface handler for the "unwatch" action.
1775 * Requires Request to pass a token as of 1.18.
1776 * @deprecated since 1.18
1778 public function unwatch() {
1779 wfDeprecated( __METHOD__, '1.18' );
1780 Action::factory( 'unwatch', $this )->show();
1784 * Stop watching a page
1785 * @return bool true on successful unwatch
1786 * @deprecated since 1.18
1788 public function doUnwatch() {
1789 global $wgUser;
1790 wfDeprecated( __METHOD__, '1.18' );
1791 return WatchAction::doUnwatch( $this->getTitle(), $wgUser );
1795 * Output a redirect back to the article.
1796 * This is typically used after an edit.
1798 * @deprecated in 1.18; call $wgOut->redirect() directly
1799 * @param $noRedir Boolean: add redirect=no
1800 * @param $sectionAnchor String: section to redirect to, including "#"
1801 * @param $extraQuery String: extra query params
1803 public function doRedirect( $noRedir = false, $sectionAnchor = '', $extraQuery = '' ) {
1804 wfDeprecated( __METHOD__, '1.18' );
1805 global $wgOut;
1807 if ( $noRedir ) {
1808 $query = 'redirect=no';
1809 if ( $extraQuery )
1810 $query .= "&$extraQuery";
1811 } else {
1812 $query = $extraQuery;
1815 $wgOut->redirect( $this->getTitle()->getFullURL( $query ) . $sectionAnchor );
1819 * Use PHP's magic __get handler to handle accessing of
1820 * raw WikiPage fields for backwards compatibility.
1822 * @param $fname String Field name
1824 public function __get( $fname ) {
1825 if ( property_exists( $this->mPage, $fname ) ) {
1826 #wfWarn( "Access to raw $fname field " . __CLASS__ );
1827 return $this->mPage->$fname;
1829 trigger_error( 'Inaccessible property via __get(): ' . $fname, E_USER_NOTICE );
1833 * Use PHP's magic __set handler to handle setting of
1834 * raw WikiPage fields for backwards compatibility.
1836 * @param $fname String Field name
1837 * @param $fvalue mixed New value
1839 public function __set( $fname, $fvalue ) {
1840 if ( property_exists( $this->mPage, $fname ) ) {
1841 #wfWarn( "Access to raw $fname field of " . __CLASS__ );
1842 $this->mPage->$fname = $fvalue;
1843 // Note: extensions may want to toss on new fields
1844 } elseif ( !in_array( $fname, array( 'mContext', 'mPage' ) ) ) {
1845 $this->mPage->$fname = $fvalue;
1846 } else {
1847 trigger_error( 'Inaccessible property via __set(): ' . $fname, E_USER_NOTICE );
1852 * Use PHP's magic __call handler to transform instance calls to
1853 * WikiPage functions for backwards compatibility.
1855 * @param $fname String Name of called method
1856 * @param $args Array Arguments to the method
1857 * @return mixed
1859 public function __call( $fname, $args ) {
1860 if ( is_callable( array( $this->mPage, $fname ) ) ) {
1861 #wfWarn( "Call to " . __CLASS__ . "::$fname; please use WikiPage instead" );
1862 return call_user_func_array( array( $this->mPage, $fname ), $args );
1864 trigger_error( 'Inaccessible function via __call(): ' . $fname, E_USER_ERROR );
1867 // ****** B/C functions to work-around PHP silliness with __call and references ****** //
1870 * @param $limit array
1871 * @param $expiry array
1872 * @param $cascade bool
1873 * @param $reason string
1874 * @param $user User
1875 * @return Status
1877 public function doUpdateRestrictions( array $limit, array $expiry, &$cascade, $reason, User $user ) {
1878 return $this->mPage->doUpdateRestrictions( $limit, $expiry, $cascade, $reason, $user );
1882 * @param $limit array
1883 * @param $reason string
1884 * @param $cascade int
1885 * @param $expiry array
1886 * @return bool
1888 public function updateRestrictions( $limit = array(), $reason = '', &$cascade = 0, $expiry = array() ) {
1889 return $this->mPage->updateRestrictions( $limit, $reason, $cascade, $expiry );
1893 * @param $reason string
1894 * @param $suppress bool
1895 * @param $id int
1896 * @param $commit bool
1897 * @param $error string
1898 * @return bool
1900 public function doDeleteArticle( $reason, $suppress = false, $id = 0, $commit = true, &$error = '' ) {
1901 return $this->mPage->doDeleteArticle( $reason, $suppress, $id, $commit, $error );
1905 * @param $fromP
1906 * @param $summary
1907 * @param $token
1908 * @param $bot
1909 * @param $resultDetails
1910 * @param $user User
1911 * @return array
1913 public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails, User $user = null ) {
1914 global $wgUser;
1915 $user = is_null( $user ) ? $wgUser : $user;
1916 return $this->mPage->doRollback( $fromP, $summary, $token, $bot, $resultDetails, $user );
1920 * @param $fromP
1921 * @param $summary
1922 * @param $bot
1923 * @param $resultDetails
1924 * @param $guser User
1925 * @return array
1927 public function commitRollback( $fromP, $summary, $bot, &$resultDetails, User $guser = null ) {
1928 global $wgUser;
1929 $guser = is_null( $guser ) ? $wgUser : $guser;
1930 return $this->mPage->commitRollback( $fromP, $summary, $bot, $resultDetails, $guser );
1934 * @param $hasHistory bool
1935 * @return mixed
1937 public function generateReason( &$hasHistory ) {
1938 $title = $this->mPage->getTitle();
1939 $handler = ContentHandler::getForTitle( $title );
1940 return $handler->getAutoDeleteReason( $title, $hasHistory );
1943 // ****** B/C functions for static methods ( __callStatic is PHP>=5.3 ) ****** //
1946 * @return array
1948 public static function selectFields() {
1949 return WikiPage::selectFields();
1953 * @param $title Title
1955 public static function onArticleCreate( $title ) {
1956 WikiPage::onArticleCreate( $title );
1960 * @param $title Title
1962 public static function onArticleDelete( $title ) {
1963 WikiPage::onArticleDelete( $title );
1967 * @param $title Title
1969 public static function onArticleEdit( $title ) {
1970 WikiPage::onArticleEdit( $title );
1974 * @param $oldtext
1975 * @param $newtext
1976 * @param $flags
1977 * @return string
1978 * @deprecated since 1.20, use ContentHandler::getAutosummary() instead
1980 public static function getAutosummary( $oldtext, $newtext, $flags ) {
1981 return WikiPage::getAutosummary( $oldtext, $newtext, $flags );
1983 // ******