Merge "Allow partially blocked users to tag unrelated revisions"
[lhc/web/wiklou.git] / includes / page / WikiPage.php
1 <?php
2 /**
3 * Base representation for a MediaWiki page.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\Edit\PreparedEdit;
24 use MediaWiki\Logger\LoggerFactory;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Revision\RevisionRecord;
27 use MediaWiki\Revision\RevisionRenderer;
28 use MediaWiki\Revision\RevisionStore;
29 use MediaWiki\Revision\SlotRoleRegistry;
30 use MediaWiki\Revision\SlotRecord;
31 use MediaWiki\Storage\DerivedPageDataUpdater;
32 use MediaWiki\Storage\PageUpdater;
33 use MediaWiki\Storage\RevisionSlotsUpdate;
34 use Wikimedia\Assert\Assert;
35 use Wikimedia\Rdbms\FakeResultWrapper;
36 use Wikimedia\Rdbms\IDatabase;
37 use Wikimedia\Rdbms\LoadBalancer;
38
39 /**
40 * Class representing a MediaWiki article and history.
41 *
42 * Some fields are public only for backwards-compatibility. Use accessors.
43 * In the past, this class was part of Article.php and everything was public.
44 *
45 * @phan-file-suppress PhanAccessMethodInternal Due to the use of DerivedPageDataUpdater
46 */
47 class WikiPage implements Page, IDBAccessObject {
48 // Constants for $mDataLoadedFrom and related
49
50 /**
51 * @var Title
52 */
53 public $mTitle = null;
54
55 /**
56 * @var bool
57 * @protected
58 */
59 public $mDataLoaded = false;
60
61 /**
62 * @var bool
63 * @protected
64 */
65 public $mIsRedirect = false;
66
67 /**
68 * @var int|false False means "not loaded"
69 * @protected
70 */
71 public $mLatest = false;
72
73 /**
74 * @var PreparedEdit|false Map of cache fields (text, parser output, ect) for a proposed/new edit
75 */
76 public $mPreparedEdit = false;
77
78 /**
79 * @var int
80 */
81 protected $mId = null;
82
83 /**
84 * @var int One of the READ_* constants
85 */
86 protected $mDataLoadedFrom = self::READ_NONE;
87
88 /**
89 * @var Title
90 */
91 protected $mRedirectTarget = null;
92
93 /**
94 * @var Revision
95 */
96 protected $mLastRevision = null;
97
98 /**
99 * @var string Timestamp of the current revision or empty string if not loaded
100 */
101 protected $mTimestamp = '';
102
103 /**
104 * @var string
105 */
106 protected $mTouched = '19700101000000';
107
108 /**
109 * @var string
110 */
111 protected $mLinksUpdated = '19700101000000';
112
113 /**
114 * @var DerivedPageDataUpdater|null
115 */
116 private $derivedDataUpdater = null;
117
118 /**
119 * Constructor and clear the article
120 * @param Title $title Reference to a Title object.
121 */
122 public function __construct( Title $title ) {
123 $this->mTitle = $title;
124 }
125
126 /**
127 * Makes sure that the mTitle object is cloned
128 * to the newly cloned WikiPage.
129 */
130 public function __clone() {
131 $this->mTitle = clone $this->mTitle;
132 }
133
134 /**
135 * Create a WikiPage object of the appropriate class for the given title.
136 *
137 * @param Title $title
138 *
139 * @throws MWException
140 * @return WikiPage|WikiCategoryPage|WikiFilePage
141 */
142 public static function factory( Title $title ) {
143 $ns = $title->getNamespace();
144
145 if ( $ns == NS_MEDIA ) {
146 throw new MWException( "NS_MEDIA is a virtual namespace; use NS_FILE." );
147 } elseif ( $ns < 0 ) {
148 throw new MWException( "Invalid or virtual namespace $ns given." );
149 }
150
151 $page = null;
152 if ( !Hooks::run( 'WikiPageFactory', [ $title, &$page ] ) ) {
153 return $page;
154 }
155
156 switch ( $ns ) {
157 case NS_FILE:
158 $page = new WikiFilePage( $title );
159 break;
160 case NS_CATEGORY:
161 $page = new WikiCategoryPage( $title );
162 break;
163 default:
164 $page = new WikiPage( $title );
165 }
166
167 return $page;
168 }
169
170 /**
171 * Constructor from a page id
172 *
173 * @param int $id Article ID to load
174 * @param string|int $from One of the following values:
175 * - "fromdb" or WikiPage::READ_NORMAL to select from a replica DB
176 * - "fromdbmaster" or WikiPage::READ_LATEST to select from the master database
177 *
178 * @return WikiPage|null
179 */
180 public static function newFromID( $id, $from = 'fromdb' ) {
181 // page ids are never 0 or negative, see T63166
182 if ( $id < 1 ) {
183 return null;
184 }
185
186 $from = self::convertSelectType( $from );
187 $db = wfGetDB( $from === self::READ_LATEST ? DB_MASTER : DB_REPLICA );
188 $pageQuery = self::getQueryInfo();
189 $row = $db->selectRow(
190 $pageQuery['tables'], $pageQuery['fields'], [ 'page_id' => $id ], __METHOD__,
191 [], $pageQuery['joins']
192 );
193 if ( !$row ) {
194 return null;
195 }
196 return self::newFromRow( $row, $from );
197 }
198
199 /**
200 * Constructor from a database row
201 *
202 * @since 1.20
203 * @param object $row Database row containing at least fields returned by selectFields().
204 * @param string|int $from Source of $data:
205 * - "fromdb" or WikiPage::READ_NORMAL: from a replica DB
206 * - "fromdbmaster" or WikiPage::READ_LATEST: from the master DB
207 * - "forupdate" or WikiPage::READ_LOCKING: from the master DB using SELECT FOR UPDATE
208 * @return WikiPage
209 */
210 public static function newFromRow( $row, $from = 'fromdb' ) {
211 $page = self::factory( Title::newFromRow( $row ) );
212 $page->loadFromRow( $row, $from );
213 return $page;
214 }
215
216 /**
217 * Convert 'fromdb', 'fromdbmaster' and 'forupdate' to READ_* constants.
218 *
219 * @param object|string|int $type
220 * @return mixed
221 */
222 protected static function convertSelectType( $type ) {
223 switch ( $type ) {
224 case 'fromdb':
225 return self::READ_NORMAL;
226 case 'fromdbmaster':
227 return self::READ_LATEST;
228 case 'forupdate':
229 return self::READ_LOCKING;
230 default:
231 // It may already be an integer or whatever else
232 return $type;
233 }
234 }
235
236 /**
237 * @return RevisionStore
238 */
239 private function getRevisionStore() {
240 return MediaWikiServices::getInstance()->getRevisionStore();
241 }
242
243 /**
244 * @return RevisionRenderer
245 */
246 private function getRevisionRenderer() {
247 return MediaWikiServices::getInstance()->getRevisionRenderer();
248 }
249
250 /**
251 * @return SlotRoleRegistry
252 */
253 private function getSlotRoleRegistry() {
254 return MediaWikiServices::getInstance()->getSlotRoleRegistry();
255 }
256
257 /**
258 * @return ParserCache
259 */
260 private function getParserCache() {
261 return MediaWikiServices::getInstance()->getParserCache();
262 }
263
264 /**
265 * @return LoadBalancer
266 */
267 private function getDBLoadBalancer() {
268 return MediaWikiServices::getInstance()->getDBLoadBalancer();
269 }
270
271 /**
272 * @todo Move this UI stuff somewhere else
273 *
274 * @see ContentHandler::getActionOverrides
275 * @return array
276 */
277 public function getActionOverrides() {
278 return $this->getContentHandler()->getActionOverrides();
279 }
280
281 /**
282 * Returns the ContentHandler instance to be used to deal with the content of this WikiPage.
283 *
284 * Shorthand for ContentHandler::getForModelID( $this->getContentModel() );
285 *
286 * @return ContentHandler
287 *
288 * @since 1.21
289 */
290 public function getContentHandler() {
291 return ContentHandler::getForModelID( $this->getContentModel() );
292 }
293
294 /**
295 * Get the title object of the article
296 * @return Title Title object of this page
297 */
298 public function getTitle() {
299 return $this->mTitle;
300 }
301
302 /**
303 * Clear the object
304 * @return void
305 */
306 public function clear() {
307 $this->mDataLoaded = false;
308 $this->mDataLoadedFrom = self::READ_NONE;
309
310 $this->clearCacheFields();
311 }
312
313 /**
314 * Clear the object cache fields
315 * @return void
316 */
317 protected function clearCacheFields() {
318 $this->mId = null;
319 $this->mRedirectTarget = null; // Title object if set
320 $this->mLastRevision = null; // Latest revision
321 $this->mTouched = '19700101000000';
322 $this->mLinksUpdated = '19700101000000';
323 $this->mTimestamp = '';
324 $this->mIsRedirect = false;
325 $this->mLatest = false;
326 // T59026: do not clear $this->derivedDataUpdater since getDerivedDataUpdater() already
327 // checks the requested rev ID and content against the cached one. For most
328 // content types, the output should not change during the lifetime of this cache.
329 // Clearing it can cause extra parses on edit for no reason.
330 }
331
332 /**
333 * Clear the mPreparedEdit cache field, as may be needed by mutable content types
334 * @return void
335 * @since 1.23
336 */
337 public function clearPreparedEdit() {
338 $this->mPreparedEdit = false;
339 }
340
341 /**
342 * Return the list of revision fields that should be selected to create
343 * a new page.
344 *
345 * @deprecated since 1.31, use self::getQueryInfo() instead.
346 * @return array
347 */
348 public static function selectFields() {
349 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
350
351 wfDeprecated( __METHOD__, '1.31' );
352
353 $fields = [
354 'page_id',
355 'page_namespace',
356 'page_title',
357 'page_restrictions',
358 'page_is_redirect',
359 'page_is_new',
360 'page_random',
361 'page_touched',
362 'page_links_updated',
363 'page_latest',
364 'page_len',
365 ];
366
367 if ( $wgContentHandlerUseDB ) {
368 $fields[] = 'page_content_model';
369 }
370
371 if ( $wgPageLanguageUseDB ) {
372 $fields[] = 'page_lang';
373 }
374
375 return $fields;
376 }
377
378 /**
379 * Return the tables, fields, and join conditions to be selected to create
380 * a new page object.
381 * @since 1.31
382 * @return array With three keys:
383 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
384 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
385 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
386 */
387 public static function getQueryInfo() {
388 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
389
390 $ret = [
391 'tables' => [ 'page' ],
392 'fields' => [
393 'page_id',
394 'page_namespace',
395 'page_title',
396 'page_restrictions',
397 'page_is_redirect',
398 'page_is_new',
399 'page_random',
400 'page_touched',
401 'page_links_updated',
402 'page_latest',
403 'page_len',
404 ],
405 'joins' => [],
406 ];
407
408 if ( $wgContentHandlerUseDB ) {
409 $ret['fields'][] = 'page_content_model';
410 }
411
412 if ( $wgPageLanguageUseDB ) {
413 $ret['fields'][] = 'page_lang';
414 }
415
416 return $ret;
417 }
418
419 /**
420 * Fetch a page record with the given conditions
421 * @param IDatabase $dbr
422 * @param array $conditions
423 * @param array $options
424 * @return object|bool Database result resource, or false on failure
425 */
426 protected function pageData( $dbr, $conditions, $options = [] ) {
427 $pageQuery = self::getQueryInfo();
428
429 // Avoid PHP 7.1 warning of passing $this by reference
430 $wikiPage = $this;
431
432 Hooks::run( 'ArticlePageDataBefore', [
433 &$wikiPage, &$pageQuery['fields'], &$pageQuery['tables'], &$pageQuery['joins']
434 ] );
435
436 $row = $dbr->selectRow(
437 $pageQuery['tables'],
438 $pageQuery['fields'],
439 $conditions,
440 __METHOD__,
441 $options,
442 $pageQuery['joins']
443 );
444
445 Hooks::run( 'ArticlePageDataAfter', [ &$wikiPage, &$row ] );
446
447 return $row;
448 }
449
450 /**
451 * Fetch a page record matching the Title object's namespace and title
452 * using a sanitized title string
453 *
454 * @param IDatabase $dbr
455 * @param Title $title
456 * @param array $options
457 * @return object|bool Database result resource, or false on failure
458 */
459 public function pageDataFromTitle( $dbr, $title, $options = [] ) {
460 return $this->pageData( $dbr, [
461 'page_namespace' => $title->getNamespace(),
462 'page_title' => $title->getDBkey() ], $options );
463 }
464
465 /**
466 * Fetch a page record matching the requested ID
467 *
468 * @param IDatabase $dbr
469 * @param int $id
470 * @param array $options
471 * @return object|bool Database result resource, or false on failure
472 */
473 public function pageDataFromId( $dbr, $id, $options = [] ) {
474 return $this->pageData( $dbr, [ 'page_id' => $id ], $options );
475 }
476
477 /**
478 * Load the object from a given source by title
479 *
480 * @param object|string|int $from One of the following:
481 * - A DB query result object.
482 * - "fromdb" or WikiPage::READ_NORMAL to get from a replica DB.
483 * - "fromdbmaster" or WikiPage::READ_LATEST to get from the master DB.
484 * - "forupdate" or WikiPage::READ_LOCKING to get from the master DB
485 * using SELECT FOR UPDATE.
486 *
487 * @return void
488 */
489 public function loadPageData( $from = 'fromdb' ) {
490 $from = self::convertSelectType( $from );
491 if ( is_int( $from ) && $from <= $this->mDataLoadedFrom ) {
492 // We already have the data from the correct location, no need to load it twice.
493 return;
494 }
495
496 if ( is_int( $from ) ) {
497 list( $index, $opts ) = DBAccessObjectUtils::getDBOptions( $from );
498 $loadBalancer = $this->getDBLoadBalancer();
499 $db = $loadBalancer->getConnection( $index );
500 $data = $this->pageDataFromTitle( $db, $this->mTitle, $opts );
501
502 if ( !$data
503 && $index == DB_REPLICA
504 && $loadBalancer->getServerCount() > 1
505 && $loadBalancer->hasOrMadeRecentMasterChanges()
506 ) {
507 $from = self::READ_LATEST;
508 list( $index, $opts ) = DBAccessObjectUtils::getDBOptions( $from );
509 $db = $loadBalancer->getConnection( $index );
510 $data = $this->pageDataFromTitle( $db, $this->mTitle, $opts );
511 }
512 } else {
513 // No idea from where the caller got this data, assume replica DB.
514 $data = $from;
515 $from = self::READ_NORMAL;
516 }
517
518 $this->loadFromRow( $data, $from );
519 }
520
521 /**
522 * Checks whether the page data was loaded using the given database access mode (or better).
523 *
524 * @since 1.32
525 *
526 * @param string|int $from One of the following:
527 * - "fromdb" or WikiPage::READ_NORMAL to get from a replica DB.
528 * - "fromdbmaster" or WikiPage::READ_LATEST to get from the master DB.
529 * - "forupdate" or WikiPage::READ_LOCKING to get from the master DB
530 * using SELECT FOR UPDATE.
531 *
532 * @return bool
533 */
534 public function wasLoadedFrom( $from ) {
535 $from = self::convertSelectType( $from );
536
537 if ( !is_int( $from ) ) {
538 // No idea from where the caller got this data, assume replica DB.
539 $from = self::READ_NORMAL;
540 }
541
542 if ( is_int( $from ) && $from <= $this->mDataLoadedFrom ) {
543 return true;
544 }
545
546 return false;
547 }
548
549 /**
550 * Load the object from a database row
551 *
552 * @since 1.20
553 * @param object|bool $data DB row containing fields returned by selectFields() or false
554 * @param string|int $from One of the following:
555 * - "fromdb" or WikiPage::READ_NORMAL if the data comes from a replica DB
556 * - "fromdbmaster" or WikiPage::READ_LATEST if the data comes from the master DB
557 * - "forupdate" or WikiPage::READ_LOCKING if the data comes from
558 * the master DB using SELECT FOR UPDATE
559 */
560 public function loadFromRow( $data, $from ) {
561 $lc = MediaWikiServices::getInstance()->getLinkCache();
562 $lc->clearLink( $this->mTitle );
563
564 if ( $data ) {
565 $lc->addGoodLinkObjFromRow( $this->mTitle, $data );
566
567 $this->mTitle->loadFromRow( $data );
568
569 // Old-fashioned restrictions
570 $this->mTitle->loadRestrictions( $data->page_restrictions );
571
572 $this->mId = intval( $data->page_id );
573 $this->mTouched = wfTimestamp( TS_MW, $data->page_touched );
574 $this->mLinksUpdated = wfTimestampOrNull( TS_MW, $data->page_links_updated );
575 $this->mIsRedirect = intval( $data->page_is_redirect );
576 $this->mLatest = intval( $data->page_latest );
577 // T39225: $latest may no longer match the cached latest Revision object.
578 // Double-check the ID of any cached latest Revision object for consistency.
579 if ( $this->mLastRevision && $this->mLastRevision->getId() != $this->mLatest ) {
580 $this->mLastRevision = null;
581 $this->mTimestamp = '';
582 }
583 } else {
584 $lc->addBadLinkObj( $this->mTitle );
585
586 $this->mTitle->loadFromRow( false );
587
588 $this->clearCacheFields();
589
590 $this->mId = 0;
591 }
592
593 $this->mDataLoaded = true;
594 $this->mDataLoadedFrom = self::convertSelectType( $from );
595 }
596
597 /**
598 * @return int Page ID
599 */
600 public function getId() {
601 if ( !$this->mDataLoaded ) {
602 $this->loadPageData();
603 }
604 return $this->mId;
605 }
606
607 /**
608 * @return bool Whether or not the page exists in the database
609 */
610 public function exists() {
611 if ( !$this->mDataLoaded ) {
612 $this->loadPageData();
613 }
614 return $this->mId > 0;
615 }
616
617 /**
618 * Check if this page is something we're going to be showing
619 * some sort of sensible content for. If we return false, page
620 * views (plain action=view) will return an HTTP 404 response,
621 * so spiders and robots can know they're following a bad link.
622 *
623 * @return bool
624 */
625 public function hasViewableContent() {
626 return $this->mTitle->isKnown();
627 }
628
629 /**
630 * Tests if the article content represents a redirect
631 *
632 * @return bool
633 */
634 public function isRedirect() {
635 if ( !$this->mDataLoaded ) {
636 $this->loadPageData();
637 }
638
639 return (bool)$this->mIsRedirect;
640 }
641
642 /**
643 * Returns the page's content model id (see the CONTENT_MODEL_XXX constants).
644 *
645 * Will use the revisions actual content model if the page exists,
646 * and the page's default if the page doesn't exist yet.
647 *
648 * @return string
649 *
650 * @since 1.21
651 */
652 public function getContentModel() {
653 if ( $this->exists() ) {
654 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
655
656 return $cache->getWithSetCallback(
657 $cache->makeKey( 'page-content-model', $this->getLatest() ),
658 $cache::TTL_MONTH,
659 function () {
660 $rev = $this->getRevision();
661 if ( $rev ) {
662 // Look at the revision's actual content model
663 return $rev->getContentModel();
664 } else {
665 $title = $this->mTitle->getPrefixedDBkey();
666 wfWarn( "Page $title exists but has no (visible) revisions!" );
667 return $this->mTitle->getContentModel();
668 }
669 }
670 );
671 }
672
673 // use the default model for this page
674 return $this->mTitle->getContentModel();
675 }
676
677 /**
678 * Loads page_touched and returns a value indicating if it should be used
679 * @return bool True if this page exists and is not a redirect
680 */
681 public function checkTouched() {
682 if ( !$this->mDataLoaded ) {
683 $this->loadPageData();
684 }
685 return ( $this->mId && !$this->mIsRedirect );
686 }
687
688 /**
689 * Get the page_touched field
690 * @return string Containing GMT timestamp
691 */
692 public function getTouched() {
693 if ( !$this->mDataLoaded ) {
694 $this->loadPageData();
695 }
696 return $this->mTouched;
697 }
698
699 /**
700 * Get the page_links_updated field
701 * @return string|null Containing GMT timestamp
702 */
703 public function getLinksTimestamp() {
704 if ( !$this->mDataLoaded ) {
705 $this->loadPageData();
706 }
707 return $this->mLinksUpdated;
708 }
709
710 /**
711 * Get the page_latest field
712 * @return int The rev_id of current revision
713 */
714 public function getLatest() {
715 if ( !$this->mDataLoaded ) {
716 $this->loadPageData();
717 }
718 return (int)$this->mLatest;
719 }
720
721 /**
722 * Get the Revision object of the oldest revision
723 * @return Revision|null
724 */
725 public function getOldestRevision() {
726 // Try using the replica DB first, then try the master
727 $rev = $this->mTitle->getFirstRevision();
728 if ( !$rev ) {
729 $rev = $this->mTitle->getFirstRevision( Title::READ_LATEST );
730 }
731 return $rev;
732 }
733
734 /**
735 * Loads everything except the text
736 * This isn't necessary for all uses, so it's only done if needed.
737 */
738 protected function loadLastEdit() {
739 if ( $this->mLastRevision !== null ) {
740 return; // already loaded
741 }
742
743 $latest = $this->getLatest();
744 if ( !$latest ) {
745 return; // page doesn't exist or is missing page_latest info
746 }
747
748 if ( $this->mDataLoadedFrom == self::READ_LOCKING ) {
749 // T39225: if session S1 loads the page row FOR UPDATE, the result always
750 // includes the latest changes committed. This is true even within REPEATABLE-READ
751 // transactions, where S1 normally only sees changes committed before the first S1
752 // SELECT. Thus we need S1 to also gets the revision row FOR UPDATE; otherwise, it
753 // may not find it since a page row UPDATE and revision row INSERT by S2 may have
754 // happened after the first S1 SELECT.
755 // https://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read
756 $flags = Revision::READ_LOCKING;
757 $revision = Revision::newFromPageId( $this->getId(), $latest, $flags );
758 } elseif ( $this->mDataLoadedFrom == self::READ_LATEST ) {
759 // Bug T93976: if page_latest was loaded from the master, fetch the
760 // revision from there as well, as it may not exist yet on a replica DB.
761 // Also, this keeps the queries in the same REPEATABLE-READ snapshot.
762 $flags = Revision::READ_LATEST;
763 $revision = Revision::newFromPageId( $this->getId(), $latest, $flags );
764 } else {
765 $dbr = wfGetDB( DB_REPLICA );
766 $revision = Revision::newKnownCurrent( $dbr, $this->getTitle(), $latest );
767 }
768
769 if ( $revision ) { // sanity
770 $this->setLastEdit( $revision );
771 }
772 }
773
774 /**
775 * Set the latest revision
776 * @param Revision $revision
777 */
778 protected function setLastEdit( Revision $revision ) {
779 $this->mLastRevision = $revision;
780 $this->mTimestamp = $revision->getTimestamp();
781 }
782
783 /**
784 * Get the latest revision
785 * @return Revision|null
786 */
787 public function getRevision() {
788 $this->loadLastEdit();
789 if ( $this->mLastRevision ) {
790 return $this->mLastRevision;
791 }
792 return null;
793 }
794
795 /**
796 * Get the latest revision
797 * @return RevisionRecord|null
798 */
799 public function getRevisionRecord() {
800 $this->loadLastEdit();
801 if ( $this->mLastRevision ) {
802 return $this->mLastRevision->getRevisionRecord();
803 }
804 return null;
805 }
806
807 /**
808 * Get the content of the current revision. No side-effects...
809 *
810 * @param int $audience One of:
811 * Revision::FOR_PUBLIC to be displayed to all users
812 * Revision::FOR_THIS_USER to be displayed to $wgUser
813 * Revision::RAW get the text regardless of permissions
814 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
815 * to the $audience parameter
816 * @return Content|null The content of the current revision
817 *
818 * @since 1.21
819 */
820 public function getContent( $audience = RevisionRecord::FOR_PUBLIC, User $user = null ) {
821 $this->loadLastEdit();
822 if ( $this->mLastRevision ) {
823 return $this->mLastRevision->getContent( $audience, $user );
824 }
825 return null;
826 }
827
828 /**
829 * @return string MW timestamp of last article revision
830 */
831 public function getTimestamp() {
832 // Check if the field has been filled by WikiPage::setTimestamp()
833 if ( !$this->mTimestamp ) {
834 $this->loadLastEdit();
835 }
836
837 return wfTimestamp( TS_MW, $this->mTimestamp );
838 }
839
840 /**
841 * Set the page timestamp (use only to avoid DB queries)
842 * @param string $ts MW timestamp of last article revision
843 * @return void
844 */
845 public function setTimestamp( $ts ) {
846 $this->mTimestamp = wfTimestamp( TS_MW, $ts );
847 }
848
849 /**
850 * @param int $audience One of:
851 * Revision::FOR_PUBLIC to be displayed to all users
852 * Revision::FOR_THIS_USER to be displayed to the given user
853 * Revision::RAW get the text regardless of permissions
854 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
855 * to the $audience parameter
856 * @return int User ID for the user that made the last article revision
857 */
858 public function getUser( $audience = RevisionRecord::FOR_PUBLIC, User $user = null ) {
859 $this->loadLastEdit();
860 if ( $this->mLastRevision ) {
861 return $this->mLastRevision->getUser( $audience, $user );
862 } else {
863 return -1;
864 }
865 }
866
867 /**
868 * Get the User object of the user who created the page
869 * @param int $audience One of:
870 * Revision::FOR_PUBLIC to be displayed to all users
871 * Revision::FOR_THIS_USER to be displayed to the given user
872 * Revision::RAW get the text regardless of permissions
873 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
874 * to the $audience parameter
875 * @return User|null
876 */
877 public function getCreator( $audience = RevisionRecord::FOR_PUBLIC, User $user = null ) {
878 $revision = $this->getOldestRevision();
879 if ( $revision ) {
880 $userName = $revision->getUserText( $audience, $user );
881 return User::newFromName( $userName, false );
882 } else {
883 return null;
884 }
885 }
886
887 /**
888 * @param int $audience One of:
889 * Revision::FOR_PUBLIC to be displayed to all users
890 * Revision::FOR_THIS_USER to be displayed to the given user
891 * Revision::RAW get the text regardless of permissions
892 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
893 * to the $audience parameter
894 * @return string Username of the user that made the last article revision
895 */
896 public function getUserText( $audience = RevisionRecord::FOR_PUBLIC, User $user = null ) {
897 $this->loadLastEdit();
898 if ( $this->mLastRevision ) {
899 return $this->mLastRevision->getUserText( $audience, $user );
900 } else {
901 return '';
902 }
903 }
904
905 /**
906 * @param int $audience One of:
907 * Revision::FOR_PUBLIC to be displayed to all users
908 * Revision::FOR_THIS_USER to be displayed to the given user
909 * Revision::RAW get the text regardless of permissions
910 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
911 * to the $audience parameter
912 * @return string|null Comment stored for the last article revision, or null if the specified
913 * audience does not have access to the comment.
914 */
915 public function getComment( $audience = RevisionRecord::FOR_PUBLIC, User $user = null ) {
916 $this->loadLastEdit();
917 if ( $this->mLastRevision ) {
918 return $this->mLastRevision->getComment( $audience, $user );
919 } else {
920 return '';
921 }
922 }
923
924 /**
925 * Returns true if last revision was marked as "minor edit"
926 *
927 * @return bool Minor edit indicator for the last article revision.
928 */
929 public function getMinorEdit() {
930 $this->loadLastEdit();
931 if ( $this->mLastRevision ) {
932 return $this->mLastRevision->isMinor();
933 } else {
934 return false;
935 }
936 }
937
938 /**
939 * Determine whether a page would be suitable for being counted as an
940 * article in the site_stats table based on the title & its content
941 *
942 * @param PreparedEdit|bool $editInfo (false): object returned by prepareTextForEdit(),
943 * if false, the current database state will be used
944 * @return bool
945 */
946 public function isCountable( $editInfo = false ) {
947 global $wgArticleCountMethod;
948
949 // NOTE: Keep in sync with DerivedPageDataUpdater::isCountable.
950
951 if ( !$this->mTitle->isContentPage() ) {
952 return false;
953 }
954
955 if ( $editInfo ) {
956 // NOTE: only the main slot can make a page a redirect
957 $content = $editInfo->pstContent;
958 } else {
959 $content = $this->getContent();
960 }
961
962 if ( !$content || $content->isRedirect() ) {
963 return false;
964 }
965
966 $hasLinks = null;
967
968 if ( $wgArticleCountMethod === 'link' ) {
969 // nasty special case to avoid re-parsing to detect links
970
971 if ( $editInfo ) {
972 // ParserOutput::getLinks() is a 2D array of page links, so
973 // to be really correct we would need to recurse in the array
974 // but the main array should only have items in it if there are
975 // links.
976 $hasLinks = (bool)count( $editInfo->output->getLinks() );
977 } else {
978 // NOTE: keep in sync with RevisionRenderer::getLinkCount
979 // NOTE: keep in sync with DerivedPageDataUpdater::isCountable
980 $hasLinks = (bool)wfGetDB( DB_REPLICA )->selectField( 'pagelinks', 1,
981 [ 'pl_from' => $this->getId() ], __METHOD__ );
982 }
983 }
984
985 // TODO: MCR: determine $hasLinks for each slot, and use that info
986 // with that slot's Content's isCountable method. That requires per-
987 // slot ParserOutput in the ParserCache, or per-slot info in the
988 // pagelinks table.
989 return $content->isCountable( $hasLinks );
990 }
991
992 /**
993 * If this page is a redirect, get its target
994 *
995 * The target will be fetched from the redirect table if possible.
996 * If this page doesn't have an entry there, call insertRedirect()
997 * @return Title|null Title object, or null if this page is not a redirect
998 */
999 public function getRedirectTarget() {
1000 if ( !$this->mTitle->isRedirect() ) {
1001 return null;
1002 }
1003
1004 if ( $this->mRedirectTarget !== null ) {
1005 return $this->mRedirectTarget;
1006 }
1007
1008 // Query the redirect table
1009 $dbr = wfGetDB( DB_REPLICA );
1010 $row = $dbr->selectRow( 'redirect',
1011 [ 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1012 [ 'rd_from' => $this->getId() ],
1013 __METHOD__
1014 );
1015
1016 // rd_fragment and rd_interwiki were added later, populate them if empty
1017 if ( $row && !is_null( $row->rd_fragment ) && !is_null( $row->rd_interwiki ) ) {
1018 // (T203942) We can't redirect to Media namespace because it's virtual.
1019 // We don't want to modify Title objects farther down the
1020 // line. So, let's fix this here by changing to File namespace.
1021 if ( $row->rd_namespace == NS_MEDIA ) {
1022 $namespace = NS_FILE;
1023 } else {
1024 $namespace = $row->rd_namespace;
1025 }
1026 $this->mRedirectTarget = Title::makeTitle(
1027 $namespace, $row->rd_title,
1028 $row->rd_fragment, $row->rd_interwiki
1029 );
1030 return $this->mRedirectTarget;
1031 }
1032
1033 // This page doesn't have an entry in the redirect table
1034 $this->mRedirectTarget = $this->insertRedirect();
1035 return $this->mRedirectTarget;
1036 }
1037
1038 /**
1039 * Insert an entry for this page into the redirect table if the content is a redirect
1040 *
1041 * The database update will be deferred via DeferredUpdates
1042 *
1043 * Don't call this function directly unless you know what you're doing.
1044 * @return Title|null Title object or null if not a redirect
1045 */
1046 public function insertRedirect() {
1047 $content = $this->getContent();
1048 $retval = $content ? $content->getUltimateRedirectTarget() : null;
1049 if ( !$retval ) {
1050 return null;
1051 }
1052
1053 // Update the DB post-send if the page has not cached since now
1054 $latest = $this->getLatest();
1055 DeferredUpdates::addCallableUpdate(
1056 function () use ( $retval, $latest ) {
1057 $this->insertRedirectEntry( $retval, $latest );
1058 },
1059 DeferredUpdates::POSTSEND,
1060 wfGetDB( DB_MASTER )
1061 );
1062
1063 return $retval;
1064 }
1065
1066 /**
1067 * Insert or update the redirect table entry for this page to indicate it redirects to $rt
1068 * @param Title $rt Redirect target
1069 * @param int|null $oldLatest Prior page_latest for check and set
1070 * @return bool Success
1071 */
1072 public function insertRedirectEntry( Title $rt, $oldLatest = null ) {
1073 $dbw = wfGetDB( DB_MASTER );
1074 $dbw->startAtomic( __METHOD__ );
1075
1076 if ( !$oldLatest || $oldLatest == $this->lockAndGetLatest() ) {
1077 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
1078 $truncatedFragment = $contLang->truncateForDatabase( $rt->getFragment(), 255 );
1079 $dbw->upsert(
1080 'redirect',
1081 [
1082 'rd_from' => $this->getId(),
1083 'rd_namespace' => $rt->getNamespace(),
1084 'rd_title' => $rt->getDBkey(),
1085 'rd_fragment' => $truncatedFragment,
1086 'rd_interwiki' => $rt->getInterwiki(),
1087 ],
1088 [ 'rd_from' ],
1089 [
1090 'rd_namespace' => $rt->getNamespace(),
1091 'rd_title' => $rt->getDBkey(),
1092 'rd_fragment' => $truncatedFragment,
1093 'rd_interwiki' => $rt->getInterwiki(),
1094 ],
1095 __METHOD__
1096 );
1097 $success = true;
1098 } else {
1099 $success = false;
1100 }
1101
1102 $dbw->endAtomic( __METHOD__ );
1103
1104 return $success;
1105 }
1106
1107 /**
1108 * Get the Title object or URL this page redirects to
1109 *
1110 * @return bool|Title|string False, Title of in-wiki target, or string with URL
1111 */
1112 public function followRedirect() {
1113 return $this->getRedirectURL( $this->getRedirectTarget() );
1114 }
1115
1116 /**
1117 * Get the Title object or URL to use for a redirect. We use Title
1118 * objects for same-wiki, non-special redirects and URLs for everything
1119 * else.
1120 * @param Title $rt Redirect target
1121 * @return bool|Title|string False, Title object of local target, or string with URL
1122 */
1123 public function getRedirectURL( $rt ) {
1124 if ( !$rt ) {
1125 return false;
1126 }
1127
1128 if ( $rt->isExternal() ) {
1129 if ( $rt->isLocal() ) {
1130 // Offsite wikis need an HTTP redirect.
1131 // This can be hard to reverse and may produce loops,
1132 // so they may be disabled in the site configuration.
1133 $source = $this->mTitle->getFullURL( 'redirect=no' );
1134 return $rt->getFullURL( [ 'rdfrom' => $source ] );
1135 } else {
1136 // External pages without "local" bit set are not valid
1137 // redirect targets
1138 return false;
1139 }
1140 }
1141
1142 if ( $rt->isSpecialPage() ) {
1143 // Gotta handle redirects to special pages differently:
1144 // Fill the HTTP response "Location" header and ignore the rest of the page we're on.
1145 // Some pages are not valid targets.
1146 if ( $rt->isValidRedirectTarget() ) {
1147 return $rt->getFullURL();
1148 } else {
1149 return false;
1150 }
1151 }
1152
1153 return $rt;
1154 }
1155
1156 /**
1157 * Get a list of users who have edited this article, not including the user who made
1158 * the most recent revision, which you can get from $article->getUser() if you want it
1159 * @return UserArrayFromResult
1160 */
1161 public function getContributors() {
1162 // @todo: This is expensive; cache this info somewhere.
1163
1164 $dbr = wfGetDB( DB_REPLICA );
1165
1166 $actorMigration = ActorMigration::newMigration();
1167 $actorQuery = $actorMigration->getJoin( 'rev_user' );
1168
1169 $tables = array_merge( [ 'revision' ], $actorQuery['tables'], [ 'user' ] );
1170
1171 $fields = [
1172 'user_id' => $actorQuery['fields']['rev_user'],
1173 'user_name' => $actorQuery['fields']['rev_user_text'],
1174 'actor_id' => $actorQuery['fields']['rev_actor'],
1175 'user_real_name' => 'MIN(user_real_name)',
1176 'timestamp' => 'MAX(rev_timestamp)',
1177 ];
1178
1179 $conds = [ 'rev_page' => $this->getId() ];
1180
1181 // The user who made the top revision gets credited as "this page was last edited by
1182 // John, based on contributions by Tom, Dick and Harry", so don't include them twice.
1183 $user = $this->getUser()
1184 ? User::newFromId( $this->getUser() )
1185 : User::newFromName( $this->getUserText(), false );
1186 $conds[] = 'NOT(' . $actorMigration->getWhere( $dbr, 'rev_user', $user )['conds'] . ')';
1187
1188 // Username hidden?
1189 $conds[] = "{$dbr->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER )} = 0";
1190
1191 $jconds = [
1192 'user' => [ 'LEFT JOIN', $actorQuery['fields']['rev_user'] . ' = user_id' ],
1193 ] + $actorQuery['joins'];
1194
1195 $options = [
1196 'GROUP BY' => [ $fields['user_id'], $fields['user_name'] ],
1197 'ORDER BY' => 'timestamp DESC',
1198 ];
1199
1200 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $jconds );
1201 return new UserArrayFromResult( $res );
1202 }
1203
1204 /**
1205 * Should the parser cache be used?
1206 *
1207 * @param ParserOptions $parserOptions ParserOptions to check
1208 * @param int $oldId
1209 * @return bool
1210 */
1211 public function shouldCheckParserCache( ParserOptions $parserOptions, $oldId ) {
1212 return $parserOptions->getStubThreshold() == 0
1213 && $this->exists()
1214 && ( $oldId === null || $oldId === 0 || $oldId === $this->getLatest() )
1215 && $this->getContentHandler()->isParserCacheSupported();
1216 }
1217
1218 /**
1219 * Get a ParserOutput for the given ParserOptions and revision ID.
1220 *
1221 * The parser cache will be used if possible. Cache misses that result
1222 * in parser runs are debounced with PoolCounter.
1223 *
1224 * XXX merge this with updateParserCache()?
1225 *
1226 * @since 1.19
1227 * @param ParserOptions $parserOptions ParserOptions to use for the parse operation
1228 * @param null|int $oldid Revision ID to get the text from, passing null or 0 will
1229 * get the current revision (default value)
1230 * @param bool $forceParse Force reindexing, regardless of cache settings
1231 * @return bool|ParserOutput ParserOutput or false if the revision was not found
1232 */
1233 public function getParserOutput(
1234 ParserOptions $parserOptions, $oldid = null, $forceParse = false
1235 ) {
1236 $useParserCache =
1237 ( !$forceParse ) && $this->shouldCheckParserCache( $parserOptions, $oldid );
1238
1239 if ( $useParserCache && !$parserOptions->isSafeToCache() ) {
1240 throw new InvalidArgumentException(
1241 'The supplied ParserOptions are not safe to cache. Fix the options or set $forceParse = true.'
1242 );
1243 }
1244
1245 wfDebug( __METHOD__ .
1246 ': using parser cache: ' . ( $useParserCache ? 'yes' : 'no' ) . "\n" );
1247 if ( $parserOptions->getStubThreshold() ) {
1248 wfIncrStats( 'pcache.miss.stub' );
1249 }
1250
1251 if ( $useParserCache ) {
1252 $parserOutput = $this->getParserCache()
1253 ->get( $this, $parserOptions );
1254 if ( $parserOutput !== false ) {
1255 return $parserOutput;
1256 }
1257 }
1258
1259 if ( $oldid === null || $oldid === 0 ) {
1260 $oldid = $this->getLatest();
1261 }
1262
1263 $pool = new PoolWorkArticleView( $this, $parserOptions, $oldid, $useParserCache );
1264 $pool->execute();
1265
1266 return $pool->getParserOutput();
1267 }
1268
1269 /**
1270 * Do standard deferred updates after page view (existing or missing page)
1271 * @param User $user The relevant user
1272 * @param int $oldid Revision id being viewed; if not given or 0, latest revision is assumed
1273 */
1274 public function doViewUpdates( User $user, $oldid = 0 ) {
1275 if ( wfReadOnly() ) {
1276 return;
1277 }
1278
1279 // Update newtalk / watchlist notification status;
1280 // Avoid outage if the master is not reachable by using a deferred updated
1281 DeferredUpdates::addCallableUpdate(
1282 function () use ( $user, $oldid ) {
1283 Hooks::run( 'PageViewUpdates', [ $this, $user ] );
1284
1285 $user->clearNotification( $this->mTitle, $oldid );
1286 },
1287 DeferredUpdates::PRESEND
1288 );
1289 }
1290
1291 /**
1292 * Perform the actions of a page purging
1293 * @return bool
1294 * @note In 1.28 (and only 1.28), this took a $flags parameter that
1295 * controlled how much purging was done.
1296 */
1297 public function doPurge() {
1298 // Avoid PHP 7.1 warning of passing $this by reference
1299 $wikiPage = $this;
1300
1301 if ( !Hooks::run( 'ArticlePurge', [ &$wikiPage ] ) ) {
1302 return false;
1303 }
1304
1305 $this->mTitle->invalidateCache();
1306
1307 // Clear file cache
1308 HTMLFileCache::clearFileCache( $this->getTitle() );
1309 // Send purge after above page_touched update was committed
1310 DeferredUpdates::addUpdate(
1311 new CdnCacheUpdate( $this->mTitle->getCdnUrls() ),
1312 DeferredUpdates::PRESEND
1313 );
1314
1315 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
1316 $messageCache = MessageCache::singleton();
1317 $messageCache->updateMessageOverride( $this->mTitle, $this->getContent() );
1318 }
1319
1320 return true;
1321 }
1322
1323 /**
1324 * Insert a new empty page record for this article.
1325 * This *must* be followed up by creating a revision
1326 * and running $this->updateRevisionOn( ... );
1327 * or else the record will be left in a funky state.
1328 * Best if all done inside a transaction.
1329 *
1330 * @todo Factor out into a PageStore service, to be used by PageUpdater.
1331 *
1332 * @param IDatabase $dbw
1333 * @param int|null $pageId Custom page ID that will be used for the insert statement
1334 *
1335 * @return bool|int The newly created page_id key; false if the row was not
1336 * inserted, e.g. because the title already existed or because the specified
1337 * page ID is already in use.
1338 */
1339 public function insertOn( $dbw, $pageId = null ) {
1340 $pageIdForInsert = $pageId ? [ 'page_id' => $pageId ] : [];
1341 $dbw->insert(
1342 'page',
1343 [
1344 'page_namespace' => $this->mTitle->getNamespace(),
1345 'page_title' => $this->mTitle->getDBkey(),
1346 'page_restrictions' => '',
1347 'page_is_redirect' => 0, // Will set this shortly...
1348 'page_is_new' => 1,
1349 'page_random' => wfRandom(),
1350 'page_touched' => $dbw->timestamp(),
1351 'page_latest' => 0, // Fill this in shortly...
1352 'page_len' => 0, // Fill this in shortly...
1353 ] + $pageIdForInsert,
1354 __METHOD__,
1355 [ 'IGNORE' ]
1356 );
1357
1358 if ( $dbw->affectedRows() > 0 ) {
1359 $newid = $pageId ? (int)$pageId : $dbw->insertId();
1360 $this->mId = $newid;
1361 $this->mTitle->resetArticleID( $newid );
1362
1363 return $newid;
1364 } else {
1365 return false; // nothing changed
1366 }
1367 }
1368
1369 /**
1370 * Update the page record to point to a newly saved revision.
1371 *
1372 * @todo Factor out into a PageStore service, or move into PageUpdater.
1373 *
1374 * @param IDatabase $dbw
1375 * @param Revision $revision For ID number, and text used to set
1376 * length and redirect status fields
1377 * @param int|null $lastRevision If given, will not overwrite the page field
1378 * when different from the currently set value.
1379 * Giving 0 indicates the new page flag should be set on.
1380 * @param bool|null $lastRevIsRedirect If given, will optimize adding and
1381 * removing rows in redirect table.
1382 * @return bool Success; false if the page row was missing or page_latest changed
1383 */
1384 public function updateRevisionOn( $dbw, $revision, $lastRevision = null,
1385 $lastRevIsRedirect = null
1386 ) {
1387 global $wgContentHandlerUseDB;
1388
1389 // TODO: move into PageUpdater or PageStore
1390 // NOTE: when doing that, make sure cached fields get reset in doEditContent,
1391 // and in the compat stub!
1392
1393 // Assertion to try to catch T92046
1394 if ( (int)$revision->getId() === 0 ) {
1395 throw new InvalidArgumentException(
1396 __METHOD__ . ': Revision has ID ' . var_export( $revision->getId(), 1 )
1397 );
1398 }
1399
1400 $content = $revision->getContent();
1401 $len = $content ? $content->getSize() : 0;
1402 $rt = $content ? $content->getUltimateRedirectTarget() : null;
1403
1404 $conditions = [ 'page_id' => $this->getId() ];
1405
1406 if ( !is_null( $lastRevision ) ) {
1407 // An extra check against threads stepping on each other
1408 $conditions['page_latest'] = $lastRevision;
1409 }
1410
1411 $revId = $revision->getId();
1412 Assert::parameter( $revId > 0, '$revision->getId()', 'must be > 0' );
1413
1414 $row = [ /* SET */
1415 'page_latest' => $revId,
1416 'page_touched' => $dbw->timestamp( $revision->getTimestamp() ),
1417 'page_is_new' => ( $lastRevision === 0 ) ? 1 : 0,
1418 'page_is_redirect' => $rt !== null ? 1 : 0,
1419 'page_len' => $len,
1420 ];
1421
1422 if ( $wgContentHandlerUseDB ) {
1423 $row['page_content_model'] = $revision->getContentModel();
1424 }
1425
1426 $dbw->update( 'page',
1427 $row,
1428 $conditions,
1429 __METHOD__ );
1430
1431 $result = $dbw->affectedRows() > 0;
1432 if ( $result ) {
1433 $this->updateRedirectOn( $dbw, $rt, $lastRevIsRedirect );
1434 $this->setLastEdit( $revision );
1435 $this->mLatest = $revision->getId();
1436 $this->mIsRedirect = (bool)$rt;
1437 // Update the LinkCache.
1438 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
1439 $linkCache->addGoodLinkObj(
1440 $this->getId(),
1441 $this->mTitle,
1442 $len,
1443 $this->mIsRedirect,
1444 $this->mLatest,
1445 $revision->getContentModel()
1446 );
1447 }
1448
1449 return $result;
1450 }
1451
1452 /**
1453 * Add row to the redirect table if this is a redirect, remove otherwise.
1454 *
1455 * @param IDatabase $dbw
1456 * @param Title|null $redirectTitle Title object pointing to the redirect target,
1457 * or NULL if this is not a redirect
1458 * @param null|bool $lastRevIsRedirect If given, will optimize adding and
1459 * removing rows in redirect table.
1460 * @return bool True on success, false on failure
1461 * @private
1462 */
1463 public function updateRedirectOn( $dbw, $redirectTitle, $lastRevIsRedirect = null ) {
1464 // Always update redirects (target link might have changed)
1465 // Update/Insert if we don't know if the last revision was a redirect or not
1466 // Delete if changing from redirect to non-redirect
1467 $isRedirect = !is_null( $redirectTitle );
1468
1469 if ( !$isRedirect && $lastRevIsRedirect === false ) {
1470 return true;
1471 }
1472
1473 if ( $isRedirect ) {
1474 $success = $this->insertRedirectEntry( $redirectTitle );
1475 } else {
1476 // This is not a redirect, remove row from redirect table
1477 $where = [ 'rd_from' => $this->getId() ];
1478 $dbw->delete( 'redirect', $where, __METHOD__ );
1479 $success = true;
1480 }
1481
1482 if ( $this->getTitle()->getNamespace() == NS_FILE ) {
1483 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $this->getTitle() );
1484 }
1485
1486 return $success;
1487 }
1488
1489 /**
1490 * If the given revision is newer than the currently set page_latest,
1491 * update the page record. Otherwise, do nothing.
1492 *
1493 * @deprecated since 1.24, use updateRevisionOn instead
1494 *
1495 * @param IDatabase $dbw
1496 * @param Revision $revision
1497 * @return bool
1498 */
1499 public function updateIfNewerOn( $dbw, $revision ) {
1500 $row = $dbw->selectRow(
1501 [ 'revision', 'page' ],
1502 [ 'rev_id', 'rev_timestamp', 'page_is_redirect' ],
1503 [
1504 'page_id' => $this->getId(),
1505 'page_latest=rev_id' ],
1506 __METHOD__ );
1507
1508 if ( $row ) {
1509 if ( wfTimestamp( TS_MW, $row->rev_timestamp ) >= $revision->getTimestamp() ) {
1510 return false;
1511 }
1512 $prev = $row->rev_id;
1513 $lastRevIsRedirect = (bool)$row->page_is_redirect;
1514 } else {
1515 // No or missing previous revision; mark the page as new
1516 $prev = 0;
1517 $lastRevIsRedirect = null;
1518 }
1519
1520 $ret = $this->updateRevisionOn( $dbw, $revision, $prev, $lastRevIsRedirect );
1521
1522 return $ret;
1523 }
1524
1525 /**
1526 * Helper method for checking whether two revisions have differences that go
1527 * beyond the main slot.
1528 *
1529 * MCR migration note: this method should go away!
1530 *
1531 * @deprecated Use only as a stop-gap before refactoring to support MCR.
1532 *
1533 * @param Revision $a
1534 * @param Revision $b
1535 * @return bool
1536 */
1537 public static function hasDifferencesOutsideMainSlot( Revision $a, Revision $b ) {
1538 $aSlots = $a->getRevisionRecord()->getSlots();
1539 $bSlots = $b->getRevisionRecord()->getSlots();
1540 $changedRoles = $aSlots->getRolesWithDifferentContent( $bSlots );
1541
1542 return ( $changedRoles !== [ SlotRecord::MAIN ] && $changedRoles !== [] );
1543 }
1544
1545 /**
1546 * Get the content that needs to be saved in order to undo all revisions
1547 * between $undo and $undoafter. Revisions must belong to the same page,
1548 * must exist and must not be deleted
1549 *
1550 * @param Revision $undo
1551 * @param Revision $undoafter Must be an earlier revision than $undo
1552 * @return Content|bool Content on success, false on failure
1553 * @since 1.21
1554 * Before we had the Content object, this was done in getUndoText
1555 */
1556 public function getUndoContent( Revision $undo, Revision $undoafter ) {
1557 // TODO: MCR: replace this with a method that returns a RevisionSlotsUpdate
1558
1559 if ( self::hasDifferencesOutsideMainSlot( $undo, $undoafter ) ) {
1560 // Cannot yet undo edits that involve anything other the main slot.
1561 return false;
1562 }
1563
1564 $handler = $undo->getContentHandler();
1565 return $handler->getUndoContent( $this->getRevision(), $undo, $undoafter );
1566 }
1567
1568 /**
1569 * Returns true if this page's content model supports sections.
1570 *
1571 * @return bool
1572 *
1573 * @todo The skin should check this and not offer section functionality if
1574 * sections are not supported.
1575 * @todo The EditPage should check this and not offer section functionality
1576 * if sections are not supported.
1577 */
1578 public function supportsSections() {
1579 return $this->getContentHandler()->supportsSections();
1580 }
1581
1582 /**
1583 * @param string|int|null|bool $sectionId Section identifier as a number or string
1584 * (e.g. 0, 1 or 'T-1'), null/false or an empty string for the whole page
1585 * or 'new' for a new section.
1586 * @param Content $sectionContent New content of the section.
1587 * @param string $sectionTitle New section's subject, only if $section is "new".
1588 * @param string $edittime Revision timestamp or null to use the current revision.
1589 *
1590 * @throws MWException
1591 * @return Content|null New complete article content, or null if error.
1592 *
1593 * @since 1.21
1594 * @deprecated since 1.24, use replaceSectionAtRev instead
1595 */
1596 public function replaceSectionContent(
1597 $sectionId, Content $sectionContent, $sectionTitle = '', $edittime = null
1598 ) {
1599 $baseRevId = null;
1600 if ( $edittime && $sectionId !== 'new' ) {
1601 $lb = $this->getDBLoadBalancer();
1602 $dbr = $lb->getConnectionRef( DB_REPLICA );
1603 $rev = Revision::loadFromTimestamp( $dbr, $this->mTitle, $edittime );
1604 // Try the master if this thread may have just added it.
1605 // This could be abstracted into a Revision method, but we don't want
1606 // to encourage loading of revisions by timestamp.
1607 if ( !$rev
1608 && $lb->getServerCount() > 1
1609 && $lb->hasOrMadeRecentMasterChanges()
1610 ) {
1611 $dbw = $lb->getConnectionRef( DB_MASTER );
1612 $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
1613 }
1614 if ( $rev ) {
1615 $baseRevId = $rev->getId();
1616 }
1617 }
1618
1619 return $this->replaceSectionAtRev( $sectionId, $sectionContent, $sectionTitle, $baseRevId );
1620 }
1621
1622 /**
1623 * @param string|int|null|bool $sectionId Section identifier as a number or string
1624 * (e.g. 0, 1 or 'T-1'), null/false or an empty string for the whole page
1625 * or 'new' for a new section.
1626 * @param Content $sectionContent New content of the section.
1627 * @param string $sectionTitle New section's subject, only if $section is "new".
1628 * @param int|null $baseRevId
1629 *
1630 * @throws MWException
1631 * @return Content|null New complete article content, or null if error.
1632 *
1633 * @since 1.24
1634 */
1635 public function replaceSectionAtRev( $sectionId, Content $sectionContent,
1636 $sectionTitle = '', $baseRevId = null
1637 ) {
1638 if ( strval( $sectionId ) === '' ) {
1639 // Whole-page edit; let the whole text through
1640 $newContent = $sectionContent;
1641 } else {
1642 if ( !$this->supportsSections() ) {
1643 throw new MWException( "sections not supported for content model " .
1644 $this->getContentHandler()->getModelID() );
1645 }
1646
1647 // T32711: always use current version when adding a new section
1648 if ( is_null( $baseRevId ) || $sectionId === 'new' ) {
1649 $oldContent = $this->getContent();
1650 } else {
1651 $rev = Revision::newFromId( $baseRevId );
1652 if ( !$rev ) {
1653 wfDebug( __METHOD__ . " asked for bogus section (page: " .
1654 $this->getId() . "; section: $sectionId)\n" );
1655 return null;
1656 }
1657
1658 $oldContent = $rev->getContent();
1659 }
1660
1661 if ( !$oldContent ) {
1662 wfDebug( __METHOD__ . ": no page text\n" );
1663 return null;
1664 }
1665
1666 $newContent = $oldContent->replaceSection( $sectionId, $sectionContent, $sectionTitle );
1667 }
1668
1669 return $newContent;
1670 }
1671
1672 /**
1673 * Check flags and add EDIT_NEW or EDIT_UPDATE to them as needed.
1674 *
1675 * @deprecated since 1.32, use exists() instead, or simply omit the EDIT_UPDATE
1676 * and EDIT_NEW flags. To protect against race conditions, use PageUpdater::grabParentRevision.
1677 *
1678 * @param int $flags
1679 * @return int Updated $flags
1680 */
1681 public function checkFlags( $flags ) {
1682 if ( !( $flags & EDIT_NEW ) && !( $flags & EDIT_UPDATE ) ) {
1683 if ( $this->exists() ) {
1684 $flags |= EDIT_UPDATE;
1685 } else {
1686 $flags |= EDIT_NEW;
1687 }
1688 }
1689
1690 return $flags;
1691 }
1692
1693 /**
1694 * @return DerivedPageDataUpdater
1695 */
1696 private function newDerivedDataUpdater() {
1697 global $wgRCWatchCategoryMembership, $wgArticleCountMethod;
1698
1699 $derivedDataUpdater = new DerivedPageDataUpdater(
1700 $this, // NOTE: eventually, PageUpdater should not know about WikiPage
1701 $this->getRevisionStore(),
1702 $this->getRevisionRenderer(),
1703 $this->getSlotRoleRegistry(),
1704 $this->getParserCache(),
1705 JobQueueGroup::singleton(),
1706 MessageCache::singleton(),
1707 MediaWikiServices::getInstance()->getContentLanguage(),
1708 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()
1709 );
1710
1711 $derivedDataUpdater->setLogger( LoggerFactory::getInstance( 'SaveParse' ) );
1712 $derivedDataUpdater->setRcWatchCategoryMembership( $wgRCWatchCategoryMembership );
1713 $derivedDataUpdater->setArticleCountMethod( $wgArticleCountMethod );
1714
1715 return $derivedDataUpdater;
1716 }
1717
1718 /**
1719 * Returns a DerivedPageDataUpdater for use with the given target revision or new content.
1720 * This method attempts to re-use the same DerivedPageDataUpdater instance for subsequent calls.
1721 * The parameters passed to this method are used to ensure that the DerivedPageDataUpdater
1722 * returned matches that caller's expectations, allowing an existing instance to be re-used
1723 * if the given parameters match that instance's internal state according to
1724 * DerivedPageDataUpdater::isReusableFor(), and creating a new instance of the parameters do not
1725 * match the existign one.
1726 *
1727 * If neither $forRevision nor $forUpdate is given, a new DerivedPageDataUpdater is always
1728 * created, replacing any DerivedPageDataUpdater currently cached.
1729 *
1730 * MCR migration note: this replaces WikiPage::prepareContentForEdit.
1731 *
1732 * @since 1.32
1733 *
1734 * @param User|null $forUser The user that will be used for, or was used for, PST.
1735 * @param RevisionRecord|null $forRevision The revision created by the edit for which
1736 * to perform updates, if the edit was already saved.
1737 * @param RevisionSlotsUpdate|null $forUpdate The new content to be saved by the edit (pre PST),
1738 * if the edit was not yet saved.
1739 * @param bool $forEdit Only re-use if the cached DerivedPageDataUpdater has the current
1740 * revision as the edit's parent revision. This ensures that the same
1741 * DerivedPageDataUpdater cannot be re-used for two consecutive edits.
1742 *
1743 * @return DerivedPageDataUpdater
1744 */
1745 private function getDerivedDataUpdater(
1746 User $forUser = null,
1747 RevisionRecord $forRevision = null,
1748 RevisionSlotsUpdate $forUpdate = null,
1749 $forEdit = false
1750 ) {
1751 if ( !$forRevision && !$forUpdate ) {
1752 // NOTE: can't re-use an existing derivedDataUpdater if we don't know what the caller is
1753 // going to use it with.
1754 $this->derivedDataUpdater = null;
1755 }
1756
1757 if ( $this->derivedDataUpdater && !$this->derivedDataUpdater->isContentPrepared() ) {
1758 // NOTE: can't re-use an existing derivedDataUpdater if other code that has a reference
1759 // to it did not yet initialize it, because we don't know what data it will be
1760 // initialized with.
1761 $this->derivedDataUpdater = null;
1762 }
1763
1764 // XXX: It would be nice to have an LRU cache instead of trying to re-use a single instance.
1765 // However, there is no good way to construct a cache key. We'd need to check against all
1766 // cached instances.
1767
1768 if ( $this->derivedDataUpdater
1769 && !$this->derivedDataUpdater->isReusableFor(
1770 $forUser,
1771 $forRevision,
1772 $forUpdate,
1773 $forEdit ? $this->getLatest() : null
1774 )
1775 ) {
1776 $this->derivedDataUpdater = null;
1777 }
1778
1779 if ( !$this->derivedDataUpdater ) {
1780 $this->derivedDataUpdater = $this->newDerivedDataUpdater();
1781 }
1782
1783 return $this->derivedDataUpdater;
1784 }
1785
1786 /**
1787 * Returns a PageUpdater for creating new revisions on this page (or creating the page).
1788 *
1789 * The PageUpdater can also be used to detect the need for edit conflict resolution,
1790 * and to protected such conflict resolution from concurrent edits using a check-and-set
1791 * mechanism.
1792 *
1793 * @since 1.32
1794 *
1795 * @param User $user
1796 * @param RevisionSlotsUpdate|null $forUpdate If given, allows any cached ParserOutput
1797 * that may already have been returned via getDerivedDataUpdater to be re-used.
1798 *
1799 * @return PageUpdater
1800 */
1801 public function newPageUpdater( User $user, RevisionSlotsUpdate $forUpdate = null ) {
1802 global $wgAjaxEditStash, $wgUseAutomaticEditSummaries, $wgPageCreationLog;
1803
1804 $pageUpdater = new PageUpdater(
1805 $user,
1806 $this, // NOTE: eventually, PageUpdater should not know about WikiPage
1807 $this->getDerivedDataUpdater( $user, null, $forUpdate, true ),
1808 $this->getDBLoadBalancer(),
1809 $this->getRevisionStore(),
1810 $this->getSlotRoleRegistry()
1811 );
1812
1813 $pageUpdater->setUsePageCreationLog( $wgPageCreationLog );
1814 $pageUpdater->setAjaxEditStash( $wgAjaxEditStash );
1815 $pageUpdater->setUseAutomaticEditSummaries( $wgUseAutomaticEditSummaries );
1816
1817 return $pageUpdater;
1818 }
1819
1820 /**
1821 * Change an existing article or create a new article. Updates RC and all necessary caches,
1822 * optionally via the deferred update array.
1823 *
1824 * @deprecated since 1.32, use PageUpdater::saveRevision instead. Note that the new method
1825 * expects callers to take care of checking EDIT_MINOR against the minoredit right, and to
1826 * apply the autopatrol right as appropriate.
1827 *
1828 * @param Content $content New content
1829 * @param string|CommentStoreComment $summary Edit summary
1830 * @param int $flags Bitfield:
1831 * EDIT_NEW
1832 * Article is known or assumed to be non-existent, create a new one
1833 * EDIT_UPDATE
1834 * Article is known or assumed to be pre-existing, update it
1835 * EDIT_MINOR
1836 * Mark this edit minor, if the user is allowed to do so
1837 * EDIT_SUPPRESS_RC
1838 * Do not log the change in recentchanges
1839 * EDIT_FORCE_BOT
1840 * Mark the edit a "bot" edit regardless of user rights
1841 * EDIT_AUTOSUMMARY
1842 * Fill in blank summaries with generated text where possible
1843 * EDIT_INTERNAL
1844 * Signal that the page retrieve/save cycle happened entirely in this request.
1845 *
1846 * If neither EDIT_NEW nor EDIT_UPDATE is specified, the status of the
1847 * article will be detected. If EDIT_UPDATE is specified and the article
1848 * doesn't exist, the function will return an edit-gone-missing error. If
1849 * EDIT_NEW is specified and the article does exist, an edit-already-exists
1850 * error will be returned. These two conditions are also possible with
1851 * auto-detection due to MediaWiki's performance-optimised locking strategy.
1852 *
1853 * @param bool|int $originalRevId: The ID of an original revision that the edit
1854 * restores or repeats. The new revision is expected to have the exact same content as
1855 * the given original revision. This is used with rollbacks and with dummy "null" revisions
1856 * which are created to record things like page moves.
1857 * @param User|null $user The user doing the edit
1858 * @param string|null $serialFormat IGNORED.
1859 * @param array|null $tags Change tags to apply to this edit
1860 * Callers are responsible for permission checks
1861 * (with ChangeTags::canAddTagsAccompanyingChange)
1862 * @param Int $undidRevId Id of revision that was undone or 0
1863 *
1864 * @throws MWException
1865 * @return Status Possible errors:
1866 * edit-hook-aborted: The ArticleSave hook aborted the edit but didn't
1867 * set the fatal flag of $status.
1868 * edit-gone-missing: In update mode, but the article didn't exist.
1869 * edit-conflict: In update mode, the article changed unexpectedly.
1870 * edit-no-change: Warning that the text was the same as before.
1871 * edit-already-exists: In creation mode, but the article already exists.
1872 *
1873 * Extensions may define additional errors.
1874 *
1875 * $return->value will contain an associative array with members as follows:
1876 * new: Boolean indicating if the function attempted to create a new article.
1877 * revision: The revision object for the inserted revision, or null.
1878 *
1879 * @since 1.21
1880 * @throws MWException
1881 */
1882 public function doEditContent(
1883 Content $content, $summary, $flags = 0, $originalRevId = false,
1884 User $user = null, $serialFormat = null, $tags = [], $undidRevId = 0
1885 ) {
1886 global $wgUser, $wgUseNPPatrol, $wgUseRCPatrol;
1887
1888 if ( !( $summary instanceof CommentStoreComment ) ) {
1889 $summary = CommentStoreComment::newUnsavedComment( trim( $summary ) );
1890 }
1891
1892 if ( !$user ) {
1893 $user = $wgUser;
1894 }
1895
1896 // TODO: this check is here for backwards-compatibility with 1.31 behavior.
1897 // Checking the minoredit right should be done in the same place the 'bot' right is
1898 // checked for the EDIT_FORCE_BOT flag, which is currently in EditPage::attemptSave.
1899 if ( ( $flags & EDIT_MINOR ) && !$user->isAllowed( 'minoredit' ) ) {
1900 $flags = ( $flags & ~EDIT_MINOR );
1901 }
1902
1903 $slotsUpdate = new RevisionSlotsUpdate();
1904 $slotsUpdate->modifyContent( SlotRecord::MAIN, $content );
1905
1906 // NOTE: while doEditContent() executes, callbacks to getDerivedDataUpdater and
1907 // prepareContentForEdit will generally use the DerivedPageDataUpdater that is also
1908 // used by this PageUpdater. However, there is no guarantee for this.
1909 $updater = $this->newPageUpdater( $user, $slotsUpdate );
1910 $updater->setContent( SlotRecord::MAIN, $content );
1911 $updater->setOriginalRevisionId( $originalRevId );
1912 $updater->setUndidRevisionId( $undidRevId );
1913
1914 $needsPatrol = $wgUseRCPatrol || ( $wgUseNPPatrol && !$this->exists() );
1915
1916 // TODO: this logic should not be in the storage layer, it's here for compatibility
1917 // with 1.31 behavior. Applying the 'autopatrol' right should be done in the same
1918 // place the 'bot' right is handled, which is currently in EditPage::attemptSave.
1919 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
1920
1921 if ( $needsPatrol && $permissionManager->userCan(
1922 'autopatrol', $user, $this->getTitle()
1923 ) ) {
1924 $updater->setRcPatrolStatus( RecentChange::PRC_AUTOPATROLLED );
1925 }
1926
1927 $updater->addTags( $tags );
1928
1929 $revRec = $updater->saveRevision(
1930 $summary,
1931 $flags
1932 );
1933
1934 // $revRec will be null if the edit failed, or if no new revision was created because
1935 // the content did not change.
1936 if ( $revRec ) {
1937 // update cached fields
1938 // TODO: this is currently redundant to what is done in updateRevisionOn.
1939 // But updateRevisionOn() should move into PageStore, and then this will be needed.
1940 $this->setLastEdit( new Revision( $revRec ) ); // TODO: use RevisionRecord
1941 $this->mLatest = $revRec->getId();
1942 }
1943
1944 return $updater->getStatus();
1945 }
1946
1947 /**
1948 * Get parser options suitable for rendering the primary article wikitext
1949 *
1950 * @see ParserOptions::newCanonical
1951 *
1952 * @param IContextSource|User|string $context One of the following:
1953 * - IContextSource: Use the User and the Language of the provided
1954 * context
1955 * - User: Use the provided User object and $wgLang for the language,
1956 * so use an IContextSource object if possible.
1957 * - 'canonical': Canonical options (anonymous user with default
1958 * preferences and content language).
1959 * @return ParserOptions
1960 */
1961 public function makeParserOptions( $context ) {
1962 $options = ParserOptions::newCanonical( $context );
1963
1964 if ( $this->getTitle()->isConversionTable() ) {
1965 // @todo ConversionTable should become a separate content model, so
1966 // we don't need special cases like this one.
1967 $options->disableContentConversion();
1968 }
1969
1970 return $options;
1971 }
1972
1973 /**
1974 * Prepare content which is about to be saved.
1975 *
1976 * Prior to 1.30, this returned a stdClass.
1977 *
1978 * @deprecated since 1.32, use getDerivedDataUpdater instead.
1979 *
1980 * @param Content $content
1981 * @param Revision|RevisionRecord|null $revision Revision object.
1982 * Used with vary-revision or vary-revision-id.
1983 * @param User|null $user
1984 * @param string|null $serialFormat IGNORED
1985 * @param bool $useCache Check shared prepared edit cache
1986 *
1987 * @return PreparedEdit
1988 *
1989 * @since 1.21
1990 */
1991 public function prepareContentForEdit(
1992 Content $content,
1993 $revision = null,
1994 User $user = null,
1995 $serialFormat = null,
1996 $useCache = true
1997 ) {
1998 global $wgUser;
1999
2000 if ( !$user ) {
2001 $user = $wgUser;
2002 }
2003
2004 if ( $revision !== null ) {
2005 if ( $revision instanceof Revision ) {
2006 $revision = $revision->getRevisionRecord();
2007 } elseif ( !( $revision instanceof RevisionRecord ) ) {
2008 throw new InvalidArgumentException(
2009 __METHOD__ . ': invalid $revision argument type ' . gettype( $revision ) );
2010 }
2011 }
2012
2013 $slots = RevisionSlotsUpdate::newFromContent( [ SlotRecord::MAIN => $content ] );
2014 $updater = $this->getDerivedDataUpdater( $user, $revision, $slots );
2015
2016 if ( !$updater->isUpdatePrepared() ) {
2017 $updater->prepareContent( $user, $slots, $useCache );
2018
2019 if ( $revision ) {
2020 $updater->prepareUpdate(
2021 $revision,
2022 [
2023 'causeAction' => 'prepare-edit',
2024 'causeAgent' => $user->getName(),
2025 ]
2026 );
2027 }
2028 }
2029
2030 return $updater->getPreparedEdit();
2031 }
2032
2033 /**
2034 * Do standard deferred updates after page edit.
2035 * Update links tables, site stats, search index and message cache.
2036 * Purges pages that include this page if the text was changed here.
2037 * Every 100th edit, prune the recent changes table.
2038 *
2039 * @deprecated since 1.32, use PageUpdater::doUpdates instead.
2040 *
2041 * @param Revision $revision
2042 * @param User $user User object that did the revision
2043 * @param array $options Array of options, following indexes are used:
2044 * - changed: bool, whether the revision changed the content (default true)
2045 * - created: bool, whether the revision created the page (default false)
2046 * - moved: bool, whether the page was moved (default false)
2047 * - restored: bool, whether the page was undeleted (default false)
2048 * - oldrevision: Revision object for the pre-update revision (default null)
2049 * - oldcountable: bool, null, or string 'no-change' (default null):
2050 * - bool: whether the page was counted as an article before that
2051 * revision, only used in changed is true and created is false
2052 * - null: if created is false, don't update the article count; if created
2053 * is true, do update the article count
2054 * - 'no-change': don't update the article count, ever
2055 * - causeAction: an arbitrary string identifying the reason for the update.
2056 * See DataUpdate::getCauseAction(). (default 'edit-page')
2057 * - causeAgent: name of the user who caused the update. See DataUpdate::getCauseAgent().
2058 * (string, defaults to the passed user)
2059 */
2060 public function doEditUpdates( Revision $revision, User $user, array $options = [] ) {
2061 $options += [
2062 'causeAction' => 'edit-page',
2063 'causeAgent' => $user->getName(),
2064 ];
2065
2066 $revision = $revision->getRevisionRecord();
2067
2068 $updater = $this->getDerivedDataUpdater( $user, $revision );
2069
2070 $updater->prepareUpdate( $revision, $options );
2071
2072 $updater->doUpdates();
2073 }
2074
2075 /**
2076 * Update the parser cache.
2077 *
2078 * @note This is a temporary workaround until there is a proper data updater class.
2079 * It will become deprecated soon.
2080 *
2081 * @param array $options
2082 * - causeAction: an arbitrary string identifying the reason for the update.
2083 * See DataUpdate::getCauseAction(). (default 'edit-page')
2084 * - causeAgent: name of the user who caused the update (string, defaults to the
2085 * user who created the revision)
2086 * @since 1.32
2087 */
2088 public function updateParserCache( array $options = [] ) {
2089 $revision = $this->getRevisionRecord();
2090 if ( !$revision || !$revision->getId() ) {
2091 LoggerFactory::getInstance( 'wikipage' )->info(
2092 __METHOD__ . 'called with ' . ( $revision ? 'unsaved' : 'no' ) . ' revision'
2093 );
2094 return;
2095 }
2096 $user = User::newFromIdentity( $revision->getUser( RevisionRecord::RAW ) );
2097
2098 $updater = $this->getDerivedDataUpdater( $user, $revision );
2099 $updater->prepareUpdate( $revision, $options );
2100 $updater->doParserCacheUpdate();
2101 }
2102
2103 /**
2104 * Do secondary data updates (such as updating link tables).
2105 * Secondary data updates are only a small part of the updates needed after saving
2106 * a new revision; normally PageUpdater::doUpdates should be used instead (which includes
2107 * secondary data updates). This method is provided for partial purges.
2108 *
2109 * @note This is a temporary workaround until there is a proper data updater class.
2110 * It will become deprecated soon.
2111 *
2112 * @param array $options
2113 * - recursive (bool, default true): whether to do a recursive update (update pages that
2114 * depend on this page, e.g. transclude it). This will set the $recursive parameter of
2115 * Content::getSecondaryDataUpdates. Typically this should be true unless the update
2116 * was something that did not really change the page, such as a null edit.
2117 * - triggeringUser: The user triggering the update (UserIdentity, defaults to the
2118 * user who created the revision)
2119 * - causeAction: an arbitrary string identifying the reason for the update.
2120 * See DataUpdate::getCauseAction(). (default 'unknown')
2121 * - causeAgent: name of the user who caused the update (string, default 'unknown')
2122 * - defer: one of the DeferredUpdates constants, or false to run immediately (default: false).
2123 * Note that even when this is set to false, some updates might still get deferred (as
2124 * some update might directly add child updates to DeferredUpdates).
2125 * - known-revision-output: a combined canonical ParserOutput for the revision, perhaps
2126 * from some cache. The caller is responsible for ensuring that the ParserOutput indeed
2127 * matched the $rev and $options. This mechanism is intended as a temporary stop-gap,
2128 * for the time until caches have been changed to store RenderedRevision states instead
2129 * of ParserOutput objects. (default: null) (since 1.33)
2130 * @since 1.32
2131 */
2132 public function doSecondaryDataUpdates( array $options = [] ) {
2133 $options['recursive'] = $options['recursive'] ?? true;
2134 $revision = $this->getRevisionRecord();
2135 if ( !$revision || !$revision->getId() ) {
2136 LoggerFactory::getInstance( 'wikipage' )->info(
2137 __METHOD__ . 'called with ' . ( $revision ? 'unsaved' : 'no' ) . ' revision'
2138 );
2139 return;
2140 }
2141 $user = User::newFromIdentity( $revision->getUser( RevisionRecord::RAW ) );
2142
2143 $updater = $this->getDerivedDataUpdater( $user, $revision );
2144 $updater->prepareUpdate( $revision, $options );
2145 $updater->doSecondaryDataUpdates( $options );
2146 }
2147
2148 /**
2149 * Update the article's restriction field, and leave a log entry.
2150 * This works for protection both existing and non-existing pages.
2151 *
2152 * @param array $limit Set of restriction keys
2153 * @param array $expiry Per restriction type expiration
2154 * @param int &$cascade Set to false if cascading protection isn't allowed.
2155 * @param string $reason
2156 * @param User $user The user updating the restrictions
2157 * @param string|string[]|null $tags Change tags to add to the pages and protection log entries
2158 * ($user should be able to add the specified tags before this is called)
2159 * @return Status Status object; if action is taken, $status->value is the log_id of the
2160 * protection log entry.
2161 */
2162 public function doUpdateRestrictions( array $limit, array $expiry,
2163 &$cascade, $reason, User $user, $tags = null
2164 ) {
2165 global $wgCascadingRestrictionLevels;
2166
2167 if ( wfReadOnly() ) {
2168 return Status::newFatal( wfMessage( 'readonlytext', wfReadOnlyReason() ) );
2169 }
2170
2171 $this->loadPageData( 'fromdbmaster' );
2172 $this->mTitle->loadRestrictions( null, Title::READ_LATEST );
2173 $restrictionTypes = $this->mTitle->getRestrictionTypes();
2174 $id = $this->getId();
2175
2176 if ( !$cascade ) {
2177 $cascade = false;
2178 }
2179
2180 // Take this opportunity to purge out expired restrictions
2181 Title::purgeExpiredRestrictions();
2182
2183 // @todo: Same limitations as described in ProtectionForm.php (line 37);
2184 // we expect a single selection, but the schema allows otherwise.
2185 $isProtected = false;
2186 $protect = false;
2187 $changed = false;
2188
2189 $dbw = wfGetDB( DB_MASTER );
2190
2191 foreach ( $restrictionTypes as $action ) {
2192 if ( !isset( $expiry[$action] ) || $expiry[$action] === $dbw->getInfinity() ) {
2193 $expiry[$action] = 'infinity';
2194 }
2195 if ( !isset( $limit[$action] ) ) {
2196 $limit[$action] = '';
2197 } elseif ( $limit[$action] != '' ) {
2198 $protect = true;
2199 }
2200
2201 // Get current restrictions on $action
2202 $current = implode( '', $this->mTitle->getRestrictions( $action ) );
2203 if ( $current != '' ) {
2204 $isProtected = true;
2205 }
2206
2207 if ( $limit[$action] != $current ) {
2208 $changed = true;
2209 } elseif ( $limit[$action] != '' ) {
2210 // Only check expiry change if the action is actually being
2211 // protected, since expiry does nothing on an not-protected
2212 // action.
2213 if ( $this->mTitle->getRestrictionExpiry( $action ) != $expiry[$action] ) {
2214 $changed = true;
2215 }
2216 }
2217 }
2218
2219 if ( !$changed && $protect && $this->mTitle->areRestrictionsCascading() != $cascade ) {
2220 $changed = true;
2221 }
2222
2223 // If nothing has changed, do nothing
2224 if ( !$changed ) {
2225 return Status::newGood();
2226 }
2227
2228 if ( !$protect ) { // No protection at all means unprotection
2229 $revCommentMsg = 'unprotectedarticle-comment';
2230 $logAction = 'unprotect';
2231 } elseif ( $isProtected ) {
2232 $revCommentMsg = 'modifiedarticleprotection-comment';
2233 $logAction = 'modify';
2234 } else {
2235 $revCommentMsg = 'protectedarticle-comment';
2236 $logAction = 'protect';
2237 }
2238
2239 $logRelationsValues = [];
2240 $logRelationsField = null;
2241 $logParamsDetails = [];
2242
2243 // Null revision (used for change tag insertion)
2244 $nullRevision = null;
2245
2246 if ( $id ) { // Protection of existing page
2247 // Avoid PHP 7.1 warning of passing $this by reference
2248 $wikiPage = $this;
2249
2250 if ( !Hooks::run( 'ArticleProtect', [ &$wikiPage, &$user, $limit, $reason ] ) ) {
2251 return Status::newGood();
2252 }
2253
2254 // Only certain restrictions can cascade...
2255 $editrestriction = isset( $limit['edit'] )
2256 ? [ $limit['edit'] ]
2257 : $this->mTitle->getRestrictions( 'edit' );
2258 foreach ( array_keys( $editrestriction, 'sysop' ) as $key ) {
2259 $editrestriction[$key] = 'editprotected'; // backwards compatibility
2260 }
2261 foreach ( array_keys( $editrestriction, 'autoconfirmed' ) as $key ) {
2262 $editrestriction[$key] = 'editsemiprotected'; // backwards compatibility
2263 }
2264
2265 $cascadingRestrictionLevels = $wgCascadingRestrictionLevels;
2266 foreach ( array_keys( $cascadingRestrictionLevels, 'sysop' ) as $key ) {
2267 $cascadingRestrictionLevels[$key] = 'editprotected'; // backwards compatibility
2268 }
2269 foreach ( array_keys( $cascadingRestrictionLevels, 'autoconfirmed' ) as $key ) {
2270 $cascadingRestrictionLevels[$key] = 'editsemiprotected'; // backwards compatibility
2271 }
2272
2273 // The schema allows multiple restrictions
2274 if ( !array_intersect( $editrestriction, $cascadingRestrictionLevels ) ) {
2275 $cascade = false;
2276 }
2277
2278 // insert null revision to identify the page protection change as edit summary
2279 $latest = $this->getLatest();
2280 $nullRevision = $this->insertProtectNullRevision(
2281 $revCommentMsg,
2282 $limit,
2283 $expiry,
2284 $cascade,
2285 $reason,
2286 $user
2287 );
2288
2289 if ( $nullRevision === null ) {
2290 return Status::newFatal( 'no-null-revision', $this->mTitle->getPrefixedText() );
2291 }
2292
2293 $logRelationsField = 'pr_id';
2294
2295 // Update restrictions table
2296 foreach ( $limit as $action => $restrictions ) {
2297 $dbw->delete(
2298 'page_restrictions',
2299 [
2300 'pr_page' => $id,
2301 'pr_type' => $action
2302 ],
2303 __METHOD__
2304 );
2305 if ( $restrictions != '' ) {
2306 $cascadeValue = ( $cascade && $action == 'edit' ) ? 1 : 0;
2307 $dbw->insert(
2308 'page_restrictions',
2309 [
2310 'pr_page' => $id,
2311 'pr_type' => $action,
2312 'pr_level' => $restrictions,
2313 'pr_cascade' => $cascadeValue,
2314 'pr_expiry' => $dbw->encodeExpiry( $expiry[$action] )
2315 ],
2316 __METHOD__
2317 );
2318 $logRelationsValues[] = $dbw->insertId();
2319 $logParamsDetails[] = [
2320 'type' => $action,
2321 'level' => $restrictions,
2322 'expiry' => $expiry[$action],
2323 'cascade' => (bool)$cascadeValue,
2324 ];
2325 }
2326 }
2327
2328 // Clear out legacy restriction fields
2329 $dbw->update(
2330 'page',
2331 [ 'page_restrictions' => '' ],
2332 [ 'page_id' => $id ],
2333 __METHOD__
2334 );
2335
2336 // Avoid PHP 7.1 warning of passing $this by reference
2337 $wikiPage = $this;
2338
2339 Hooks::run( 'NewRevisionFromEditComplete',
2340 [ $this, $nullRevision, $latest, $user ] );
2341 Hooks::run( 'ArticleProtectComplete', [ &$wikiPage, &$user, $limit, $reason ] );
2342 } else { // Protection of non-existing page (also known as "title protection")
2343 // Cascade protection is meaningless in this case
2344 $cascade = false;
2345
2346 if ( $limit['create'] != '' ) {
2347 $commentFields = CommentStore::getStore()->insert( $dbw, 'pt_reason', $reason );
2348 $dbw->replace( 'protected_titles',
2349 [ [ 'pt_namespace', 'pt_title' ] ],
2350 [
2351 'pt_namespace' => $this->mTitle->getNamespace(),
2352 'pt_title' => $this->mTitle->getDBkey(),
2353 'pt_create_perm' => $limit['create'],
2354 'pt_timestamp' => $dbw->timestamp(),
2355 'pt_expiry' => $dbw->encodeExpiry( $expiry['create'] ),
2356 'pt_user' => $user->getId(),
2357 ] + $commentFields, __METHOD__
2358 );
2359 $logParamsDetails[] = [
2360 'type' => 'create',
2361 'level' => $limit['create'],
2362 'expiry' => $expiry['create'],
2363 ];
2364 } else {
2365 $dbw->delete( 'protected_titles',
2366 [
2367 'pt_namespace' => $this->mTitle->getNamespace(),
2368 'pt_title' => $this->mTitle->getDBkey()
2369 ], __METHOD__
2370 );
2371 }
2372 }
2373
2374 $this->mTitle->flushRestrictions();
2375 InfoAction::invalidateCache( $this->mTitle );
2376
2377 if ( $logAction == 'unprotect' ) {
2378 $params = [];
2379 } else {
2380 $protectDescriptionLog = $this->protectDescriptionLog( $limit, $expiry );
2381 $params = [
2382 '4::description' => $protectDescriptionLog, // parameter for IRC
2383 '5:bool:cascade' => $cascade,
2384 'details' => $logParamsDetails, // parameter for localize and api
2385 ];
2386 }
2387
2388 // Update the protection log
2389 $logEntry = new ManualLogEntry( 'protect', $logAction );
2390 $logEntry->setTarget( $this->mTitle );
2391 $logEntry->setComment( $reason );
2392 $logEntry->setPerformer( $user );
2393 $logEntry->setParameters( $params );
2394 if ( !is_null( $nullRevision ) ) {
2395 $logEntry->setAssociatedRevId( $nullRevision->getId() );
2396 }
2397 $logEntry->addTags( $tags );
2398 if ( $logRelationsField !== null && count( $logRelationsValues ) ) {
2399 $logEntry->setRelations( [ $logRelationsField => $logRelationsValues ] );
2400 }
2401 $logId = $logEntry->insert();
2402 $logEntry->publish( $logId );
2403
2404 return Status::newGood( $logId );
2405 }
2406
2407 /**
2408 * Insert a new null revision for this page.
2409 *
2410 * @param string $revCommentMsg Comment message key for the revision
2411 * @param array $limit Set of restriction keys
2412 * @param array $expiry Per restriction type expiration
2413 * @param int $cascade Set to false if cascading protection isn't allowed.
2414 * @param string $reason
2415 * @param User|null $user
2416 * @return Revision|null Null on error
2417 */
2418 public function insertProtectNullRevision( $revCommentMsg, array $limit,
2419 array $expiry, $cascade, $reason, $user = null
2420 ) {
2421 $dbw = wfGetDB( DB_MASTER );
2422
2423 // Prepare a null revision to be added to the history
2424 $editComment = wfMessage(
2425 $revCommentMsg,
2426 $this->mTitle->getPrefixedText(),
2427 $user ? $user->getName() : ''
2428 )->inContentLanguage()->text();
2429 if ( $reason ) {
2430 $editComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
2431 }
2432 $protectDescription = $this->protectDescription( $limit, $expiry );
2433 if ( $protectDescription ) {
2434 $editComment .= wfMessage( 'word-separator' )->inContentLanguage()->text();
2435 $editComment .= wfMessage( 'parentheses' )->params( $protectDescription )
2436 ->inContentLanguage()->text();
2437 }
2438 if ( $cascade ) {
2439 $editComment .= wfMessage( 'word-separator' )->inContentLanguage()->text();
2440 $editComment .= wfMessage( 'brackets' )->params(
2441 wfMessage( 'protect-summary-cascade' )->inContentLanguage()->text()
2442 )->inContentLanguage()->text();
2443 }
2444
2445 $nullRev = Revision::newNullRevision( $dbw, $this->getId(), $editComment, true, $user );
2446 if ( $nullRev ) {
2447 $nullRev->insertOn( $dbw );
2448
2449 // Update page record and touch page
2450 $oldLatest = $nullRev->getParentId();
2451 $this->updateRevisionOn( $dbw, $nullRev, $oldLatest );
2452 }
2453
2454 return $nullRev;
2455 }
2456
2457 /**
2458 * @param string $expiry 14-char timestamp or "infinity", or false if the input was invalid
2459 * @return string
2460 */
2461 protected function formatExpiry( $expiry ) {
2462 if ( $expiry != 'infinity' ) {
2463 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
2464 return wfMessage(
2465 'protect-expiring',
2466 $contLang->timeanddate( $expiry, false, false ),
2467 $contLang->date( $expiry, false, false ),
2468 $contLang->time( $expiry, false, false )
2469 )->inContentLanguage()->text();
2470 } else {
2471 return wfMessage( 'protect-expiry-indefinite' )
2472 ->inContentLanguage()->text();
2473 }
2474 }
2475
2476 /**
2477 * Builds the description to serve as comment for the edit.
2478 *
2479 * @param array $limit Set of restriction keys
2480 * @param array $expiry Per restriction type expiration
2481 * @return string
2482 */
2483 public function protectDescription( array $limit, array $expiry ) {
2484 $protectDescription = '';
2485
2486 foreach ( array_filter( $limit ) as $action => $restrictions ) {
2487 # $action is one of $wgRestrictionTypes = [ 'create', 'edit', 'move', 'upload' ].
2488 # All possible message keys are listed here for easier grepping:
2489 # * restriction-create
2490 # * restriction-edit
2491 # * restriction-move
2492 # * restriction-upload
2493 $actionText = wfMessage( 'restriction-' . $action )->inContentLanguage()->text();
2494 # $restrictions is one of $wgRestrictionLevels = [ '', 'autoconfirmed', 'sysop' ],
2495 # with '' filtered out. All possible message keys are listed below:
2496 # * protect-level-autoconfirmed
2497 # * protect-level-sysop
2498 $restrictionsText = wfMessage( 'protect-level-' . $restrictions )
2499 ->inContentLanguage()->text();
2500
2501 $expiryText = $this->formatExpiry( $expiry[$action] );
2502
2503 if ( $protectDescription !== '' ) {
2504 $protectDescription .= wfMessage( 'word-separator' )->inContentLanguage()->text();
2505 }
2506 $protectDescription .= wfMessage( 'protect-summary-desc' )
2507 ->params( $actionText, $restrictionsText, $expiryText )
2508 ->inContentLanguage()->text();
2509 }
2510
2511 return $protectDescription;
2512 }
2513
2514 /**
2515 * Builds the description to serve as comment for the log entry.
2516 *
2517 * Some bots may parse IRC lines, which are generated from log entries which contain plain
2518 * protect description text. Keep them in old format to avoid breaking compatibility.
2519 * TODO: Fix protection log to store structured description and format it on-the-fly.
2520 *
2521 * @param array $limit Set of restriction keys
2522 * @param array $expiry Per restriction type expiration
2523 * @return string
2524 */
2525 public function protectDescriptionLog( array $limit, array $expiry ) {
2526 $protectDescriptionLog = '';
2527
2528 $dirMark = MediaWikiServices::getInstance()->getContentLanguage()->getDirMark();
2529 foreach ( array_filter( $limit ) as $action => $restrictions ) {
2530 $expiryText = $this->formatExpiry( $expiry[$action] );
2531 $protectDescriptionLog .=
2532 $dirMark .
2533 "[$action=$restrictions] ($expiryText)";
2534 }
2535
2536 return trim( $protectDescriptionLog );
2537 }
2538
2539 /**
2540 * Take an array of page restrictions and flatten it to a string
2541 * suitable for insertion into the page_restrictions field.
2542 *
2543 * @param string[] $limit
2544 *
2545 * @throws MWException
2546 * @return string
2547 */
2548 protected static function flattenRestrictions( $limit ) {
2549 if ( !is_array( $limit ) ) {
2550 throw new MWException( __METHOD__ . ' given non-array restriction set' );
2551 }
2552
2553 $bits = [];
2554 ksort( $limit );
2555
2556 foreach ( array_filter( $limit ) as $action => $restrictions ) {
2557 $bits[] = "$action=$restrictions";
2558 }
2559
2560 return implode( ':', $bits );
2561 }
2562
2563 /**
2564 * Determines if deletion of this page would be batched (executed over time by the job queue)
2565 * or not (completed in the same request as the delete call).
2566 *
2567 * It is unlikely but possible that an edit from another request could push the page over the
2568 * batching threshold after this function is called, but before the caller acts upon the
2569 * return value. Callers must decide for themselves how to deal with this. $safetyMargin
2570 * is provided as an unreliable but situationally useful help for some common cases.
2571 *
2572 * @param int $safetyMargin Added to the revision count when checking for batching
2573 * @return bool True if deletion would be batched, false otherwise
2574 */
2575 public function isBatchedDelete( $safetyMargin = 0 ) {
2576 global $wgDeleteRevisionsBatchSize;
2577
2578 $dbr = wfGetDB( DB_REPLICA );
2579 $revCount = $this->getRevisionStore()->countRevisionsByPageId( $dbr, $this->getId() );
2580 $revCount += $safetyMargin;
2581
2582 return $revCount >= $wgDeleteRevisionsBatchSize;
2583 }
2584
2585 /**
2586 * Same as doDeleteArticleReal(), but returns a simple boolean. This is kept around for
2587 * backwards compatibility, if you care about error reporting you should use
2588 * doDeleteArticleReal() instead.
2589 *
2590 * Deletes the article with database consistency, writes logs, purges caches
2591 *
2592 * @param string $reason Delete reason for deletion log
2593 * @param bool $suppress Suppress all revisions and log the deletion in
2594 * the suppression log instead of the deletion log
2595 * @param int|null $u1 Unused
2596 * @param bool|null $u2 Unused
2597 * @param array|string &$error Array of errors to append to
2598 * @param User|null $user The deleting user
2599 * @param bool $immediate false allows deleting over time via the job queue
2600 * @return bool True if successful
2601 * @throws FatalError
2602 * @throws MWException
2603 */
2604 public function doDeleteArticle(
2605 $reason, $suppress = false, $u1 = null, $u2 = null, &$error = '', User $user = null,
2606 $immediate = false
2607 ) {
2608 $status = $this->doDeleteArticleReal( $reason, $suppress, $u1, $u2, $error, $user,
2609 [], 'delete', $immediate );
2610
2611 // Returns true if the page was actually deleted, or is scheduled for deletion
2612 return $status->isOK();
2613 }
2614
2615 /**
2616 * Back-end article deletion
2617 * Deletes the article with database consistency, writes logs, purges caches
2618 *
2619 * @since 1.19
2620 *
2621 * @param string $reason Delete reason for deletion log
2622 * @param bool $suppress Suppress all revisions and log the deletion in
2623 * the suppression log instead of the deletion log
2624 * @param int|null $u1 Unused
2625 * @param bool|null $u2 Unused
2626 * @param array|string &$error Array of errors to append to
2627 * @param User|null $deleter The deleting user
2628 * @param array $tags Tags to apply to the deletion action
2629 * @param string $logsubtype
2630 * @param bool $immediate false allows deleting over time via the job queue
2631 * @return Status Status object; if successful, $status->value is the log_id of the
2632 * deletion log entry. If the page couldn't be deleted because it wasn't
2633 * found, $status is a non-fatal 'cannotdelete' error
2634 * @throws FatalError
2635 * @throws MWException
2636 */
2637 public function doDeleteArticleReal(
2638 $reason, $suppress = false, $u1 = null, $u2 = null, &$error = '', User $deleter = null,
2639 $tags = [], $logsubtype = 'delete', $immediate = false
2640 ) {
2641 global $wgUser;
2642
2643 wfDebug( __METHOD__ . "\n" );
2644
2645 $status = Status::newGood();
2646
2647 // Avoid PHP 7.1 warning of passing $this by reference
2648 $wikiPage = $this;
2649
2650 if ( !$deleter ) {
2651 $deleter = $wgUser;
2652 }
2653 if ( !Hooks::run( 'ArticleDelete',
2654 [ &$wikiPage, &$deleter, &$reason, &$error, &$status, $suppress ]
2655 ) ) {
2656 if ( $status->isOK() ) {
2657 // Hook aborted but didn't set a fatal status
2658 $status->fatal( 'delete-hook-aborted' );
2659 }
2660 return $status;
2661 }
2662
2663 return $this->doDeleteArticleBatched( $reason, $suppress, $deleter, $tags,
2664 $logsubtype, $immediate );
2665 }
2666
2667 /**
2668 * Back-end article deletion
2669 *
2670 * Only invokes batching via the job queue if necessary per $wgDeleteRevisionsBatchSize.
2671 * Deletions can often be completed inline without involving the job queue.
2672 *
2673 * Potentially called many times per deletion operation for pages with many revisions.
2674 */
2675 public function doDeleteArticleBatched(
2676 $reason, $suppress, User $deleter, $tags,
2677 $logsubtype, $immediate = false, $webRequestId = null
2678 ) {
2679 wfDebug( __METHOD__ . "\n" );
2680
2681 $status = Status::newGood();
2682
2683 $dbw = wfGetDB( DB_MASTER );
2684 $dbw->startAtomic( __METHOD__ );
2685
2686 $this->loadPageData( self::READ_LATEST );
2687 $id = $this->getId();
2688 // T98706: lock the page from various other updates but avoid using
2689 // WikiPage::READ_LOCKING as that will carry over the FOR UPDATE to
2690 // the revisions queries (which also JOIN on user). Only lock the page
2691 // row and CAS check on page_latest to see if the trx snapshot matches.
2692 $lockedLatest = $this->lockAndGetLatest();
2693 if ( $id == 0 || $this->getLatest() != $lockedLatest ) {
2694 $dbw->endAtomic( __METHOD__ );
2695 // Page not there or trx snapshot is stale
2696 $status->error( 'cannotdelete',
2697 wfEscapeWikiText( $this->getTitle()->getPrefixedText() ) );
2698 return $status;
2699 }
2700
2701 // At this point we are now committed to returning an OK
2702 // status unless some DB query error or other exception comes up.
2703 // This way callers don't have to call rollback() if $status is bad
2704 // unless they actually try to catch exceptions (which is rare).
2705
2706 // we need to remember the old content so we can use it to generate all deletion updates.
2707 $revision = $this->getRevision();
2708 try {
2709 $content = $this->getContent( RevisionRecord::RAW );
2710 } catch ( Exception $ex ) {
2711 wfLogWarning( __METHOD__ . ': failed to load content during deletion! '
2712 . $ex->getMessage() );
2713
2714 $content = null;
2715 }
2716
2717 // Archive revisions. In immediate mode, archive all revisions. Otherwise, archive
2718 // one batch of revisions and defer archival of any others to the job queue.
2719 $explictTrxLogged = false;
2720 while ( true ) {
2721 $done = $this->archiveRevisions( $dbw, $id, $suppress );
2722 if ( $done || !$immediate ) {
2723 break;
2724 }
2725 $dbw->endAtomic( __METHOD__ );
2726 if ( $dbw->explicitTrxActive() ) {
2727 // Explict transactions may never happen here in practice. Log to be sure.
2728 if ( !$explictTrxLogged ) {
2729 $explictTrxLogged = true;
2730 LoggerFactory::getInstance( 'wfDebug' )->debug(
2731 'explicit transaction active in ' . __METHOD__ . ' while deleting {title}', [
2732 'title' => $this->getTitle()->getText(),
2733 ] );
2734 }
2735 continue;
2736 }
2737 if ( $dbw->trxLevel() ) {
2738 $dbw->commit();
2739 }
2740 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
2741 $lbFactory->waitForReplication();
2742 $dbw->startAtomic( __METHOD__ );
2743 }
2744
2745 // If done archiving, also delete the article.
2746 if ( !$done ) {
2747 $dbw->endAtomic( __METHOD__ );
2748
2749 $jobParams = [
2750 'namespace' => $this->getTitle()->getNamespace(),
2751 'title' => $this->getTitle()->getDBkey(),
2752 'wikiPageId' => $id,
2753 'requestId' => $webRequestId ?? WebRequest::getRequestId(),
2754 'reason' => $reason,
2755 'suppress' => $suppress,
2756 'userId' => $deleter->getId(),
2757 'tags' => json_encode( $tags ),
2758 'logsubtype' => $logsubtype,
2759 ];
2760
2761 $job = new DeletePageJob( $jobParams );
2762 JobQueueGroup::singleton()->push( $job );
2763
2764 $status->warning( 'delete-scheduled',
2765 wfEscapeWikiText( $this->getTitle()->getPrefixedText() ) );
2766 } else {
2767 // Get archivedRevisionCount by db query, because there's no better alternative.
2768 // Jobs cannot pass a count of archived revisions to the next job, because additional
2769 // deletion operations can be started while the first is running. Jobs from each
2770 // gracefully interleave, but would not know about each other's count. Deduplication
2771 // in the job queue to avoid simultaneous deletion operations would add overhead.
2772 // Number of archived revisions cannot be known beforehand, because edits can be made
2773 // while deletion operations are being processed, changing the number of archivals.
2774 $archivedRevisionCount = (int)$dbw->selectField(
2775 'archive', 'COUNT(*)',
2776 [
2777 'ar_namespace' => $this->getTitle()->getNamespace(),
2778 'ar_title' => $this->getTitle()->getDBkey(),
2779 'ar_page_id' => $id
2780 ], __METHOD__
2781 );
2782
2783 // Clone the title and wikiPage, so we have the information we need when
2784 // we log and run the ArticleDeleteComplete hook.
2785 $logTitle = clone $this->mTitle;
2786 $wikiPageBeforeDelete = clone $this;
2787
2788 // Now that it's safely backed up, delete it
2789 $dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
2790
2791 // Log the deletion, if the page was suppressed, put it in the suppression log instead
2792 $logtype = $suppress ? 'suppress' : 'delete';
2793
2794 $logEntry = new ManualLogEntry( $logtype, $logsubtype );
2795 $logEntry->setPerformer( $deleter );
2796 $logEntry->setTarget( $logTitle );
2797 $logEntry->setComment( $reason );
2798 $logEntry->addTags( $tags );
2799 $logid = $logEntry->insert();
2800
2801 $dbw->onTransactionPreCommitOrIdle(
2802 function () use ( $logEntry, $logid ) {
2803 // T58776: avoid deadlocks (especially from FileDeleteForm)
2804 $logEntry->publish( $logid );
2805 },
2806 __METHOD__
2807 );
2808
2809 $dbw->endAtomic( __METHOD__ );
2810
2811 $this->doDeleteUpdates( $id, $content, $revision, $deleter );
2812
2813 Hooks::run( 'ArticleDeleteComplete', [
2814 &$wikiPageBeforeDelete,
2815 &$deleter,
2816 $reason,
2817 $id,
2818 $content,
2819 $logEntry,
2820 $archivedRevisionCount
2821 ] );
2822 $status->value = $logid;
2823
2824 // Show log excerpt on 404 pages rather than just a link
2825 $dbCache = ObjectCache::getInstance( 'db-replicated' );
2826 $key = $dbCache->makeKey( 'page-recent-delete', md5( $logTitle->getPrefixedText() ) );
2827 $dbCache->set( $key, 1, $dbCache::TTL_DAY );
2828 }
2829
2830 return $status;
2831 }
2832
2833 /**
2834 * Archives revisions as part of page deletion.
2835 *
2836 * @param IDatabase $dbw
2837 * @param int $id
2838 * @param bool $suppress Suppress all revisions and log the deletion in
2839 * the suppression log instead of the deletion log
2840 * @return bool
2841 */
2842 protected function archiveRevisions( $dbw, $id, $suppress ) {
2843 global $wgContentHandlerUseDB, $wgMultiContentRevisionSchemaMigrationStage,
2844 $wgDeleteRevisionsBatchSize;
2845
2846 // Given the lock above, we can be confident in the title and page ID values
2847 $namespace = $this->getTitle()->getNamespace();
2848 $dbKey = $this->getTitle()->getDBkey();
2849
2850 $commentStore = CommentStore::getStore();
2851 $actorMigration = ActorMigration::newMigration();
2852
2853 $revQuery = Revision::getQueryInfo();
2854 $bitfield = false;
2855
2856 // Bitfields to further suppress the content
2857 if ( $suppress ) {
2858 $bitfield = RevisionRecord::SUPPRESSED_ALL;
2859 $revQuery['fields'] = array_diff( $revQuery['fields'], [ 'rev_deleted' ] );
2860 }
2861
2862 // For now, shunt the revision data into the archive table.
2863 // Text is *not* removed from the text table; bulk storage
2864 // is left intact to avoid breaking block-compression or
2865 // immutable storage schemes.
2866 // In the future, we may keep revisions and mark them with
2867 // the rev_deleted field, which is reserved for this purpose.
2868
2869 // Lock rows in `revision` and its temp tables, but not any others.
2870 // Note array_intersect() preserves keys from the first arg, and we're
2871 // assuming $revQuery has `revision` primary and isn't using subtables
2872 // for anything we care about.
2873 $dbw->lockForUpdate(
2874 array_intersect(
2875 $revQuery['tables'],
2876 [ 'revision', 'revision_comment_temp', 'revision_actor_temp' ]
2877 ),
2878 [ 'rev_page' => $id ],
2879 __METHOD__,
2880 [],
2881 $revQuery['joins']
2882 );
2883
2884 // If SCHEMA_COMPAT_WRITE_OLD is set, also select all extra fields we still write,
2885 // so we can copy it to the archive table.
2886 // We know the fields exist, otherwise SCHEMA_COMPAT_WRITE_OLD could not function.
2887 if ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
2888 $revQuery['fields'][] = 'rev_text_id';
2889
2890 if ( $wgContentHandlerUseDB ) {
2891 $revQuery['fields'][] = 'rev_content_model';
2892 $revQuery['fields'][] = 'rev_content_format';
2893 }
2894 }
2895
2896 // Get as many of the page revisions as we are allowed to. The +1 lets us recognize the
2897 // unusual case where there were exactly $wgDeleteRevisionBatchSize revisions remaining.
2898 $res = $dbw->select(
2899 $revQuery['tables'],
2900 $revQuery['fields'],
2901 [ 'rev_page' => $id ],
2902 __METHOD__,
2903 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $wgDeleteRevisionsBatchSize + 1 ],
2904 $revQuery['joins']
2905 );
2906
2907 // Build their equivalent archive rows
2908 $rowsInsert = [];
2909 $revids = [];
2910
2911 /** @var int[] Revision IDs of edits that were made by IPs */
2912 $ipRevIds = [];
2913
2914 $done = true;
2915 foreach ( $res as $row ) {
2916 if ( count( $revids ) >= $wgDeleteRevisionsBatchSize ) {
2917 $done = false;
2918 break;
2919 }
2920
2921 $comment = $commentStore->getComment( 'rev_comment', $row );
2922 $user = User::newFromAnyId( $row->rev_user, $row->rev_user_text, $row->rev_actor );
2923 $rowInsert = [
2924 'ar_namespace' => $namespace,
2925 'ar_title' => $dbKey,
2926 'ar_timestamp' => $row->rev_timestamp,
2927 'ar_minor_edit' => $row->rev_minor_edit,
2928 'ar_rev_id' => $row->rev_id,
2929 'ar_parent_id' => $row->rev_parent_id,
2930 /**
2931 * ar_text_id should probably not be written to when the multi content schema has
2932 * been migrated to (wgMultiContentRevisionSchemaMigrationStage) however there is no
2933 * default for the field in WMF production currently so we must keep writing
2934 * writing until a default of 0 is set.
2935 * Task: https://phabricator.wikimedia.org/T190148
2936 * Copying the value from the revision table should not lead to any issues for now.
2937 */
2938 'ar_len' => $row->rev_len,
2939 'ar_page_id' => $id,
2940 'ar_deleted' => $suppress ? $bitfield : $row->rev_deleted,
2941 'ar_sha1' => $row->rev_sha1,
2942 ] + $commentStore->insert( $dbw, 'ar_comment', $comment )
2943 + $actorMigration->getInsertValues( $dbw, 'ar_user', $user );
2944
2945 if ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
2946 $rowInsert['ar_text_id'] = $row->rev_text_id;
2947
2948 if ( $wgContentHandlerUseDB ) {
2949 $rowInsert['ar_content_model'] = $row->rev_content_model;
2950 $rowInsert['ar_content_format'] = $row->rev_content_format;
2951 }
2952 }
2953
2954 $rowsInsert[] = $rowInsert;
2955 $revids[] = $row->rev_id;
2956
2957 // Keep track of IP edits, so that the corresponding rows can
2958 // be deleted in the ip_changes table.
2959 if ( (int)$row->rev_user === 0 && IP::isValid( $row->rev_user_text ) ) {
2960 $ipRevIds[] = $row->rev_id;
2961 }
2962 }
2963
2964 // This conditional is just a sanity check
2965 if ( count( $revids ) > 0 ) {
2966 // Copy them into the archive table
2967 $dbw->insert( 'archive', $rowsInsert, __METHOD__ );
2968
2969 $dbw->delete( 'revision', [ 'rev_id' => $revids ], __METHOD__ );
2970 $dbw->delete( 'revision_comment_temp', [ 'revcomment_rev' => $revids ], __METHOD__ );
2971 $dbw->delete( 'revision_actor_temp', [ 'revactor_rev' => $revids ], __METHOD__ );
2972
2973 // Also delete records from ip_changes as applicable.
2974 if ( count( $ipRevIds ) > 0 ) {
2975 $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $ipRevIds ], __METHOD__ );
2976 }
2977 }
2978
2979 return $done;
2980 }
2981
2982 /**
2983 * Lock the page row for this title+id and return page_latest (or 0)
2984 *
2985 * @return int Returns 0 if no row was found with this title+id
2986 * @since 1.27
2987 */
2988 public function lockAndGetLatest() {
2989 return (int)wfGetDB( DB_MASTER )->selectField(
2990 'page',
2991 'page_latest',
2992 [
2993 'page_id' => $this->getId(),
2994 // Typically page_id is enough, but some code might try to do
2995 // updates assuming the title is the same, so verify that
2996 'page_namespace' => $this->getTitle()->getNamespace(),
2997 'page_title' => $this->getTitle()->getDBkey()
2998 ],
2999 __METHOD__,
3000 [ 'FOR UPDATE' ]
3001 );
3002 }
3003
3004 /**
3005 * Do some database updates after deletion
3006 *
3007 * @param int $id The page_id value of the page being deleted
3008 * @param Content|null $content Page content to be used when determining
3009 * the required updates. This may be needed because $this->getContent()
3010 * may already return null when the page proper was deleted.
3011 * @param Revision|null $revision The current page revision at the time of
3012 * deletion, used when determining the required updates. This may be needed because
3013 * $this->getRevision() may already return null when the page proper was deleted.
3014 * @param User|null $user The user that caused the deletion
3015 */
3016 public function doDeleteUpdates(
3017 $id, Content $content = null, Revision $revision = null, User $user = null
3018 ) {
3019 if ( $id !== $this->getId() ) {
3020 throw new InvalidArgumentException( 'Mismatching page ID' );
3021 }
3022
3023 try {
3024 $countable = $this->isCountable();
3025 } catch ( Exception $ex ) {
3026 // fallback for deleting broken pages for which we cannot load the content for
3027 // some reason. Note that doDeleteArticleReal() already logged this problem.
3028 $countable = false;
3029 }
3030
3031 // Update site status
3032 DeferredUpdates::addUpdate( SiteStatsUpdate::factory(
3033 [ 'edits' => 1, 'articles' => -$countable, 'pages' => -1 ]
3034 ) );
3035
3036 // Delete pagelinks, update secondary indexes, etc
3037 $updates = $this->getDeletionUpdates(
3038 $revision ? $revision->getRevisionRecord() : $content
3039 );
3040 foreach ( $updates as $update ) {
3041 DeferredUpdates::addUpdate( $update );
3042 }
3043
3044 $causeAgent = $user ? $user->getName() : 'unknown';
3045 // Reparse any pages transcluding this page
3046 LinksUpdate::queueRecursiveJobsForTable(
3047 $this->mTitle, 'templatelinks', 'delete-page', $causeAgent );
3048 // Reparse any pages including this image
3049 if ( $this->mTitle->getNamespace() == NS_FILE ) {
3050 LinksUpdate::queueRecursiveJobsForTable(
3051 $this->mTitle, 'imagelinks', 'delete-page', $causeAgent );
3052 }
3053
3054 // Clear caches
3055 self::onArticleDelete( $this->mTitle );
3056 ResourceLoaderWikiModule::invalidateModuleCache(
3057 $this->mTitle,
3058 $revision,
3059 null,
3060 WikiMap::getCurrentWikiDbDomain()->getId()
3061 );
3062
3063 // Reset this object and the Title object
3064 $this->loadFromRow( false, self::READ_LATEST );
3065
3066 // Search engine
3067 DeferredUpdates::addUpdate( new SearchUpdate( $id, $this->mTitle ) );
3068 }
3069
3070 /**
3071 * Roll back the most recent consecutive set of edits to a page
3072 * from the same user; fails if there are no eligible edits to
3073 * roll back to, e.g. user is the sole contributor. This function
3074 * performs permissions checks on $user, then calls commitRollback()
3075 * to do the dirty work
3076 *
3077 * @todo Separate the business/permission stuff out from backend code
3078 * @todo Remove $token parameter. Already verified by RollbackAction and ApiRollback.
3079 *
3080 * @param string $fromP Name of the user whose edits to rollback.
3081 * @param string $summary Custom summary. Set to default summary if empty.
3082 * @param string $token Rollback token.
3083 * @param bool $bot If true, mark all reverted edits as bot.
3084 *
3085 * @param array &$resultDetails Array contains result-specific array of additional values
3086 * 'alreadyrolled' : 'current' (rev)
3087 * success : 'summary' (str), 'current' (rev), 'target' (rev)
3088 *
3089 * @param User $user The user performing the rollback
3090 * @param array|null $tags Change tags to apply to the rollback
3091 * Callers are responsible for permission checks
3092 * (with ChangeTags::canAddTagsAccompanyingChange)
3093 *
3094 * @return array Array of errors, each error formatted as
3095 * [ messagekey, param1, param2, ... ].
3096 * On success, the array is empty. This array can also be passed to
3097 * OutputPage::showPermissionsErrorPage().
3098 */
3099 public function doRollback(
3100 $fromP, $summary, $token, $bot, &$resultDetails, User $user, $tags = null
3101 ) {
3102 $resultDetails = null;
3103
3104 // Check permissions
3105 $editErrors = $this->mTitle->getUserPermissionsErrors( 'edit', $user );
3106 $rollbackErrors = $this->mTitle->getUserPermissionsErrors( 'rollback', $user );
3107 $errors = array_merge( $editErrors, wfArrayDiff2( $rollbackErrors, $editErrors ) );
3108
3109 if ( !$user->matchEditToken( $token, 'rollback' ) ) {
3110 $errors[] = [ 'sessionfailure' ];
3111 }
3112
3113 if ( $user->pingLimiter( 'rollback' ) || $user->pingLimiter() ) {
3114 $errors[] = [ 'actionthrottledtext' ];
3115 }
3116
3117 // If there were errors, bail out now
3118 if ( !empty( $errors ) ) {
3119 return $errors;
3120 }
3121
3122 return $this->commitRollback( $fromP, $summary, $bot, $resultDetails, $user, $tags );
3123 }
3124
3125 /**
3126 * Backend implementation of doRollback(), please refer there for parameter
3127 * and return value documentation
3128 *
3129 * NOTE: This function does NOT check ANY permissions, it just commits the
3130 * rollback to the DB. Therefore, you should only call this function direct-
3131 * ly if you want to use custom permissions checks. If you don't, use
3132 * doRollback() instead.
3133 * @param string $fromP Name of the user whose edits to rollback.
3134 * @param string $summary Custom summary. Set to default summary if empty.
3135 * @param bool $bot If true, mark all reverted edits as bot.
3136 *
3137 * @param array &$resultDetails Contains result-specific array of additional values
3138 * @param User $guser The user performing the rollback
3139 * @param array|null $tags Change tags to apply to the rollback
3140 * Callers are responsible for permission checks
3141 * (with ChangeTags::canAddTagsAccompanyingChange)
3142 *
3143 * @return array An array of error messages, as returned by Status::getErrorsArray()
3144 */
3145 public function commitRollback( $fromP, $summary, $bot,
3146 &$resultDetails, User $guser, $tags = null
3147 ) {
3148 global $wgUseRCPatrol, $wgDisableAnonTalk;
3149
3150 $dbw = wfGetDB( DB_MASTER );
3151
3152 if ( wfReadOnly() ) {
3153 return [ [ 'readonlytext' ] ];
3154 }
3155
3156 // Begin revision creation cycle by creating a PageUpdater.
3157 // If the page is changed concurrently after grabParentRevision(), the rollback will fail.
3158 $updater = $this->newPageUpdater( $guser );
3159 $current = $updater->grabParentRevision();
3160
3161 if ( is_null( $current ) ) {
3162 // Something wrong... no page?
3163 return [ [ 'notanarticle' ] ];
3164 }
3165
3166 $currentEditorForPublic = $current->getUser( RevisionRecord::FOR_PUBLIC );
3167 $legacyCurrent = new Revision( $current );
3168 $from = str_replace( '_', ' ', $fromP );
3169
3170 // User name given should match up with the top revision.
3171 // If the revision's user is not visible, then $from should be empty.
3172 if ( $from !== ( $currentEditorForPublic ? $currentEditorForPublic->getName() : '' ) ) {
3173 $resultDetails = [ 'current' => $legacyCurrent ];
3174 return [ [ 'alreadyrolled',
3175 htmlspecialchars( $this->mTitle->getPrefixedText() ),
3176 htmlspecialchars( $fromP ),
3177 htmlspecialchars( $currentEditorForPublic ? $currentEditorForPublic->getName() : '' )
3178 ] ];
3179 }
3180
3181 // Get the last edit not by this person...
3182 // Note: these may not be public values
3183 $actorWhere = ActorMigration::newMigration()->getWhere(
3184 $dbw,
3185 'rev_user',
3186 $current->getUser( RevisionRecord::RAW )
3187 );
3188
3189 $s = $dbw->selectRow(
3190 [ 'revision' ] + $actorWhere['tables'],
3191 [ 'rev_id', 'rev_timestamp', 'rev_deleted' ],
3192 [
3193 'rev_page' => $current->getPageId(),
3194 'NOT(' . $actorWhere['conds'] . ')',
3195 ],
3196 __METHOD__,
3197 [
3198 'USE INDEX' => [ 'revision' => 'page_timestamp' ],
3199 'ORDER BY' => 'rev_timestamp DESC'
3200 ],
3201 $actorWhere['joins']
3202 );
3203 if ( $s === false ) {
3204 // No one else ever edited this page
3205 return [ [ 'cantrollback' ] ];
3206 } elseif ( $s->rev_deleted & RevisionRecord::DELETED_TEXT
3207 || $s->rev_deleted & RevisionRecord::DELETED_USER
3208 ) {
3209 // Only admins can see this text
3210 return [ [ 'notvisiblerev' ] ];
3211 }
3212
3213 // Generate the edit summary if necessary
3214 $target = $this->getRevisionStore()->getRevisionById(
3215 $s->rev_id,
3216 RevisionStore::READ_LATEST
3217 );
3218 if ( empty( $summary ) ) {
3219 if ( !$currentEditorForPublic ) { // no public user name
3220 $summary = wfMessage( 'revertpage-nouser' );
3221 } elseif ( $wgDisableAnonTalk && $current->getUser() === 0 ) {
3222 $summary = wfMessage( 'revertpage-anon' );
3223 } else {
3224 $summary = wfMessage( 'revertpage' );
3225 }
3226 }
3227 $legacyTarget = new Revision( $target );
3228 $targetEditorForPublic = $target->getUser( RevisionRecord::FOR_PUBLIC );
3229
3230 // Allow the custom summary to use the same args as the default message
3231 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
3232 $args = [
3233 $targetEditorForPublic ? $targetEditorForPublic->getName() : null,
3234 $currentEditorForPublic ? $currentEditorForPublic->getName() : null,
3235 $s->rev_id,
3236 $contLang->timeanddate( wfTimestamp( TS_MW, $s->rev_timestamp ) ),
3237 $current->getId(),
3238 $contLang->timeanddate( $current->getTimestamp() )
3239 ];
3240 if ( $summary instanceof Message ) {
3241 $summary = $summary->params( $args )->inContentLanguage()->text();
3242 } else {
3243 $summary = wfMsgReplaceArgs( $summary, $args );
3244 }
3245
3246 // Trim spaces on user supplied text
3247 $summary = trim( $summary );
3248
3249 // Save
3250 $flags = EDIT_UPDATE | EDIT_INTERNAL;
3251
3252 if ( $guser->isAllowed( 'minoredit' ) ) {
3253 $flags |= EDIT_MINOR;
3254 }
3255
3256 if ( $bot && ( MediaWikiServices::getInstance()
3257 ->getPermissionManager()
3258 ->userHasAnyRight( $guser, 'markbotedits', 'bot' ) )
3259 ) {
3260 $flags |= EDIT_FORCE_BOT;
3261 }
3262
3263 // TODO: MCR: also log model changes in other slots, in case that becomes possible!
3264 $currentContent = $current->getContent( SlotRecord::MAIN );
3265 $targetContent = $target->getContent( SlotRecord::MAIN );
3266 $changingContentModel = $targetContent->getModel() !== $currentContent->getModel();
3267
3268 if ( in_array( 'mw-rollback', ChangeTags::getSoftwareTags() ) ) {
3269 $tags[] = 'mw-rollback';
3270 }
3271
3272 // Build rollback revision:
3273 // Restore old content
3274 // TODO: MCR: test this once we can store multiple slots
3275 foreach ( $target->getSlots()->getSlots() as $slot ) {
3276 $updater->inheritSlot( $slot );
3277 }
3278
3279 // Remove extra slots
3280 // TODO: MCR: test this once we can store multiple slots
3281 foreach ( $current->getSlotRoles() as $role ) {
3282 if ( !$target->hasSlot( $role ) ) {
3283 $updater->removeSlot( $role );
3284 }
3285 }
3286
3287 $updater->setOriginalRevisionId( $target->getId() );
3288 // Do not call setUndidRevisionId(), that causes an extra "mw-undo" tag to be added (T190374)
3289 $updater->addTags( $tags );
3290
3291 // TODO: this logic should not be in the storage layer, it's here for compatibility
3292 // with 1.31 behavior. Applying the 'autopatrol' right should be done in the same
3293 // place the 'bot' right is handled, which is currently in EditPage::attemptSave.
3294 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
3295
3296 if ( $wgUseRCPatrol && $permissionManager->userCan(
3297 'autopatrol', $guser, $this->getTitle()
3298 ) ) {
3299 $updater->setRcPatrolStatus( RecentChange::PRC_AUTOPATROLLED );
3300 }
3301
3302 // Actually store the rollback
3303 $rev = $updater->saveRevision(
3304 CommentStoreComment::newUnsavedComment( $summary ),
3305 $flags
3306 );
3307
3308 // Set patrolling and bot flag on the edits, which gets rollbacked.
3309 // This is done even on edit failure to have patrolling in that case (T64157).
3310 $set = [];
3311 if ( $bot && $guser->isAllowed( 'markbotedits' ) ) {
3312 // Mark all reverted edits as bot
3313 $set['rc_bot'] = 1;
3314 }
3315
3316 if ( $wgUseRCPatrol ) {
3317 // Mark all reverted edits as patrolled
3318 $set['rc_patrolled'] = RecentChange::PRC_AUTOPATROLLED;
3319 }
3320
3321 if ( count( $set ) ) {
3322 $actorWhere = ActorMigration::newMigration()->getWhere(
3323 $dbw,
3324 'rc_user',
3325 $current->getUser( RevisionRecord::RAW ),
3326 false
3327 );
3328 $dbw->update( 'recentchanges', $set,
3329 [ /* WHERE */
3330 'rc_cur_id' => $current->getPageId(),
3331 'rc_timestamp > ' . $dbw->addQuotes( $s->rev_timestamp ),
3332 $actorWhere['conds'], // No tables/joins are needed for rc_user
3333 ],
3334 __METHOD__
3335 );
3336 }
3337
3338 if ( !$updater->wasSuccessful() ) {
3339 return $updater->getStatus()->getErrorsArray();
3340 }
3341
3342 // Report if the edit was not created because it did not change the content.
3343 if ( $updater->isUnchanged() ) {
3344 $resultDetails = [ 'current' => $legacyCurrent ];
3345 return [ [ 'alreadyrolled',
3346 htmlspecialchars( $this->mTitle->getPrefixedText() ),
3347 htmlspecialchars( $fromP ),
3348 htmlspecialchars( $currentEditorForPublic ? $currentEditorForPublic->getName() : '' )
3349 ] ];
3350 }
3351
3352 if ( $changingContentModel ) {
3353 // If the content model changed during the rollback,
3354 // make sure it gets logged to Special:Log/contentmodel
3355 $log = new ManualLogEntry( 'contentmodel', 'change' );
3356 $log->setPerformer( $guser );
3357 $log->setTarget( $this->mTitle );
3358 $log->setComment( $summary );
3359 $log->setParameters( [
3360 '4::oldmodel' => $currentContent->getModel(),
3361 '5::newmodel' => $targetContent->getModel(),
3362 ] );
3363
3364 $logId = $log->insert( $dbw );
3365 $log->publish( $logId );
3366 }
3367
3368 $revId = $rev->getId();
3369
3370 Hooks::run( 'ArticleRollbackComplete', [ $this, $guser, $legacyTarget, $legacyCurrent ] );
3371
3372 $resultDetails = [
3373 'summary' => $summary,
3374 'current' => $legacyCurrent,
3375 'target' => $legacyTarget,
3376 'newid' => $revId,
3377 'tags' => $tags
3378 ];
3379
3380 // TODO: make this return a Status object and wrap $resultDetails in that.
3381 return [];
3382 }
3383
3384 /**
3385 * The onArticle*() functions are supposed to be a kind of hooks
3386 * which should be called whenever any of the specified actions
3387 * are done.
3388 *
3389 * This is a good place to put code to clear caches, for instance.
3390 *
3391 * This is called on page move and undelete, as well as edit
3392 *
3393 * @param Title $title
3394 */
3395 public static function onArticleCreate( Title $title ) {
3396 // TODO: move this into a PageEventEmitter service
3397
3398 // Update existence markers on article/talk tabs...
3399 $other = $title->getOtherPage();
3400
3401 $other->purgeSquid();
3402
3403 $title->touchLinks();
3404 $title->purgeSquid();
3405 $title->deleteTitleProtection();
3406
3407 MediaWikiServices::getInstance()->getLinkCache()->invalidateTitle( $title );
3408
3409 // Invalidate caches of articles which include this page
3410 DeferredUpdates::addUpdate(
3411 new HTMLCacheUpdate( $title, 'templatelinks', 'page-create' )
3412 );
3413
3414 if ( $title->getNamespace() == NS_CATEGORY ) {
3415 // Load the Category object, which will schedule a job to create
3416 // the category table row if necessary. Checking a replica DB is ok
3417 // here, in the worst case it'll run an unnecessary recount job on
3418 // a category that probably doesn't have many members.
3419 Category::newFromTitle( $title )->getID();
3420 }
3421 }
3422
3423 /**
3424 * Clears caches when article is deleted
3425 *
3426 * @param Title $title
3427 */
3428 public static function onArticleDelete( Title $title ) {
3429 // TODO: move this into a PageEventEmitter service
3430
3431 // Update existence markers on article/talk tabs...
3432 // Clear Backlink cache first so that purge jobs use more up-to-date backlink information
3433 BacklinkCache::get( $title )->clear();
3434 $other = $title->getOtherPage();
3435
3436 $other->purgeSquid();
3437
3438 $title->touchLinks();
3439 $title->purgeSquid();
3440
3441 MediaWikiServices::getInstance()->getLinkCache()->invalidateTitle( $title );
3442
3443 // File cache
3444 HTMLFileCache::clearFileCache( $title );
3445 InfoAction::invalidateCache( $title );
3446
3447 // Messages
3448 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
3449 MessageCache::singleton()->updateMessageOverride( $title, null );
3450 }
3451
3452 // Images
3453 if ( $title->getNamespace() == NS_FILE ) {
3454 DeferredUpdates::addUpdate(
3455 new HTMLCacheUpdate( $title, 'imagelinks', 'page-delete' )
3456 );
3457 }
3458
3459 // User talk pages
3460 if ( $title->getNamespace() == NS_USER_TALK ) {
3461 $user = User::newFromName( $title->getText(), false );
3462 if ( $user ) {
3463 $user->setNewtalk( false );
3464 }
3465 }
3466
3467 // Image redirects
3468 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $title );
3469
3470 // Purge cross-wiki cache entities referencing this page
3471 self::purgeInterwikiCheckKey( $title );
3472 }
3473
3474 /**
3475 * Purge caches on page update etc
3476 *
3477 * @param Title $title
3478 * @param Revision|null $revision Revision that was just saved, may be null
3479 * @param string[]|null $slotsChanged The role names of the slots that were changed.
3480 * If not given, all slots are assumed to have changed.
3481 */
3482 public static function onArticleEdit(
3483 Title $title,
3484 Revision $revision = null,
3485 $slotsChanged = null
3486 ) {
3487 // TODO: move this into a PageEventEmitter service
3488
3489 if ( $slotsChanged === null || in_array( SlotRecord::MAIN, $slotsChanged ) ) {
3490 // Invalidate caches of articles which include this page.
3491 // Only for the main slot, because only the main slot is transcluded.
3492 // TODO: MCR: not true for TemplateStyles! [SlotHandler]
3493 DeferredUpdates::addUpdate(
3494 new HTMLCacheUpdate( $title, 'templatelinks', 'page-edit' )
3495 );
3496 }
3497
3498 // Invalidate the caches of all pages which redirect here
3499 DeferredUpdates::addUpdate(
3500 new HTMLCacheUpdate( $title, 'redirect', 'page-edit' )
3501 );
3502
3503 MediaWikiServices::getInstance()->getLinkCache()->invalidateTitle( $title );
3504
3505 // Purge CDN for this page only
3506 $title->purgeSquid();
3507 // Clear file cache for this page only
3508 HTMLFileCache::clearFileCache( $title );
3509
3510 // Purge ?action=info cache
3511 $revid = $revision ? $revision->getId() : null;
3512 DeferredUpdates::addCallableUpdate( function () use ( $title, $revid ) {
3513 InfoAction::invalidateCache( $title, $revid );
3514 } );
3515
3516 // Purge cross-wiki cache entities referencing this page
3517 self::purgeInterwikiCheckKey( $title );
3518 }
3519
3520 /** #@- */
3521
3522 /**
3523 * Purge the check key for cross-wiki cache entries referencing this page
3524 *
3525 * @param Title $title
3526 */
3527 private static function purgeInterwikiCheckKey( Title $title ) {
3528 global $wgEnableScaryTranscluding;
3529
3530 if ( !$wgEnableScaryTranscluding ) {
3531 return; // @todo: perhaps this wiki is only used as a *source* for content?
3532 }
3533
3534 DeferredUpdates::addCallableUpdate( function () use ( $title ) {
3535 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
3536 $cache->resetCheckKey(
3537 // Do not include the namespace since there can be multiple aliases to it
3538 // due to different namespace text definitions on different wikis. This only
3539 // means that some cache invalidations happen that are not strictly needed.
3540 $cache->makeGlobalKey(
3541 'interwiki-page',
3542 WikiMap::getCurrentWikiDbDomain()->getId(),
3543 $title->getDBkey()
3544 )
3545 );
3546 } );
3547 }
3548
3549 /**
3550 * Returns a list of categories this page is a member of.
3551 * Results will include hidden categories
3552 *
3553 * @return TitleArray
3554 */
3555 public function getCategories() {
3556 $id = $this->getId();
3557 if ( $id == 0 ) {
3558 return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
3559 }
3560
3561 $dbr = wfGetDB( DB_REPLICA );
3562 $res = $dbr->select( 'categorylinks',
3563 [ 'cl_to AS page_title, ' . NS_CATEGORY . ' AS page_namespace' ],
3564 // Have to do that since Database::fieldNamesWithAlias treats numeric indexes
3565 // as not being aliases, and NS_CATEGORY is numeric
3566 [ 'cl_from' => $id ],
3567 __METHOD__ );
3568
3569 return TitleArray::newFromResult( $res );
3570 }
3571
3572 /**
3573 * Returns a list of hidden categories this page is a member of.
3574 * Uses the page_props and categorylinks tables.
3575 *
3576 * @return array Array of Title objects
3577 */
3578 public function getHiddenCategories() {
3579 $result = [];
3580 $id = $this->getId();
3581
3582 if ( $id == 0 ) {
3583 return [];
3584 }
3585
3586 $dbr = wfGetDB( DB_REPLICA );
3587 $res = $dbr->select( [ 'categorylinks', 'page_props', 'page' ],
3588 [ 'cl_to' ],
3589 [ 'cl_from' => $id, 'pp_page=page_id', 'pp_propname' => 'hiddencat',
3590 'page_namespace' => NS_CATEGORY, 'page_title=cl_to' ],
3591 __METHOD__ );
3592
3593 if ( $res !== false ) {
3594 foreach ( $res as $row ) {
3595 $result[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
3596 }
3597 }
3598
3599 return $result;
3600 }
3601
3602 /**
3603 * Auto-generates a deletion reason
3604 *
3605 * @param bool &$hasHistory Whether the page has a history
3606 * @return string|bool String containing deletion reason or empty string, or boolean false
3607 * if no revision occurred
3608 */
3609 public function getAutoDeleteReason( &$hasHistory ) {
3610 return $this->getContentHandler()->getAutoDeleteReason( $this->getTitle(), $hasHistory );
3611 }
3612
3613 /**
3614 * Update all the appropriate counts in the category table, given that
3615 * we've added the categories $added and deleted the categories $deleted.
3616 *
3617 * This should only be called from deferred updates or jobs to avoid contention.
3618 *
3619 * @param array $added The names of categories that were added
3620 * @param array $deleted The names of categories that were deleted
3621 * @param int $id Page ID (this should be the original deleted page ID)
3622 */
3623 public function updateCategoryCounts( array $added, array $deleted, $id = 0 ) {
3624 $id = $id ?: $this->getId();
3625 $type = MediaWikiServices::getInstance()->getNamespaceInfo()->
3626 getCategoryLinkType( $this->getTitle()->getNamespace() );
3627
3628 $addFields = [ 'cat_pages = cat_pages + 1' ];
3629 $removeFields = [ 'cat_pages = cat_pages - 1' ];
3630 if ( $type !== 'page' ) {
3631 $addFields[] = "cat_{$type}s = cat_{$type}s + 1";
3632 $removeFields[] = "cat_{$type}s = cat_{$type}s - 1";
3633 }
3634
3635 $dbw = wfGetDB( DB_MASTER );
3636
3637 if ( count( $added ) ) {
3638 $existingAdded = $dbw->selectFieldValues(
3639 'category',
3640 'cat_title',
3641 [ 'cat_title' => $added ],
3642 __METHOD__
3643 );
3644
3645 // For category rows that already exist, do a plain
3646 // UPDATE instead of INSERT...ON DUPLICATE KEY UPDATE
3647 // to avoid creating gaps in the cat_id sequence.
3648 if ( count( $existingAdded ) ) {
3649 $dbw->update(
3650 'category',
3651 $addFields,
3652 [ 'cat_title' => $existingAdded ],
3653 __METHOD__
3654 );
3655 }
3656
3657 $missingAdded = array_diff( $added, $existingAdded );
3658 if ( count( $missingAdded ) ) {
3659 $insertRows = [];
3660 foreach ( $missingAdded as $cat ) {
3661 $insertRows[] = [
3662 'cat_title' => $cat,
3663 'cat_pages' => 1,
3664 'cat_subcats' => ( $type === 'subcat' ) ? 1 : 0,
3665 'cat_files' => ( $type === 'file' ) ? 1 : 0,
3666 ];
3667 }
3668 $dbw->upsert(
3669 'category',
3670 $insertRows,
3671 [ 'cat_title' ],
3672 $addFields,
3673 __METHOD__
3674 );
3675 }
3676 }
3677
3678 if ( count( $deleted ) ) {
3679 $dbw->update(
3680 'category',
3681 $removeFields,
3682 [ 'cat_title' => $deleted ],
3683 __METHOD__
3684 );
3685 }
3686
3687 foreach ( $added as $catName ) {
3688 $cat = Category::newFromName( $catName );
3689 Hooks::run( 'CategoryAfterPageAdded', [ $cat, $this ] );
3690 }
3691
3692 foreach ( $deleted as $catName ) {
3693 $cat = Category::newFromName( $catName );
3694 Hooks::run( 'CategoryAfterPageRemoved', [ $cat, $this, $id ] );
3695 // Refresh counts on categories that should be empty now (after commit, T166757)
3696 DeferredUpdates::addCallableUpdate( function () use ( $cat ) {
3697 $cat->refreshCountsIfEmpty();
3698 } );
3699 }
3700 }
3701
3702 /**
3703 * Opportunistically enqueue link update jobs given fresh parser output if useful
3704 *
3705 * @param ParserOutput $parserOutput Current version page output
3706 * @since 1.25
3707 */
3708 public function triggerOpportunisticLinksUpdate( ParserOutput $parserOutput ) {
3709 if ( wfReadOnly() ) {
3710 return;
3711 }
3712
3713 if ( !Hooks::run( 'OpportunisticLinksUpdate',
3714 [ $this, $this->mTitle, $parserOutput ]
3715 ) ) {
3716 return;
3717 }
3718
3719 $config = RequestContext::getMain()->getConfig();
3720
3721 $params = [
3722 'isOpportunistic' => true,
3723 'rootJobTimestamp' => $parserOutput->getCacheTime()
3724 ];
3725
3726 if ( $this->mTitle->areRestrictionsCascading() ) {
3727 // If the page is cascade protecting, the links should really be up-to-date
3728 JobQueueGroup::singleton()->lazyPush(
3729 RefreshLinksJob::newPrioritized( $this->mTitle, $params )
3730 );
3731 } elseif ( !$config->get( 'MiserMode' ) && $parserOutput->hasDynamicContent() ) {
3732 // Assume the output contains "dynamic" time/random based magic words.
3733 // Only update pages that expired due to dynamic content and NOT due to edits
3734 // to referenced templates/files. When the cache expires due to dynamic content,
3735 // page_touched is unchanged. We want to avoid triggering redundant jobs due to
3736 // views of pages that were just purged via HTMLCacheUpdateJob. In that case, the
3737 // template/file edit already triggered recursive RefreshLinksJob jobs.
3738 if ( $this->getLinksTimestamp() > $this->getTouched() ) {
3739 // If a page is uncacheable, do not keep spamming a job for it.
3740 // Although it would be de-duplicated, it would still waste I/O.
3741 $cache = ObjectCache::getLocalClusterInstance();
3742 $key = $cache->makeKey( 'dynamic-linksupdate', 'last', $this->getId() );
3743 $ttl = max( $parserOutput->getCacheExpiry(), 3600 );
3744 if ( $cache->add( $key, time(), $ttl ) ) {
3745 JobQueueGroup::singleton()->lazyPush(
3746 RefreshLinksJob::newDynamic( $this->mTitle, $params )
3747 );
3748 }
3749 }
3750 }
3751 }
3752
3753 /**
3754 * Returns a list of updates to be performed when this page is deleted. The
3755 * updates should remove any information about this page from secondary data
3756 * stores such as links tables.
3757 *
3758 * @param RevisionRecord|Content|null $rev The revision being deleted. Also accepts a Content
3759 * object for backwards compatibility.
3760 * @return DeferrableUpdate[]
3761 */
3762 public function getDeletionUpdates( $rev = null ) {
3763 if ( !$rev ) {
3764 wfDeprecated( __METHOD__ . ' without a RevisionRecord', '1.32' );
3765
3766 try {
3767 $rev = $this->getRevisionRecord();
3768 } catch ( Exception $ex ) {
3769 // If we can't load the content, something is wrong. Perhaps that's why
3770 // the user is trying to delete the page, so let's not fail in that case.
3771 // Note that doDeleteArticleReal() will already have logged an issue with
3772 // loading the content.
3773 wfDebug( __METHOD__ . ' failed to load current revision of page ' . $this->getId() );
3774 }
3775 }
3776
3777 if ( !$rev ) {
3778 $slotContent = [];
3779 } elseif ( $rev instanceof Content ) {
3780 wfDeprecated( __METHOD__ . ' with a Content object instead of a RevisionRecord', '1.32' );
3781
3782 $slotContent = [ SlotRecord::MAIN => $rev ];
3783 } else {
3784 $slotContent = array_map( function ( SlotRecord $slot ) {
3785 return $slot->getContent( RevisionRecord::RAW );
3786 }, $rev->getSlots()->getSlots() );
3787 }
3788
3789 $allUpdates = [ new LinksDeletionUpdate( $this ) ];
3790
3791 // NOTE: once Content::getDeletionUpdates() is removed, we only need to content
3792 // model here, not the content object!
3793 // TODO: consolidate with similar logic in DerivedPageDataUpdater::getSecondaryDataUpdates()
3794 /** @var Content $content */
3795 foreach ( $slotContent as $role => $content ) {
3796 $handler = $content->getContentHandler();
3797
3798 $updates = $handler->getDeletionUpdates(
3799 $this->getTitle(),
3800 $role
3801 );
3802 $allUpdates = array_merge( $allUpdates, $updates );
3803
3804 // TODO: remove B/C hack in 1.32!
3805 $legacyUpdates = $content->getDeletionUpdates( $this );
3806
3807 // HACK: filter out redundant and incomplete LinksDeletionUpdate
3808 $legacyUpdates = array_filter( $legacyUpdates, function ( $update ) {
3809 return !( $update instanceof LinksDeletionUpdate );
3810 } );
3811
3812 $allUpdates = array_merge( $allUpdates, $legacyUpdates );
3813 }
3814
3815 Hooks::run( 'PageDeletionDataUpdates', [ $this->getTitle(), $rev, &$allUpdates ] );
3816
3817 // TODO: hard deprecate old hook in 1.33
3818 Hooks::run( 'WikiPageDeletionUpdates', [ $this, $content, &$allUpdates ] );
3819 return $allUpdates;
3820 }
3821
3822 /**
3823 * Whether this content displayed on this page
3824 * comes from the local database
3825 *
3826 * @since 1.28
3827 * @return bool
3828 */
3829 public function isLocal() {
3830 return true;
3831 }
3832
3833 /**
3834 * The display name for the site this content
3835 * come from. If a subclass overrides isLocal(),
3836 * this could return something other than the
3837 * current site name
3838 *
3839 * @since 1.28
3840 * @return string
3841 */
3842 public function getWikiDisplayName() {
3843 global $wgSitename;
3844 return $wgSitename;
3845 }
3846
3847 /**
3848 * Get the source URL for the content on this page,
3849 * typically the canonical URL, but may be a remote
3850 * link if the content comes from another site
3851 *
3852 * @since 1.28
3853 * @return string
3854 */
3855 public function getSourceURL() {
3856 return $this->getTitle()->getCanonicalURL();
3857 }
3858
3859 /**
3860 * @param WANObjectCache $cache
3861 * @return string[]
3862 * @since 1.28
3863 */
3864 public function getMutableCacheKeys( WANObjectCache $cache ) {
3865 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
3866
3867 return $linkCache->getMutableCacheKeys( $cache, $this->getTitle() );
3868 }
3869
3870 }