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