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