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