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