dcde2cfba2b79d9cc6c02173b05970a6b7127120
[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', $user ) ) {
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', $user ) ) {
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 } elseif ( $this->getTitle()->quickUserCan( 'create', $this->getContext()->getUser() )
1054 && $this->getTitle()->quickUserCan( 'edit', $this->getContext()->getUser() )
1055 ) {
1056 $text = wfMsgNoTrans( 'noarticletext' );
1057 } else {
1058 $text = wfMsgNoTrans( 'noarticletext-nopermission' );
1059 }
1060 $text = "<div class='noarticletext'>\n$text\n</div>";
1061
1062 $outputPage->addWikiText( $text );
1063 }
1064
1065 /**
1066 * If the revision requested for view is deleted, check permissions.
1067 * Send either an error message or a warning header to the output.
1068 *
1069 * @return boolean true if the view is allowed, false if not.
1070 */
1071 public function showDeletedRevisionHeader() {
1072 if ( !$this->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
1073 // Not deleted
1074 return true;
1075 }
1076
1077 $outputPage = $this->getContext()->getOutput();
1078 // If the user is not allowed to see it...
1079 if ( !$this->mRevision->userCan( Revision::DELETED_TEXT ) ) {
1080 $outputPage->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1081 'rev-deleted-text-permission' );
1082
1083 return false;
1084 // If the user needs to confirm that they want to see it...
1085 } elseif ( $this->getContext()->getRequest()->getInt( 'unhide' ) != 1 ) {
1086 # Give explanation and add a link to view the revision...
1087 $oldid = intval( $this->getOldID() );
1088 $link = $this->getTitle()->getFullUrl( "oldid={$oldid}&unhide=1" );
1089 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1090 'rev-suppressed-text-unhide' : 'rev-deleted-text-unhide';
1091 $outputPage->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
1092 array( $msg, $link ) );
1093
1094 return false;
1095 // We are allowed to see...
1096 } else {
1097 $msg = $this->mRevision->isDeleted( Revision::DELETED_RESTRICTED ) ?
1098 'rev-suppressed-text-view' : 'rev-deleted-text-view';
1099 $outputPage->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", $msg );
1100
1101 return true;
1102 }
1103 }
1104
1105 /**
1106 * Generate the navigation links when browsing through an article revisions
1107 * It shows the information as:
1108 * Revision as of \<date\>; view current revision
1109 * \<- Previous version | Next Version -\>
1110 *
1111 * @param $oldid int: revision ID of this article revision
1112 */
1113 public function setOldSubtitle( $oldid = 0 ) {
1114 if ( !wfRunHooks( 'DisplayOldSubtitle', array( &$this, &$oldid ) ) ) {
1115 return;
1116 }
1117
1118 $unhide = $this->getContext()->getRequest()->getInt( 'unhide' ) == 1;
1119
1120 # Cascade unhide param in links for easy deletion browsing
1121 $extraParams = array();
1122 if ( $unhide ) {
1123 $extraParams['unhide'] = 1;
1124 }
1125
1126 if ( $this->mRevision && $this->mRevision->getId() === $oldid ) {
1127 $revision = $this->mRevision;
1128 } else {
1129 $revision = Revision::newFromId( $oldid );
1130 }
1131
1132 $timestamp = $revision->getTimestamp();
1133
1134 $current = ( $oldid == $this->mPage->getLatest() );
1135 $language = $this->getContext()->getLanguage();
1136 $user = $this->getContext()->getUser();
1137
1138 $td = $language->userTimeAndDate( $timestamp, $user );
1139 $tddate = $language->userDate( $timestamp, $user );
1140 $tdtime = $language->userTime( $timestamp, $user );
1141
1142 # Show user links if allowed to see them. If hidden, then show them only if requested...
1143 $userlinks = Linker::revUserTools( $revision, !$unhide );
1144
1145 $infomsg = $current && !wfMessage( 'revision-info-current' )->isDisabled()
1146 ? 'revision-info-current'
1147 : 'revision-info';
1148
1149 $outputPage = $this->getContext()->getOutput();
1150 $outputPage->addSubtitle( "<div id=\"mw-{$infomsg}\">" . wfMessage( $infomsg,
1151 $td )->rawParams( $userlinks )->params( $revision->getID(), $tddate,
1152 $tdtime, $revision->getUser() )->parse() . "</div>" );
1153
1154 $lnk = $current
1155 ? wfMsgHtml( 'currentrevisionlink' )
1156 : Linker::link(
1157 $this->getTitle(),
1158 wfMsgHtml( 'currentrevisionlink' ),
1159 array(),
1160 $extraParams,
1161 array( 'known', 'noclasses' )
1162 );
1163 $curdiff = $current
1164 ? wfMsgHtml( 'diff' )
1165 : Linker::link(
1166 $this->getTitle(),
1167 wfMsgHtml( 'diff' ),
1168 array(),
1169 array(
1170 'diff' => 'cur',
1171 'oldid' => $oldid
1172 ) + $extraParams,
1173 array( 'known', 'noclasses' )
1174 );
1175 $prev = $this->getTitle()->getPreviousRevisionID( $oldid ) ;
1176 $prevlink = $prev
1177 ? Linker::link(
1178 $this->getTitle(),
1179 wfMsgHtml( 'previousrevision' ),
1180 array(),
1181 array(
1182 'direction' => 'prev',
1183 'oldid' => $oldid
1184 ) + $extraParams,
1185 array( 'known', 'noclasses' )
1186 )
1187 : wfMsgHtml( 'previousrevision' );
1188 $prevdiff = $prev
1189 ? Linker::link(
1190 $this->getTitle(),
1191 wfMsgHtml( 'diff' ),
1192 array(),
1193 array(
1194 'diff' => 'prev',
1195 'oldid' => $oldid
1196 ) + $extraParams,
1197 array( 'known', 'noclasses' )
1198 )
1199 : wfMsgHtml( 'diff' );
1200 $nextlink = $current
1201 ? wfMsgHtml( 'nextrevision' )
1202 : Linker::link(
1203 $this->getTitle(),
1204 wfMsgHtml( 'nextrevision' ),
1205 array(),
1206 array(
1207 'direction' => 'next',
1208 'oldid' => $oldid
1209 ) + $extraParams,
1210 array( 'known', 'noclasses' )
1211 );
1212 $nextdiff = $current
1213 ? wfMsgHtml( 'diff' )
1214 : Linker::link(
1215 $this->getTitle(),
1216 wfMsgHtml( 'diff' ),
1217 array(),
1218 array(
1219 'diff' => 'next',
1220 'oldid' => $oldid
1221 ) + $extraParams,
1222 array( 'known', 'noclasses' )
1223 );
1224
1225 $cdel = Linker::getRevDeleteLink( $user, $revision, $this->getTitle() );
1226 if ( $cdel !== '' ) {
1227 $cdel .= ' ';
1228 }
1229
1230 $outputPage->addSubtitle( "<div id=\"mw-revision-nav\">" . $cdel .
1231 wfMsgExt( 'revision-nav', array( 'escapenoentities', 'parsemag', 'replaceafter' ),
1232 $prevdiff, $prevlink, $lnk, $curdiff, $nextlink, $nextdiff ) . "</div>" );
1233 }
1234
1235 /**
1236 * View redirect
1237 *
1238 * @param $target Title|Array of destination(s) to redirect
1239 * @param $appendSubtitle Boolean [optional]
1240 * @param $forceKnown Boolean: should the image be shown as a bluelink regardless of existence?
1241 * @return string containing HMTL with redirect link
1242 */
1243 public function viewRedirect( $target, $appendSubtitle = true, $forceKnown = false ) {
1244 global $wgStylePath;
1245
1246 if ( !is_array( $target ) ) {
1247 $target = array( $target );
1248 }
1249
1250 $lang = $this->getTitle()->getPageLanguage();
1251 $imageDir = $lang->getDir();
1252
1253 if ( $appendSubtitle ) {
1254 $this->getContext()->getOutput()->appendSubtitle( wfMsgHtml( 'redirectpagesub' ) );
1255 }
1256
1257 // the loop prepends the arrow image before the link, so the first case needs to be outside
1258
1259 /**
1260 * @var $title Title
1261 */
1262 $title = array_shift( $target );
1263
1264 if ( $forceKnown ) {
1265 $link = Linker::linkKnown( $title, htmlspecialchars( $title->getFullText() ) );
1266 } else {
1267 $link = Linker::link( $title, htmlspecialchars( $title->getFullText() ) );
1268 }
1269
1270 $nextRedirect = $wgStylePath . '/common/images/nextredirect' . $imageDir . '.png';
1271 $alt = $lang->isRTL() ? '←' : '→';
1272 // Automatically append redirect=no to each link, since most of them are redirect pages themselves.
1273 foreach ( $target as $rt ) {
1274 $link .= Html::element( 'img', array( 'src' => $nextRedirect, 'alt' => $alt ) );
1275 if ( $forceKnown ) {
1276 $link .= Linker::linkKnown( $rt, htmlspecialchars( $rt->getFullText(), array(), array( 'redirect' => 'no' ) ) );
1277 } else {
1278 $link .= Linker::link( $rt, htmlspecialchars( $rt->getFullText() ), array(), array( 'redirect' => 'no' ) );
1279 }
1280 }
1281
1282 $imageUrl = $wgStylePath . '/common/images/redirect' . $imageDir . '.png';
1283 return '<div class="redirectMsg">' .
1284 Html::element( 'img', array( 'src' => $imageUrl, 'alt' => '#REDIRECT' ) ) .
1285 '<span class="redirectText">' . $link . '</span></div>';
1286 }
1287
1288 /**
1289 * Handle action=render
1290 */
1291 public function render() {
1292 $this->getContext()->getOutput()->setArticleBodyOnly( true );
1293 $this->view();
1294 }
1295
1296 /**
1297 * action=protect handler
1298 */
1299 public function protect() {
1300 $form = new ProtectionForm( $this );
1301 $form->execute();
1302 }
1303
1304 /**
1305 * action=unprotect handler (alias)
1306 */
1307 public function unprotect() {
1308 $this->protect();
1309 }
1310
1311 /**
1312 * UI entry point for page deletion
1313 */
1314 public function delete() {
1315 # This code desperately needs to be totally rewritten
1316
1317 $title = $this->getTitle();
1318 $user = $this->getContext()->getUser();
1319
1320 # Check permissions
1321 $permission_errors = $title->getUserPermissionsErrors( 'delete', $user );
1322 if ( count( $permission_errors ) ) {
1323 throw new PermissionsError( 'delete', $permission_errors );
1324 }
1325
1326 # Read-only check...
1327 if ( wfReadOnly() ) {
1328 throw new ReadOnlyError;
1329 }
1330
1331 # Better double-check that it hasn't been deleted yet!
1332 $dbw = wfGetDB( DB_MASTER );
1333 $conds = $title->pageCond();
1334 $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
1335 if ( $latest === false ) {
1336 $outputPage = $this->getContext()->getOutput();
1337 $outputPage->setPageTitle( wfMessage( 'cannotdelete-title', $title->getPrefixedText() ) );
1338 $outputPage->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1339 array( 'cannotdelete', wfEscapeWikiText( $title->getPrefixedText() ) )
1340 );
1341 $outputPage->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1342 LogEventsList::showLogExtract(
1343 $outputPage,
1344 'delete',
1345 $title->getPrefixedText()
1346 );
1347
1348 return;
1349 }
1350
1351 $request = $this->getContext()->getRequest();
1352 $deleteReasonList = $request->getText( 'wpDeleteReasonList', 'other' );
1353 $deleteReason = $request->getText( 'wpReason' );
1354
1355 if ( $deleteReasonList == 'other' ) {
1356 $reason = $deleteReason;
1357 } elseif ( $deleteReason != '' ) {
1358 // Entry from drop down menu + additional comment
1359 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
1360 } else {
1361 $reason = $deleteReasonList;
1362 }
1363
1364 if ( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'wpEditToken' ),
1365 array( 'delete', $this->getTitle()->getPrefixedText() ) ) )
1366 {
1367 # Flag to hide all contents of the archived revisions
1368 $suppress = $request->getVal( 'wpSuppress' ) && $user->isAllowed( 'suppressrevision' );
1369
1370 $this->doDelete( $reason, $suppress );
1371
1372 if ( $request->getCheck( 'wpWatch' ) && $user->isLoggedIn() ) {
1373 WatchAction::doWatch( $title, $user );
1374 } elseif ( $title->userIsWatching() ) {
1375 WatchAction::doUnwatch( $title, $user );
1376 }
1377
1378 return;
1379 }
1380
1381 // Generate deletion reason
1382 $hasHistory = false;
1383 if ( !$reason ) {
1384 $reason = $this->generateReason( $hasHistory );
1385 }
1386
1387 // If the page has a history, insert a warning
1388 if ( $hasHistory ) {
1389 $revisions = $this->mTitle->estimateRevisionCount();
1390 // @todo FIXME: i18n issue/patchwork message
1391 $this->getContext()->getOutput()->addHTML( '<strong class="mw-delete-warning-revisions">' .
1392 wfMsgExt( 'historywarning', array( 'parseinline' ), $this->getContext()->getLanguage()->formatNum( $revisions ) ) .
1393 wfMsgHtml( 'word-separator' ) . Linker::link( $title,
1394 wfMsgHtml( 'history' ),
1395 array( 'rel' => 'archives' ),
1396 array( 'action' => 'history' ) ) .
1397 '</strong>'
1398 );
1399
1400 if ( $this->mTitle->isBigDeletion() ) {
1401 global $wgDeleteRevisionsLimit;
1402 $this->getContext()->getOutput()->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n",
1403 array( 'delete-warning-toobig', $this->getContext()->getLanguage()->formatNum( $wgDeleteRevisionsLimit ) ) );
1404 }
1405 }
1406
1407 $this->confirmDelete( $reason );
1408 }
1409
1410 /**
1411 * Output deletion confirmation dialog
1412 * @todo FIXME: Move to another file?
1413 * @param $reason String: prefilled reason
1414 */
1415 public function confirmDelete( $reason ) {
1416 wfDebug( "Article::confirmDelete\n" );
1417
1418 $outputPage = $this->getContext()->getOutput();
1419 $outputPage->setPageTitle( wfMessage( 'delete-confirm', $this->getTitle()->getPrefixedText() ) );
1420 $outputPage->addBacklinkSubtitle( $this->getTitle() );
1421 $outputPage->setRobotPolicy( 'noindex,nofollow' );
1422 $outputPage->addWikiMsg( 'confirmdeletetext' );
1423
1424 wfRunHooks( 'ArticleConfirmDelete', array( $this, $outputPage, &$reason ) );
1425
1426 $user = $this->getContext()->getUser();
1427
1428 if ( $user->isAllowed( 'suppressrevision' ) ) {
1429 $suppress = "<tr id=\"wpDeleteSuppressRow\">
1430 <td></td>
1431 <td class='mw-input'><strong>" .
1432 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
1433 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '4' ) ) .
1434 "</strong></td>
1435 </tr>";
1436 } else {
1437 $suppress = '';
1438 }
1439 $checkWatch = $user->getBoolOption( 'watchdeletion' ) || $this->getTitle()->userIsWatching();
1440
1441 $form = Xml::openElement( 'form', array( 'method' => 'post',
1442 'action' => $this->getTitle()->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) .
1443 Xml::openElement( 'fieldset', array( 'id' => 'mw-delete-table' ) ) .
1444 Xml::tags( 'legend', null, wfMsgExt( 'delete-legend', array( 'parsemag', 'escapenoentities' ) ) ) .
1445 Xml::openElement( 'table', array( 'id' => 'mw-deleteconfirm-table' ) ) .
1446 "<tr id=\"wpDeleteReasonListRow\">
1447 <td class='mw-label'>" .
1448 Xml::label( wfMsg( 'deletecomment' ), 'wpDeleteReasonList' ) .
1449 "</td>
1450 <td class='mw-input'>" .
1451 Xml::listDropDown( 'wpDeleteReasonList',
1452 wfMsgForContent( 'deletereason-dropdown' ),
1453 wfMsgForContent( 'deletereasonotherlist' ), '', 'wpReasonDropDown', 1 ) .
1454 "</td>
1455 </tr>
1456 <tr id=\"wpDeleteReasonRow\">
1457 <td class='mw-label'>" .
1458 Xml::label( wfMsg( 'deleteotherreason' ), 'wpReason' ) .
1459 "</td>
1460 <td class='mw-input'>" .
1461 Html::input( 'wpReason', $reason, 'text', array(
1462 'size' => '60',
1463 'maxlength' => '255',
1464 'tabindex' => '2',
1465 'id' => 'wpReason',
1466 'autofocus'
1467 ) ) .
1468 "</td>
1469 </tr>";
1470
1471 # Disallow watching if user is not logged in
1472 if ( $user->isLoggedIn() ) {
1473 $form .= "
1474 <tr>
1475 <td></td>
1476 <td class='mw-input'>" .
1477 Xml::checkLabel( wfMsg( 'watchthis' ),
1478 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
1479 "</td>
1480 </tr>";
1481 }
1482
1483 $form .= "
1484 $suppress
1485 <tr>
1486 <td></td>
1487 <td class='mw-submit'>" .
1488 Xml::submitButton( wfMsg( 'deletepage' ),
1489 array( 'name' => 'wpConfirmB', 'id' => 'wpConfirmB', 'tabindex' => '5' ) ) .
1490 "</td>
1491 </tr>" .
1492 Xml::closeElement( 'table' ) .
1493 Xml::closeElement( 'fieldset' ) .
1494 Html::hidden( 'wpEditToken', $user->getEditToken( array( 'delete', $this->getTitle()->getPrefixedText() ) ) ) .
1495 Xml::closeElement( 'form' );
1496
1497 if ( $user->isAllowed( 'editinterface' ) ) {
1498 $title = Title::makeTitle( NS_MEDIAWIKI, 'Deletereason-dropdown' );
1499 $link = Linker::link(
1500 $title,
1501 wfMsgHtml( 'delete-edit-reasonlist' ),
1502 array(),
1503 array( 'action' => 'edit' )
1504 );
1505 $form .= '<p class="mw-delete-editreasons">' . $link . '</p>';
1506 }
1507
1508 $outputPage->addHTML( $form );
1509 $outputPage->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1510 LogEventsList::showLogExtract( $outputPage, 'delete',
1511 $this->getTitle()->getPrefixedText()
1512 );
1513 }
1514
1515 /**
1516 * Perform a deletion and output success or failure messages
1517 * @param $reason
1518 * @param $suppress bool
1519 */
1520 public function doDelete( $reason, $suppress = false ) {
1521 $error = '';
1522 $outputPage = $this->getContext()->getOutput();
1523 if ( $this->mPage->doDeleteArticle( $reason, $suppress, 0, true, $error ) ) {
1524 $deleted = $this->getTitle()->getPrefixedText();
1525
1526 $outputPage->setPageTitle( wfMessage( 'actioncomplete' ) );
1527 $outputPage->setRobotPolicy( 'noindex,nofollow' );
1528
1529 $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]';
1530
1531 $outputPage->addWikiMsg( 'deletedtext', wfEscapeWikiText( $deleted ), $loglink );
1532 $outputPage->returnToMain( false );
1533 } else {
1534 $outputPage->setPageTitle( wfMessage( 'cannotdelete-title', $this->getTitle()->getPrefixedText() ) );
1535 if ( $error == '' ) {
1536 $outputPage->wrapWikiMsg( "<div class=\"error mw-error-cannotdelete\">\n$1\n</div>",
1537 array( 'cannotdelete', wfEscapeWikiText( $this->getTitle()->getPrefixedText() ) )
1538 );
1539 $outputPage->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) );
1540
1541 LogEventsList::showLogExtract(
1542 $outputPage,
1543 'delete',
1544 $this->getTitle()->getPrefixedText()
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 $user = is_null( $user ) ? $this->getContext()->getUser() : $user;
1621 $parserOptions = $this->mPage->makeParserOptions( $user );
1622
1623 return $this->mPage->getParserOutput( $parserOptions, $oldid );
1624 }
1625
1626 /**
1627 * Get parser options suitable for rendering the primary article wikitext
1628 * @return ParserOptions
1629 */
1630 public function getParserOptions() {
1631 if ( !$this->mParserOptions ) {
1632 $this->mParserOptions = $this->mPage->makeParserOptions( $this->getContext()->getUser() );
1633 }
1634 // Clone to allow modifications of the return value without affecting cache
1635 return clone $this->mParserOptions;
1636 }
1637
1638 /**
1639 * Sets the context this Article is executed in
1640 *
1641 * @param $context IContextSource
1642 * @since 1.18
1643 */
1644 public function setContext( $context ) {
1645 $this->mContext = $context;
1646 }
1647
1648 /**
1649 * Gets the context this Article is executed in
1650 *
1651 * @return IContextSource
1652 * @since 1.18
1653 */
1654 public function getContext() {
1655 if ( $this->mContext instanceof IContextSource ) {
1656 return $this->mContext;
1657 } else {
1658 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
1659 return RequestContext::getMain();
1660 }
1661 }
1662
1663 /**
1664 * Info about this page
1665 * @deprecated since 1.19
1666 */
1667 public function info() {
1668 wfDeprecated( __METHOD__, '1.19' );
1669 Action::factory( 'info', $this )->show();
1670 }
1671
1672 /**
1673 * Mark this particular edit/page as patrolled
1674 * @deprecated since 1.18
1675 */
1676 public function markpatrolled() {
1677 wfDeprecated( __METHOD__, '1.18' );
1678 Action::factory( 'markpatrolled', $this )->show();
1679 }
1680
1681 /**
1682 * Handle action=purge
1683 * @deprecated since 1.19
1684 * @return Action|bool|null false if the action is disabled, null if it is not recognised
1685 */
1686 public function purge() {
1687 return Action::factory( 'purge', $this )->show();
1688 }
1689
1690 /**
1691 * Handle action=revert
1692 * @deprecated since 1.19
1693 */
1694 public function revert() {
1695 wfDeprecated( __METHOD__, '1.19' );
1696 Action::factory( 'revert', $this )->show();
1697 }
1698
1699 /**
1700 * Handle action=rollback
1701 * @deprecated since 1.19
1702 */
1703 public function rollback() {
1704 wfDeprecated( __METHOD__, '1.19' );
1705 Action::factory( 'rollback', $this )->show();
1706 }
1707
1708 /**
1709 * User-interface handler for the "watch" action.
1710 * Requires Request to pass a token as of 1.18.
1711 * @deprecated since 1.18
1712 */
1713 public function watch() {
1714 wfDeprecated( __METHOD__, '1.18' );
1715 Action::factory( 'watch', $this )->show();
1716 }
1717
1718 /**
1719 * Add this page to the current user's watchlist
1720 *
1721 * This is safe to be called multiple times
1722 *
1723 * @return bool true on successful watch operation
1724 * @deprecated since 1.18
1725 */
1726 public function doWatch() {
1727 wfDeprecated( __METHOD__, '1.18' );
1728 return WatchAction::doWatch( $this->getTitle(), $this->getContext()->getUser() );
1729 }
1730
1731 /**
1732 * User interface handler for the "unwatch" action.
1733 * Requires Request to pass a token as of 1.18.
1734 * @deprecated since 1.18
1735 */
1736 public function unwatch() {
1737 wfDeprecated( __METHOD__, '1.18' );
1738 Action::factory( 'unwatch', $this )->show();
1739 }
1740
1741 /**
1742 * Stop watching a page
1743 * @return bool true on successful unwatch
1744 * @deprecated since 1.18
1745 */
1746 public function doUnwatch() {
1747 wfDeprecated( __METHOD__, '1.18' );
1748 return WatchAction::doUnwatch( $this->getTitle(), $this->getContext()->getUser() );
1749 }
1750
1751 /**
1752 * Output a redirect back to the article.
1753 * This is typically used after an edit.
1754 *
1755 * @deprecated in 1.18; call OutputPage::redirect() directly
1756 * @param $noRedir Boolean: add redirect=no
1757 * @param $sectionAnchor String: section to redirect to, including "#"
1758 * @param $extraQuery String: extra query params
1759 */
1760 public function doRedirect( $noRedir = false, $sectionAnchor = '', $extraQuery = '' ) {
1761 wfDeprecated( __METHOD__, '1.18' );
1762 if ( $noRedir ) {
1763 $query = 'redirect=no';
1764 if ( $extraQuery )
1765 $query .= "&$extraQuery";
1766 } else {
1767 $query = $extraQuery;
1768 }
1769
1770 $this->getContext()->getOutput()->redirect( $this->getTitle()->getFullURL( $query ) . $sectionAnchor );
1771 }
1772
1773 /**
1774 * Use PHP's magic __get handler to handle accessing of
1775 * raw WikiPage fields for backwards compatibility.
1776 *
1777 * @param $fname String Field name
1778 */
1779 public function __get( $fname ) {
1780 if ( property_exists( $this->mPage, $fname ) ) {
1781 #wfWarn( "Access to raw $fname field " . __CLASS__ );
1782 return $this->mPage->$fname;
1783 }
1784 trigger_error( 'Inaccessible property via __get(): ' . $fname, E_USER_NOTICE );
1785 }
1786
1787 /**
1788 * Use PHP's magic __set handler to handle setting of
1789 * raw WikiPage fields for backwards compatibility.
1790 *
1791 * @param $fname String Field name
1792 * @param $fvalue mixed New value
1793 */
1794 public function __set( $fname, $fvalue ) {
1795 if ( property_exists( $this->mPage, $fname ) ) {
1796 #wfWarn( "Access to raw $fname field of " . __CLASS__ );
1797 $this->mPage->$fname = $fvalue;
1798 // Note: extensions may want to toss on new fields
1799 } elseif ( !in_array( $fname, array( 'mContext', 'mPage' ) ) ) {
1800 $this->mPage->$fname = $fvalue;
1801 } else {
1802 trigger_error( 'Inaccessible property via __set(): ' . $fname, E_USER_NOTICE );
1803 }
1804 }
1805
1806 /**
1807 * Use PHP's magic __call handler to transform instance calls to
1808 * WikiPage functions for backwards compatibility.
1809 *
1810 * @param $fname String Name of called method
1811 * @param $args Array Arguments to the method
1812 * @return mixed
1813 */
1814 public function __call( $fname, $args ) {
1815 if ( is_callable( array( $this->mPage, $fname ) ) ) {
1816 #wfWarn( "Call to " . __CLASS__ . "::$fname; please use WikiPage instead" );
1817 return call_user_func_array( array( $this->mPage, $fname ), $args );
1818 }
1819 trigger_error( 'Inaccessible function via __call(): ' . $fname, E_USER_ERROR );
1820 }
1821
1822 // ****** B/C functions to work-around PHP silliness with __call and references ****** //
1823
1824 /**
1825 * @param $limit array
1826 * @param $expiry array
1827 * @param $cascade bool
1828 * @param $reason string
1829 * @param $user User
1830 * @return Status
1831 */
1832 public function doUpdateRestrictions( array $limit, array $expiry, &$cascade, $reason, User $user ) {
1833 return $this->mPage->doUpdateRestrictions( $limit, $expiry, $cascade, $reason, $user );
1834 }
1835
1836 /**
1837 * @param $limit array
1838 * @param $reason string
1839 * @param $cascade int
1840 * @param $expiry array
1841 * @return bool
1842 */
1843 public function updateRestrictions( $limit = array(), $reason = '', &$cascade = 0, $expiry = array() ) {
1844 return $this->mPage->updateRestrictions( $limit, $reason, $cascade, $expiry );
1845 }
1846
1847 /**
1848 * @param $reason string
1849 * @param $suppress bool
1850 * @param $id int
1851 * @param $commit bool
1852 * @param $error string
1853 * @return bool
1854 */
1855 public function doDeleteArticle( $reason, $suppress = false, $id = 0, $commit = true, &$error = '' ) {
1856 return $this->mPage->doDeleteArticle( $reason, $suppress, $id, $commit, $error );
1857 }
1858
1859 /**
1860 * @param $fromP
1861 * @param $summary
1862 * @param $token
1863 * @param $bot
1864 * @param $resultDetails
1865 * @param $user User
1866 * @return array
1867 */
1868 public function doRollback( $fromP, $summary, $token, $bot, &$resultDetails, User $user = null ) {
1869 $user = is_null( $user ) ? $this->getContext()->getUser() : $user;
1870 return $this->mPage->doRollback( $fromP, $summary, $token, $bot, $resultDetails, $user );
1871 }
1872
1873 /**
1874 * @param $fromP
1875 * @param $summary
1876 * @param $bot
1877 * @param $resultDetails
1878 * @param $guser User
1879 * @return array
1880 */
1881 public function commitRollback( $fromP, $summary, $bot, &$resultDetails, User $guser = null ) {
1882 $guser = is_null( $guser ) ? $this->getContext()->getUser() : $guser;
1883 return $this->mPage->commitRollback( $fromP, $summary, $bot, $resultDetails, $guser );
1884 }
1885
1886 /**
1887 * @param $hasHistory bool
1888 * @return mixed
1889 */
1890 public function generateReason( &$hasHistory ) {
1891 return $this->mPage->getAutoDeleteReason( $hasHistory );
1892 }
1893
1894 // ****** B/C functions for static methods ( __callStatic is PHP>=5.3 ) ****** //
1895
1896 /**
1897 * @return array
1898 */
1899 public static function selectFields() {
1900 return WikiPage::selectFields();
1901 }
1902
1903 /**
1904 * @param $title Title
1905 */
1906 public static function onArticleCreate( $title ) {
1907 WikiPage::onArticleCreate( $title );
1908 }
1909
1910 /**
1911 * @param $title Title
1912 */
1913 public static function onArticleDelete( $title ) {
1914 WikiPage::onArticleDelete( $title );
1915 }
1916
1917 /**
1918 * @param $title Title
1919 */
1920 public static function onArticleEdit( $title ) {
1921 WikiPage::onArticleEdit( $title );
1922 }
1923
1924 /**
1925 * @param $oldtext
1926 * @param $newtext
1927 * @param $flags
1928 * @return string
1929 */
1930 public static function getAutosummary( $oldtext, $newtext, $flags ) {
1931 return WikiPage::getAutosummary( $oldtext, $newtext, $flags );
1932 }
1933 // ******
1934 }