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