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