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