build: Upgrade mediawiki-codesniffer from 26.0.0 to 28.0.0
[lhc/web/wiklou.git] / includes / Revision.php
1 <?php
2 /**
3 * Representation of a page version.
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\Revision\MutableRevisionRecord;
24 use MediaWiki\Revision\RevisionAccessException;
25 use MediaWiki\Revision\RevisionFactory;
26 use MediaWiki\Revision\RevisionLookup;
27 use MediaWiki\Revision\RevisionRecord;
28 use MediaWiki\Revision\RevisionStore;
29 use MediaWiki\Revision\RevisionStoreRecord;
30 use MediaWiki\Revision\SlotRecord;
31 use MediaWiki\Storage\SqlBlobStore;
32 use Wikimedia\Assert\Assert;
33 use Wikimedia\Rdbms\IDatabase;
34 use MediaWiki\Linker\LinkTarget;
35 use MediaWiki\MediaWikiServices;
36
37 /**
38 * @deprecated since 1.31, use RevisionRecord, RevisionStore, and BlobStore instead.
39 */
40 class Revision implements IDBAccessObject {
41
42 /** @var RevisionRecord */
43 protected $mRecord;
44
45 // Revision deletion constants
46 const DELETED_TEXT = RevisionRecord::DELETED_TEXT;
47 const DELETED_COMMENT = RevisionRecord::DELETED_COMMENT;
48 const DELETED_USER = RevisionRecord::DELETED_USER;
49 const DELETED_RESTRICTED = RevisionRecord::DELETED_RESTRICTED;
50 const SUPPRESSED_USER = RevisionRecord::SUPPRESSED_USER;
51 const SUPPRESSED_ALL = RevisionRecord::SUPPRESSED_ALL;
52
53 // Audience options for accessors
54 const FOR_PUBLIC = RevisionRecord::FOR_PUBLIC;
55 const FOR_THIS_USER = RevisionRecord::FOR_THIS_USER;
56 const RAW = RevisionRecord::RAW;
57
58 const TEXT_CACHE_GROUP = SqlBlobStore::TEXT_CACHE_GROUP;
59
60 /**
61 * @return RevisionStore
62 */
63 protected static function getRevisionStore( $wiki = false ) {
64 if ( $wiki ) {
65 return MediaWikiServices::getInstance()->getRevisionStoreFactory()
66 ->getRevisionStore( $wiki );
67 } else {
68 return MediaWikiServices::getInstance()->getRevisionStore();
69 }
70 }
71
72 /**
73 * @return RevisionLookup
74 */
75 protected static function getRevisionLookup() {
76 return MediaWikiServices::getInstance()->getRevisionLookup();
77 }
78
79 /**
80 * @return RevisionFactory
81 */
82 protected static function getRevisionFactory() {
83 return MediaWikiServices::getInstance()->getRevisionFactory();
84 }
85
86 /**
87 * @param bool|string $wiki The ID of the target wiki database. Use false for the local wiki.
88 *
89 * @return SqlBlobStore
90 */
91 protected static function getBlobStore( $wiki = false ) {
92 // @phan-suppress-next-line PhanAccessMethodInternal
93 $store = MediaWikiServices::getInstance()
94 ->getBlobStoreFactory()
95 ->newSqlBlobStore( $wiki );
96
97 if ( !$store instanceof SqlBlobStore ) {
98 throw new RuntimeException(
99 'The backwards compatibility code in Revision currently requires the BlobStore '
100 . 'service to be an SqlBlobStore instance, but it is a ' . get_class( $store )
101 );
102 }
103
104 return $store;
105 }
106
107 /**
108 * Load a page revision from a given revision ID number.
109 * Returns null if no such revision can be found.
110 *
111 * $flags include:
112 * Revision::READ_LATEST : Select the data from the master
113 * Revision::READ_LOCKING : Select & lock the data from the master
114 *
115 * @param int $id
116 * @param int $flags (optional)
117 * @return Revision|null
118 */
119 public static function newFromId( $id, $flags = 0 ) {
120 $rec = self::getRevisionLookup()->getRevisionById( $id, $flags );
121 return $rec ? new Revision( $rec, $flags ) : null;
122 }
123
124 /**
125 * Load either the current, or a specified, revision
126 * that's attached to a given link target. If not attached
127 * to that link target, will return null.
128 *
129 * $flags include:
130 * Revision::READ_LATEST : Select the data from the master
131 * Revision::READ_LOCKING : Select & lock the data from the master
132 *
133 * @param LinkTarget $linkTarget
134 * @param int $id (optional)
135 * @param int $flags Bitfield (optional)
136 * @return Revision|null
137 */
138 public static function newFromTitle( LinkTarget $linkTarget, $id = 0, $flags = 0 ) {
139 $rec = self::getRevisionLookup()->getRevisionByTitle( $linkTarget, $id, $flags );
140 return $rec ? new Revision( $rec, $flags ) : null;
141 }
142
143 /**
144 * Load either the current, or a specified, revision
145 * that's attached to a given page ID.
146 * Returns null if no such revision can be found.
147 *
148 * $flags include:
149 * Revision::READ_LATEST : Select the data from the master (since 1.20)
150 * Revision::READ_LOCKING : Select & lock the data from the master
151 *
152 * @param int $pageId
153 * @param int $revId (optional)
154 * @param int $flags Bitfield (optional)
155 * @return Revision|null
156 */
157 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
158 $rec = self::getRevisionLookup()->getRevisionByPageId( $pageId, $revId, $flags );
159 return $rec ? new Revision( $rec, $flags ) : null;
160 }
161
162 /**
163 * Make a fake revision object from an archive table row. This is queried
164 * for permissions or even inserted (as in Special:Undelete)
165 *
166 * @param object $row
167 * @param array $overrides
168 *
169 * @throws MWException
170 * @return Revision
171 */
172 public static function newFromArchiveRow( $row, $overrides = [] ) {
173 /**
174 * MCR Migration: https://phabricator.wikimedia.org/T183564
175 * This method used to overwrite attributes, then passed to Revision::__construct
176 * RevisionStore::newRevisionFromArchiveRow instead overrides row field names
177 * So do a conversion here.
178 */
179 if ( array_key_exists( 'page', $overrides ) ) {
180 $overrides['page_id'] = $overrides['page'];
181 unset( $overrides['page'] );
182 }
183
184 /**
185 * We require a Title for both the Revision object and the RevisionRecord.
186 * Below is duplicated logic from RevisionStore::newRevisionFromArchiveRow
187 * to fetch a title in order pass it into the Revision object.
188 */
189 $title = null;
190 if ( isset( $overrides['title'] ) ) {
191 if ( !( $overrides['title'] instanceof Title ) ) {
192 throw new MWException( 'title field override must contain a Title object.' );
193 }
194
195 $title = $overrides['title'];
196 }
197 if ( $title !== null ) {
198 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
199 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
200 } else {
201 throw new InvalidArgumentException(
202 'A Title or ar_namespace and ar_title must be given'
203 );
204 }
205 }
206
207 $rec = self::getRevisionFactory()->newRevisionFromArchiveRow( $row, 0, $title, $overrides );
208 return new Revision( $rec, self::READ_NORMAL, $title );
209 }
210
211 /**
212 * @since 1.19
213 *
214 * MCR migration note: replaced by RevisionStore::newRevisionFromRow(). Note that
215 * newFromRow() also accepts arrays, while newRevisionFromRow() does not. Instead,
216 * a MutableRevisionRecord should be constructed directly.
217 * RevisionStore::newMutableRevisionFromArray() can be used as a temporary replacement,
218 * but should be avoided.
219 *
220 * @param object|array $row
221 * @return Revision
222 */
223 public static function newFromRow( $row ) {
224 if ( is_array( $row ) ) {
225 $rec = self::getRevisionFactory()->newMutableRevisionFromArray( $row );
226 } else {
227 $rec = self::getRevisionFactory()->newRevisionFromRow( $row );
228 }
229
230 return new Revision( $rec );
231 }
232
233 /**
234 * Load a page revision from a given revision ID number.
235 * Returns null if no such revision can be found.
236 *
237 * @deprecated since 1.31, use RevisionStore::getRevisionById() instead.
238 *
239 * @param IDatabase $db
240 * @param int $id
241 * @return Revision|null
242 */
243 public static function loadFromId( $db, $id ) {
244 wfDeprecated( __METHOD__, '1.31' ); // no known callers
245 $rec = self::getRevisionStore()->loadRevisionFromId( $db, $id );
246 return $rec ? new Revision( $rec ) : null;
247 }
248
249 /**
250 * Load either the current, or a specified, revision
251 * that's attached to a given page. If not attached
252 * to that page, will return null.
253 *
254 * @deprecated since 1.31, use RevisionStore::getRevisionByPageId() instead.
255 *
256 * @param IDatabase $db
257 * @param int $pageid
258 * @param int $id
259 * @return Revision|null
260 */
261 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
262 $rec = self::getRevisionStore()->loadRevisionFromPageId( $db, $pageid, $id );
263 return $rec ? new Revision( $rec ) : null;
264 }
265
266 /**
267 * Load either the current, or a specified, revision
268 * that's attached to a given page. If not attached
269 * to that page, will return null.
270 *
271 * @deprecated since 1.31, use RevisionStore::getRevisionByTitle() instead.
272 *
273 * @param IDatabase $db
274 * @param Title $title
275 * @param int $id
276 * @return Revision|null
277 */
278 public static function loadFromTitle( $db, $title, $id = 0 ) {
279 $rec = self::getRevisionStore()->loadRevisionFromTitle( $db, $title, $id );
280 return $rec ? new Revision( $rec ) : null;
281 }
282
283 /**
284 * Load the revision for the given title with the given timestamp.
285 * WARNING: Timestamps may in some circumstances not be unique,
286 * so this isn't the best key to use.
287 *
288 * @deprecated since 1.31, use RevisionStore::getRevisionByTimestamp()
289 * or RevisionStore::loadRevisionFromTimestamp() instead.
290 *
291 * @param IDatabase $db
292 * @param Title $title
293 * @param string $timestamp
294 * @return Revision|null
295 */
296 public static function loadFromTimestamp( $db, $title, $timestamp ) {
297 $rec = self::getRevisionStore()->loadRevisionFromTimestamp( $db, $title, $timestamp );
298 return $rec ? new Revision( $rec ) : null;
299 }
300
301 /**
302 * Return the tables, fields, and join conditions to be selected to create
303 * a new revision object.
304 * @since 1.31
305 * @deprecated since 1.31, use RevisionStore::getQueryInfo() instead.
306 * @param array $options Any combination of the following strings
307 * - 'page': Join with the page table, and select fields to identify the page
308 * - 'user': Join with the user table, and select the user name
309 * - 'text': Join with the text table, and select fields to load page text
310 * @return array With three keys:
311 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
312 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
313 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
314 */
315 public static function getQueryInfo( $options = [] ) {
316 return self::getRevisionStore()->getQueryInfo( $options );
317 }
318
319 /**
320 * Return the tables, fields, and join conditions to be selected to create
321 * a new archived revision object.
322 * @since 1.31
323 * @deprecated since 1.31, use RevisionStore::getArchiveQueryInfo() instead.
324 * @return array With three keys:
325 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
326 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
327 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
328 */
329 public static function getArchiveQueryInfo() {
330 return self::getRevisionStore()->getArchiveQueryInfo();
331 }
332
333 /**
334 * Do a batched query to get the parent revision lengths
335 *
336 * @deprecated in 1.31, use RevisionStore::getRevisionSizes instead.
337 *
338 * @param IDatabase $db
339 * @param array $revIds
340 * @return array
341 */
342 public static function getParentLengths( $db, array $revIds ) {
343 return self::getRevisionStore()->listRevisionSizes( $db, $revIds );
344 }
345
346 /**
347 * @param object|array|RevisionRecord $row Either a database row or an array
348 * @param int $queryFlags
349 * @param Title|null $title
350 *
351 * @private
352 */
353 function __construct( $row, $queryFlags = 0, Title $title = null ) {
354 global $wgUser;
355
356 if ( $row instanceof RevisionRecord ) {
357 $this->mRecord = $row;
358 } elseif ( is_array( $row ) ) {
359 // If no user is specified, fall back to using the global user object, to stay
360 // compatible with pre-1.31 behavior.
361 if ( !isset( $row['user'] ) && !isset( $row['user_text'] ) ) {
362 $row['user'] = $wgUser;
363 }
364
365 $this->mRecord = self::getRevisionFactory()->newMutableRevisionFromArray(
366 $row,
367 $queryFlags,
368 $this->ensureTitle( $row, $queryFlags, $title )
369 );
370 } elseif ( is_object( $row ) ) {
371 $this->mRecord = self::getRevisionFactory()->newRevisionFromRow(
372 $row,
373 $queryFlags,
374 $this->ensureTitle( $row, $queryFlags, $title )
375 );
376 } else {
377 throw new InvalidArgumentException(
378 '$row must be a row object, an associative array, or a RevisionRecord'
379 );
380 }
381
382 Assert::postcondition( $this->mRecord !== null, 'Failed to construct a RevisionRecord' );
383 }
384
385 /**
386 * Make sure we have *some* Title object for use by the constructor.
387 * For B/C, the constructor shouldn't fail even for a bad page ID or bad revision ID.
388 *
389 * @param array|object $row
390 * @param int $queryFlags
391 * @param Title|null $title
392 *
393 * @return Title $title if not null, or a Title constructed from information in $row.
394 */
395 private function ensureTitle( $row, $queryFlags, $title = null ) {
396 if ( $title ) {
397 return $title;
398 }
399
400 if ( is_array( $row ) ) {
401 if ( isset( $row['title'] ) ) {
402 if ( !( $row['title'] instanceof Title ) ) {
403 throw new MWException( 'title field must contain a Title object.' );
404 }
405
406 return $row['title'];
407 }
408
409 $pageId = $row['page'] ?? 0;
410 $revId = $row['id'] ?? 0;
411 } else {
412 $pageId = $row->rev_page ?? 0;
413 $revId = $row->rev_id ?? 0;
414 }
415
416 try {
417 $title = self::getRevisionStore()->getTitle( $pageId, $revId, $queryFlags );
418 } catch ( RevisionAccessException $ex ) {
419 // construct a dummy title!
420 wfLogWarning( __METHOD__ . ': ' . $ex->getMessage() );
421
422 // NOTE: this Title will only be used inside RevisionRecord
423 $title = Title::makeTitleSafe( NS_SPECIAL, "Badtitle/ID=$pageId" );
424 $title->resetArticleID( $pageId );
425 }
426
427 return $title;
428 }
429
430 /**
431 * @return RevisionRecord
432 */
433 public function getRevisionRecord() {
434 return $this->mRecord;
435 }
436
437 /**
438 * Get revision ID
439 *
440 * @return int|null
441 */
442 public function getId() {
443 return $this->mRecord->getId();
444 }
445
446 /**
447 * Set the revision ID
448 *
449 * This should only be used for proposed revisions that turn out to be null edits.
450 *
451 * @note Only supported on Revisions that were constructed based on associative arrays,
452 * since they are mutable.
453 *
454 * @since 1.19
455 * @param int|string $id
456 * @throws MWException
457 */
458 public function setId( $id ) {
459 if ( $this->mRecord instanceof MutableRevisionRecord ) {
460 $this->mRecord->setId( intval( $id ) );
461 } else {
462 throw new MWException( __METHOD__ . ' is not supported on this instance' );
463 }
464 }
465
466 /**
467 * Set the user ID/name
468 *
469 * This should only be used for proposed revisions that turn out to be null edits
470 *
471 * @note Only supported on Revisions that were constructed based on associative arrays,
472 * since they are mutable.
473 *
474 * @since 1.28
475 * @deprecated since 1.31, please reuse old Revision object
476 * @param int $id User ID
477 * @param string $name User name
478 * @throws MWException
479 */
480 public function setUserIdAndName( $id, $name ) {
481 if ( $this->mRecord instanceof MutableRevisionRecord ) {
482 $user = User::newFromAnyId( intval( $id ), $name, null );
483 $this->mRecord->setUser( $user );
484 } else {
485 throw new MWException( __METHOD__ . ' is not supported on this instance' );
486 }
487 }
488
489 /**
490 * @return SlotRecord
491 */
492 private function getMainSlotRaw() {
493 return $this->mRecord->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
494 }
495
496 /**
497 * Get the ID of the row of the text table that contains the content of the
498 * revision's main slot, if that content is stored in the text table.
499 *
500 * If the content is stored elsewhere, this returns null.
501 *
502 * @deprecated since 1.31, use RevisionRecord()->getSlot()->getContentAddress() to
503 * get that actual address that can be used with BlobStore::getBlob(); or use
504 * RevisionRecord::hasSameContent() to check if two revisions have the same content.
505 *
506 * @return int|null
507 */
508 public function getTextId() {
509 $slot = $this->getMainSlotRaw();
510 return $slot->hasAddress()
511 ? self::getBlobStore()->getTextIdFromAddress( $slot->getAddress() )
512 : null;
513 }
514
515 /**
516 * Get parent revision ID (the original previous page revision)
517 *
518 * @return int|null The ID of the parent revision. 0 indicates that there is no
519 * parent revision. Null indicates that the parent revision is not known.
520 */
521 public function getParentId() {
522 return $this->mRecord->getParentId();
523 }
524
525 /**
526 * Returns the length of the text in this revision, or null if unknown.
527 *
528 * @return int|null
529 */
530 public function getSize() {
531 try {
532 return $this->mRecord->getSize();
533 } catch ( RevisionAccessException $ex ) {
534 return null;
535 }
536 }
537
538 /**
539 * Returns the base36 sha1 of the content in this revision, or null if unknown.
540 *
541 * @return string|null
542 */
543 public function getSha1() {
544 try {
545 return $this->mRecord->getSha1();
546 } catch ( RevisionAccessException $ex ) {
547 return null;
548 }
549 }
550
551 /**
552 * Returns the title of the page associated with this entry.
553 * Since 1.31, this will never return null.
554 *
555 * Will do a query, when title is not set and id is given.
556 *
557 * @return Title
558 */
559 public function getTitle() {
560 $linkTarget = $this->mRecord->getPageAsLinkTarget();
561 return Title::newFromLinkTarget( $linkTarget );
562 }
563
564 /**
565 * Set the title of the revision
566 *
567 * @deprecated since 1.31, this is now a noop. Pass the Title to the constructor instead.
568 *
569 * @param Title $title
570 */
571 public function setTitle( $title ) {
572 if ( !$title->equals( $this->getTitle() ) ) {
573 throw new InvalidArgumentException(
574 $title->getPrefixedText()
575 . ' is not the same as '
576 . $this->mRecord->getPageAsLinkTarget()->__toString()
577 );
578 }
579 }
580
581 /**
582 * Get the page ID
583 *
584 * @return int|null
585 */
586 public function getPage() {
587 return $this->mRecord->getPageId();
588 }
589
590 /**
591 * Fetch revision's user id if it's available to the specified audience.
592 * If the specified audience does not have access to it, zero will be
593 * returned.
594 *
595 * @param int $audience One of:
596 * Revision::FOR_PUBLIC to be displayed to all users
597 * Revision::FOR_THIS_USER to be displayed to the given user
598 * Revision::RAW get the ID regardless of permissions
599 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
600 * to the $audience parameter
601 * @return int
602 */
603 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
604 global $wgUser;
605
606 if ( $audience === self::FOR_THIS_USER && !$user ) {
607 $user = $wgUser;
608 }
609
610 $user = $this->mRecord->getUser( $audience, $user );
611 return $user ? $user->getId() : 0;
612 }
613
614 /**
615 * Fetch revision's username if it's available to the specified audience.
616 * If the specified audience does not have access to the username, an
617 * empty string will be returned.
618 *
619 * @param int $audience One of:
620 * Revision::FOR_PUBLIC to be displayed to all users
621 * Revision::FOR_THIS_USER to be displayed to the given user
622 * Revision::RAW get the text regardless of permissions
623 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
624 * to the $audience parameter
625 * @return string
626 */
627 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
628 global $wgUser;
629
630 if ( $audience === self::FOR_THIS_USER && !$user ) {
631 $user = $wgUser;
632 }
633
634 $user = $this->mRecord->getUser( $audience, $user );
635 return $user ? $user->getName() : '';
636 }
637
638 /**
639 * @param int $audience One of:
640 * Revision::FOR_PUBLIC to be displayed to all users
641 * Revision::FOR_THIS_USER to be displayed to the given user
642 * Revision::RAW get the text regardless of permissions
643 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
644 * to the $audience parameter
645 *
646 * @return string|null Returns null if the specified audience does not have access to the
647 * comment.
648 */
649 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
650 global $wgUser;
651
652 if ( $audience === self::FOR_THIS_USER && !$user ) {
653 $user = $wgUser;
654 }
655
656 $comment = $this->mRecord->getComment( $audience, $user );
657 return $comment === null ? null : $comment->text;
658 }
659
660 /**
661 * @return bool
662 */
663 public function isMinor() {
664 return $this->mRecord->isMinor();
665 }
666
667 /**
668 * @return int Rcid of the unpatrolled row, zero if there isn't one
669 */
670 public function isUnpatrolled() {
671 return self::getRevisionStore()->getRcIdIfUnpatrolled( $this->mRecord );
672 }
673
674 /**
675 * Get the RC object belonging to the current revision, if there's one
676 *
677 * @param int $flags (optional) $flags include:
678 * Revision::READ_LATEST : Select the data from the master
679 *
680 * @since 1.22
681 * @return RecentChange|null
682 */
683 public function getRecentChange( $flags = 0 ) {
684 return self::getRevisionStore()->getRecentChange( $this->mRecord, $flags );
685 }
686
687 /**
688 * @param int $field One of DELETED_* bitfield constants
689 *
690 * @return bool
691 */
692 public function isDeleted( $field ) {
693 return $this->mRecord->isDeleted( $field );
694 }
695
696 /**
697 * Get the deletion bitfield of the revision
698 *
699 * @return int
700 */
701 public function getVisibility() {
702 return $this->mRecord->getVisibility();
703 }
704
705 /**
706 * Fetch revision content if it's available to the specified audience.
707 * If the specified audience does not have the ability to view this
708 * revision, or the content could not be loaded, null will be returned.
709 *
710 * @param int $audience One of:
711 * Revision::FOR_PUBLIC to be displayed to all users
712 * Revision::FOR_THIS_USER to be displayed to $user
713 * Revision::RAW get the text regardless of permissions
714 * @param User|null $user User object to check for, only if FOR_THIS_USER is passed
715 * to the $audience parameter
716 * @since 1.21
717 * @return Content|null
718 */
719 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
720 global $wgUser;
721
722 if ( $audience === self::FOR_THIS_USER && !$user ) {
723 $user = $wgUser;
724 }
725
726 try {
727 return $this->mRecord->getContent( SlotRecord::MAIN, $audience, $user );
728 }
729 catch ( RevisionAccessException $e ) {
730 return null;
731 }
732 }
733
734 /**
735 * Get original serialized data (without checking view restrictions)
736 *
737 * @since 1.21
738 * @deprecated since 1.31, use BlobStore::getBlob instead.
739 *
740 * @return string
741 */
742 public function getSerializedData() {
743 $slot = $this->getMainSlotRaw();
744 return $slot->getContent()->serialize();
745 }
746
747 /**
748 * Returns the content model for the main slot of this revision.
749 *
750 * If no content model was stored in the database, the default content model for the title is
751 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
752 * is used as a last resort.
753 *
754 * @todo drop this, with MCR, there no longer is a single model associated with a revision.
755 *
756 * @return string The content model id associated with this revision,
757 * see the CONTENT_MODEL_XXX constants.
758 */
759 public function getContentModel() {
760 return $this->getMainSlotRaw()->getModel();
761 }
762
763 /**
764 * Returns the content format for the main slot of this revision.
765 *
766 * If no content format was stored in the database, the default format for this
767 * revision's content model is returned.
768 *
769 * @todo drop this, the format is irrelevant to the revision!
770 *
771 * @return string The content format id associated with this revision,
772 * see the CONTENT_FORMAT_XXX constants.
773 */
774 public function getContentFormat() {
775 $format = $this->getMainSlotRaw()->getFormat();
776
777 if ( $format === null ) {
778 // if no format was stored along with the blob, fall back to default format
779 $format = $this->getContentHandler()->getDefaultFormat();
780 }
781
782 return $format;
783 }
784
785 /**
786 * Returns the content handler appropriate for this revision's content model.
787 *
788 * @throws MWException
789 * @return ContentHandler
790 */
791 public function getContentHandler() {
792 return ContentHandler::getForModelID( $this->getContentModel() );
793 }
794
795 /**
796 * @return string
797 */
798 public function getTimestamp() {
799 return $this->mRecord->getTimestamp();
800 }
801
802 /**
803 * @return bool
804 */
805 public function isCurrent() {
806 return ( $this->mRecord instanceof RevisionStoreRecord ) && $this->mRecord->isCurrent();
807 }
808
809 /**
810 * Get previous revision for this title
811 *
812 * @return Revision|null
813 */
814 public function getPrevious() {
815 $rec = self::getRevisionLookup()->getPreviousRevision( $this->mRecord );
816 return $rec ? new Revision( $rec, self::READ_NORMAL, $this->getTitle() ) : null;
817 }
818
819 /**
820 * Get next revision for this title
821 *
822 * @return Revision|null
823 */
824 public function getNext() {
825 $rec = self::getRevisionLookup()->getNextRevision( $this->mRecord );
826 return $rec ? new Revision( $rec, self::READ_NORMAL, $this->getTitle() ) : null;
827 }
828
829 /**
830 * Get revision text associated with an old or archive row
831 *
832 * If the text field is not included, this uses RevisionStore to load the appropriate slot
833 * and return its serialized content. This is the default backwards-compatibility behavior
834 * when reading from the MCR aware database schema is enabled. For this to work, either
835 * the revision ID or the page ID must be included in the row.
836 *
837 * When using the old text field, the flags field must also be set. Including the old_id
838 * field will activate cache usage as long as the $wiki parameter is not set.
839 *
840 * @deprecated since 1.32, use RevisionStore::newRevisionFromRow instead.
841 *
842 * @param stdClass $row The text data. If a falsy value is passed instead, false is returned.
843 * @param string $prefix Table prefix (default 'old_')
844 * @param string|bool $wiki The name of the wiki to load the revision text from
845 * (same as the wiki $row was loaded from) or false to indicate the local
846 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
847 * identifier as understood by the LoadBalancer class.
848 * @return string|false Text the text requested or false on failure
849 */
850 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
851 global $wgMultiContentRevisionSchemaMigrationStage;
852
853 if ( !$row ) {
854 return false;
855 }
856
857 $textField = $prefix . 'text';
858 $flagsField = $prefix . 'flags';
859
860 if ( isset( $row->$textField ) ) {
861 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
862 // The text field was read, but it's no longer being populated!
863 // We could gloss over this by using the text when it's there and loading
864 // if when it's not, but it seems preferable to complain loudly about a
865 // query that is no longer guaranteed to work reliably.
866 throw new LogicException(
867 'Cannot use ' . __METHOD__ . ' with the ' . $textField . ' field when'
868 . ' $wgMultiContentRevisionSchemaMigrationStage does not include'
869 . ' SCHEMA_COMPAT_WRITE_OLD. The field may not be populated for all revisions!'
870 );
871 }
872
873 $text = $row->$textField;
874 } else {
875 // Missing text field, we are probably looking at the MCR-enabled DB schema.
876
877 if ( !( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
878 // This method should no longer be used with the new schema. Ideally, we
879 // would already trigger a deprecation warning when SCHEMA_COMPAT_READ_NEW is set.
880 wfDeprecated( __METHOD__ . ' (MCR without SCHEMA_COMPAT_WRITE_OLD)', '1.32' );
881 }
882
883 $store = self::getRevisionStore( $wiki );
884 $rev = $prefix === 'ar_'
885 ? $store->newRevisionFromArchiveRow( $row )
886 : $store->newRevisionFromRow( $row );
887
888 $content = $rev->getContent( SlotRecord::MAIN );
889 return $content ? $content->serialize() : false;
890 }
891
892 if ( isset( $row->$flagsField ) ) {
893 $flags = explode( ',', $row->$flagsField );
894 } else {
895 $flags = [];
896 }
897
898 $cacheKey = isset( $row->old_id )
899 ? SqlBlobStore::makeAddressFromTextId( $row->old_id )
900 : null;
901
902 $revisionText = self::getBlobStore( $wiki )->expandBlob( $text, $flags, $cacheKey );
903
904 if ( $revisionText === false ) {
905 if ( isset( $row->old_id ) ) {
906 wfLogWarning( __METHOD__ . ": Bad data in text row {$row->old_id}! " );
907 } else {
908 wfLogWarning( __METHOD__ . ": Bad data in text row! " );
909 }
910 return false;
911 }
912
913 return $revisionText;
914 }
915
916 /**
917 * If $wgCompressRevisions is enabled, we will compress data.
918 * The input string is modified in place.
919 * Return value is the flags field: contains 'gzip' if the
920 * data is compressed, and 'utf-8' if we're saving in UTF-8
921 * mode.
922 *
923 * @param mixed &$text Reference to a text
924 * @return string
925 */
926 public static function compressRevisionText( &$text ) {
927 return self::getBlobStore()->compressData( $text );
928 }
929
930 /**
931 * Re-converts revision text according to it's flags.
932 *
933 * @param mixed $text Reference to a text
934 * @param array $flags Compression flags
935 * @return string|bool Decompressed text, or false on failure
936 */
937 public static function decompressRevisionText( $text, $flags ) {
938 if ( $text === false ) {
939 // Text failed to be fetched; nothing to do
940 return false;
941 }
942
943 return self::getBlobStore()->decompressData( $text, $flags );
944 }
945
946 /**
947 * Insert a new revision into the database, returning the new revision ID
948 * number on success and dies horribly on failure.
949 *
950 * @param IDatabase $dbw (master connection)
951 * @throws MWException
952 * @return int The revision ID
953 */
954 public function insertOn( $dbw ) {
955 global $wgUser;
956
957 // Note that $this->mRecord->getId() will typically return null here, but not always,
958 // e.g. not when restoring a revision.
959
960 if ( $this->mRecord->getUser( RevisionRecord::RAW ) === null ) {
961 if ( $this->mRecord instanceof MutableRevisionRecord ) {
962 $this->mRecord->setUser( $wgUser );
963 } else {
964 throw new MWException( 'Cannot insert revision with no associated user.' );
965 }
966 }
967
968 $rec = self::getRevisionStore()->insertRevisionOn( $this->mRecord, $dbw );
969
970 $this->mRecord = $rec;
971 Assert::postcondition( $this->mRecord !== null, 'Failed to acquire a RevisionRecord' );
972
973 return $rec->getId();
974 }
975
976 /**
977 * Get the base 36 SHA-1 value for a string of text
978 * @param string $text
979 * @return string
980 */
981 public static function base36Sha1( $text ) {
982 return SlotRecord::base36Sha1( $text );
983 }
984
985 /**
986 * Create a new null-revision for insertion into a page's
987 * history. This will not re-save the text, but simply refer
988 * to the text from the previous version.
989 *
990 * Such revisions can for instance identify page rename
991 * operations and other such meta-modifications.
992 *
993 * @param IDatabase $dbw
994 * @param int $pageId ID number of the page to read from
995 * @param string $summary Revision's summary
996 * @param bool $minor Whether the revision should be considered as minor
997 * @param User|null $user User object to use or null for $wgUser
998 * @return Revision|null Revision or null on error
999 */
1000 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1001 global $wgUser;
1002 if ( !$user ) {
1003 $user = $wgUser;
1004 }
1005
1006 $comment = CommentStoreComment::newUnsavedComment( $summary, null );
1007
1008 $title = Title::newFromID( $pageId, Title::READ_LATEST );
1009 if ( $title === null ) {
1010 return null;
1011 }
1012
1013 $rec = self::getRevisionStore()->newNullRevision( $dbw, $title, $comment, $minor, $user );
1014
1015 return $rec ? new Revision( $rec ) : null;
1016 }
1017
1018 /**
1019 * Determine if the current user is allowed to view a particular
1020 * field of this revision, if it's marked as deleted.
1021 *
1022 * @param int $field One of self::DELETED_TEXT,
1023 * self::DELETED_COMMENT,
1024 * self::DELETED_USER
1025 * @param User|null $user User object to check, or null to use $wgUser
1026 * @return bool
1027 */
1028 public function userCan( $field, User $user = null ) {
1029 return self::userCanBitfield( $this->getVisibility(), $field, $user );
1030 }
1031
1032 /**
1033 * Determine if the current user is allowed to view a particular
1034 * field of this revision, if it's marked as deleted. This is used
1035 * by various classes to avoid duplication.
1036 *
1037 * @param int $bitfield Current field
1038 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1039 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1040 * self::DELETED_USER = File::DELETED_USER
1041 * @param User|null $user User object to check, or null to use $wgUser
1042 * @param Title|null $title A Title object to check for per-page restrictions on,
1043 * instead of just plain userrights
1044 * @return bool
1045 */
1046 public static function userCanBitfield( $bitfield, $field, User $user = null,
1047 Title $title = null
1048 ) {
1049 global $wgUser;
1050
1051 if ( !$user ) {
1052 $user = $wgUser;
1053 }
1054
1055 return RevisionRecord::userCanBitfield( $bitfield, $field, $user, $title );
1056 }
1057
1058 /**
1059 * Get rev_timestamp from rev_id, without loading the rest of the row
1060 *
1061 * @param Title $title (ignored since 1.34)
1062 * @param int $id
1063 * @param int $flags
1064 * @return string|bool False if not found
1065 */
1066 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1067 return self::getRevisionStore()->getTimestampFromId( $id, $flags );
1068 }
1069
1070 /**
1071 * Get count of revisions per page...not very efficient
1072 *
1073 * @param IDatabase $db
1074 * @param int $id Page id
1075 * @return int
1076 */
1077 static function countByPageId( $db, $id ) {
1078 return self::getRevisionStore()->countRevisionsByPageId( $db, $id );
1079 }
1080
1081 /**
1082 * Get count of revisions per page...not very efficient
1083 *
1084 * @param IDatabase $db
1085 * @param Title $title
1086 * @return int
1087 */
1088 static function countByTitle( $db, $title ) {
1089 return self::getRevisionStore()->countRevisionsByTitle( $db, $title );
1090 }
1091
1092 /**
1093 * Check if no edits were made by other users since
1094 * the time a user started editing the page. Limit to
1095 * 50 revisions for the sake of performance.
1096 *
1097 * @since 1.20
1098 * @deprecated since 1.24
1099 *
1100 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1101 * Database object or a database identifier usable with wfGetDB.
1102 * @param int $pageId The ID of the page in question
1103 * @param int $userId The ID of the user in question
1104 * @param string $since Look at edits since this time
1105 *
1106 * @return bool True if the given user was the only one to edit since the given timestamp
1107 */
1108 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1109 if ( is_int( $db ) ) {
1110 $db = wfGetDB( $db );
1111 }
1112
1113 return self::getRevisionStore()->userWasLastToEdit( $db, $pageId, $userId, $since );
1114 }
1115
1116 /**
1117 * Load a revision based on a known page ID and current revision ID from the DB
1118 *
1119 * This method allows for the use of caching, though accessing anything that normally
1120 * requires permission checks (aside from the text) will trigger a small DB lookup.
1121 * The title will also be loaded if $pageIdOrTitle is an integer ID.
1122 *
1123 * @param IDatabase $db ignored!
1124 * @param int|Title $pageIdOrTitle Page ID or Title object
1125 * @param int $revId Known current revision of this page. Determined automatically if not given.
1126 * @return Revision|bool Returns false if missing
1127 * @since 1.28
1128 */
1129 public static function newKnownCurrent( IDatabase $db, $pageIdOrTitle, $revId = 0 ) {
1130 $title = $pageIdOrTitle instanceof Title
1131 ? $pageIdOrTitle
1132 : Title::newFromID( $pageIdOrTitle );
1133
1134 if ( !$title ) {
1135 return false;
1136 }
1137
1138 $record = self::getRevisionLookup()->getKnownCurrentRevision( $title, $revId );
1139 return $record ? new Revision( $record ) : false;
1140 }
1141 }