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