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