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