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