/
Article.php
3074 lines (2725 loc) 路 90.6 KB
/
Article.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* User interface for page actions.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Edit\PreparedEdit;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use Wikimedia\IPUtils;
use Wikimedia\Rdbms\IDatabase;
/**
* Class for viewing MediaWiki article and history.
*
* This maintains WikiPage functions for backwards compatibility.
*
* @todo Move and rewrite code to an Action class
*
* Note: edit user interface and cache support functions have been
* moved to separate EditPage and HTMLFileCache classes.
*/
class Article implements Page {
/**
* @var IContextSource|null The context this Article is executed in.
* If null, RequestContext::getMain() is used.
*/
protected $mContext;
/** @var WikiPage|null The WikiPage object of this instance */
protected $mPage;
/**
* @var ParserOptions|null ParserOptions object for $wgUser articles.
* Initialized by getParserOptions by calling $this->mPage->makeParserOptions().
*/
public $mParserOptions;
/**
* @var Content|null Content of the main slot of $this->mRevision.
* @note This variable is read only, setting it has no effect.
* Extensions that wish to override the output of Article::view should use a hook.
* @todo MCR: Remove in 1.33
* @deprecated since 1.32
* @since 1.21
*/
public $mContentObject;
/**
* @var bool Is the target revision loaded? Set by fetchRevisionRecord().
*
* @deprecated since 1.32. Whether content has been loaded should not be relevant to
* code outside this class.
*/
public $mContentLoaded = false;
/**
* @var int|null The oldid of the article that was requested to be shown,
* 0 for the current revision.
* @see $mRevIdFetched
*/
public $mOldId;
/** @var Title|null Title from which we were redirected here, if any. */
public $mRedirectedFrom = null;
/** @var string|bool URL to redirect to or false if none */
public $mRedirectUrl = false;
/**
* @var int Revision ID of revision that was loaded.
* @see $mOldId
* @deprecated since 1.32, use getRevIdFetched() instead.
*/
public $mRevIdFetched = 0;
/**
* @var Status|null represents the outcome of fetchRevisionRecord().
* $fetchResult->value is the RevisionRecord object, if the operation was successful.
*
* The information in $fetchResult is duplicated by the following deprecated public fields:
* $mRevIdFetched, $mContentLoaded. $mRevision (and $mContentObject) also typically duplicate
* information of the loaded revision, but may be overwritten by extensions or due to errors.
*/
private $fetchResult = null;
/**
* @var Revision|null Revision to be shown. Initialized by getOldIDFromRequest()
* or fetchContentObject(). Normally loaded from the database, but may be replaced
* by an extension, or be a fake representing an error message or some such.
* While the output of Article::view is typically based on this revision,
* it may be overwritten by error messages or replaced by extensions.
*/
public $mRevision = null;
/**
* @var ParserOutput|null|false The ParserOutput generated for viewing the page,
* initialized by view(). If no ParserOutput could be generated, this is set to false.
* @deprecated since 1.32
*/
public $mParserOutput = null;
/**
* @var bool Whether render() was called. With the way subclasses work
* here, there doesn't seem to be any other way to stop calling
* OutputPage::enableSectionEditLinks() and still have it work as it did before.
*/
protected $viewIsRenderAction = false;
/**
* @var LinkRenderer
*/
protected $linkRenderer;
/**
* @var PermissionManager
*/
private $permManager;
/**
* Constructor and clear the article
* @param Title $title Reference to a Title object.
* @param int|null $oldId Revision ID, null to fetch from request, zero for current
*/
public function __construct( Title $title, $oldId = null ) {
$this->mOldId = $oldId;
$this->mPage = $this->newPage( $title );
$services = MediaWikiServices::getInstance();
$this->linkRenderer = $services->getLinkRenderer();
$this->permManager = $services->getPermissionManager();
}
/**
* @param Title $title
* @return WikiPage
*/
protected function newPage( Title $title ) {
return new WikiPage( $title );
}
/**
* Constructor from a page id
* @param int $id Article ID to load
* @return Article|null
*/
public static function newFromID( $id ) {
$t = Title::newFromID( $id );
return $t == null ? null : new static( $t );
}
/**
* Create an Article object of the appropriate class for the given page.
*
* @param Title $title
* @param IContextSource $context
* @return Article
*/
public static function newFromTitle( $title, IContextSource $context ) {
if ( NS_MEDIA == $title->getNamespace() ) {
// XXX: This should not be here, but where should it go?
$title = Title::makeTitle( NS_FILE, $title->getDBkey() );
}
$page = null;
Hooks::run( 'ArticleFromTitle', [ &$title, &$page, $context ] );
if ( !$page ) {
switch ( $title->getNamespace() ) {
case NS_FILE:
$page = new ImagePage( $title );
break;
case NS_CATEGORY:
$page = new CategoryPage( $title );
break;
default:
$page = new Article( $title );
}
}
$page->setContext( $context );
return $page;
}
/**
* Create an Article object of the appropriate class for the given page.
*
* @param WikiPage $page
* @param IContextSource $context
* @return Article
*/
public static function newFromWikiPage( WikiPage $page, IContextSource $context ) {
$article = self::newFromTitle( $page->getTitle(), $context );
$article->mPage = $page; // override to keep process cached vars
return $article;
}
/**
* Get the page this view was redirected from
* @return Title|null
* @since 1.28
*/
public function getRedirectedFrom() {
return $this->mRedirectedFrom;
}
/**
* Tell the page view functions that this view was redirected
* from another page on the wiki.
* @param Title $from
*/
public function setRedirectedFrom( Title $from ) {
$this->mRedirectedFrom = $from;
}
/**
* Get the title object of the article
*
* @return Title Title object of this page
*/
public function getTitle() {
return $this->mPage->getTitle();
}
/**
* Get the WikiPage object of this instance
*
* @since 1.19
* @return WikiPage
*/
public function getPage() {
return $this->mPage;
}
/**
* Clear the object
*/
public function clear() {
$this->mContentLoaded = false;
$this->mRedirectedFrom = null; # Title object if set
$this->mRevIdFetched = 0;
$this->mRedirectUrl = false;
$this->mRevision = null;
$this->mContentObject = null;
$this->fetchResult = null;
// TODO hard-deprecate direct access to public fields
$this->mPage->clear();
}
/**
* Returns a Content object representing the pages effective display content,
* not necessarily the revision's content!
*
* Note that getContent does not follow redirects anymore.
* If you need to fetch redirectable content easily, try
* the shortcut in WikiPage::getRedirectTarget()
*
* This function has side effects! Do not use this function if you
* only want the real revision text if any.
*
* @deprecated since 1.32, use getRevisionFetched() or fetchRevisionRecord() instead.
*
* @return Content
*
* @since 1.21
*/
protected function getContentObject() {
if ( $this->mPage->getId() === 0 ) {
$content = $this->getSubstituteContent();
} else {
$this->fetchContentObject();
$content = $this->mContentObject;
}
return $content;
}
/**
* Returns Content object to use when the page does not exist.
*
* @return Content
*/
private function getSubstituteContent() {
# If this is a MediaWiki:x message, then load the messages
# and return the message value for x.
if ( $this->getTitle()->getNamespace() == NS_MEDIAWIKI ) {
$text = $this->getTitle()->getDefaultMessageText();
if ( $text === false ) {
$text = '';
}
$content = ContentHandler::makeContent( $text, $this->getTitle() );
} else {
$message = $this->getContext()->getUser()->isLoggedIn() ? 'noarticletext' : 'noarticletextanon';
$content = new MessageContent( $message, null );
}
return $content;
}
/**
* Returns ParserOutput to use when a page does not exist. In some cases, we still want to show
* "virtual" content, e.g. in the MediaWiki namespace, or in the File namespace for non-local
* files.
*
* @param ParserOptions $options
*
* @return ParserOutput
*/
protected function getEmptyPageParserOutput( ParserOptions $options ) {
$content = $this->getSubstituteContent();
return $content->getParserOutput( $this->getTitle(), 0, $options );
}
/**
* @see getOldIDFromRequest()
* @see getRevIdFetched()
*
* @return int The oldid of the article that is was requested in the constructor or via the
* context's WebRequest.
*/
public function getOldID() {
if ( $this->mOldId === null ) {
$this->mOldId = $this->getOldIDFromRequest();
}
return $this->mOldId;
}
/**
* Sets $this->mRedirectUrl to a correct URL if the query parameters are incorrect
*
* @return int The old id for the request
*/
public function getOldIDFromRequest() {
$this->mRedirectUrl = false;
$request = $this->getContext()->getRequest();
$oldid = $request->getIntOrNull( 'oldid' );
if ( $oldid === null ) {
return 0;
}
if ( $oldid !== 0 ) {
# Load the given revision and check whether the page is another one.
# In that case, update this instance to reflect the change.
if ( $oldid === $this->mPage->getLatest() ) {
$this->mRevision = $this->mPage->getRevision();
} else {
$this->mRevision = Revision::newFromId( $oldid );
if ( $this->mRevision !== null ) {
// Revision title doesn't match the page title given?
if ( $this->mPage->getId() != $this->mRevision->getPage() ) {
$function = get_class( $this->mPage ) . '::newFromID';
$this->mPage = $function( $this->mRevision->getPage() );
}
}
}
}
$rl = MediaWikiServices::getInstance()->getRevisionLookup();
$oldRev = $this->mRevision ? $this->mRevision->getRevisionRecord() : null;
if ( $request->getVal( 'direction' ) == 'next' ) {
$nextid = 0;
if ( $oldRev ) {
$nextRev = $rl->getNextRevision( $oldRev );
if ( $nextRev ) {
$nextid = $nextRev->getId();
}
}
if ( $nextid ) {
$oldid = $nextid;
$this->mRevision = null;
} else {
$this->mRedirectUrl = $this->getTitle()->getFullURL( 'redirect=no' );
}
} elseif ( $request->getVal( 'direction' ) == 'prev' ) {
$previd = 0;
if ( $oldRev ) {
$prevRev = $rl->getPreviousRevision( $oldRev );
if ( $prevRev ) {
$previd = $prevRev->getId();
}
}
if ( $previd ) {
$oldid = $previd;
$this->mRevision = null;
}
}
$this->mRevIdFetched = $this->mRevision ? $this->mRevision->getId() : 0;
return $oldid;
}
/**
* Get text content object
* Does *NOT* follow redirects.
* @todo When is this null?
* @deprecated since 1.32, use fetchRevisionRecord() instead.
*
* @note Code that wants to retrieve page content from the database should
* use WikiPage::getContent().
*
* @return Content|null|bool
*
* @since 1.21
*/
protected function fetchContentObject() {
if ( !$this->mContentLoaded ) {
$this->fetchRevisionRecord();
}
return $this->mContentObject;
}
/**
* Fetches the revision to work on.
* The revision is typically loaded from the database, but may also be a fake representing
* an error message or content supplied by an extension. Refer to $this->fetchResult for
* the revision actually loaded from the database, and any errors encountered while doing
* that.
*
* @return RevisionRecord|null
*/
protected function fetchRevisionRecord() {
if ( $this->fetchResult ) {
return $this->mRevision ? $this->mRevision->getRevisionRecord() : null;
}
$this->mContentLoaded = true;
$this->mContentObject = null;
$oldid = $this->getOldID();
// $this->mRevision might already be fetched by getOldIDFromRequest()
if ( !$this->mRevision ) {
if ( !$oldid ) {
$this->mRevision = $this->mPage->getRevision();
if ( !$this->mRevision ) {
wfDebug( __METHOD__ . " failed to find page data for title " .
$this->getTitle()->getPrefixedText() . "\n" );
// Just for sanity, output for this case is done by showMissingArticle().
$this->fetchResult = Status::newFatal( 'noarticletext' );
$this->applyContentOverride( $this->makeFetchErrorContent() );
return null;
}
} else {
$this->mRevision = Revision::newFromId( $oldid );
if ( !$this->mRevision ) {
wfDebug( __METHOD__ . " failed to load revision, rev_id $oldid\n" );
$this->fetchResult = Status::newFatal( 'missing-revision', $oldid );
$this->applyContentOverride( $this->makeFetchErrorContent() );
return null;
}
}
}
$this->mRevIdFetched = $this->mRevision->getId();
$this->fetchResult = Status::newGood( $this->mRevision );
if ( !RevisionRecord::userCanBitfield(
$this->mRevision->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getContext()->getUser()
) ) {
wfDebug( __METHOD__ . " failed to retrieve content of revision " .
$this->mRevision->getId() . "\n" );
// Just for sanity, output for this case is done by showDeletedRevisionHeader().
$this->fetchResult = Status::newFatal( 'rev-deleted-text-permission' );
$this->applyContentOverride( $this->makeFetchErrorContent() );
return null;
}
// For B/C only
$this->mContentObject = $this->mRevision->getContent(
RevisionRecord::FOR_THIS_USER,
$this->getContext()->getUser()
);
return $this->mRevision->getRevisionRecord();
}
/**
* Returns a Content object representing any error in $this->fetchContent, or null
* if there is no such error.
*
* @return Content|null
*/
private function makeFetchErrorContent() {
if ( !$this->fetchResult || $this->fetchResult->isOK() ) {
return null;
}
return new MessageContent( $this->fetchResult->getMessage() );
}
/**
* Applies a content override by constructing a fake Revision object and assigning
* it to mRevision. The fake revision will not have a user, timestamp or summary set.
*
* @todo This mechanism was created mainly to accommodate extensions that use the
* ArticleAfterFetchContentObject. fetchRevisionRecord() presently also uses this mechanism
* to report errors, but that could be changed to use $this->fetchResult instead.
*
* @param Content $override Content to be used instead of the actual page content,
* coming from an extension or representing an error message.
*/
private function applyContentOverride( Content $override ) {
// Construct a fake revision
$rev = new MutableRevisionRecord( $this->getTitle() );
$rev->setContent( SlotRecord::MAIN, $override );
$this->mRevision = new Revision( $rev );
// For B/C only
$this->mContentObject = $override;
}
/**
* Returns true if the currently-referenced revision is the current edit
* to this page (and it exists).
* @return bool
*/
public function isCurrent() {
# If no oldid, this is the current version.
if ( $this->getOldID() == 0 ) {
return true;
}
return $this->mPage->exists() && $this->mRevision && $this->mRevision->isCurrent();
}
/**
* Get the fetched Revision object depending on request parameters or null
* on failure. The revision returned may be a fake representing an error message or
* wrapping content supplied by an extension. Refer to $this->fetchResult for the
* revision actually loaded from the database.
*
* @since 1.19
* @return Revision|null
*/
public function getRevisionFetched() {
$this->fetchRevisionRecord();
if ( $this->fetchResult->isOK() ) {
return $this->mRevision;
}
}
/**
* Use this to fetch the rev ID used on page views
*
* Before fetchRevisionRecord was called, this returns the page's latest revision,
* regardless of what getOldID() returns.
*
* @return int Revision ID of last article revision
*/
public function getRevIdFetched() {
if ( $this->fetchResult && $this->fetchResult->isOK() ) {
return $this->fetchResult->value->getId();
} else {
return $this->mPage->getLatest();
}
}
/**
* This is the default action of the index.php entry point: just view the
* page of the given title.
*/
public function view() {
global $wgUseFileCache;
# Get variables from query string
# As side effect this will load the revision and update the title
# in a revision ID is passed in the request, so this should remain
# the first call of this method even if $oldid is used way below.
$oldid = $this->getOldID();
$user = $this->getContext()->getUser();
# Another whitelist check in case getOldID() is altering the title
$permErrors = $this->permManager->getPermissionErrors(
'read',
$user,
$this->getTitle()
);
if ( count( $permErrors ) ) {
wfDebug( __METHOD__ . ": denied on secondary read check\n" );
throw new PermissionsError( 'read', $permErrors );
}
$outputPage = $this->getContext()->getOutput();
# getOldID() may as well want us to redirect somewhere else
if ( $this->mRedirectUrl ) {
$outputPage->redirect( $this->mRedirectUrl );
wfDebug( __METHOD__ . ": redirecting due to oldid\n" );
return;
}
# If we got diff in the query, we want to see a diff page instead of the article.
if ( $this->getContext()->getRequest()->getCheck( 'diff' ) ) {
wfDebug( __METHOD__ . ": showing diff page\n" );
$this->showDiffPage();
return;
}
# Set page title (may be overridden by DISPLAYTITLE)
$outputPage->setPageTitle( $this->getTitle()->getPrefixedText() );
$outputPage->setArticleFlag( true );
# Allow frames by default
$outputPage->allowClickjacking();
$parserCache = MediaWikiServices::getInstance()->getParserCache();
$parserOptions = $this->getParserOptions();
$poOptions = [];
# Render printable version, use printable version cache
if ( $outputPage->isPrintable() ) {
$parserOptions->setIsPrintable( true );
$poOptions['enableSectionEditLinks'] = false;
} elseif ( $this->viewIsRenderAction || !$this->isCurrent() ||
!$this->permManager->quickUserCan( 'edit', $user, $this->getTitle() )
) {
$poOptions['enableSectionEditLinks'] = false;
}
# Try client and file cache
if ( $oldid === 0 && $this->mPage->checkTouched() ) {
# Try to stream the output from file cache
if ( $wgUseFileCache && $this->tryFileCache() ) {
wfDebug( __METHOD__ . ": done file cache\n" );
# tell wgOut that output is taken care of
$outputPage->disable();
$this->mPage->doViewUpdates( $user, $oldid );
return;
}
}
# Should the parser cache be used?
$useParserCache = $this->mPage->shouldCheckParserCache( $parserOptions, $oldid );
wfDebug( 'Article::view using parser cache: ' . ( $useParserCache ? 'yes' : 'no' ) . "\n" );
if ( $user->getStubThreshold() ) {
MediaWikiServices::getInstance()->getStatsdDataFactory()->increment( 'pcache_miss_stub' );
}
$this->showRedirectedFromHeader();
$this->showNamespaceHeader();
# Iterate through the possible ways of constructing the output text.
# Keep going until $outputDone is set, or we run out of things to do.
$pass = 0;
$outputDone = false;
$this->mParserOutput = false;
while ( !$outputDone && ++$pass ) {
switch ( $pass ) {
case 1:
// Avoid PHP 7.1 warning of passing $this by reference
$articlePage = $this;
Hooks::run( 'ArticleViewHeader', [ &$articlePage, &$outputDone, &$useParserCache ] );
break;
case 2:
# Early abort if the page doesn't exist
if ( !$this->mPage->exists() ) {
wfDebug( __METHOD__ . ": showing missing article\n" );
$this->showMissingArticle();
$this->mPage->doViewUpdates( $user );
return;
}
# Try the parser cache
if ( $useParserCache ) {
$this->mParserOutput = $parserCache->get( $this->mPage, $parserOptions );
if ( $this->mParserOutput !== false ) {
if ( $oldid ) {
wfDebug( __METHOD__ . ": showing parser cache contents for current rev permalink\n" );
$this->setOldSubtitle( $oldid );
} else {
wfDebug( __METHOD__ . ": showing parser cache contents\n" );
}
$outputPage->addParserOutput( $this->mParserOutput, $poOptions );
# Ensure that UI elements requiring revision ID have
# the correct version information.
$outputPage->setRevisionId( $this->mPage->getLatest() );
# Preload timestamp to avoid a DB hit
$cachedTimestamp = $this->mParserOutput->getTimestamp();
if ( $cachedTimestamp !== null ) {
$outputPage->setRevisionTimestamp( $cachedTimestamp );
$this->mPage->setTimestamp( $cachedTimestamp );
}
$outputDone = true;
}
}
break;
case 3:
# Are we looking at an old revision
$rev = $this->fetchRevisionRecord();
if ( $oldid && $this->fetchResult->isOK() ) {
$this->setOldSubtitle( $oldid );
if ( !$this->showDeletedRevisionHeader() ) {
wfDebug( __METHOD__ . ": cannot view deleted revision\n" );
return;
}
}
# Ensure that UI elements requiring revision ID have
# the correct version information.
$outputPage->setRevisionId( $this->getRevIdFetched() );
# Preload timestamp to avoid a DB hit
$outputPage->setRevisionTimestamp( $this->mPage->getTimestamp() );
# Pages containing custom CSS or JavaScript get special treatment
if ( $this->getTitle()->isSiteConfigPage() || $this->getTitle()->isUserConfigPage() ) {
$dir = $this->getContext()->getLanguage()->getDir();
$lang = $this->getContext()->getLanguage()->getHtmlCode();
$outputPage->wrapWikiMsg(
"<div id='mw-clearyourcache' lang='$lang' dir='$dir' class='mw-content-$dir'>\n$1\n</div>",
'clearyourcache'
);
} elseif ( !Hooks::run( 'ArticleRevisionViewCustom', [
$rev,
$this->getTitle(),
$oldid,
$outputPage,
] )
) {
// NOTE: sync with hooks called in DifferenceEngine::renderNewRevision()
// Allow extensions do their own custom view for certain pages
$outputDone = true;
}
break;
case 4:
# Run the parse, protected by a pool counter
wfDebug( __METHOD__ . ": doing uncached parse\n" );
$rev = $this->fetchRevisionRecord();
$error = null;
if ( $rev ) {
$poolArticleView = new PoolWorkArticleView(
$this->getPage(),
$parserOptions,
$this->getRevIdFetched(),
$useParserCache,
$rev,
// permission checking was done earlier via showDeletedRevisionHeader()
RevisionRecord::RAW
);
$ok = $poolArticleView->execute();
$error = $poolArticleView->getError();
$this->mParserOutput = $poolArticleView->getParserOutput() ?: null;
# Don't cache a dirty ParserOutput object
if ( $poolArticleView->getIsDirty() ) {
$outputPage->setCdnMaxage( 0 );
$outputPage->addHTML( "<!-- parser cache is expired, " .
"sending anyway due to pool overload-->\n" );
}
} else {
$ok = false;
}
if ( !$ok ) {
if ( $error ) {
$outputPage->clearHTML(); // for release() errors
$outputPage->enableClientCache( false );
$outputPage->setRobotPolicy( 'noindex,nofollow' );
$errortext = $error->getWikiText(
false, 'view-pool-error', $this->getContext()->getLanguage()
);
$outputPage->wrapWikiTextAsInterface( 'errorbox', $errortext );
}
# Connection or timeout error
return;
}
if ( $this->mParserOutput ) {
$outputPage->addParserOutput( $this->mParserOutput, $poOptions );
}
if ( $rev && $this->getRevisionRedirectTarget( $rev ) ) {
$outputPage->addSubtitle( "<span id=\"redirectsub\">" .
$this->getContext()->msg( 'redirectpagesub' )->parse() . "</span>" );
}
$outputDone = true;
break;
# Should be unreachable, but just in case...
default:
break 2;
}
}
// Get the ParserOutput actually *displayed* here.
// Note that $this->mParserOutput is the *current*/oldid version output.
// Note that the ArticleViewHeader hook is allowed to set $outputDone to a
// ParserOutput instance.
$pOutput = ( $outputDone instanceof ParserOutput )
? $outputDone // object fetched by hook
: ( $this->mParserOutput ?: null ); // ParserOutput or null, avoid false
# Adjust title for main page & pages with displaytitle
if ( $pOutput ) {
$this->adjustDisplayTitle( $pOutput );
}
# For the main page, overwrite the <title> element with the con-
# tents of 'pagetitle-view-mainpage' instead of the default (if
# that's not empty).
# This message always exists because it is in the i18n files
if ( $this->getTitle()->isMainPage() ) {
$msg = wfMessage( 'pagetitle-view-mainpage' )->inContentLanguage();
if ( !$msg->isDisabled() ) {
$outputPage->setHTMLTitle( $msg->title( $this->getTitle() )->text() );
}
}
# Use adaptive TTLs for CDN so delayed/failed purges are noticed less often.
# This could use getTouched(), but that could be scary for major template edits.
$outputPage->adaptCdnTTL( $this->mPage->getTimestamp(), IExpiringStore::TTL_DAY );
# Check for any __NOINDEX__ tags on the page using $pOutput
$policy = $this->getRobotPolicy( 'view', $pOutput ?: null );
$outputPage->setIndexPolicy( $policy['index'] );
$outputPage->setFollowPolicy( $policy['follow'] ); // FIXME: test this
$this->showViewFooter();
$this->mPage->doViewUpdates( $user, $oldid ); // FIXME: test this
# Load the postEdit module if the user just saved this revision
# See also EditPage::setPostEditCookie
$request = $this->getContext()->getRequest();
$cookieKey = EditPage::POST_EDIT_COOKIE_KEY_PREFIX . $this->getRevIdFetched();
$postEdit = $request->getCookie( $cookieKey );
if ( $postEdit ) {
# Clear the cookie. This also prevents caching of the response.
$request->response()->clearCookie( $cookieKey );
$outputPage->addJsConfigVars( 'wgPostEdit', $postEdit );
$outputPage->addModules( 'mediawiki.action.view.postEdit' ); // FIXME: test this
}
}
/**
* @param RevisionRecord $revision
* @return null|Title
*/
private function getRevisionRedirectTarget( RevisionRecord $revision ) {
// TODO: find a *good* place for the code that determines the redirect target for
// a given revision!
// NOTE: Use main slot content. Compare code in DerivedPageDataUpdater::revisionIsRedirect.
$content = $revision->getContent( SlotRecord::MAIN );
return $content ? $content->getRedirectTarget() : null;
}
/**
* Adjust title for pages with displaytitle, -{T|}- or language conversion
* @param ParserOutput $pOutput
*/
public function adjustDisplayTitle( ParserOutput $pOutput ) {
$out = $this->getContext()->getOutput();
# Adjust the title if it was set by displaytitle, -{T|}- or language conversion
$titleText = $pOutput->getTitleText();
if ( strval( $titleText ) !== '' ) {
$out->setPageTitle( $titleText );
$out->setDisplayTitle( $titleText );
}
}
/**
* Show a diff page according to current request variables. For use within
* Article::view() only, other callers should use the DifferenceEngine class.
*/
protected function showDiffPage() {
$request = $this->getContext()->getRequest();
$user = $this->getContext()->getUser();
$diff = $request->getVal( 'diff' );
$rcid = $request->getVal( 'rcid' );
$diffOnly = $request->getBool( 'diffonly', $user->getOption( 'diffonly' ) );
$purge = $request->getVal( 'action' ) == 'purge';
$unhide = $request->getInt( 'unhide' ) == 1;
$oldid = $this->getOldID();
$rev = $this->getRevisionFetched();
if ( !$rev ) {
$this->getContext()->getOutput()->setPageTitle( wfMessage( 'errorpagetitle' ) );
$msg = $this->getContext()->msg( 'difference-missing-revision' )
->params( $oldid )
->numParams( 1 )
->parseAsBlock();
$this->getContext()->getOutput()->addHTML( $msg );
return;
}
$contentHandler = $rev->getContentHandler();
$de = $contentHandler->createDifferenceEngine(
$this->getContext(),
$oldid,
$diff,
$rcid,
$purge,
$unhide
);
$de->setSlotDiffOptions( [
'diff-type' => $request->getVal( 'diff-type' )
] );
// DifferenceEngine directly fetched the revision:
$this->mRevIdFetched = $de->getNewid();
$de->showDiffPage( $diffOnly );
// Run view updates for the newer revision being diffed (and shown
// below the diff if not $diffOnly).
list( $old, $new ) = $de->mapDiffPrevNext( $oldid, $diff );
// New can be false, convert it to 0 - this conveniently means the latest revision
$this->mPage->doViewUpdates( $user, (int)$new );
}
/**
* Get the robot policy to be used for the current view
* @param string $action The action= GET parameter
* @param ParserOutput|null $pOutput
* @return array The policy that should be set
* @todo actions other than 'view'
*/
public function getRobotPolicy( $action, ParserOutput $pOutput = null ) {
global $wgArticleRobotPolicies, $wgNamespaceRobotPolicies, $wgDefaultRobotPolicy;
$ns = $this->getTitle()->getNamespace();
# Don't index user and user talk pages for blocked users (T13443)
if ( ( $ns == NS_USER || $ns == NS_USER_TALK ) && !$this->getTitle()->isSubpage() ) {
$specificTarget = null;
$vagueTarget = null;
$titleText = $this->getTitle()->getText();
if ( IPUtils::isValid( $titleText ) ) {
$vagueTarget = $titleText;
} else {
$specificTarget = $titleText;
}
if ( DatabaseBlock::newFromTarget( $specificTarget, $vagueTarget ) instanceof DatabaseBlock ) {
return [
'index' => 'noindex',
'follow' => 'nofollow'
];
}
}
if ( $this->mPage->getId() === 0 || $this->getOldID() ) {
# Non-articles (special pages etc), and old revisions
return [
'index' => 'noindex',
'follow' => 'nofollow'
];
} elseif ( $this->getContext()->getOutput()->isPrintable() ) {
# Discourage indexing of printable versions, but encourage following
return [
'index' => 'noindex',
'follow' => 'follow'
];