-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
WikiPage.php
3638 lines (3202 loc) · 111 KB
/
WikiPage.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
/**
* Base representation for a MediaWiki page.
*
* 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\Logger\LoggerFactory;
use \MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\DBError;
use Wikimedia\Rdbms\DBUnexpectedError;
/**
* Class representing a MediaWiki article and history.
*
* Some fields are public only for backwards-compatibility. Use accessors.
* In the past, this class was part of Article.php and everything was public.
*/
class WikiPage implements Page, IDBAccessObject {
// Constants for $mDataLoadedFrom and related
/**
* @var Title
*/
public $mTitle = null;
/**@{{
* @protected
*/
public $mDataLoaded = false; // !< Boolean
public $mIsRedirect = false; // !< Boolean
public $mLatest = false; // !< Integer (false means "not loaded")
/**@}}*/
/** @var stdClass Map of cache fields (text, parser output, ect) for a proposed/new edit */
public $mPreparedEdit = false;
/**
* @var int
*/
protected $mId = null;
/**
* @var int One of the READ_* constants
*/
protected $mDataLoadedFrom = self::READ_NONE;
/**
* @var Title
*/
protected $mRedirectTarget = null;
/**
* @var Revision
*/
protected $mLastRevision = null;
/**
* @var string Timestamp of the current revision or empty string if not loaded
*/
protected $mTimestamp = '';
/**
* @var string
*/
protected $mTouched = '19700101000000';
/**
* @var string
*/
protected $mLinksUpdated = '19700101000000';
/** @deprecated since 1.29. Added in 1.28 for partial purging, no longer used. */
const PURGE_CDN_CACHE = 1;
const PURGE_CLUSTER_PCACHE = 2;
const PURGE_GLOBAL_PCACHE = 4;
const PURGE_ALL = 7;
/**
* Constructor and clear the article
* @param Title $title Reference to a Title object.
*/
public function __construct( Title $title ) {
$this->mTitle = $title;
}
/**
* Makes sure that the mTitle object is cloned
* to the newly cloned WikiPage.
*/
public function __clone() {
$this->mTitle = clone $this->mTitle;
}
/**
* Create a WikiPage object of the appropriate class for the given title.
*
* @param Title $title
*
* @throws MWException
* @return WikiPage|WikiCategoryPage|WikiFilePage
*/
public static function factory( Title $title ) {
$ns = $title->getNamespace();
if ( $ns == NS_MEDIA ) {
throw new MWException( "NS_MEDIA is a virtual namespace; use NS_FILE." );
} elseif ( $ns < 0 ) {
throw new MWException( "Invalid or virtual namespace $ns given." );
}
$page = null;
if ( !Hooks::run( 'WikiPageFactory', [ $title, &$page ] ) ) {
return $page;
}
switch ( $ns ) {
case NS_FILE:
$page = new WikiFilePage( $title );
break;
case NS_CATEGORY:
$page = new WikiCategoryPage( $title );
break;
default:
$page = new WikiPage( $title );
}
return $page;
}
/**
* Constructor from a page id
*
* @param int $id Article ID to load
* @param string|int $from One of the following values:
* - "fromdb" or WikiPage::READ_NORMAL to select from a replica DB
* - "fromdbmaster" or WikiPage::READ_LATEST to select from the master database
*
* @return WikiPage|null
*/
public static function newFromID( $id, $from = 'fromdb' ) {
// page ids are never 0 or negative, see T63166
if ( $id < 1 ) {
return null;
}
$from = self::convertSelectType( $from );
$db = wfGetDB( $from === self::READ_LATEST ? DB_MASTER : DB_REPLICA );
$row = $db->selectRow(
'page', self::selectFields(), [ 'page_id' => $id ], __METHOD__ );
if ( !$row ) {
return null;
}
return self::newFromRow( $row, $from );
}
/**
* Constructor from a database row
*
* @since 1.20
* @param object $row Database row containing at least fields returned by selectFields().
* @param string|int $from Source of $data:
* - "fromdb" or WikiPage::READ_NORMAL: from a replica DB
* - "fromdbmaster" or WikiPage::READ_LATEST: from the master DB
* - "forupdate" or WikiPage::READ_LOCKING: from the master DB using SELECT FOR UPDATE
* @return WikiPage
*/
public static function newFromRow( $row, $from = 'fromdb' ) {
$page = self::factory( Title::newFromRow( $row ) );
$page->loadFromRow( $row, $from );
return $page;
}
/**
* Convert 'fromdb', 'fromdbmaster' and 'forupdate' to READ_* constants.
*
* @param object|string|int $type
* @return mixed
*/
private static function convertSelectType( $type ) {
switch ( $type ) {
case 'fromdb':
return self::READ_NORMAL;
case 'fromdbmaster':
return self::READ_LATEST;
case 'forupdate':
return self::READ_LOCKING;
default:
// It may already be an integer or whatever else
return $type;
}
}
/**
* @todo Move this UI stuff somewhere else
*
* @see ContentHandler::getActionOverrides
*/
public function getActionOverrides() {
return $this->getContentHandler()->getActionOverrides();
}
/**
* Returns the ContentHandler instance to be used to deal with the content of this WikiPage.
*
* Shorthand for ContentHandler::getForModelID( $this->getContentModel() );
*
* @return ContentHandler
*
* @since 1.21
*/
public function getContentHandler() {
return ContentHandler::getForModelID( $this->getContentModel() );
}
/**
* Get the title object of the article
* @return Title Title object of this page
*/
public function getTitle() {
return $this->mTitle;
}
/**
* Clear the object
* @return void
*/
public function clear() {
$this->mDataLoaded = false;
$this->mDataLoadedFrom = self::READ_NONE;
$this->clearCacheFields();
}
/**
* Clear the object cache fields
* @return void
*/
protected function clearCacheFields() {
$this->mId = null;
$this->mRedirectTarget = null; // Title object if set
$this->mLastRevision = null; // Latest revision
$this->mTouched = '19700101000000';
$this->mLinksUpdated = '19700101000000';
$this->mTimestamp = '';
$this->mIsRedirect = false;
$this->mLatest = false;
// T59026: do not clear mPreparedEdit since prepareTextForEdit() already checks
// the requested rev ID and content against the cached one for equality. For most
// content types, the output should not change during the lifetime of this cache.
// Clearing it can cause extra parses on edit for no reason.
}
/**
* Clear the mPreparedEdit cache field, as may be needed by mutable content types
* @return void
* @since 1.23
*/
public function clearPreparedEdit() {
$this->mPreparedEdit = false;
}
/**
* Return the list of revision fields that should be selected to create
* a new page.
*
* @return array
*/
public static function selectFields() {
global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
$fields = [
'page_id',
'page_namespace',
'page_title',
'page_restrictions',
'page_is_redirect',
'page_is_new',
'page_random',
'page_touched',
'page_links_updated',
'page_latest',
'page_len',
];
if ( $wgContentHandlerUseDB ) {
$fields[] = 'page_content_model';
}
if ( $wgPageLanguageUseDB ) {
$fields[] = 'page_lang';
}
return $fields;
}
/**
* Fetch a page record with the given conditions
* @param IDatabase $dbr
* @param array $conditions
* @param array $options
* @return object|bool Database result resource, or false on failure
*/
protected function pageData( $dbr, $conditions, $options = [] ) {
$fields = self::selectFields();
// Avoid PHP 7.1 warning of passing $this by reference
$wikiPage = $this;
Hooks::run( 'ArticlePageDataBefore', [ &$wikiPage, &$fields ] );
$row = $dbr->selectRow( 'page', $fields, $conditions, __METHOD__, $options );
Hooks::run( 'ArticlePageDataAfter', [ &$wikiPage, &$row ] );
return $row;
}
/**
* Fetch a page record matching the Title object's namespace and title
* using a sanitized title string
*
* @param IDatabase $dbr
* @param Title $title
* @param array $options
* @return object|bool Database result resource, or false on failure
*/
public function pageDataFromTitle( $dbr, $title, $options = [] ) {
return $this->pageData( $dbr, [
'page_namespace' => $title->getNamespace(),
'page_title' => $title->getDBkey() ], $options );
}
/**
* Fetch a page record matching the requested ID
*
* @param IDatabase $dbr
* @param int $id
* @param array $options
* @return object|bool Database result resource, or false on failure
*/
public function pageDataFromId( $dbr, $id, $options = [] ) {
return $this->pageData( $dbr, [ 'page_id' => $id ], $options );
}
/**
* Load the object from a given source by title
*
* @param object|string|int $from One of the following:
* - A DB query result object.
* - "fromdb" or WikiPage::READ_NORMAL to get from a replica DB.
* - "fromdbmaster" or WikiPage::READ_LATEST to get from the master DB.
* - "forupdate" or WikiPage::READ_LOCKING to get from the master DB
* using SELECT FOR UPDATE.
*
* @return void
*/
public function loadPageData( $from = 'fromdb' ) {
$from = self::convertSelectType( $from );
if ( is_int( $from ) && $from <= $this->mDataLoadedFrom ) {
// We already have the data from the correct location, no need to load it twice.
return;
}
if ( is_int( $from ) ) {
list( $index, $opts ) = DBAccessObjectUtils::getDBOptions( $from );
$data = $this->pageDataFromTitle( wfGetDB( $index ), $this->mTitle, $opts );
if ( !$data
&& $index == DB_REPLICA
&& wfGetLB()->getServerCount() > 1
&& wfGetLB()->hasOrMadeRecentMasterChanges()
) {
$from = self::READ_LATEST;
list( $index, $opts ) = DBAccessObjectUtils::getDBOptions( $from );
$data = $this->pageDataFromTitle( wfGetDB( $index ), $this->mTitle, $opts );
}
} else {
// No idea from where the caller got this data, assume replica DB.
$data = $from;
$from = self::READ_NORMAL;
}
$this->loadFromRow( $data, $from );
}
/**
* Load the object from a database row
*
* @since 1.20
* @param object|bool $data DB row containing fields returned by selectFields() or false
* @param string|int $from One of the following:
* - "fromdb" or WikiPage::READ_NORMAL if the data comes from a replica DB
* - "fromdbmaster" or WikiPage::READ_LATEST if the data comes from the master DB
* - "forupdate" or WikiPage::READ_LOCKING if the data comes from
* the master DB using SELECT FOR UPDATE
*/
public function loadFromRow( $data, $from ) {
$lc = LinkCache::singleton();
$lc->clearLink( $this->mTitle );
if ( $data ) {
$lc->addGoodLinkObjFromRow( $this->mTitle, $data );
$this->mTitle->loadFromRow( $data );
// Old-fashioned restrictions
$this->mTitle->loadRestrictions( $data->page_restrictions );
$this->mId = intval( $data->page_id );
$this->mTouched = wfTimestamp( TS_MW, $data->page_touched );
$this->mLinksUpdated = wfTimestampOrNull( TS_MW, $data->page_links_updated );
$this->mIsRedirect = intval( $data->page_is_redirect );
$this->mLatest = intval( $data->page_latest );
// T39225: $latest may no longer match the cached latest Revision object.
// Double-check the ID of any cached latest Revision object for consistency.
if ( $this->mLastRevision && $this->mLastRevision->getId() != $this->mLatest ) {
$this->mLastRevision = null;
$this->mTimestamp = '';
}
} else {
$lc->addBadLinkObj( $this->mTitle );
$this->mTitle->loadFromRow( false );
$this->clearCacheFields();
$this->mId = 0;
}
$this->mDataLoaded = true;
$this->mDataLoadedFrom = self::convertSelectType( $from );
}
/**
* @return int Page ID
*/
public function getId() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return $this->mId;
}
/**
* @return bool Whether or not the page exists in the database
*/
public function exists() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return $this->mId > 0;
}
/**
* Check if this page is something we're going to be showing
* some sort of sensible content for. If we return false, page
* views (plain action=view) will return an HTTP 404 response,
* so spiders and robots can know they're following a bad link.
*
* @return bool
*/
public function hasViewableContent() {
return $this->mTitle->isKnown();
}
/**
* Tests if the article content represents a redirect
*
* @return bool
*/
public function isRedirect() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return (bool)$this->mIsRedirect;
}
/**
* Returns the page's content model id (see the CONTENT_MODEL_XXX constants).
*
* Will use the revisions actual content model if the page exists,
* and the page's default if the page doesn't exist yet.
*
* @return string
*
* @since 1.21
*/
public function getContentModel() {
if ( $this->exists() ) {
$cache = ObjectCache::getMainWANInstance();
return $cache->getWithSetCallback(
$cache->makeKey( 'page', 'content-model', $this->getLatest() ),
$cache::TTL_MONTH,
function () {
$rev = $this->getRevision();
if ( $rev ) {
// Look at the revision's actual content model
return $rev->getContentModel();
} else {
$title = $this->mTitle->getPrefixedDBkey();
wfWarn( "Page $title exists but has no (visible) revisions!" );
return $this->mTitle->getContentModel();
}
}
);
}
// use the default model for this page
return $this->mTitle->getContentModel();
}
/**
* Loads page_touched and returns a value indicating if it should be used
* @return bool True if this page exists and is not a redirect
*/
public function checkTouched() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return ( $this->mId && !$this->mIsRedirect );
}
/**
* Get the page_touched field
* @return string Containing GMT timestamp
*/
public function getTouched() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return $this->mTouched;
}
/**
* Get the page_links_updated field
* @return string|null Containing GMT timestamp
*/
public function getLinksTimestamp() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return $this->mLinksUpdated;
}
/**
* Get the page_latest field
* @return int The rev_id of current revision
*/
public function getLatest() {
if ( !$this->mDataLoaded ) {
$this->loadPageData();
}
return (int)$this->mLatest;
}
/**
* Get the Revision object of the oldest revision
* @return Revision|null
*/
public function getOldestRevision() {
// Try using the replica DB first, then try the master
$rev = $this->mTitle->getFirstRevision();
if ( !$rev ) {
$rev = $this->mTitle->getFirstRevision( Title::GAID_FOR_UPDATE );
}
return $rev;
}
/**
* Loads everything except the text
* This isn't necessary for all uses, so it's only done if needed.
*/
protected function loadLastEdit() {
if ( $this->mLastRevision !== null ) {
return; // already loaded
}
$latest = $this->getLatest();
if ( !$latest ) {
return; // page doesn't exist or is missing page_latest info
}
if ( $this->mDataLoadedFrom == self::READ_LOCKING ) {
// T39225: if session S1 loads the page row FOR UPDATE, the result always
// includes the latest changes committed. This is true even within REPEATABLE-READ
// transactions, where S1 normally only sees changes committed before the first S1
// SELECT. Thus we need S1 to also gets the revision row FOR UPDATE; otherwise, it
// may not find it since a page row UPDATE and revision row INSERT by S2 may have
// happened after the first S1 SELECT.
// https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read
$flags = Revision::READ_LOCKING;
$revision = Revision::newFromPageId( $this->getId(), $latest, $flags );
} elseif ( $this->mDataLoadedFrom == self::READ_LATEST ) {
// Bug T93976: if page_latest was loaded from the master, fetch the
// revision from there as well, as it may not exist yet on a replica DB.
// Also, this keeps the queries in the same REPEATABLE-READ snapshot.
$flags = Revision::READ_LATEST;
$revision = Revision::newFromPageId( $this->getId(), $latest, $flags );
} else {
$dbr = wfGetDB( DB_REPLICA );
$revision = Revision::newKnownCurrent( $dbr, $this->getId(), $latest );
}
if ( $revision ) { // sanity
$this->setLastEdit( $revision );
}
}
/**
* Set the latest revision
* @param Revision $revision
*/
protected function setLastEdit( Revision $revision ) {
$this->mLastRevision = $revision;
$this->mTimestamp = $revision->getTimestamp();
}
/**
* Get the latest revision
* @return Revision|null
*/
public function getRevision() {
$this->loadLastEdit();
if ( $this->mLastRevision ) {
return $this->mLastRevision;
}
return null;
}
/**
* Get the content of the current revision. No side-effects...
*
* @param int $audience One of:
* Revision::FOR_PUBLIC to be displayed to all users
* Revision::FOR_THIS_USER to be displayed to $wgUser
* Revision::RAW get the text regardless of permissions
* @param User $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
* @return Content|null The content of the current revision
*
* @since 1.21
*/
public function getContent( $audience = Revision::FOR_PUBLIC, User $user = null ) {
$this->loadLastEdit();
if ( $this->mLastRevision ) {
return $this->mLastRevision->getContent( $audience, $user );
}
return null;
}
/**
* @return string MW timestamp of last article revision
*/
public function getTimestamp() {
// Check if the field has been filled by WikiPage::setTimestamp()
if ( !$this->mTimestamp ) {
$this->loadLastEdit();
}
return wfTimestamp( TS_MW, $this->mTimestamp );
}
/**
* Set the page timestamp (use only to avoid DB queries)
* @param string $ts MW timestamp of last article revision
* @return void
*/
public function setTimestamp( $ts ) {
$this->mTimestamp = wfTimestamp( TS_MW, $ts );
}
/**
* @param int $audience One of:
* Revision::FOR_PUBLIC to be displayed to all users
* Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param User $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
* @return int User ID for the user that made the last article revision
*/
public function getUser( $audience = Revision::FOR_PUBLIC, User $user = null ) {
$this->loadLastEdit();
if ( $this->mLastRevision ) {
return $this->mLastRevision->getUser( $audience, $user );
} else {
return -1;
}
}
/**
* Get the User object of the user who created the page
* @param int $audience One of:
* Revision::FOR_PUBLIC to be displayed to all users
* Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param User $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
* @return User|null
*/
public function getCreator( $audience = Revision::FOR_PUBLIC, User $user = null ) {
$revision = $this->getOldestRevision();
if ( $revision ) {
$userName = $revision->getUserText( $audience, $user );
return User::newFromName( $userName, false );
} else {
return null;
}
}
/**
* @param int $audience One of:
* Revision::FOR_PUBLIC to be displayed to all users
* Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param User $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
* @return string Username of the user that made the last article revision
*/
public function getUserText( $audience = Revision::FOR_PUBLIC, User $user = null ) {
$this->loadLastEdit();
if ( $this->mLastRevision ) {
return $this->mLastRevision->getUserText( $audience, $user );
} else {
return '';
}
}
/**
* @param int $audience One of:
* Revision::FOR_PUBLIC to be displayed to all users
* Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param User $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
* @return string Comment stored for the last article revision
*/
public function getComment( $audience = Revision::FOR_PUBLIC, User $user = null ) {
$this->loadLastEdit();
if ( $this->mLastRevision ) {
return $this->mLastRevision->getComment( $audience, $user );
} else {
return '';
}
}
/**
* Returns true if last revision was marked as "minor edit"
*
* @return bool Minor edit indicator for the last article revision.
*/
public function getMinorEdit() {
$this->loadLastEdit();
if ( $this->mLastRevision ) {
return $this->mLastRevision->isMinor();
} else {
return false;
}
}
/**
* Determine whether a page would be suitable for being counted as an
* article in the site_stats table based on the title & its content
*
* @param object|bool $editInfo (false): object returned by prepareTextForEdit(),
* if false, the current database state will be used
* @return bool
*/
public function isCountable( $editInfo = false ) {
global $wgArticleCountMethod;
if ( !$this->mTitle->isContentPage() ) {
return false;
}
if ( $editInfo ) {
$content = $editInfo->pstContent;
} else {
$content = $this->getContent();
}
if ( !$content || $content->isRedirect() ) {
return false;
}
$hasLinks = null;
if ( $wgArticleCountMethod === 'link' ) {
// nasty special case to avoid re-parsing to detect links
if ( $editInfo ) {
// ParserOutput::getLinks() is a 2D array of page links, so
// to be really correct we would need to recurse in the array
// but the main array should only have items in it if there are
// links.
$hasLinks = (bool)count( $editInfo->output->getLinks() );
} else {
$hasLinks = (bool)wfGetDB( DB_REPLICA )->selectField( 'pagelinks', 1,
[ 'pl_from' => $this->getId() ], __METHOD__ );
}
}
return $content->isCountable( $hasLinks );
}
/**
* If this page is a redirect, get its target
*
* The target will be fetched from the redirect table if possible.
* If this page doesn't have an entry there, call insertRedirect()
* @return Title|null Title object, or null if this page is not a redirect
*/
public function getRedirectTarget() {
if ( !$this->mTitle->isRedirect() ) {
return null;
}
if ( $this->mRedirectTarget !== null ) {
return $this->mRedirectTarget;
}
// Query the redirect table
$dbr = wfGetDB( DB_REPLICA );
$row = $dbr->selectRow( 'redirect',
[ 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
[ 'rd_from' => $this->getId() ],
__METHOD__
);
// rd_fragment and rd_interwiki were added later, populate them if empty
if ( $row && !is_null( $row->rd_fragment ) && !is_null( $row->rd_interwiki ) ) {
$this->mRedirectTarget = Title::makeTitle(
$row->rd_namespace, $row->rd_title,
$row->rd_fragment, $row->rd_interwiki
);
return $this->mRedirectTarget;
}
// This page doesn't have an entry in the redirect table
$this->mRedirectTarget = $this->insertRedirect();
return $this->mRedirectTarget;
}
/**
* Insert an entry for this page into the redirect table if the content is a redirect
*
* The database update will be deferred via DeferredUpdates
*
* Don't call this function directly unless you know what you're doing.
* @return Title|null Title object or null if not a redirect
*/
public function insertRedirect() {
$content = $this->getContent();
$retval = $content ? $content->getUltimateRedirectTarget() : null;
if ( !$retval ) {
return null;
}
// Update the DB post-send if the page has not cached since now
$that = $this;
$latest = $this->getLatest();
DeferredUpdates::addCallableUpdate(
function () use ( $that, $retval, $latest ) {
$that->insertRedirectEntry( $retval, $latest );
},
DeferredUpdates::POSTSEND,
wfGetDB( DB_MASTER )
);
return $retval;
}
/**
* Insert or update the redirect table entry for this page to indicate it redirects to $rt
* @param Title $rt Redirect target
* @param int|null $oldLatest Prior page_latest for check and set
*/
public function insertRedirectEntry( Title $rt, $oldLatest = null ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->startAtomic( __METHOD__ );
if ( !$oldLatest || $oldLatest == $this->lockAndGetLatest() ) {
$dbw->upsert(
'redirect',
[
'rd_from' => $this->getId(),
'rd_namespace' => $rt->getNamespace(),
'rd_title' => $rt->getDBkey(),
'rd_fragment' => $rt->getFragment(),
'rd_interwiki' => $rt->getInterwiki(),
],
[ 'rd_from' ],
[
'rd_namespace' => $rt->getNamespace(),
'rd_title' => $rt->getDBkey(),
'rd_fragment' => $rt->getFragment(),
'rd_interwiki' => $rt->getInterwiki(),
],
__METHOD__
);
}
$dbw->endAtomic( __METHOD__ );
}
/**
* Get the Title object or URL this page redirects to
*
* @return bool|Title|string False, Title of in-wiki target, or string with URL
*/
public function followRedirect() {
return $this->getRedirectURL( $this->getRedirectTarget() );
}
/**
* Get the Title object or URL to use for a redirect. We use Title
* objects for same-wiki, non-special redirects and URLs for everything
* else.
* @param Title $rt Redirect target
* @return bool|Title|string False, Title object of local target, or string with URL
*/
public function getRedirectURL( $rt ) {
if ( !$rt ) {
return false;
}
if ( $rt->isExternal() ) {
if ( $rt->isLocal() ) {
// Offsite wikis need an HTTP redirect.
// This can be hard to reverse and may produce loops,
// so they may be disabled in the site configuration.
$source = $this->mTitle->getFullURL( 'redirect=no' );
return $rt->getFullURL( [ 'rdfrom' => $source ] );
} else {
// External pages without "local" bit set are not valid
// redirect targets
return false;
}
}
if ( $rt->isSpecialPage() ) {
// Gotta handle redirects to special pages differently:
// Fill the HTTP response "Location" header and ignore the rest of the page we're on.
// Some pages are not valid targets.
if ( $rt->isValidRedirectTarget() ) {
return $rt->getFullURL();
} else {
return false;
}
}
return $rt;
}
/**
* Get a list of users who have edited this article, not including the user who made
* the most recent revision, which you can get from $article->getUser() if you want it
* @return UserArrayFromResult
*/
public function getContributors() {
// @todo FIXME: This is expensive; cache this info somewhere.
$dbr = wfGetDB( DB_REPLICA );
if ( $dbr->implicitGroupby() ) {
$realNameField = 'user_real_name';
} else {
$realNameField = 'MIN(user_real_name) AS user_real_name';
}
$tables = [ 'revision', 'user' ];
$fields = [
'user_id' => 'rev_user',
'user_name' => 'rev_user_text',
$realNameField,
'timestamp' => 'MAX(rev_timestamp)',
];
$conds = [ 'rev_page' => $this->getId() ];