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