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