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