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