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