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