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