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