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