Per Aaron, fix for r105764: only set the timestamp if it's set in the cached ParserOu...
[lhc/web/wiklou.git] / includes / Article.php
1 <?php
2 /**
3 * File for articles
4 * @file
5 */
6
7 /**
8 * Class for viewing MediaWiki article and history.
9 *
10 * This maintains WikiPage functions for backwards compatibility.
11 *
12 * @TODO: move and rewrite code to an Action class
13 *
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.
17 *
18 * @internal documentation reviewed 15 Mar 2010
19 */
20 class Article extends Page {
21 /**@{{
22 * @private
23 */
24
25 /**
26 * @var IContextSource
27 */
28 protected $mContext;
29
30 /**
31 * @var WikiPage
32 */
33 protected $mPage;
34
35 /**
36 * @var ParserOptions: ParserOptions object for $wgUser articles
37 */
38 public $mParserOptions;
39
40 var $mContent; // !<
41 var $mContentLoaded = false; // !<
42 var $mOldId; // !<
43
44 /**
45 * @var Title
46 */
47 var $mRedirectedFrom = null;
48
49 /**
50 * @var mixed: boolean false or URL string
51 */
52 var $mRedirectUrl = false; // !<
53 var $mRevIdFetched = 0; // !<
54
55 /**
56 * @var Revision
57 */
58 var $mRevision = null;
59
60 /**
61 * @var ParserOutput
62 */
63 var $mParserOutput;
64
65 /**@}}*/
66
67 /**
68 * Constructor and clear the article
69 * @param $title Title Reference to a Title object.
70 * @param $oldId Integer revision ID, null to fetch from request, zero for current
71 */
72 public function __construct( Title $title, $oldId = null ) {
73 $this->mOldId = $oldId;
74 $this->mPage = $this->newPage( $title );
75 }
76
77 /**
78 * @param $title Title
79 * @return WikiPage
80 */
81 protected function newPage( Title $title ) {
82 return new WikiPage( $title );
83 }
84
85 /**
86 * Constructor from a page id
87 * @param $id Int article ID to load
88 * @return Article|null
89 */
90 public static function newFromID( $id ) {
91 $t = Title::newFromID( $id );
92 # @todo FIXME: Doesn't inherit right
93 return $t == null ? null : new self( $t );
94 # return $t == null ? null : new static( $t ); // PHP 5.3
95 }
96
97 /**
98 * Create an Article object of the appropriate class for the given page.
99 *
100 * @param $title Title
101 * @param $context IContextSource
102 * @return Article object
103 */
104 public static function newFromTitle( $title, IContextSource $context ) {
105 if ( NS_MEDIA == $title->getNamespace() ) {
106 // FIXME: where should this go?
107 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
108 }
109
110 $page = null;
111 wfRunHooks( 'ArticleFromTitle', array( &$title, &$page ) );
112 if ( !$page ) {
113 switch( $title->getNamespace() ) {
114 case NS_FILE:
115 $page = new ImagePage( $title );
116 break;
117 case NS_CATEGORY:
118 $page = new CategoryPage( $title );
119 break;
120 default:
121 $page = new Article( $title );
122 }
123 }
124 $page->setContext( $context );
125
126 return $page;
127 }
128
129 /**
130 * Create an Article object of the appropriate class for the given page.
131 *
132 * @param $page WikiPage
133 * @param $context IContextSource
134 * @return Article object
135 */
136 public static function newFromWikiPage( WikiPage $page, IContextSource $context ) {
137 $article = self::newFromTitle( $page->getTitle(), $context );
138 $article->mPage = $page; // override to keep process cached vars
139 return $article;
140 }
141
142 /**
143 * Tell the page view functions that this view was redirected
144 * from another page on the wiki.
145 * @param $from Title object.
146 */
147 public function setRedirectedFrom( Title $from ) {
148 $this->mRedirectedFrom = $from;
149 }
150
151 /**
152 * Get the title object of the article
153 * @return Title object of this page
154 */
155 public function getTitle() {
156 return $this->mPage->getTitle();
157 }
158
159 /**
160 * Clear the object
161 */
162 public function clear() {
163 $this->mContentLoaded = false;
164
165 $this->mRedirectedFrom = null; # Title object if set
166 $this->mRevIdFetched = 0;
167 $this->mRedirectUrl = false;
168
169 $this->mPage->clear();
170 }
171
172 /**
173 * Note that getContent/loadContent do not follow redirects anymore.
174 * If you need to fetch redirectable content easily, try
175 * the shortcut in Article::followRedirect()
176 *
177 * This function has side effects! Do not use this function if you
178 * only want the real revision text if any.
179 *
180 * @return Return the text of this revision
181 */
182 public function getContent() {
183 global $wgUser;
184
185 wfProfileIn( __METHOD__ );
186
187 if ( $this->mPage->getID() === 0 ) {
188 # If this is a MediaWiki:x message, then load the messages
189 # and return the message value for x.
190 if ( $this->getTitle()->getNamespace() == NS_MEDIAWIKI ) {
191 $text = $this->getTitle()->getDefaultMessageText();
192 if ( $text === false ) {
193 $text = '';
194 }
195 } else {
196 $text = wfMsgExt( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon', 'parsemag' );
197 }
198 wfProfileOut( __METHOD__ );
199
200 return $text;
201 } else {
202 $this->fetchContent();
203 wfProfileOut( __METHOD__ );
204
205 return $this->mContent;
206 }
207 }
208
209 /**
210 * @return int The oldid of the article that is to be shown, 0 for the
211 * current revision
212 */
213 public function getOldID() {
214 if ( is_null( $this->mOldId ) ) {
215 $this->mOldId = $this->getOldIDFromRequest();
216 }
217
218 return $this->mOldId;
219 }
220
221 /**
222 * Sets $this->mRedirectUrl to a correct URL if the query parameters are incorrect
223 *
224 * @return int The old id for the request
225 */
226 public function getOldIDFromRequest() {
227 global $wgRequest;
228
229 $this->mRedirectUrl = false;
230
231 $oldid = $wgRequest->getIntOrNull( 'oldid' );
232
233 if ( $oldid === null ) {
234 return 0;
235 }
236
237 if ( $oldid !== 0 ) {
238 # Load the given revision and check whether the page is another one.
239 # In that case, update this instance to reflect the change.
240 $this->mRevision = Revision::newFromId( $oldid );
241 if ( $this->mRevision !== null ) {
242 // Revision title doesn't match the page title given?
243 if ( $this->mPage->getID() != $this->mRevision->getPage() ) {
244 $function = array( get_class( $this->mPage ), 'newFromID' );
245 $this->mPage = call_user_func( $function, $this->mRevision->getPage() );
246 }
247 }
248 }
249
250 if ( $wgRequest->getVal( 'direction' ) == 'next' ) {
251 $nextid = $this->getTitle()->getNextRevisionID( $oldid );
252 if ( $nextid ) {
253 $oldid = $nextid;
254 } else {
255 $this->mRedirectUrl = $this->getTitle()->getFullURL( 'redirect=no' );
256 }
257 } elseif ( $wgRequest->getVal( 'direction' ) == 'prev' ) {
258 $previd = $this->getTitle()->getPreviousRevisionID( $oldid );
259 if ( $previd ) {
260 $oldid = $previd;
261 }
262 }
263
264 return $oldid;
265 }
266
267 /**
268 * Load the revision (including text) into this object
269 *
270 * @deprecated in 1.19; use fetchContent()
271 */
272 function loadContent() {
273 wfDeprecated( __METHOD__, '1.19' );
274 $this->fetchContent();
275 }
276
277 /**
278 * Get text of an article from database
279 * Does *NOT* follow redirects.
280 *
281 * @return mixed string containing article contents, or false if null
282 */
283 function fetchContent() {
284 if ( $this->mContentLoaded ) {
285 return $this->mContent;
286 }
287
288 wfProfileIn( __METHOD__ );
289
290 $this->mContentLoaded = true;
291
292 $oldid = $this->getOldID();
293
294 # Pre-fill content with error message so that if something
295 # fails we'll have something telling us what we intended.
296 $t = $this->getTitle()->getPrefixedText();
297 $d = $oldid ? wfMsgExt( 'missingarticle-rev', array( 'escape' ), $oldid ) : '';
298 $this->mContent = wfMsgNoTrans( 'missing-article', $t, $d ) ;
299
300 if ( $oldid ) {
301 # $this->mRevision might already be fetched by getOldIDFromRequest()
302 if ( !$this->mRevision ) {
303 $this->mRevision = Revision::newFromId( $oldid );
304 if ( !$this->mRevision ) {
305 wfDebug( __METHOD__ . " failed to retrieve specified revision, id $oldid\n" );
306 wfProfileOut( __METHOD__ );
307 return false;
308 }
309 }
310 } else {
311 if ( !$this->mPage->getLatest() ) {
312 wfDebug( __METHOD__ . " failed to find page data for title " . $this->getTitle()->getPrefixedText() . "\n" );
313 wfProfileOut( __METHOD__ );
314 return false;
315 }
316
317 $this->mRevision = $this->mPage->getRevision();
318 if ( !$this->mRevision ) {
319 wfDebug( __METHOD__ . " failed to retrieve current page, rev_id " . $this->mPage->getLatest() . "\n" );
320 wfProfileOut( __METHOD__ );
321 return false;
322 }
323 }
324
325 // @todo FIXME: Horrible, horrible! This content-loading interface just plain sucks.
326 // We should instead work with the Revision object when we need it...
327 $this->mContent = $this->mRevision->getText( Revision::FOR_THIS_USER ); // Loads if user is allowed
328 $this->mRevIdFetched = $this->mRevision->getId();
329
330 wfRunHooks( 'ArticleAfterFetchContent', array( &$this, &$this->mContent ) );
331
332 wfProfileOut( __METHOD__ );
333
334 return $this->mContent;
335 }
336
337 /**
338 * No-op
339 * @deprecated since 1.18
340 */
341 public function forUpdate() {
342 wfDeprecated( __METHOD__, '1.18' );
343 }
344
345 /**
346 * Returns true if the currently-referenced revision is the current edit
347 * to this page (and it exists).
348 * @return bool
349 */
350 public function isCurrent() {
351 # If no oldid, this is the current version.
352 if ( $this->getOldID() == 0 ) {
353 return true;
354 }
355
356 return $this->mPage->exists() && $this->mRevision && $this->mRevision->isCurrent();
357 }
358
359 /**
360 * Use this to fetch the rev ID used on page views
361 *
362 * @return int revision ID of last article revision
363 */
364 public function getRevIdFetched() {
365 if ( $this->mRevIdFetched ) {
366 return $this->mRevIdFetched;
367 } else {
368 return $this->mPage->getLatest();
369 }
370 }
371
372 /**
373 * This is the default action of the index.php entry point: just view the
374 * page of the given title.
375 */
376 public function view() {
377 global $wgUser, $wgOut, $wgRequest, $wgParser;
378 global $wgUseFileCache, $wgUseETag, $wgDebugToolbar;
379
380 wfProfileIn( __METHOD__ );
381
382 # Get variables from query string
383 # As side effect this will load the revision and update the title
384 # in a revision ID is passed in the request, so this should remain
385 # the first call of this method even if $oldid is used way below.
386 $oldid = $this->getOldID();
387
388 # Another whitelist check in case getOldID() is altering the title
389 $permErrors = $this->getTitle()->getUserPermissionsErrors( 'read', $wgUser );
390 if ( count( $permErrors ) ) {
391 wfDebug( __METHOD__ . ": denied on secondary read check\n" );
392 wfProfileOut( __METHOD__ );
393 throw new PermissionsError( 'read', $permErrors );
394 }
395
396 # getOldID() may as well want us to redirect somewhere else
397 if ( $this->mRedirectUrl ) {
398 $wgOut->redirect( $this->mRedirectUrl );
399 wfDebug( __METHOD__ . ": redirecting due to oldid\n" );
400 wfProfileOut( __METHOD__ );
401
402 return;
403 }
404
405 # If we got diff in the query, we want to see a diff page instead of the article.
406 if ( $wgRequest->getCheck( 'diff' ) ) {
407 wfDebug( __METHOD__ . ": showing diff page\n" );
408 $this->showDiffPage();
409 wfProfileOut( __METHOD__ );
410
411 return;
412 }
413
414 # Set page title (may be overridden by DISPLAYTITLE)
415 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
416
417 $wgOut->setArticleFlag( true );
418 # Allow frames by default
419 $wgOut->allowClickjacking();
420
421 $parserCache = ParserCache::singleton();
422
423 $parserOptions = $this->getParserOptions();
424 # Render printable version, use printable version cache
425 if ( $wgOut->isPrintable() ) {
426 $parserOptions->setIsPrintable( true );
427 $parserOptions->setEditSection( false );
428 } elseif ( !$this->getTitle()->quickUserCan( 'edit' ) ) {
429 $parserOptions->setEditSection( false );
430 }
431
432 # Try client and file cache
433 if ( !$wgDebugToolbar && $oldid === 0 && $this->mPage->checkTouched() ) {
434 if ( $wgUseETag ) {
435 $wgOut->setETag( $parserCache->getETag( $this, $parserOptions ) );
436 }
437
438 # Is it client cached?
439 if ( $wgOut->checkLastModified( $this->mPage->getTouched() ) ) {
440 wfDebug( __METHOD__ . ": done 304\n" );
441 wfProfileOut( __METHOD__ );
442
443 return;
444 # Try file cache
445 } elseif ( $wgUseFileCache && $this->tryFileCache() ) {
446 wfDebug( __METHOD__ . ": done file cache\n" );
447 # tell wgOut that output is taken care of
448 $wgOut->disable();
449 $this->mPage->doViewUpdates( $this->getContext()->getUser() );
450 wfProfileOut( __METHOD__ );
451
452 return;
453 }
454 }
455
456 # Should the parser cache be used?
457 $useParserCache = $this->mPage->isParserCacheUsed( $parserOptions, $oldid );
458 wfDebug( 'Article::view using parser cache: ' . ( $useParserCache ? 'yes' : 'no' ) . "\n" );
459 if ( $wgUser->getStubThreshold() ) {
460 wfIncrStats( 'pcache_miss_stub' );
461 }
462
463 $this->showRedirectedFromHeader();
464 $this->showNamespaceHeader();
465
466 # Iterate through the possible ways of constructing the output text.
467 # Keep going until $outputDone is set, or we run out of things to do.
468 $pass = 0;
469 $outputDone = false;
470 $this->mParserOutput = false;
471
472 while ( !$outputDone && ++$pass ) {
473 switch( $pass ) {
474 case 1:
475 wfRunHooks( 'ArticleViewHeader', array( &$this, &$outputDone, &$useParserCache ) );
476 break;
477 case 2:
478 # Early abort if the page doesn't exist
479 if ( !$this->mPage->exists() ) {
480 wfDebug( __METHOD__ . ": showing missing article\n" );
481 $this->showMissingArticle();
482 wfProfileOut( __METHOD__ );
483 return;
484 }
485
486 # Try the parser cache
487 if ( $useParserCache ) {
488 $this->mParserOutput = $parserCache->get( $this, $parserOptions );
489
490 if ( $this->mParserOutput !== false ) {
491 if ( $oldid ) {
492 wfDebug( __METHOD__ . ": showing parser cache contents for current rev permalink\n" );
493 $this->setOldSubtitle( $oldid );
494 } else {
495 wfDebug( __METHOD__ . ": showing parser cache contents\n" );
496 }
497 $wgOut->addParserOutput( $this->mParserOutput );
498 # Ensure that UI elements requiring revision ID have
499 # the correct version information.
500 $wgOut->setRevisionId( $this->mPage->getLatest() );
501 # Preload timestamp to avoid a DB hit
502 $cachedTimestamp = $this->mParserOutput->getTimestamp();
503 var_dump( $cachedTimestamp );
504 if ( $cachedTimestamp !== null ) {
505 $wgOut->setRevisionTimestamp( $cachedTimestamp );
506 $this->mPage->setTimestamp( $cachedTimestamp );
507 }
508 $outputDone = true;
509 }
510 }
511 break;
512 case 3:
513 # This will set $this->mRevision if needed
514 $this->fetchContent();
515
516 # Are we looking at an old revision
517 if ( $oldid && $this->mRevision ) {
518 $this->setOldSubtitle( $oldid );
519
520 if ( !$this->showDeletedRevisionHeader() ) {
521 wfDebug( __METHOD__ . ": cannot view deleted revision\n" );
522 wfProfileOut( __METHOD__ );
523 return;
524 }
525 }
526
527 # Ensure that UI elements requiring revision ID have
528 # the correct version information.
529 $wgOut->setRevisionId( $this->getRevIdFetched() );
530 # Preload timestamp to avoid a DB hit
531 $wgOut->setRevisionTimestamp( $this->getTimestamp() );
532
533 # Pages containing custom CSS or JavaScript get special treatment
534 if ( $this->getTitle()->isCssOrJsPage() || $this->getTitle()->isCssJsSubpage() ) {
535 wfDebug( __METHOD__ . ": showing CSS/JS source\n" );
536 $this->showCssOrJsPage();
537 $outputDone = true;
538 } elseif( !wfRunHooks( 'ArticleViewCustom', array( $this->mContent, $this->getTitle(), $wgOut ) ) ) {
539 # Allow extensions do their own custom view for certain pages
540 $outputDone = true;
541 } else {
542 $text = $this->getContent();
543 $rt = Title::newFromRedirectArray( $text );
544 if ( $rt ) {
545 wfDebug( __METHOD__ . ": showing redirect=no page\n" );
546 # Viewing a redirect page (e.g. with parameter redirect=no)
547 $wgOut->addHTML( $this->viewRedirect( $rt ) );
548 # Parse just to get categories, displaytitle, etc.
549 $this->mParserOutput = $wgParser->parse( $text, $this->getTitle(), $parserOptions );
550 $wgOut->addParserOutputNoText( $this->mParserOutput );
551 $outputDone = true;
552 }
553 }
554 break;
555 case 4:
556 # Run the parse, protected by a pool counter
557 wfDebug( __METHOD__ . ": doing uncached parse\n" );
558
559 $poolArticleView = new PoolWorkArticleView( $this, $parserOptions,
560 $this->getRevIdFetched(), $useParserCache, $this->getContent() );
561
562 if ( !$poolArticleView->execute() ) {
563 $error = $poolArticleView->getError();
564 if ( $error ) {
565 $wgOut->clearHTML(); // for release() errors
566 $wgOut->enableClientCache( false );
567 $wgOut->setRobotPolicy( 'noindex,nofollow' );
568
569 $errortext = $error->getWikiText( false, 'view-pool-error' );
570 $wgOut->addWikiText( '<div class="errorbox">' . $errortext . '</div>' );
571 }
572 # Connection or timeout error
573 wfProfileOut( __METHOD__ );
574 return;
575 }
576
577 $this->mParserOutput = $poolArticleView->getParserOutput();
578 $wgOut->addParserOutput( $this->mParserOutput );
579
580 # Don't cache a dirty ParserOutput object
581 if ( $poolArticleView->getIsDirty() ) {
582 $wgOut->setSquidMaxage( 0 );
583 $wgOut->addHTML( "<!-- parser cache is expired, sending anyway due to pool overload-->\n" );
584 }
585
586 $outputDone = true;
587 break;
588 # Should be unreachable, but just in case...
589 default:
590 break 2;
591 }
592 }
593
594 # Get the ParserOutput actually *displayed* here.
595 # Note that $this->mParserOutput is the *current* version output.
596 $pOutput = ( $outputDone instanceof ParserOutput )
597 ? $outputDone // object fetched by hook
598 : $this->mParserOutput;
599
600 # Adjust title for main page & pages with displaytitle
601 if ( $pOutput ) {
602 $this->adjustDisplayTitle( $pOutput );
603 }
604
605 # For the main page, overwrite the <title> element with the con-
606 # tents of 'pagetitle-view-mainpage' instead of the default (if
607 # that's not empty).
608 # This message always exists because it is in the i18n files
609 if ( $this->getTitle()->isMainPage() ) {
610 $msg = wfMessage( 'pagetitle-view-mainpage' )->inContentLanguage();
611 if ( !$msg->isDisabled() ) {
612 $wgOut->setHTMLTitle( $msg->title( $this->getTitle() )->text() );
613 }
614 }
615
616 # Check for any __NOINDEX__ tags on the page using $pOutput
617 $policy = $this->getRobotPolicy( 'view', $pOutput );
618 $wgOut->setIndexPolicy( $policy['index'] );
619 $wgOut->setFollowPolicy( $policy['follow'] );
620
621 $this->showViewFooter();
622 $this->mPage->doViewUpdates( $this->getContext()->getUser() );
623
624 wfProfileOut( __METHOD__ );
625 }
626
627 /**
628 * Adjust title for pages with displaytitle, -{T|}- or language conversion
629 * @param $pOutput ParserOutput
630 */
631 public function adjustDisplayTitle( ParserOutput $pOutput ) {
632 global $wgOut;
633 # Adjust the title if it was set by displaytitle, -{T|}- or language conversion
634 $titleText = $pOutput->getTitleText();
635 if ( strval( $titleText ) !== '' ) {
636 $wgOut->setPageTitle( $titleText );
637 }
638 }
639
640 /**
641 * Show a diff page according to current request variables. For use within
642 * Article::view() only, other callers should use the DifferenceEngine class.
643 */
644 public function showDiffPage() {
645 global $wgRequest, $wgUser;
646
647 $diff = $wgRequest->getVal( 'diff' );
648 $rcid = $wgRequest->getVal( 'rcid' );
649 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
650 $purge = $wgRequest->getVal( 'action' ) == 'purge';
651 $unhide = $wgRequest->getInt( 'unhide' ) == 1;
652 $oldid = $this->getOldID();
653
654 $de = new DifferenceEngine( $this->getContext(), $oldid, $diff, $rcid, $purge, $unhide );
655 // DifferenceEngine directly fetched the revision:
656 $this->mRevIdFetched = $de->mNewid;
657 $de->showDiffPage( $diffOnly );
658
659 if ( $diff == 0 || $diff == $this->mPage->getLatest() ) {
660 # Run view updates for current revision only
661 $this->mPage->doViewUpdates( $this->getContext()->getUser() );
662 }
663 }
664
665 /**
666 * Show a page view for a page formatted as CSS or JavaScript. To be called by
667 * Article::view() only.
668 *
669 * This is hooked by SyntaxHighlight_GeSHi to do syntax highlighting of these
670 * page views.
671 */
672 protected function showCssOrJsPage() {
673 global $wgOut;
674
675 $dir = $this->getContext()->getLanguage()->getDir();
676 $lang = $this->getContext()->getLanguage()->getCode();
677
678 $wgOut->wrapWikiMsg( "<div id='mw-clearyourcache' lang='$lang' dir='$dir' class='mw-content-$dir'>\n$1\n</div>",
679 'clearyourcache' );
680
681 // Give hooks a chance to customise the output
682 if ( wfRunHooks( 'ShowRawCssJs', array( $this->mContent, $this->getTitle(), $wgOut ) ) ) {
683 // Wrap the whole lot in a <pre> and don't parse
684 $m = array();
685 preg_match( '!\.(css|js)$!u', $this->getTitle()->getText(), $m );
686 $wgOut->addHTML( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
687 $wgOut->addHTML( htmlspecialchars( $this->mContent ) );
688 $wgOut->addHTML( "\n</pre>\n" );
689 }
690 }
691
692 /**
693 * Get the robot policy to be used for the current view
694 * @param $action String the action= GET parameter
695 * @param $pOutput ParserOutput
696 * @return Array the policy that should be set
697 * TODO: actions other than 'view'
698 */
699 public function getRobotPolicy( $action, $pOutput ) {
700 global $wgOut, $wgArticleRobotPolicies, $wgNamespaceRobotPolicies;
701 global $wgDefaultRobotPolicy, $wgRequest;
702
703 $ns = $this->getTitle()->getNamespace();
704
705 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
706 # Don't index user and user talk pages for blocked users (bug 11443)
707 if ( !$this->getTitle()->isSubpage() ) {
708 if ( Block::newFromTarget( null, $this->getTitle()->getText() ) instanceof Block ) {
709 return array(
710 'index' => 'noindex',
711 'follow' => 'nofollow'
712 );
713 }
714 }
715 }
716
717 if ( $this->mPage->getID() === 0 || $this->getOldID() ) {
718 # Non-articles (special pages etc), and old revisions
719 return array(
720 'index' => 'noindex',
721 'follow' => 'nofollow'
722 );
723 } elseif ( $wgOut->isPrintable() ) {
724 # Discourage indexing of printable versions, but encourage following
725 return array(
726 'index' => 'noindex',
727 'follow' => 'follow'
728 );
729 } elseif ( $wgRequest->getInt( 'curid' ) ) {
730 # For ?curid=x urls, disallow indexing
731 return array(
732 'index' => 'noindex',
733 'follow' => 'follow'
734 );
735 }
736
737 # Otherwise, construct the policy based on the various config variables.
738 $policy = self::formatRobotPolicy( $wgDefaultRobotPolicy );
739
740 if ( isset( $wgNamespaceRobotPolicies[$ns] ) ) {
741 # Honour customised robot policies for this namespace
742 $policy = array_merge(
743 $policy,
744 self::formatRobotPolicy( $wgNamespaceRobotPolicies[$ns] )
745 );
746 }
747 if ( $this->getTitle()->canUseNoindex() && is_object( $pOutput ) && $pOutput->getIndexPolicy() ) {
748 # __INDEX__ and __NOINDEX__ magic words, if allowed. Incorporates
749 # a final sanity check that we have really got the parser output.
750 $policy = array_merge(
751 $policy,
752 array( 'index' => $pOutput->getIndexPolicy() )
753 );
754 }
755
756 if ( isset( $wgArticleRobotPolicies[$this->getTitle()->getPrefixedText()] ) ) {
757 # (bug 14900) site config can override user-defined __INDEX__ or __NOINDEX__
758 $policy = array_merge(
759 $policy,
760 self::formatRobotPolicy( $wgArticleRobotPolicies[$this->getTitle()->getPrefixedText()] )
761 );
762 }
763
764 return $policy;
765 }
766
767 /**
768 * Converts a String robot policy into an associative array, to allow
769 * merging of several policies using array_merge().
770 * @param $policy Mixed, returns empty array on null/false/'', transparent
771 * to already-converted arrays, converts String.
772 * @return Array: 'index' => <indexpolicy>, 'follow' => <followpolicy>
773 */
774 public static function formatRobotPolicy( $policy ) {
775 if ( is_array( $policy ) ) {
776 return $policy;
777 } elseif ( !$policy ) {
778 return array();
779 }
780
781 $policy = explode( ',', $policy );
782 $policy = array_map( 'trim', $policy );
783
784 $arr = array();
785 foreach ( $policy as $var ) {
786 if ( in_array( $var, array( 'index', 'noindex' ) ) ) {
787 $arr['index'] = $var;
788 } elseif ( in_array( $var, array( 'follow', 'nofollow' ) ) ) {
789 $arr['follow'] = $var;
790 }
791 }
792
793 return $arr;
794 }
795
796 /**
797 * If this request is a redirect view, send "redirected from" subtitle to
798 * $wgOut. Returns true if the header was needed, false if this is not a
799 * redirect view. Handles both local and remote redirects.
800 *
801 * @return boolean
802 */
803 public function showRedirectedFromHeader() {
804 global $wgOut, $wgRequest, $wgRedirectSources;
805
806 $rdfrom = $wgRequest->getVal( 'rdfrom' );
807
808 if ( isset( $this->mRedirectedFrom ) ) {
809 // This is an internally redirected page view.
810 // We'll need a backlink to the source page for navigation.
811 if ( wfRunHooks( 'ArticleViewRedirect', array( &$this ) ) ) {
812 $redir = Linker::linkKnown(
813 $this->mRedirectedFrom,
814 null,
815 array(),
816 array( 'redirect' => 'no' )
817 );
818
819 $wgOut->addSubtitle( wfMessage( 'redirectedfrom' )->rawParams( $redir ) );
820
821 // Set the fragment if one was specified in the redirect
822 if ( strval( $this->getTitle()->getFragment() ) != '' ) {
823 $fragment = Xml::escapeJsString( $this->getTitle()->getFragmentForURL() );
824 $wgOut->addInlineScript( "redirectToFragment(\"$fragment\");" );
825 }
826
827 // Add a <link rel="canonical"> tag
828 $wgOut->addLink( array( 'rel' => 'canonical',
829 'href' => $this->getTitle()->getLocalURL() )
830 );
831
832 // Tell $wgOut the user arrived at this article through a redirect
833 $wgOut->setRedirectedFrom( $this->mRedirectedFrom );
834
835 return true;
836 }
837 } elseif ( $rdfrom ) {
838 // This is an externally redirected view, from some other wiki.
839 // If it was reported from a trusted site, supply a backlink.
840 if ( $wgRedirectSources && preg_match( $wgRedirectSources, $rdfrom ) ) {
841 $redir = Linker::makeExternalLink( $rdfrom, $rdfrom );
842 $wgOut->addSubtitle( wfMessage( 'redirectedfrom' )->rawParams( $redir ) );
843
844 return true;
845 }
846 }
847
848 return false;
849 }
850
851 /**
852 * Show a header specific to the namespace currently being viewed, like
853 * [[MediaWiki:Talkpagetext]]. For Article::view().
854 */
855 public function showNamespaceHeader() {
856 global $wgOut;
857
858 if ( $this->getTitle()->isTalkPage() ) {
859 if ( !wfMessage( 'talkpageheader' )->isDisabled() ) {
860 $wgOut->wrapWikiMsg( "<div class=\"mw-talkpageheader\">\n$1\n</div>", array( 'talkpageheader' ) );
861 }
862 }
863 }
864
865 /**
866 * Show the footer section of an ordinary page view
867 */
868 public function showViewFooter() {
869 global $wgOut;
870
871 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
872 if ( $this->getTitle()->getNamespace() == NS_USER_TALK && IP::isValid( $this->getTitle()->getText() ) ) {
873 $wgOut->addWikiMsg( 'anontalkpagetext' );
874 }
875
876 # If we have been passed an &rcid= parameter, we want to give the user a
877 # chance to mark this new article as patrolled.
878 $this->showPatrolFooter();
879
880 wfRunHooks( 'ArticleViewFooter', array( $this ) );
881
882 }
883
884 /**
885 * If patrol is possible, output a patrol UI box. This is called from the
886 * footer section of ordinary page views. If patrol is not possible or not
887 * desired, does nothing.
888 */
889 public function showPatrolFooter() {
890 global $wgOut, $wgRequest, $wgUser;
891
892 $rcid = $wgRequest->getVal( 'rcid' );
893
894 if ( !$rcid || !$this->getTitle()->quickUserCan( 'patrol' ) ) {
895 return;
896 }
897
898 $token = $wgUser->getEditToken( $rcid );
899 $wgOut->preventClickjacking();
900
901 $wgOut->addHTML(
902 "<div class='patrollink'>" .
903 wfMsgHtml(
904 'markaspatrolledlink',
905 Linker::link(
906 $this->getTitle(),
907 wfMsgHtml( 'markaspatrolledtext' ),
908 array(),
909 array(
910 'action' => 'markpatrolled',
911 'rcid' => $rcid,
912 'token' => $token,
913 ),
914 array( 'known', 'noclasses' )
915 )
916 ) .
917 '</div>'
918 );
919 }
920
921 /**
922 * Show the error text for a missing article. For articles in the MediaWiki
923 * namespace, show the default message text. To be called from Article::view().
924 */
925 public function showMissingArticle() {
926 global $wgOut, $wgRequest, $wgUser, $wgSend404Code;
927
928 # Show info in user (talk) namespace. Does the user exist? Is he blocked?
929 if ( $this->getTitle()->getNamespace() == NS_USER || $this->getTitle()->getNamespace() == NS_USER_TALK ) {
930 $parts = explode( '/', $this->getTitle()->getText() );
931 $rootPart = $parts[0];
932 $user = User::newFromName( $rootPart, false /* allow IP users*/ );
933 $ip = User::isIP( $rootPart );
934
935 if ( !($user && $user->isLoggedIn()) && !$ip ) { # User does not exist
936 $wgOut->wrapWikiMsg( "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
937 array( 'userpage-userdoesnotexist-view', wfEscapeWikiText( $rootPart ) ) );
938 } elseif ( $user->isBlocked() ) { # Show log extract if the user is currently blocked
939 LogEventsList::showLogExtract(
940 $wgOut,
941 'block',
942 $user->getUserPage()->getPrefixedText(),
943 '',
944 array(
945 'lim' => 1,
946 'showIfEmpty' => false,
947 'msgKey' => array(
948 'blocked-notice-logextract',
949 $user->getName() # Support GENDER in notice
950 )
951 )
952 );
953 }
954 }
955
956 wfRunHooks( 'ShowMissingArticle', array( $this ) );
957
958 # Show delete and move logs
959 LogEventsList::showLogExtract( $wgOut, array( 'delete', 'move' ), $this->getTitle()->getPrefixedText(), '',
960 array( 'lim' => 10,
961 'conds' => array( "log_action != 'revision'" ),
962 'showIfEmpty' => false,
963 'msgKey' => array( 'moveddeleted-notice' ) )
964 );
965
966 # Show error message
967 $oldid = $this->getOldID();
968 if ( $oldid ) {
969 $text = wfMsgNoTrans( 'missing-article',
970 $this->getTitle()->getPrefixedText(),
971 wfMsgNoTrans( 'missingarticle-rev', $oldid ) );
972 } elseif ( $this->getTitle()->getNamespace() === NS_MEDIAWIKI ) {
973 // Use the default message text
974 $text = $this->getTitle()->getDefaultMessageText();
975 } else {
976 $createErrors = $this->getTitle()->getUserPermissionsErrors( 'create', $wgUser );
977 $editErrors = $this->getTitle()->getUserPermissionsErrors( 'edit', $wgUser );
978 $errors = array_merge( $createErrors, $editErrors );
979
980 if ( !count( $errors ) ) {
981 $text = wfMsgNoTrans( 'noarticletext' );
982 } else {
983 $text = wfMsgNoTrans( 'noarticletext-nopermission' );
984 }
985 }
986 $text = "<div class='noarticletext'>\n$text\n</div>";
987
988 if ( !$this->mPage->hasViewableContent() && $wgSend404Code ) {
989 // If there's no backing content, send a 404 Not Found
990 // for better machine handling of broken links.
991 $wgRequest->response()->header( "HTTP/1.1 404 Not Found" );
992 }
993
994 $wgOut->addWikiText( $text );
995 }
996
997 /**
998 * If the revision requested for view is deleted, check permissions.
999 * Send either an error message or a warning header to $wgOut.
1000 *
1001 * @return boolean true if the view is allowed, false if not.
1002 */
1003 public function showDeletedRevisionHeader() {
1004 global $wgOut, $wgRequest;
1005
1006 if ( !$this->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
1007 // Not deleted
1008 return true;
1009 }
1010
1011 // If the user is not allowed to see it...
1012 if ( !$this->mRevision->userCan( Revision::DELETED_TEXT ) ) {
1013 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1014 'rev-deleted-text-permission' );
1015
1016 return false;
1017 // If the user needs to confirm that they want to see it...
1018 } elseif ( $wgRequest->getInt( 'unhide' ) != 1 ) {
1019 # Give explanation and add a link to view the revision...
1020 $oldid = intval( $this->getOldID() );
1021 $link = $this->getTitle()->getFullUrl( "oldid={$oldid}&unhide=1" );
1022 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1023 'rev-suppressed-text-unhide' : 'rev-deleted-text-unhide';
1024 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1025 array( $msg, $link ) );
1026
1027 return false;
1028 // We are allowed to see...
1029 } else {
1030 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1031 'rev-suppressed-text-view' : 'rev-deleted-text-view';
1032 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", $msg );
1033
1034 return true;
1035 }
1036 }
1037
1038 /**
1039 * Generate the navigation links when browsing through an article revisions
1040 * It shows the information as:
1041 * Revision as of \<date\>; view current revision
1042 * \<- Previous version | Next Version -\>
1043 *
1044 * @param $oldid String: revision ID of this article revision
1045 */
1046 public function setOldSubtitle( $oldid = 0 ) {
1047 global $wgLang, $wgOut, $wgUser, $wgRequest;
1048
1049 if ( !wfRunHooks( 'DisplayOldSubtitle', array( &$this, &$oldid ) ) ) {
1050 return;
1051 }
1052
1053 $unhide = $wgRequest->getInt( 'unhide' ) == 1;
1054
1055 # Cascade unhide param in links for easy deletion browsing
1056 $extraParams = array();
1057 if ( $wgRequest->getVal( 'unhide' ) ) {
1058 $extraParams['unhide'] = 1;
1059 }
1060
1061 $revision = Revision::newFromId( $oldid );
1062 $timestamp = $revision->getTimestamp();
1063
1064 $current = ( $oldid == $this->mPage->getLatest() );
1065 $td = $wgLang->timeanddate( $timestamp, true );
1066 $tddate = $wgLang->date( $timestamp, true );
1067 $tdtime = $wgLang->time( $timestamp, true );
1068
1069 # Show user links if allowed to see them. If hidden, then show them only if requested...
1070 $userlinks = Linker::revUserTools( $revision, !$unhide );
1071
1072 $infomsg = $current && !wfMessage( 'revision-info-current' )->isDisabled()
1073 ? 'revision-info-current'
1074 : 'revision-info';
1075
1076 $wgOut->addSubtitle( "<div id=\"mw-{$infomsg}\">" . wfMessage( $infomsg,
1077 $td )->rawParams( $userlinks )->params( $revision->getID(), $tddate,
1078 $tdtime, $revision->getUser() )->parse() . "</div>" );
1079
1080 $lnk = $current
1081 ? wfMsgHtml( 'currentrevisionlink' )
1082 : Linker::link(
1083 $this->getTitle(),
1084 wfMsgHtml( 'currentrevisionlink' ),
1085 array(),
1086 $extraParams,
1087 array( 'known', 'noclasses' )
1088 );
1089 $curdiff = $current
1090 ? wfMsgHtml( 'diff' )
1091 : Linker::link(
1092 $this->getTitle(),
1093 wfMsgHtml( 'diff' ),
1094 array(),
1095 array(
1096 'diff' => 'cur',
1097 'oldid' => $oldid
1098 ) + $extraParams,
1099 array( 'known', 'noclasses' )
1100 );
1101 $prev = $this->getTitle()->getPreviousRevisionID( $oldid ) ;
1102 $prevlink = $prev
1103 ? Linker::link(
1104 $this->getTitle(),
1105 wfMsgHtml( 'previousrevision' ),
1106 array(),
1107 array(
1108 'direction' => 'prev',
1109 'oldid' => $oldid
1110 ) + $extraParams,
1111 array( 'known', 'noclasses' )
1112 )
1113 : wfMsgHtml( 'previousrevision' );
1114 $prevdiff = $prev
1115 ? Linker::link(
1116 $this->getTitle(),
1117 wfMsgHtml( 'diff' ),
1118 array(),
1119 array(
1120 'diff' => 'prev',
1121 'oldid' => $oldid
1122 ) + $extraParams,
1123 array( 'known', 'noclasses' )
1124 )
1125 : wfMsgHtml( 'diff' );
1126 $nextlink = $current
1127 ? wfMsgHtml( 'nextrevision' )
1128 : Linker::link(
1129 $this->getTitle(),
1130 wfMsgHtml( 'nextrevision' ),
1131 array(),
1132 array(
1133 'direction' => 'next',
1134 'oldid' => $oldid
1135 ) + $extraParams,
1136 array( 'known', 'noclasses' )
1137 );
1138 $nextdiff = $current
1139 ? wfMsgHtml( 'diff' )
1140 : Linker::link(
1141 $this->getTitle(),
1142 wfMsgHtml( 'diff' ),
1143 array(),
1144 array(
1145 'diff' => 'next',
1146 'oldid' => $oldid
1147 ) + $extraParams,
1148 array( 'known', 'noclasses' )
1149 );
1150
1151 $cdel = '';
1152
1153 // User can delete revisions or view deleted revisions...
1154 $canHide = $wgUser->isAllowed( 'deleterevision' );
1155 if ( $canHide || ( $revision->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1156 if ( !$revision->userCan( Revision::DELETED_RESTRICTED ) ) {
1157 $cdel = Linker::revDeleteLinkDisabled( $canHide ); // rev was hidden from Sysops
1158 } else {
1159 $query = array(
1160 'type' => 'revision',
1161 'target' => $this->getTitle()->getPrefixedDbkey(),
1162 'ids' => $oldid
1163 );
1164 $cdel = Linker::revDeleteLink( $query, $revision->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1165 }
1166 $cdel .= ' ';
1167 }
1168
1169 $wgOut->addSubtitle( "<div id=\"mw-revision-nav\">" . $cdel .
1170 wfMsgExt( 'revision-nav', array( 'escapenoentities', 'parsemag', 'replaceafter' ),
1171 $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>" );
1172 }
1173
1174 /**
1175 * View redirect
1176 *
1177 * @param $target Title|Array of destination(s) to redirect
1178 * @param $appendSubtitle Boolean [optional]
1179 * @param $forceKnown Boolean: should the image be shown as a bluelink regardless of existence?
1180 * @return string containing HMTL with redirect link
1181 */
1182 public function viewRedirect( $target, $appendSubtitle = true, $forceKnown = false ) {
1183 global $wgOut, $wgStylePath;
1184
1185 if ( !is_array( $target ) ) {
1186 $target = array( $target );
1187 }
1188
1189 $lang = $this->getTitle()->getPageLanguage();
1190 $imageDir = $lang->getDir();
1191
1192 if ( $appendSubtitle ) {
1193 $wgOut->appendSubtitle( wfMsgHtml( 'redirectpagesub' ) );
1194 }
1195
1196 // the loop prepends the arrow image before the link, so the first case needs to be outside
1197
1198 /**
1199 * @var $title Title
1200 */
1201 $title = array_shift( $target );
1202
1203 if ( $forceKnown ) {
1204 $link = Linker::linkKnown( $title, htmlspecialchars( $title->getFullText() ) );
1205 } else {
1206 $link = Linker::link( $title, htmlspecialchars( $title->getFullText() ) );
1207 }
1208
1209 $nextRedirect = $wgStylePath . '/common/images/nextredirect' . $imageDir . '.png';
1210 $alt = $lang->isRTL() ? '←' : '→';
1211 // Automatically append redirect=no to each link, since most of them are redirect pages themselves.
1212 foreach ( $target as $rt ) {
1213 $link .= Html::element( 'img', array( 'src' => $nextRedirect, 'alt' => $alt ) );
1214 if ( $forceKnown ) {
1215 $link .= Linker::linkKnown( $rt, htmlspecialchars( $rt->getFullText(), array(), array( 'redirect' => 'no' ) ) );
1216 } else {
1217 $link .= Linker::link( $rt, htmlspecialchars( $rt->getFullText() ), array(), array( 'redirect' => 'no' ) );
1218 }
1219 }
1220
1221 $imageUrl = $wgStylePath . '/common/images/redirect' . $imageDir . '.png';
1222 return '<div class="redirectMsg">' .
1223 Html::element( 'img', array( 'src' => $imageUrl, 'alt' => '#REDIRECT' ) ) .
1224 '<span class="redirectText">' . $link . '</span></div>';
1225 }
1226
1227 /**
1228 * Handle action=render
1229 */
1230 public function render() {
1231 global $wgOut;
1232
1233 $wgOut->setArticleBodyOnly( true );
1234 $this->view();
1235 }
1236
1237 /**
1238 * action=protect handler
1239 */
1240 public function protect() {
1241 $form = new ProtectionForm( $this );
1242 $form->execute();
1243 }
1244
1245 /**
1246 * action=unprotect handler (alias)
1247 */
1248 public function unprotect() {
1249 $this->protect();
1250 }
1251
1252 /**
1253 * UI entry point for page deletion
1254 */
1255 public function delete() {
1256 global $wgOut, $wgRequest, $wgLang;
1257
1258 # This code desperately needs to be totally rewritten
1259
1260 $title = $this->getTitle();
1261 $user = $this->getContext()->getUser();
1262
1263 # Check permissions
1264 $permission_errors = $title->getUserPermissionsErrors( 'delete', $user );
1265 if ( count( $permission_errors ) ) {
1266 throw new PermissionsError( 'delete', $permission_errors );
1267 }
1268
1269 # Read-only check...
1270 if ( wfReadOnly() ) {
1271 throw new ReadOnlyError;
1272 }
1273
1274 # Better double-check that it hasn't been deleted yet!
1275 $dbw = wfGetDB( DB_MASTER );
1276 $conds = $title->pageCond();
1277 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
1278 if ( $latest === false ) {
1279 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $title->getPrefixedText() ) );
1280 $wgOut->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1281 array( 'cannotdelete', wfEscapeWikiText( $title->getPrefixedText() ) )
1282 );
1283 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1284 LogEventsList::showLogExtract(
1285 $wgOut,
1286 'delete',
1287 $title->getPrefixedText()
1288 );
1289
1290 return;
1291 }
1292
1293 # Hack for big sites
1294 $bigHistory = $this->mPage->isBigDeletion();
1295 if ( $bigHistory && !$title->userCan( 'bigdelete' ) ) {
1296 global $wgDeleteRevisionsLimit;
1297
1298 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $title->getPrefixedText() ) );
1299 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n",
1300 array( 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
1301
1302 return;
1303 }
1304
1305 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList', 'other' );
1306 $deleteReason = $wgRequest->getText( 'wpReason' );
1307
1308 if ( $deleteReasonList == 'other' ) {
1309 $reason = $deleteReason;
1310 } elseif ( $deleteReason != '' ) {
1311 // Entry from drop down menu + additional comment
1312 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
1313 } else {
1314 $reason = $deleteReasonList;
1315 }
1316
1317 if ( $wgRequest->wasPosted() && $user->matchEditToken( $wgRequest->getVal( 'wpEditToken' ),
1318 array( 'delete', $this->getTitle()->getPrefixedText() ) ) )
1319 {
1320 # Flag to hide all contents of the archived revisions
1321 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $user->isAllowed( 'suppressrevision' );
1322
1323 $this->doDelete( $reason, $suppress );
1324
1325 if ( $wgRequest->getCheck( 'wpWatch' ) && $user->isLoggedIn() ) {
1326 $this->doWatch();
1327 } elseif ( $title->userIsWatching() ) {
1328 $this->doUnwatch();
1329 }
1330
1331 return;
1332 }
1333
1334 // Generate deletion reason
1335 $hasHistory = false;
1336 if ( !$reason ) {
1337 $reason = $this->generateReason( $hasHistory );
1338 }
1339
1340 // If the page has a history, insert a warning
1341 if ( $hasHistory ) {
1342 $revisions = $this->mPage->estimateRevisionCount();
1343 // @todo FIXME: i18n issue/patchwork message
1344 $wgOut->addHTML( '<strong class="mw-delete-warning-revisions">' .
1345 wfMsgExt( 'historywarning', array( 'parseinline' ), $wgLang->formatNum( $revisions ) ) .
1346 wfMsgHtml( 'word-separator' ) . Linker::link( $title,
1347 wfMsgHtml( 'history' ),
1348 array( 'rel' => 'archives' ),
1349 array( 'action' => 'history' ) ) .
1350 '</strong>'
1351 );
1352
1353 if ( $bigHistory ) {
1354 global $wgDeleteRevisionsLimit;
1355 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n",
1356 array( 'delete-warning-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
1357 }
1358 }
1359
1360 return $this->confirmDelete( $reason );
1361 }
1362
1363 /**
1364 * Output deletion confirmation dialog
1365 * @todo FIXME: Move to another file?
1366 * @param $reason String: prefilled reason
1367 */
1368 public function confirmDelete( $reason ) {
1369 global $wgOut;
1370
1371 wfDebug( "Article::confirmDelete\n" );
1372
1373 $wgOut->setPageTitle( wfMessage( 'delete-confirm', $this->getTitle()->getPrefixedText() ) );
1374 $wgOut->addBacklinkSubtitle( $this->getTitle() );
1375 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1376 $wgOut->addWikiMsg( 'confirmdeletetext' );
1377
1378 wfRunHooks( 'ArticleConfirmDelete', array( $this, $wgOut, &$reason ) );
1379
1380 $user = $this->getContext()->getUser();
1381
1382 if ( $user->isAllowed( 'suppressrevision' ) ) {
1383 $suppress = "<tr id=\"wpDeleteSuppressRow\">
1384 <td></td>
1385 <td class='mw-input'><strong>" .
1386 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
1387 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '4' ) ) .
1388 "</strong></td>
1389 </tr>";
1390 } else {
1391 $suppress = '';
1392 }
1393 $checkWatch = $user->getBoolOption( 'watchdeletion' ) || $this->getTitle()->userIsWatching();
1394
1395 $form = Xml::openElement( 'form', array( 'method' => 'post',
1396 'action' => $this->getTitle()->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) .
1397 Xml::openElement( 'fieldset', array( 'id' => 'mw-delete-table' ) ) .
1398 Xml::tags( 'legend', null, wfMsgExt( 'delete-legend', array( 'parsemag', 'escapenoentities' ) ) ) .
1399 Xml::openElement( 'table', array( 'id' => 'mw-deleteconfirm-table' ) ) .
1400 "<tr id=\"wpDeleteReasonListRow\">
1401 <td class='mw-label'>" .
1402 Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' ) .
1403 "</td>
1404 <td class='mw-input'>" .
1405 Xml::listDropDown( 'wpDeleteReasonList',
1406 wfMsgForContent( 'deletereason-dropdown' ),
1407 wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
1408 "</td>
1409 </tr>
1410 <tr id=\"wpDeleteReasonRow\">
1411 <td class='mw-label'>" .
1412 Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' ) .
1413 "</td>
1414 <td class='mw-input'>" .
1415 Html::input( 'wpReason', $reason, 'text', array(
1416 'size' => '60',
1417 'maxlength' => '255',
1418 'tabindex' => '2',
1419 'id' => 'wpReason',
1420 'autofocus'
1421 ) ) .
1422 "</td>
1423 </tr>";
1424
1425 # Disallow watching if user is not logged in
1426 if ( $user->isLoggedIn() ) {
1427 $form .= "
1428 <tr>
1429 <td></td>
1430 <td class='mw-input'>" .
1431 Xml::checkLabel( wfMsg( 'watchthis' ),
1432 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
1433 "</td>
1434 </tr>";
1435 }
1436
1437 $form .= "
1438 $suppress
1439 <tr>
1440 <td></td>
1441 <td class='mw-submit'>" .
1442 Xml::submitButton( wfMsg( 'deletepage' ),
1443 array( 'name' => 'wpConfirmB', 'id' => 'wpConfirmB', 'tabindex' => '5' ) ) .
1444 "</td>
1445 </tr>" .
1446 Xml::closeElement( 'table' ) .
1447 Xml::closeElement( 'fieldset' ) .
1448 Html::hidden( 'wpEditToken', $user->getEditToken( array( 'delete', $this->getTitle()->getPrefixedText() ) ) ) .
1449 Xml::closeElement( 'form' );
1450
1451 if ( $user->isAllowed( 'editinterface' ) ) {
1452 $title = Title::makeTitle( NS_MEDIAWIKI, 'Deletereason-dropdown' );
1453 $link = Linker::link(
1454 $title,
1455 wfMsgHtml( 'delete-edit-reasonlist' ),
1456 array(),
1457 array( 'action' => 'edit' )
1458 );
1459 $form .= '<p class="mw-delete-editreasons">' . $link . '</p>';
1460 }
1461
1462 $wgOut->addHTML( $form );
1463 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1464 LogEventsList::showLogExtract( $wgOut, 'delete',
1465 $this->getTitle()->getPrefixedText()
1466 );
1467 }
1468
1469 /**
1470 * Perform a deletion and output success or failure messages
1471 * @param $reason
1472 * @param $suppress bool
1473 */
1474 public function doDelete( $reason, $suppress = false ) {
1475 global $wgOut;
1476
1477 $id = $this->getTitle()->getArticleID( Title::GAID_FOR_UPDATE );
1478
1479 $error = '';
1480 if ( $this->mPage->doDeleteArticle( $reason, $suppress, $id, true, $error ) ) {
1481 $deleted = $this->getTitle()->getPrefixedText();
1482
1483 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
1484 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1485
1486 $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
1487
1488 $wgOut->addWikiMsg( 'deletedtext', wfEscapeWikiText( $deleted ), $loglink );
1489 $wgOut->returnToMain( false );
1490 } else {
1491 $wgOut->setPageTitle( wfMessage( 'cannotdelete-title', $this->getTitle()->getPrefixedText() ) );
1492 if ( $error == '' ) {
1493 $wgOut->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1494 array( 'cannotdelete', wfEscapeWikiText( $this->getTitle()->getPrefixedText() ) )
1495 );
1496 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1497
1498 LogEventsList::showLogExtract(
1499 $wgOut,
1500 'delete',
1501 $this->getTitle()->getPrefixedText()
1502 );
1503 } else {
1504 $wgOut->addHTML( $error );
1505 }
1506 }
1507 }
1508
1509 /* Caching functions */
1510
1511 /**
1512 * checkLastModified returns true if it has taken care of all
1513 * output to the client that is necessary for this request.
1514 * (that is, it has sent a cached version of the page)
1515 *
1516 * @return boolean true if cached version send, false otherwise
1517 */
1518 protected function tryFileCache() {
1519 static $called = false;
1520
1521 if ( $called ) {
1522 wfDebug( "Article::tryFileCache(): called twice!?\n" );
1523 return false;
1524 }
1525
1526 $called = true;
1527 if ( $this->isFileCacheable() ) {
1528 $cache = HTMLFileCache::newFromTitle( $this->getTitle(), 'view' );
1529 if ( $cache->isCacheGood( $this->mPage->getTouched() ) ) {
1530 wfDebug( "Article::tryFileCache(): about to load file\n" );
1531 $cache->loadFromFileCache( $this->getContext() );
1532 return true;
1533 } else {
1534 wfDebug( "Article::tryFileCache(): starting buffer\n" );
1535 ob_start( array( &$cache, 'saveToFileCache' ) );
1536 }
1537 } else {
1538 wfDebug( "Article::tryFileCache(): not cacheable\n" );
1539 }
1540
1541 return false;
1542 }
1543
1544 /**
1545 * Check if the page can be cached
1546 * @return bool
1547 */
1548 public function isFileCacheable() {
1549 $cacheable = false;
1550
1551 if ( HTMLFileCache::useFileCache( $this->getContext() ) ) {
1552 $cacheable = $this->mPage->getID()
1553 && !$this->mRedirectedFrom && !$this->getTitle()->isRedirect();
1554 // Extension may have reason to disable file caching on some pages.
1555 if ( $cacheable ) {
1556 $cacheable = wfRunHooks( 'IsFileCacheable', array( &$this ) );
1557 }
1558 }
1559
1560 return $cacheable;
1561 }
1562
1563 /**#@-*/
1564
1565 /**
1566 * Lightweight method to get the parser output for a page, checking the parser cache
1567 * and so on. Doesn't consider most of the stuff that WikiPage::view is forced to
1568 * consider, so it's not appropriate to use there.
1569 *
1570 * @since 1.16 (r52326) for LiquidThreads
1571 *
1572 * @param $oldid mixed integer Revision ID or null
1573 * @param $user User The relevant user
1574 * @return ParserOutput or false if the given revsion ID is not found
1575 */
1576 public function getParserOutput( $oldid = null, User $user = null ) {
1577 global $wgUser;
1578
1579 $user = is_null( $user ) ? $wgUser : $user;
1580 $parserOptions = $this->mPage->makeParserOptions( $user );
1581
1582 return $this->mPage->getParserOutput( $parserOptions, $oldid );
1583 }
1584
1585 /**
1586 * Get parser options suitable for rendering the primary article wikitext
1587 * @return ParserOptions|false
1588 */
1589 public function getParserOptions() {
1590 global $wgUser;
1591 if ( !$this->mParserOptions ) {
1592 $this->mParserOptions = $this->mPage->makeParserOptions( $wgUser );
1593 }
1594 // Clone to allow modifications of the return value without affecting cache
1595 return clone $this->mParserOptions;
1596 }
1597
1598 /**
1599 * Sets the context this Article is executed in
1600 *
1601 * @param $context IContextSource
1602 * @since 1.18
1603 */
1604 public function setContext( $context ) {
1605 $this->mContext = $context;
1606 }
1607
1608 /**
1609 * Gets the context this Article is executed in
1610 *
1611 * @return IContextSource
1612 * @since 1.18
1613 */
1614 public function getContext() {
1615 if ( $this->mContext instanceof IContextSource ) {
1616 return $this->mContext;
1617 } else {
1618 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
1619 return RequestContext::getMain();
1620 }
1621 }
1622
1623 /**
1624 * Info about this page
1625 * @deprecated since 1.19
1626 */
1627 public function info() {
1628 wfDeprecated( __METHOD__, '1.19' );
1629 Action::factory( 'info', $this )->show();
1630 }
1631
1632 /**
1633 * Mark this particular edit/page as patrolled
1634 * @deprecated since 1.18
1635 */
1636 public function markpatrolled() {
1637 wfDeprecated( __METHOD__, '1.18' );
1638 Action::factory( 'markpatrolled', $this )->show();
1639 }
1640
1641 /**
1642 * Handle action=purge
1643 * @deprecated since 1.19
1644 */
1645 public function purge() {
1646 return Action::factory( 'purge', $this )->show();
1647 }
1648
1649 /**
1650 * Handle action=revert
1651 * @deprecated since 1.19
1652 */
1653 public function revert() {
1654 wfDeprecated( __METHOD__, '1.19' );
1655 Action::factory( 'revert', $this )->show();
1656 }
1657
1658 /**
1659 * Handle action=rollback
1660 * @deprecated since 1.19
1661 */
1662 public function rollback() {
1663 wfDeprecated( __METHOD__, '1.19' );
1664 Action::factory( 'rollback', $this )->show();
1665 }
1666
1667 /**
1668 * User-interface handler for the "watch" action.
1669 * Requires Request to pass a token as of 1.18.
1670 * @deprecated since 1.18
1671 */
1672 public function watch() {
1673 wfDeprecated( __METHOD__, '1.18' );
1674 Action::factory( 'watch', $this )->show();
1675 }
1676
1677 /**
1678 * Add this page to $wgUser's watchlist
1679 *
1680 * This is safe to be called multiple times
1681 *
1682 * @return bool true on successful watch operation
1683 * @deprecated since 1.18
1684 */
1685 public function doWatch() {
1686 global $wgUser;
1687 wfDeprecated( __METHOD__, '1.18' );
1688 return WatchAction::doWatch( $this->getTitle(), $wgUser );
1689 }
1690
1691 /**
1692 * User interface handler for the "unwatch" action.
1693 * Requires Request to pass a token as of 1.18.
1694 * @deprecated since 1.18
1695 */
1696 public function unwatch() {
1697 wfDeprecated( __METHOD__, '1.18' );
1698 Action::factory( 'unwatch', $this )->show();
1699 }
1700
1701 /**
1702 * Stop watching a page
1703 * @return bool true on successful unwatch
1704 * @deprecated since 1.18
1705 */
1706 public function doUnwatch() {
1707 global $wgUser;
1708 wfDeprecated( __METHOD__, '1.18' );
1709 return WatchAction::doUnwatch( $this->getTitle(), $wgUser );
1710 }
1711
1712 /**
1713 * Output a redirect back to the article.
1714 * This is typically used after an edit.
1715 *
1716 * @deprecated in 1.18; call $wgOut->redirect() directly
1717 * @param $noRedir Boolean: add redirect=no
1718 * @param $sectionAnchor String: section to redirect to, including "#"
1719 * @param $extraQuery String: extra query params
1720 */
1721 public function doRedirect( $noRedir = false, $sectionAnchor = '', $extraQuery = '' ) {
1722 wfDeprecated( __METHOD__, '1.18' );
1723 global $wgOut;
1724
1725 if ( $noRedir ) {
1726 $query = 'redirect=no';
1727 if ( $extraQuery )
1728 $query .= "&$extraQuery";
1729 } else {
1730 $query = $extraQuery;
1731 }
1732
1733 $wgOut->redirect( $this->getTitle()->getFullURL( $query ) . $sectionAnchor );
1734 }
1735
1736 /**
1737 * Use PHP's magic __get handler to handle accessing of
1738 * raw WikiPage fields for backwards compatibility.
1739 *
1740 * @param $fname String Field name
1741 */
1742 public function __get( $fname ) {
1743 if ( property_exists( $this->mPage, $fname ) ) {
1744 #wfWarn( "Access to raw $fname field " . __CLASS__ );
1745 return $this->mPage->$fname;
1746 }
1747 trigger_error( 'Inaccessible property via __get(): ' . $fname, E_USER_NOTICE );
1748 }
1749
1750 /**
1751 * Use PHP's magic __set handler to handle setting of
1752 * raw WikiPage fields for backwards compatibility.
1753 *
1754 * @param $fname String Field name
1755 * @param $fvalue mixed New value
1756 */
1757 public function __set( $fname, $fvalue ) {
1758 if ( property_exists( $this->mPage, $fname ) ) {
1759 #wfWarn( "Access to raw $fname field of " . __CLASS__ );
1760 $this->mPage->$fname = $fvalue;
1761 // Note: extensions may want to toss on new fields
1762 } elseif ( !in_array( $fname, array( 'mContext', 'mPage' ) ) ) {
1763 $this->mPage->$fname = $fvalue;
1764 } else {
1765 trigger_error( 'Inaccessible property via __set(): ' . $fname, E_USER_NOTICE );
1766 }
1767 }
1768
1769 /**
1770 * Use PHP's magic __call handler to transform instance calls to
1771 * WikiPage functions for backwards compatibility.
1772 *
1773 * @param $fname String Name of called method
1774 * @param $args Array Arguments to the method
1775 */
1776 public function __call( $fname, $args ) {
1777 if ( is_callable( array( $this->mPage, $fname ) ) ) {
1778 #wfWarn( "Call to " . __CLASS__ . "::$fname; please use WikiPage instead" );
1779 return call_user_func_array( array( $this->mPage, $fname ), $args );
1780 }
1781 trigger_error( 'Inaccessible function via __call(): ' . $fname, E_USER_ERROR );
1782 }
1783
1784 // ****** B/C functions to work-around PHP silliness with __call and references ****** //
1785
1786 /**
1787 * @param $limit array
1788 * @param $reason string
1789 * @param $cascade int
1790 * @param $expiry array
1791 * @return bool
1792 */
1793 public function updateRestrictions( $limit = array(), $reason = '', &$cascade = 0, $expiry = array() ) {
1794 return $this->mPage->updateRestrictions( $limit, $reason, $cascade, $expiry );
1795 }
1796
1797 /**
1798 * @param $reason string
1799 * @param $suppress bool
1800 * @param $id int
1801 * @param $commit bool
1802 * @param $error string
1803 * @return bool
1804 */
1805 public function doDeleteArticle( $reason, $suppress = false, $id = 0, $commit = true, &$error = '' ) {
1806 return $this->mPage->doDeleteArticle( $reason, $suppress, $id, $commit, $error );
1807 }
1808
1809 /**
1810 * @param $fromP
1811 * @param $summary
1812 * @param $token
1813 * @param $bot
1814 * @param $resultDetails
1815 * @param $user User
1816 * @return array
1817 */
1818 public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails, User $user = null ) {
1819 global $wgUser;
1820 $user = is_null( $user ) ? $wgUser : $user;
1821 return $this->mPage->doRollback( $fromP, $summary, $token, $bot, $resultDetails, $user );
1822 }
1823
1824 /**
1825 * @param $fromP
1826 * @param $summary
1827 * @param $bot
1828 * @param $resultDetails
1829 * @param $guser User
1830 * @return array
1831 */
1832 public function commitRollback( $fromP, $summary, $bot, &$resultDetails, User $guser = null ) {
1833 global $wgUser;
1834 $guser = is_null( $guser ) ? $wgUser : $guser;
1835 return $this->mPage->commitRollback( $fromP, $summary, $bot, $resultDetails, $guser );
1836 }
1837
1838 /**
1839 * @param $hasHistory bool
1840 * @return mixed
1841 */
1842 public function generateReason( &$hasHistory ) {
1843 return $this->mPage->getAutoDeleteReason( $hasHistory );
1844 }
1845
1846 // ****** B/C functions for static methods ( __callStatic is PHP>=5.3 ) ****** //
1847
1848 /**
1849 * @return array
1850 */
1851 public static function selectFields() {
1852 return WikiPage::selectFields();
1853 }
1854
1855 /**
1856 * @param $title Title
1857 */
1858 public static function onArticleCreate( $title ) {
1859 WikiPage::onArticleCreate( $title );
1860 }
1861
1862 /**
1863 * @param $title Title
1864 */
1865 public static function onArticleDelete( $title ) {
1866 WikiPage::onArticleDelete( $title );
1867 }
1868
1869 /**
1870 * @param $title Title
1871 */
1872 public static function onArticleEdit( $title ) {
1873 WikiPage::onArticleEdit( $title );
1874 }
1875
1876 /**
1877 * @param $oldtext
1878 * @param $newtext
1879 * @param $flags
1880 * @return string
1881 */
1882 public static function getAutosummary( $oldtext, $newtext, $flags ) {
1883 return WikiPage::getAutosummary( $oldtext, $newtext, $flags );
1884 }
1885 // ******
1886 }