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