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