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