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