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