Merge "Move up devunt's name to Developers"
[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 use MediaWiki\Linker\LinkTarget;
23
24 /**
25 * @todo document
26 */
27 class Revision implements IDBAccessObject {
28 /** @var int|null */
29 protected $mId;
30 /** @var int|null */
31 protected $mPage;
32 /** @var string */
33 protected $mUserText;
34 /** @var string */
35 protected $mOrigUserText;
36 /** @var int */
37 protected $mUser;
38 /** @var bool */
39 protected $mMinorEdit;
40 /** @var string */
41 protected $mTimestamp;
42 /** @var int */
43 protected $mDeleted;
44 /** @var int */
45 protected $mSize;
46 /** @var string */
47 protected $mSha1;
48 /** @var int */
49 protected $mParentId;
50 /** @var string */
51 protected $mComment;
52 /** @var string */
53 protected $mText;
54 /** @var int */
55 protected $mTextId;
56 /** @var int */
57 protected $mUnpatrolled;
58
59 /** @var stdClass|null */
60 protected $mTextRow;
61
62 /** @var null|Title */
63 protected $mTitle;
64 /** @var bool */
65 protected $mCurrent;
66 /** @var string */
67 protected $mContentModel;
68 /** @var string */
69 protected $mContentFormat;
70
71 /** @var Content|null|bool */
72 protected $mContent;
73 /** @var null|ContentHandler */
74 protected $mContentHandler;
75
76 /** @var int */
77 protected $mQueryFlags = 0;
78 /** @var bool Used for cached values to reload user text and rev_deleted */
79 protected $mRefreshMutableFields = false;
80 /** @var string Wiki ID; false means the current wiki */
81 protected $mWiki = false;
82
83 // Revision deletion constants
84 const DELETED_TEXT = 1;
85 const DELETED_COMMENT = 2;
86 const DELETED_USER = 4;
87 const DELETED_RESTRICTED = 8;
88 const SUPPRESSED_USER = 12; // convenience
89
90 // Audience options for accessors
91 const FOR_PUBLIC = 1;
92 const FOR_THIS_USER = 2;
93 const RAW = 3;
94
95 const TEXT_CACHE_GROUP = 'revisiontext:10'; // process cache name and max key count
96
97 /**
98 * Load a page revision from a given revision ID number.
99 * Returns null if no such revision can be found.
100 *
101 * $flags include:
102 * Revision::READ_LATEST : Select the data from the master
103 * Revision::READ_LOCKING : Select & lock the data from the master
104 *
105 * @param int $id
106 * @param int $flags (optional)
107 * @return Revision|null
108 */
109 public static function newFromId( $id, $flags = 0 ) {
110 return self::newFromConds( [ 'rev_id' => intval( $id ) ], $flags );
111 }
112
113 /**
114 * Load either the current, or a specified, revision
115 * that's attached to a given link target. If not attached
116 * to that link target, will return null.
117 *
118 * $flags include:
119 * Revision::READ_LATEST : Select the data from the master
120 * Revision::READ_LOCKING : Select & lock the data from the master
121 *
122 * @param LinkTarget $linkTarget
123 * @param int $id (optional)
124 * @param int $flags Bitfield (optional)
125 * @return Revision|null
126 */
127 public static function newFromTitle( LinkTarget $linkTarget, $id = 0, $flags = 0 ) {
128 $conds = [
129 'page_namespace' => $linkTarget->getNamespace(),
130 'page_title' => $linkTarget->getDBkey()
131 ];
132 if ( $id ) {
133 // Use the specified ID
134 $conds['rev_id'] = $id;
135 return self::newFromConds( $conds, $flags );
136 } else {
137 // Use a join to get the latest revision
138 $conds[] = 'rev_id=page_latest';
139 $db = wfGetDB( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
140 return self::loadFromConds( $db, $conds, $flags );
141 }
142 }
143
144 /**
145 * Load either the current, or a specified, revision
146 * that's attached to a given page ID.
147 * Returns null if no such revision can be found.
148 *
149 * $flags include:
150 * Revision::READ_LATEST : Select the data from the master (since 1.20)
151 * Revision::READ_LOCKING : Select & lock the data from the master
152 *
153 * @param int $pageId
154 * @param int $revId (optional)
155 * @param int $flags Bitfield (optional)
156 * @return Revision|null
157 */
158 public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
159 $conds = [ 'page_id' => $pageId ];
160 if ( $revId ) {
161 $conds['rev_id'] = $revId;
162 return self::newFromConds( $conds, $flags );
163 } else {
164 // Use a join to get the latest revision
165 $conds[] = 'rev_id = page_latest';
166 $db = wfGetDB( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
167 return self::loadFromConds( $db, $conds, $flags );
168 }
169 }
170
171 /**
172 * Make a fake revision object from an archive table row. This is queried
173 * for permissions or even inserted (as in Special:Undelete)
174 * @todo FIXME: Should be a subclass for RevisionDelete. [TS]
175 *
176 * @param object $row
177 * @param array $overrides
178 *
179 * @throws MWException
180 * @return Revision
181 */
182 public static function newFromArchiveRow( $row, $overrides = [] ) {
183 global $wgContentHandlerUseDB;
184
185 $attribs = $overrides + [
186 'page' => isset( $row->ar_page_id ) ? $row->ar_page_id : null,
187 'id' => isset( $row->ar_rev_id ) ? $row->ar_rev_id : null,
188 'comment' => $row->ar_comment,
189 'user' => $row->ar_user,
190 'user_text' => $row->ar_user_text,
191 'timestamp' => $row->ar_timestamp,
192 'minor_edit' => $row->ar_minor_edit,
193 'text_id' => isset( $row->ar_text_id ) ? $row->ar_text_id : null,
194 'deleted' => $row->ar_deleted,
195 'len' => $row->ar_len,
196 'sha1' => isset( $row->ar_sha1 ) ? $row->ar_sha1 : null,
197 'content_model' => isset( $row->ar_content_model ) ? $row->ar_content_model : null,
198 'content_format' => isset( $row->ar_content_format ) ? $row->ar_content_format : null,
199 ];
200
201 if ( !$wgContentHandlerUseDB ) {
202 unset( $attribs['content_model'] );
203 unset( $attribs['content_format'] );
204 }
205
206 if ( !isset( $attribs['title'] )
207 && isset( $row->ar_namespace )
208 && isset( $row->ar_title )
209 ) {
210 $attribs['title'] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
211 }
212
213 if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
214 // Pre-1.5 ar_text row
215 $attribs['text'] = self::getRevisionText( $row, 'ar_' );
216 if ( $attribs['text'] === false ) {
217 throw new MWException( 'Unable to load text from archive row (possibly bug 22624)' );
218 }
219 }
220 return new self( $attribs );
221 }
222
223 /**
224 * @since 1.19
225 *
226 * @param object $row
227 * @return Revision
228 */
229 public static function newFromRow( $row ) {
230 return new self( $row );
231 }
232
233 /**
234 * Load a page revision from a given revision ID number.
235 * Returns null if no such revision can be found.
236 *
237 * @param IDatabase $db
238 * @param int $id
239 * @return Revision|null
240 */
241 public static function loadFromId( $db, $id ) {
242 return self::loadFromConds( $db, [ 'rev_id' => intval( $id ) ] );
243 }
244
245 /**
246 * Load either the current, or a specified, revision
247 * that's attached to a given page. If not attached
248 * to that page, will return null.
249 *
250 * @param IDatabase $db
251 * @param int $pageid
252 * @param int $id
253 * @return Revision|null
254 */
255 public static function loadFromPageId( $db, $pageid, $id = 0 ) {
256 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
257 if ( $id ) {
258 $conds['rev_id'] = intval( $id );
259 } else {
260 $conds[] = 'rev_id=page_latest';
261 }
262 return self::loadFromConds( $db, $conds );
263 }
264
265 /**
266 * Load either the current, or a specified, revision
267 * that's attached to a given page. If not attached
268 * to that page, will return null.
269 *
270 * @param IDatabase $db
271 * @param Title $title
272 * @param int $id
273 * @return Revision|null
274 */
275 public static function loadFromTitle( $db, $title, $id = 0 ) {
276 if ( $id ) {
277 $matchId = intval( $id );
278 } else {
279 $matchId = 'page_latest';
280 }
281 return self::loadFromConds( $db,
282 [
283 "rev_id=$matchId",
284 'page_namespace' => $title->getNamespace(),
285 'page_title' => $title->getDBkey()
286 ]
287 );
288 }
289
290 /**
291 * Load the revision for the given title with the given timestamp.
292 * WARNING: Timestamps may in some circumstances not be unique,
293 * so this isn't the best key to use.
294 *
295 * @param IDatabase $db
296 * @param Title $title
297 * @param string $timestamp
298 * @return Revision|null
299 */
300 public static function loadFromTimestamp( $db, $title, $timestamp ) {
301 return self::loadFromConds( $db,
302 [
303 'rev_timestamp' => $db->timestamp( $timestamp ),
304 'page_namespace' => $title->getNamespace(),
305 'page_title' => $title->getDBkey()
306 ]
307 );
308 }
309
310 /**
311 * Given a set of conditions, fetch a revision
312 *
313 * This method is used then a revision ID is qualified and
314 * will incorporate some basic replica DB/master fallback logic
315 *
316 * @param array $conditions
317 * @param int $flags (optional)
318 * @return Revision|null
319 */
320 private static function newFromConds( $conditions, $flags = 0 ) {
321 $db = wfGetDB( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
322
323 $rev = self::loadFromConds( $db, $conditions, $flags );
324 // Make sure new pending/committed revision are visibile later on
325 // within web requests to certain avoid bugs like T93866 and T94407.
326 if ( !$rev
327 && !( $flags & self::READ_LATEST )
328 && wfGetLB()->getServerCount() > 1
329 && wfGetLB()->hasOrMadeRecentMasterChanges()
330 ) {
331 $flags = self::READ_LATEST;
332 $db = wfGetDB( DB_MASTER );
333 $rev = self::loadFromConds( $db, $conditions, $flags );
334 }
335
336 if ( $rev ) {
337 $rev->mQueryFlags = $flags;
338 }
339
340 return $rev;
341 }
342
343 /**
344 * Given a set of conditions, fetch a revision from
345 * the given database connection.
346 *
347 * @param IDatabase $db
348 * @param array $conditions
349 * @param int $flags (optional)
350 * @return Revision|null
351 */
352 private static function loadFromConds( $db, $conditions, $flags = 0 ) {
353 $row = self::fetchFromConds( $db, $conditions, $flags );
354 if ( $row ) {
355 $rev = new Revision( $row );
356 $rev->mWiki = $db->getWikiID();
357
358 return $rev;
359 }
360
361 return null;
362 }
363
364 /**
365 * Return a wrapper for a series of database rows to
366 * fetch all of a given page's revisions in turn.
367 * Each row can be fed to the constructor to get objects.
368 *
369 * @param LinkTarget $title
370 * @return ResultWrapper
371 * @deprecated Since 1.28
372 */
373 public static function fetchRevision( LinkTarget $title ) {
374 $row = self::fetchFromConds(
375 wfGetDB( DB_REPLICA ),
376 [
377 'rev_id=page_latest',
378 'page_namespace' => $title->getNamespace(),
379 'page_title' => $title->getDBkey()
380 ]
381 );
382
383 return new FakeResultWrapper( $row ? [ $row ] : [] );
384 }
385
386 /**
387 * Given a set of conditions, return a ResultWrapper
388 * which will return matching database rows with the
389 * fields necessary to build Revision objects.
390 *
391 * @param IDatabase $db
392 * @param array $conditions
393 * @param int $flags (optional)
394 * @return stdClass
395 */
396 private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
397 $fields = array_merge(
398 self::selectFields(),
399 self::selectPageFields(),
400 self::selectUserFields()
401 );
402 $options = [];
403 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
404 $options[] = 'FOR UPDATE';
405 }
406 return $db->selectRow(
407 [ 'revision', 'page', 'user' ],
408 $fields,
409 $conditions,
410 __METHOD__,
411 $options,
412 [ 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() ]
413 );
414 }
415
416 /**
417 * Return the value of a select() JOIN conds array for the user table.
418 * This will get user table rows for logged-in users.
419 * @since 1.19
420 * @return array
421 */
422 public static function userJoinCond() {
423 return [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
424 }
425
426 /**
427 * Return the value of a select() page conds array for the page table.
428 * This will assure that the revision(s) are not orphaned from live pages.
429 * @since 1.19
430 * @return array
431 */
432 public static function pageJoinCond() {
433 return [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
434 }
435
436 /**
437 * Return the list of revision fields that should be selected to create
438 * a new revision.
439 * @return array
440 */
441 public static function selectFields() {
442 global $wgContentHandlerUseDB;
443
444 $fields = [
445 'rev_id',
446 'rev_page',
447 'rev_text_id',
448 'rev_timestamp',
449 'rev_comment',
450 'rev_user_text',
451 'rev_user',
452 'rev_minor_edit',
453 'rev_deleted',
454 'rev_len',
455 'rev_parent_id',
456 'rev_sha1',
457 ];
458
459 if ( $wgContentHandlerUseDB ) {
460 $fields[] = 'rev_content_format';
461 $fields[] = 'rev_content_model';
462 }
463
464 return $fields;
465 }
466
467 /**
468 * Return the list of revision fields that should be selected to create
469 * a new revision from an archive row.
470 * @return array
471 */
472 public static function selectArchiveFields() {
473 global $wgContentHandlerUseDB;
474 $fields = [
475 'ar_id',
476 'ar_page_id',
477 'ar_rev_id',
478 'ar_text',
479 'ar_text_id',
480 'ar_timestamp',
481 'ar_comment',
482 'ar_user_text',
483 'ar_user',
484 'ar_minor_edit',
485 'ar_deleted',
486 'ar_len',
487 'ar_parent_id',
488 'ar_sha1',
489 ];
490
491 if ( $wgContentHandlerUseDB ) {
492 $fields[] = 'ar_content_format';
493 $fields[] = 'ar_content_model';
494 }
495 return $fields;
496 }
497
498 /**
499 * Return the list of text fields that should be selected to read the
500 * revision text
501 * @return array
502 */
503 public static function selectTextFields() {
504 return [
505 'old_text',
506 'old_flags'
507 ];
508 }
509
510 /**
511 * Return the list of page fields that should be selected from page table
512 * @return array
513 */
514 public static function selectPageFields() {
515 return [
516 'page_namespace',
517 'page_title',
518 'page_id',
519 'page_latest',
520 'page_is_redirect',
521 'page_len',
522 ];
523 }
524
525 /**
526 * Return the list of user fields that should be selected from user table
527 * @return array
528 */
529 public static function selectUserFields() {
530 return [ 'user_name' ];
531 }
532
533 /**
534 * Do a batched query to get the parent revision lengths
535 * @param IDatabase $db
536 * @param array $revIds
537 * @return array
538 */
539 public static function getParentLengths( $db, array $revIds ) {
540 $revLens = [];
541 if ( !$revIds ) {
542 return $revLens; // empty
543 }
544 $res = $db->select( 'revision',
545 [ 'rev_id', 'rev_len' ],
546 [ 'rev_id' => $revIds ],
547 __METHOD__ );
548 foreach ( $res as $row ) {
549 $revLens[$row->rev_id] = $row->rev_len;
550 }
551 return $revLens;
552 }
553
554 /**
555 * Constructor
556 *
557 * @param object|array $row Either a database row or an array
558 * @throws MWException
559 * @access private
560 */
561 function __construct( $row ) {
562 if ( is_object( $row ) ) {
563 $this->mId = intval( $row->rev_id );
564 $this->mPage = intval( $row->rev_page );
565 $this->mTextId = intval( $row->rev_text_id );
566 $this->mComment = $row->rev_comment;
567 $this->mUser = intval( $row->rev_user );
568 $this->mMinorEdit = intval( $row->rev_minor_edit );
569 $this->mTimestamp = $row->rev_timestamp;
570 $this->mDeleted = intval( $row->rev_deleted );
571
572 if ( !isset( $row->rev_parent_id ) ) {
573 $this->mParentId = null;
574 } else {
575 $this->mParentId = intval( $row->rev_parent_id );
576 }
577
578 if ( !isset( $row->rev_len ) ) {
579 $this->mSize = null;
580 } else {
581 $this->mSize = intval( $row->rev_len );
582 }
583
584 if ( !isset( $row->rev_sha1 ) ) {
585 $this->mSha1 = null;
586 } else {
587 $this->mSha1 = $row->rev_sha1;
588 }
589
590 if ( isset( $row->page_latest ) ) {
591 $this->mCurrent = ( $row->rev_id == $row->page_latest );
592 $this->mTitle = Title::newFromRow( $row );
593 } else {
594 $this->mCurrent = false;
595 $this->mTitle = null;
596 }
597
598 if ( !isset( $row->rev_content_model ) ) {
599 $this->mContentModel = null; # determine on demand if needed
600 } else {
601 $this->mContentModel = strval( $row->rev_content_model );
602 }
603
604 if ( !isset( $row->rev_content_format ) ) {
605 $this->mContentFormat = null; # determine on demand if needed
606 } else {
607 $this->mContentFormat = strval( $row->rev_content_format );
608 }
609
610 // Lazy extraction...
611 $this->mText = null;
612 if ( isset( $row->old_text ) ) {
613 $this->mTextRow = $row;
614 } else {
615 // 'text' table row entry will be lazy-loaded
616 $this->mTextRow = null;
617 }
618
619 // Use user_name for users and rev_user_text for IPs...
620 $this->mUserText = null; // lazy load if left null
621 if ( $this->mUser == 0 ) {
622 $this->mUserText = $row->rev_user_text; // IP user
623 } elseif ( isset( $row->user_name ) ) {
624 $this->mUserText = $row->user_name; // logged-in user
625 }
626 $this->mOrigUserText = $row->rev_user_text;
627 } elseif ( is_array( $row ) ) {
628 // Build a new revision to be saved...
629 global $wgUser; // ugh
630
631 # if we have a content object, use it to set the model and type
632 if ( !empty( $row['content'] ) ) {
633 // @todo when is that set? test with external store setup! check out insertOn() [dk]
634 if ( !empty( $row['text_id'] ) ) {
635 throw new MWException( "Text already stored in external store (id {$row['text_id']}), " .
636 "can't serialize content object" );
637 }
638
639 $row['content_model'] = $row['content']->getModel();
640 # note: mContentFormat is initializes later accordingly
641 # note: content is serialized later in this method!
642 # also set text to null?
643 }
644
645 $this->mId = isset( $row['id'] ) ? intval( $row['id'] ) : null;
646 $this->mPage = isset( $row['page'] ) ? intval( $row['page'] ) : null;
647 $this->mTextId = isset( $row['text_id'] ) ? intval( $row['text_id'] ) : null;
648 $this->mUserText = isset( $row['user_text'] )
649 ? strval( $row['user_text'] ) : $wgUser->getName();
650 $this->mUser = isset( $row['user'] ) ? intval( $row['user'] ) : $wgUser->getId();
651 $this->mMinorEdit = isset( $row['minor_edit'] ) ? intval( $row['minor_edit'] ) : 0;
652 $this->mTimestamp = isset( $row['timestamp'] )
653 ? strval( $row['timestamp'] ) : wfTimestampNow();
654 $this->mDeleted = isset( $row['deleted'] ) ? intval( $row['deleted'] ) : 0;
655 $this->mSize = isset( $row['len'] ) ? intval( $row['len'] ) : null;
656 $this->mParentId = isset( $row['parent_id'] ) ? intval( $row['parent_id'] ) : null;
657 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
658
659 $this->mContentModel = isset( $row['content_model'] )
660 ? strval( $row['content_model'] ) : null;
661 $this->mContentFormat = isset( $row['content_format'] )
662 ? strval( $row['content_format'] ) : null;
663
664 // Enforce spacing trimming on supplied text
665 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
666 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
667 $this->mTextRow = null;
668
669 $this->mTitle = isset( $row['title'] ) ? $row['title'] : null;
670
671 // if we have a Content object, override mText and mContentModel
672 if ( !empty( $row['content'] ) ) {
673 if ( !( $row['content'] instanceof Content ) ) {
674 throw new MWException( '`content` field must contain a Content object.' );
675 }
676
677 $handler = $this->getContentHandler();
678 $this->mContent = $row['content'];
679
680 $this->mContentModel = $this->mContent->getModel();
681 $this->mContentHandler = null;
682
683 $this->mText = $handler->serializeContent( $row['content'], $this->getContentFormat() );
684 } elseif ( $this->mText !== null ) {
685 $handler = $this->getContentHandler();
686 $this->mContent = $handler->unserializeContent( $this->mText );
687 }
688
689 // If we have a Title object, make sure it is consistent with mPage.
690 if ( $this->mTitle && $this->mTitle->exists() ) {
691 if ( $this->mPage === null ) {
692 // if the page ID wasn't known, set it now
693 $this->mPage = $this->mTitle->getArticleID();
694 } elseif ( $this->mTitle->getArticleID() !== $this->mPage ) {
695 // Got different page IDs. This may be legit (e.g. during undeletion),
696 // but it seems worth mentioning it in the log.
697 wfDebug( "Page ID " . $this->mPage . " mismatches the ID " .
698 $this->mTitle->getArticleID() . " provided by the Title object." );
699 }
700 }
701
702 $this->mCurrent = false;
703
704 // If we still have no length, see it we have the text to figure it out
705 if ( !$this->mSize && $this->mContent !== null ) {
706 $this->mSize = $this->mContent->getSize();
707 }
708
709 // Same for sha1
710 if ( $this->mSha1 === null ) {
711 $this->mSha1 = $this->mText === null ? null : self::base36Sha1( $this->mText );
712 }
713
714 // force lazy init
715 $this->getContentModel();
716 $this->getContentFormat();
717 } else {
718 throw new MWException( 'Revision constructor passed invalid row format.' );
719 }
720 $this->mUnpatrolled = null;
721 }
722
723 /**
724 * Get revision ID
725 *
726 * @return int|null
727 */
728 public function getId() {
729 return $this->mId;
730 }
731
732 /**
733 * Set the revision ID
734 *
735 * This should only be used for proposed revisions that turn out to be null edits
736 *
737 * @since 1.19
738 * @param int $id
739 */
740 public function setId( $id ) {
741 $this->mId = (int)$id;
742 }
743
744 /**
745 * Set the user ID/name
746 *
747 * This should only be used for proposed revisions that turn out to be null edits
748 *
749 * @since 1.28
750 * @param integer $id User ID
751 * @param string $name User name
752 */
753 public function setUserIdAndName( $id, $name ) {
754 $this->mUser = (int)$id;
755 $this->mUserText = $name;
756 $this->mOrigUserText = $name;
757 }
758
759 /**
760 * Get text row ID
761 *
762 * @return int|null
763 */
764 public function getTextId() {
765 return $this->mTextId;
766 }
767
768 /**
769 * Get parent revision ID (the original previous page revision)
770 *
771 * @return int|null
772 */
773 public function getParentId() {
774 return $this->mParentId;
775 }
776
777 /**
778 * Returns the length of the text in this revision, or null if unknown.
779 *
780 * @return int|null
781 */
782 public function getSize() {
783 return $this->mSize;
784 }
785
786 /**
787 * Returns the base36 sha1 of the text in this revision, or null if unknown.
788 *
789 * @return string|null
790 */
791 public function getSha1() {
792 return $this->mSha1;
793 }
794
795 /**
796 * Returns the title of the page associated with this entry or null.
797 *
798 * Will do a query, when title is not set and id is given.
799 *
800 * @return Title|null
801 */
802 public function getTitle() {
803 if ( $this->mTitle !== null ) {
804 return $this->mTitle;
805 }
806 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
807 if ( $this->mId !== null ) {
808 $dbr = wfGetLB( $this->mWiki )->getConnectionRef( DB_REPLICA, [], $this->mWiki );
809 $row = $dbr->selectRow(
810 [ 'page', 'revision' ],
811 self::selectPageFields(),
812 [ 'page_id=rev_page', 'rev_id' => $this->mId ],
813 __METHOD__
814 );
815 if ( $row ) {
816 // @TODO: better foreign title handling
817 $this->mTitle = Title::newFromRow( $row );
818 }
819 }
820
821 if ( $this->mWiki === false || $this->mWiki === wfWikiID() ) {
822 // Loading by ID is best, though not possible for foreign titles
823 if ( !$this->mTitle && $this->mPage !== null && $this->mPage > 0 ) {
824 $this->mTitle = Title::newFromID( $this->mPage );
825 }
826 }
827
828 return $this->mTitle;
829 }
830
831 /**
832 * Set the title of the revision
833 *
834 * @param Title $title
835 */
836 public function setTitle( $title ) {
837 $this->mTitle = $title;
838 }
839
840 /**
841 * Get the page ID
842 *
843 * @return int|null
844 */
845 public function getPage() {
846 return $this->mPage;
847 }
848
849 /**
850 * Fetch revision's user id if it's available to the specified audience.
851 * If the specified audience does not have access to it, zero will be
852 * returned.
853 *
854 * @param int $audience One of:
855 * Revision::FOR_PUBLIC to be displayed to all users
856 * Revision::FOR_THIS_USER to be displayed to the given user
857 * Revision::RAW get the ID regardless of permissions
858 * @param User $user User object to check for, only if FOR_THIS_USER is passed
859 * to the $audience parameter
860 * @return int
861 */
862 public function getUser( $audience = self::FOR_PUBLIC, User $user = null ) {
863 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
864 return 0;
865 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
866 return 0;
867 } else {
868 return $this->mUser;
869 }
870 }
871
872 /**
873 * Fetch revision's user id without regard for the current user's permissions
874 *
875 * @return string
876 * @deprecated since 1.25, use getUser( Revision::RAW )
877 */
878 public function getRawUser() {
879 wfDeprecated( __METHOD__, '1.25' );
880 return $this->getUser( self::RAW );
881 }
882
883 /**
884 * Fetch revision's username if it's available to the specified audience.
885 * If the specified audience does not have access to the username, an
886 * empty string will be returned.
887 *
888 * @param int $audience One of:
889 * Revision::FOR_PUBLIC to be displayed to all users
890 * Revision::FOR_THIS_USER to be displayed to the given user
891 * Revision::RAW get the text regardless of permissions
892 * @param User $user User object to check for, only if FOR_THIS_USER is passed
893 * to the $audience parameter
894 * @return string
895 */
896 public function getUserText( $audience = self::FOR_PUBLIC, User $user = null ) {
897 $this->loadMutableFields();
898
899 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
900 return '';
901 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER, $user ) ) {
902 return '';
903 } else {
904 if ( $this->mUserText === null ) {
905 $this->mUserText = User::whoIs( $this->mUser ); // load on demand
906 if ( $this->mUserText === false ) {
907 # This shouldn't happen, but it can if the wiki was recovered
908 # via importing revs and there is no user table entry yet.
909 $this->mUserText = $this->mOrigUserText;
910 }
911 }
912 return $this->mUserText;
913 }
914 }
915
916 /**
917 * Fetch revision's username without regard for view restrictions
918 *
919 * @return string
920 * @deprecated since 1.25, use getUserText( Revision::RAW )
921 */
922 public function getRawUserText() {
923 wfDeprecated( __METHOD__, '1.25' );
924 return $this->getUserText( self::RAW );
925 }
926
927 /**
928 * Fetch revision comment if it's available to the specified audience.
929 * If the specified audience does not have access to the comment, an
930 * empty string will be returned.
931 *
932 * @param int $audience One of:
933 * Revision::FOR_PUBLIC to be displayed to all users
934 * Revision::FOR_THIS_USER to be displayed to the given user
935 * Revision::RAW get the text regardless of permissions
936 * @param User $user User object to check for, only if FOR_THIS_USER is passed
937 * to the $audience parameter
938 * @return string
939 */
940 function getComment( $audience = self::FOR_PUBLIC, User $user = null ) {
941 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
942 return '';
943 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT, $user ) ) {
944 return '';
945 } else {
946 return $this->mComment;
947 }
948 }
949
950 /**
951 * Fetch revision comment without regard for the current user's permissions
952 *
953 * @return string
954 * @deprecated since 1.25, use getComment( Revision::RAW )
955 */
956 public function getRawComment() {
957 wfDeprecated( __METHOD__, '1.25' );
958 return $this->getComment( self::RAW );
959 }
960
961 /**
962 * @return bool
963 */
964 public function isMinor() {
965 return (bool)$this->mMinorEdit;
966 }
967
968 /**
969 * @return int Rcid of the unpatrolled row, zero if there isn't one
970 */
971 public function isUnpatrolled() {
972 if ( $this->mUnpatrolled !== null ) {
973 return $this->mUnpatrolled;
974 }
975 $rc = $this->getRecentChange();
976 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
977 $this->mUnpatrolled = $rc->getAttribute( 'rc_id' );
978 } else {
979 $this->mUnpatrolled = 0;
980 }
981 return $this->mUnpatrolled;
982 }
983
984 /**
985 * Get the RC object belonging to the current revision, if there's one
986 *
987 * @param int $flags (optional) $flags include:
988 * Revision::READ_LATEST : Select the data from the master
989 *
990 * @since 1.22
991 * @return RecentChange|null
992 */
993 public function getRecentChange( $flags = 0 ) {
994 $dbr = wfGetDB( DB_REPLICA );
995
996 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
997
998 return RecentChange::newFromConds(
999 [
1000 'rc_user_text' => $this->getUserText( Revision::RAW ),
1001 'rc_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
1002 'rc_this_oldid' => $this->getId()
1003 ],
1004 __METHOD__,
1005 $dbType
1006 );
1007 }
1008
1009 /**
1010 * @param int $field One of DELETED_* bitfield constants
1011 *
1012 * @return bool
1013 */
1014 public function isDeleted( $field ) {
1015 if ( $this->isCurrent() && $field === self::DELETED_TEXT ) {
1016 // Current revisions of pages cannot have the content hidden. Skipping this
1017 // check is very useful for Parser as it fetches templates using newKnownCurrent().
1018 // Calling getVisibility() in that case triggers a verification database query.
1019 return false; // no need to check
1020 }
1021
1022 return ( $this->getVisibility() & $field ) == $field;
1023 }
1024
1025 /**
1026 * Get the deletion bitfield of the revision
1027 *
1028 * @return int
1029 */
1030 public function getVisibility() {
1031 $this->loadMutableFields();
1032
1033 return (int)$this->mDeleted;
1034 }
1035
1036 /**
1037 * Fetch revision text if it's available to the specified audience.
1038 * If the specified audience does not have the ability to view this
1039 * revision, an empty string will be returned.
1040 *
1041 * @param int $audience One of:
1042 * Revision::FOR_PUBLIC to be displayed to all users
1043 * Revision::FOR_THIS_USER to be displayed to the given user
1044 * Revision::RAW get the text regardless of permissions
1045 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1046 * to the $audience parameter
1047 *
1048 * @deprecated since 1.21, use getContent() instead
1049 * @return string
1050 */
1051 public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
1052 wfDeprecated( __METHOD__, '1.21' );
1053
1054 $content = $this->getContent( $audience, $user );
1055 return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
1056 }
1057
1058 /**
1059 * Fetch revision content if it's available to the specified audience.
1060 * If the specified audience does not have the ability to view this
1061 * revision, null will be returned.
1062 *
1063 * @param int $audience One of:
1064 * Revision::FOR_PUBLIC to be displayed to all users
1065 * Revision::FOR_THIS_USER to be displayed to $wgUser
1066 * Revision::RAW get the text regardless of permissions
1067 * @param User $user User object to check for, only if FOR_THIS_USER is passed
1068 * to the $audience parameter
1069 * @since 1.21
1070 * @return Content|null
1071 */
1072 public function getContent( $audience = self::FOR_PUBLIC, User $user = null ) {
1073 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
1074 return null;
1075 } elseif ( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT, $user ) ) {
1076 return null;
1077 } else {
1078 return $this->getContentInternal();
1079 }
1080 }
1081
1082 /**
1083 * Get original serialized data (without checking view restrictions)
1084 *
1085 * @since 1.21
1086 * @return string
1087 */
1088 public function getSerializedData() {
1089 if ( $this->mText === null ) {
1090 // Revision is immutable. Load on demand.
1091 $this->mText = $this->loadText();
1092 }
1093
1094 return $this->mText;
1095 }
1096
1097 /**
1098 * Gets the content object for the revision (or null on failure).
1099 *
1100 * Note that for mutable Content objects, each call to this method will return a
1101 * fresh clone.
1102 *
1103 * @since 1.21
1104 * @return Content|null The Revision's content, or null on failure.
1105 */
1106 protected function getContentInternal() {
1107 if ( $this->mContent === null ) {
1108 $text = $this->getSerializedData();
1109
1110 if ( $text !== null && $text !== false ) {
1111 // Unserialize content
1112 $handler = $this->getContentHandler();
1113 $format = $this->getContentFormat();
1114
1115 $this->mContent = $handler->unserializeContent( $text, $format );
1116 }
1117 }
1118
1119 // NOTE: copy() will return $this for immutable content objects
1120 return $this->mContent ? $this->mContent->copy() : null;
1121 }
1122
1123 /**
1124 * Returns the content model for this revision.
1125 *
1126 * If no content model was stored in the database, the default content model for the title is
1127 * used to determine the content model to use. If no title is know, CONTENT_MODEL_WIKITEXT
1128 * is used as a last resort.
1129 *
1130 * @return string The content model id associated with this revision,
1131 * see the CONTENT_MODEL_XXX constants.
1132 **/
1133 public function getContentModel() {
1134 if ( !$this->mContentModel ) {
1135 $title = $this->getTitle();
1136 if ( $title ) {
1137 $this->mContentModel = ContentHandler::getDefaultModelFor( $title );
1138 } else {
1139 $this->mContentModel = CONTENT_MODEL_WIKITEXT;
1140 }
1141
1142 assert( !empty( $this->mContentModel ) );
1143 }
1144
1145 return $this->mContentModel;
1146 }
1147
1148 /**
1149 * Returns the content format for this revision.
1150 *
1151 * If no content format was stored in the database, the default format for this
1152 * revision's content model is returned.
1153 *
1154 * @return string The content format id associated with this revision,
1155 * see the CONTENT_FORMAT_XXX constants.
1156 **/
1157 public function getContentFormat() {
1158 if ( !$this->mContentFormat ) {
1159 $handler = $this->getContentHandler();
1160 $this->mContentFormat = $handler->getDefaultFormat();
1161
1162 assert( !empty( $this->mContentFormat ) );
1163 }
1164
1165 return $this->mContentFormat;
1166 }
1167
1168 /**
1169 * Returns the content handler appropriate for this revision's content model.
1170 *
1171 * @throws MWException
1172 * @return ContentHandler
1173 */
1174 public function getContentHandler() {
1175 if ( !$this->mContentHandler ) {
1176 $model = $this->getContentModel();
1177 $this->mContentHandler = ContentHandler::getForModelID( $model );
1178
1179 $format = $this->getContentFormat();
1180
1181 if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
1182 throw new MWException( "Oops, the content format $format is not supported for "
1183 . "this content model, $model" );
1184 }
1185 }
1186
1187 return $this->mContentHandler;
1188 }
1189
1190 /**
1191 * @return string
1192 */
1193 public function getTimestamp() {
1194 return wfTimestamp( TS_MW, $this->mTimestamp );
1195 }
1196
1197 /**
1198 * @return bool
1199 */
1200 public function isCurrent() {
1201 return $this->mCurrent;
1202 }
1203
1204 /**
1205 * Get previous revision for this title
1206 *
1207 * @return Revision|null
1208 */
1209 public function getPrevious() {
1210 if ( $this->getTitle() ) {
1211 $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
1212 if ( $prev ) {
1213 return self::newFromTitle( $this->getTitle(), $prev );
1214 }
1215 }
1216 return null;
1217 }
1218
1219 /**
1220 * Get next revision for this title
1221 *
1222 * @return Revision|null
1223 */
1224 public function getNext() {
1225 if ( $this->getTitle() ) {
1226 $next = $this->getTitle()->getNextRevisionID( $this->getId() );
1227 if ( $next ) {
1228 return self::newFromTitle( $this->getTitle(), $next );
1229 }
1230 }
1231 return null;
1232 }
1233
1234 /**
1235 * Get previous revision Id for this page_id
1236 * This is used to populate rev_parent_id on save
1237 *
1238 * @param IDatabase $db
1239 * @return int
1240 */
1241 private function getPreviousRevisionId( $db ) {
1242 if ( $this->mPage === null ) {
1243 return 0;
1244 }
1245 # Use page_latest if ID is not given
1246 if ( !$this->mId ) {
1247 $prevId = $db->selectField( 'page', 'page_latest',
1248 [ 'page_id' => $this->mPage ],
1249 __METHOD__ );
1250 } else {
1251 $prevId = $db->selectField( 'revision', 'rev_id',
1252 [ 'rev_page' => $this->mPage, 'rev_id < ' . $this->mId ],
1253 __METHOD__,
1254 [ 'ORDER BY' => 'rev_id DESC' ] );
1255 }
1256 return intval( $prevId );
1257 }
1258
1259 /**
1260 * Get revision text associated with an old or archive row
1261 * $row is usually an object from wfFetchRow(), both the flags and the text
1262 * field must be included.
1263 *
1264 * @param stdClass $row The text data
1265 * @param string $prefix Table prefix (default 'old_')
1266 * @param string|bool $wiki The name of the wiki to load the revision text from
1267 * (same as the the wiki $row was loaded from) or false to indicate the local
1268 * wiki (this is the default). Otherwise, it must be a symbolic wiki database
1269 * identifier as understood by the LoadBalancer class.
1270 * @return string Text the text requested or false on failure
1271 */
1272 public static function getRevisionText( $row, $prefix = 'old_', $wiki = false ) {
1273
1274 # Get data
1275 $textField = $prefix . 'text';
1276 $flagsField = $prefix . 'flags';
1277
1278 if ( isset( $row->$flagsField ) ) {
1279 $flags = explode( ',', $row->$flagsField );
1280 } else {
1281 $flags = [];
1282 }
1283
1284 if ( isset( $row->$textField ) ) {
1285 $text = $row->$textField;
1286 } else {
1287 return false;
1288 }
1289
1290 # Use external methods for external objects, text in table is URL-only then
1291 if ( in_array( 'external', $flags ) ) {
1292 $url = $text;
1293 $parts = explode( '://', $url, 2 );
1294 if ( count( $parts ) == 1 || $parts[1] == '' ) {
1295 return false;
1296 }
1297 $text = ExternalStore::fetchFromURL( $url, [ 'wiki' => $wiki ] );
1298 }
1299
1300 // If the text was fetched without an error, convert it
1301 if ( $text !== false ) {
1302 $text = self::decompressRevisionText( $text, $flags );
1303 }
1304 return $text;
1305 }
1306
1307 /**
1308 * If $wgCompressRevisions is enabled, we will compress data.
1309 * The input string is modified in place.
1310 * Return value is the flags field: contains 'gzip' if the
1311 * data is compressed, and 'utf-8' if we're saving in UTF-8
1312 * mode.
1313 *
1314 * @param mixed $text Reference to a text
1315 * @return string
1316 */
1317 public static function compressRevisionText( &$text ) {
1318 global $wgCompressRevisions;
1319 $flags = [];
1320
1321 # Revisions not marked this way will be converted
1322 # on load if $wgLegacyCharset is set in the future.
1323 $flags[] = 'utf-8';
1324
1325 if ( $wgCompressRevisions ) {
1326 if ( function_exists( 'gzdeflate' ) ) {
1327 $deflated = gzdeflate( $text );
1328
1329 if ( $deflated === false ) {
1330 wfLogWarning( __METHOD__ . ': gzdeflate() failed' );
1331 } else {
1332 $text = $deflated;
1333 $flags[] = 'gzip';
1334 }
1335 } else {
1336 wfDebug( __METHOD__ . " -- no zlib support, not compressing\n" );
1337 }
1338 }
1339 return implode( ',', $flags );
1340 }
1341
1342 /**
1343 * Re-converts revision text according to it's flags.
1344 *
1345 * @param mixed $text Reference to a text
1346 * @param array $flags Compression flags
1347 * @return string|bool Decompressed text, or false on failure
1348 */
1349 public static function decompressRevisionText( $text, $flags ) {
1350 if ( in_array( 'gzip', $flags ) ) {
1351 # Deal with optional compression of archived pages.
1352 # This can be done periodically via maintenance/compressOld.php, and
1353 # as pages are saved if $wgCompressRevisions is set.
1354 $text = gzinflate( $text );
1355
1356 if ( $text === false ) {
1357 wfLogWarning( __METHOD__ . ': gzinflate() failed' );
1358 return false;
1359 }
1360 }
1361
1362 if ( in_array( 'object', $flags ) ) {
1363 # Generic compressed storage
1364 $obj = unserialize( $text );
1365 if ( !is_object( $obj ) ) {
1366 // Invalid object
1367 return false;
1368 }
1369 $text = $obj->getText();
1370 }
1371
1372 global $wgLegacyEncoding;
1373 if ( $text !== false && $wgLegacyEncoding
1374 && !in_array( 'utf-8', $flags ) && !in_array( 'utf8', $flags )
1375 ) {
1376 # Old revisions kept around in a legacy encoding?
1377 # Upconvert on demand.
1378 # ("utf8" checked for compatibility with some broken
1379 # conversion scripts 2008-12-30)
1380 global $wgContLang;
1381 $text = $wgContLang->iconv( $wgLegacyEncoding, 'UTF-8', $text );
1382 }
1383
1384 return $text;
1385 }
1386
1387 /**
1388 * Insert a new revision into the database, returning the new revision ID
1389 * number on success and dies horribly on failure.
1390 *
1391 * @param IDatabase $dbw (master connection)
1392 * @throws MWException
1393 * @return int
1394 */
1395 public function insertOn( $dbw ) {
1396 global $wgDefaultExternalStore, $wgContentHandlerUseDB;
1397
1398 // We're inserting a new revision, so we have to use master anyway.
1399 // If it's a null revision, it may have references to rows that
1400 // are not in the replica yet (the text row).
1401 $this->mQueryFlags |= self::READ_LATEST;
1402
1403 // Not allowed to have rev_page equal to 0, false, etc.
1404 if ( !$this->mPage ) {
1405 $title = $this->getTitle();
1406 if ( $title instanceof Title ) {
1407 $titleText = ' for page ' . $title->getPrefixedText();
1408 } else {
1409 $titleText = '';
1410 }
1411 throw new MWException( "Cannot insert revision$titleText: page ID must be nonzero" );
1412 }
1413
1414 $this->checkContentModel();
1415
1416 $data = $this->mText;
1417 $flags = self::compressRevisionText( $data );
1418
1419 # Write to external storage if required
1420 if ( $wgDefaultExternalStore ) {
1421 // Store and get the URL
1422 $data = ExternalStore::insertToDefault( $data );
1423 if ( !$data ) {
1424 throw new MWException( "Unable to store text to external storage" );
1425 }
1426 if ( $flags ) {
1427 $flags .= ',';
1428 }
1429 $flags .= 'external';
1430 }
1431
1432 # Record the text (or external storage URL) to the text table
1433 if ( $this->mTextId === null ) {
1434 $old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
1435 $dbw->insert( 'text',
1436 [
1437 'old_id' => $old_id,
1438 'old_text' => $data,
1439 'old_flags' => $flags,
1440 ], __METHOD__
1441 );
1442 $this->mTextId = $dbw->insertId();
1443 }
1444
1445 if ( $this->mComment === null ) {
1446 $this->mComment = "";
1447 }
1448
1449 # Record the edit in revisions
1450 $rev_id = $this->mId !== null
1451 ? $this->mId
1452 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1453 $row = [
1454 'rev_id' => $rev_id,
1455 'rev_page' => $this->mPage,
1456 'rev_text_id' => $this->mTextId,
1457 'rev_comment' => $this->mComment,
1458 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1459 'rev_user' => $this->mUser,
1460 'rev_user_text' => $this->mUserText,
1461 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1462 'rev_deleted' => $this->mDeleted,
1463 'rev_len' => $this->mSize,
1464 'rev_parent_id' => $this->mParentId === null
1465 ? $this->getPreviousRevisionId( $dbw )
1466 : $this->mParentId,
1467 'rev_sha1' => $this->mSha1 === null
1468 ? Revision::base36Sha1( $this->mText )
1469 : $this->mSha1,
1470 ];
1471
1472 if ( $wgContentHandlerUseDB ) {
1473 // NOTE: Store null for the default model and format, to save space.
1474 // XXX: Makes the DB sensitive to changed defaults.
1475 // Make this behavior optional? Only in miser mode?
1476
1477 $model = $this->getContentModel();
1478 $format = $this->getContentFormat();
1479
1480 $title = $this->getTitle();
1481
1482 if ( $title === null ) {
1483 throw new MWException( "Insufficient information to determine the title of the "
1484 . "revision's page!" );
1485 }
1486
1487 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1488 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
1489
1490 $row['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
1491 $row['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
1492 }
1493
1494 $dbw->insert( 'revision', $row, __METHOD__ );
1495
1496 $this->mId = $rev_id !== null ? $rev_id : $dbw->insertId();
1497
1498 // Assertion to try to catch T92046
1499 if ( (int)$this->mId === 0 ) {
1500 throw new UnexpectedValueException(
1501 'After insert, Revision mId is ' . var_export( $this->mId, 1 ) . ': ' .
1502 var_export( $row, 1 )
1503 );
1504 }
1505
1506 Hooks::run( 'RevisionInsertComplete', [ &$this, $data, $flags ] );
1507
1508 return $this->mId;
1509 }
1510
1511 protected function checkContentModel() {
1512 global $wgContentHandlerUseDB;
1513
1514 // Note: may return null for revisions that have not yet been inserted
1515 $title = $this->getTitle();
1516
1517 $model = $this->getContentModel();
1518 $format = $this->getContentFormat();
1519 $handler = $this->getContentHandler();
1520
1521 if ( !$handler->isSupportedFormat( $format ) ) {
1522 $t = $title->getPrefixedDBkey();
1523
1524 throw new MWException( "Can't use format $format with content model $model on $t" );
1525 }
1526
1527 if ( !$wgContentHandlerUseDB && $title ) {
1528 // if $wgContentHandlerUseDB is not set,
1529 // all revisions must use the default content model and format.
1530
1531 $defaultModel = ContentHandler::getDefaultModelFor( $title );
1532 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1533 $defaultFormat = $defaultHandler->getDefaultFormat();
1534
1535 if ( $this->getContentModel() != $defaultModel ) {
1536 $t = $title->getPrefixedDBkey();
1537
1538 throw new MWException( "Can't save non-default content model with "
1539 . "\$wgContentHandlerUseDB disabled: model is $model, "
1540 . "default for $t is $defaultModel" );
1541 }
1542
1543 if ( $this->getContentFormat() != $defaultFormat ) {
1544 $t = $title->getPrefixedDBkey();
1545
1546 throw new MWException( "Can't use non-default content format with "
1547 . "\$wgContentHandlerUseDB disabled: format is $format, "
1548 . "default for $t is $defaultFormat" );
1549 }
1550 }
1551
1552 $content = $this->getContent( Revision::RAW );
1553 $prefixedDBkey = $title->getPrefixedDBkey();
1554 $revId = $this->mId;
1555
1556 if ( !$content ) {
1557 throw new MWException(
1558 "Content of revision $revId ($prefixedDBkey) could not be loaded for validation!"
1559 );
1560 }
1561 if ( !$content->isValid() ) {
1562 throw new MWException(
1563 "Content of revision $revId ($prefixedDBkey) is not valid! Content model is $model"
1564 );
1565 }
1566 }
1567
1568 /**
1569 * Get the base 36 SHA-1 value for a string of text
1570 * @param string $text
1571 * @return string
1572 */
1573 public static function base36Sha1( $text ) {
1574 return Wikimedia\base_convert( sha1( $text ), 16, 36, 31 );
1575 }
1576
1577 /**
1578 * Lazy-load the revision's text.
1579 * Currently hardcoded to the 'text' table storage engine.
1580 *
1581 * @return string|bool The revision's text, or false on failure
1582 */
1583 private function loadText() {
1584 global $wgRevisionCacheExpiry;
1585
1586 $cache = ObjectCache::getMainWANInstance();
1587 if ( $cache->getQoS( $cache::ATTR_EMULATION ) <= $cache::QOS_EMULATION_SQL ) {
1588 // Do not cache RDBMs blobs in...the RDBMs store
1589 $ttl = $cache::TTL_UNCACHEABLE;
1590 } else {
1591 $ttl = $wgRevisionCacheExpiry ?: $cache::TTL_UNCACHEABLE;
1592 }
1593
1594 // No negative caching; negative hits on text rows may be due to corrupted replica DBs
1595 return $cache->getWithSetCallback(
1596 $cache->makeKey( 'revisiontext', 'textid', $this->getTextId() ),
1597 $ttl,
1598 function () {
1599 return $this->fetchText();
1600 },
1601 [ 'pcGroup' => self::TEXT_CACHE_GROUP, 'pcTTL' => $cache::TTL_PROC_LONG ]
1602 );
1603 }
1604
1605 private function fetchText() {
1606 $textId = $this->getTextId();
1607
1608 // If we kept data for lazy extraction, use it now...
1609 if ( $this->mTextRow !== null ) {
1610 $row = $this->mTextRow;
1611 $this->mTextRow = null;
1612 } else {
1613 $row = null;
1614 }
1615
1616 // Callers doing updates will pass in READ_LATEST as usual. Since the text/blob tables
1617 // do not normally get rows changed around, set READ_LATEST_IMMUTABLE in those cases.
1618 $flags = $this->mQueryFlags;
1619 $flags |= DBAccessObjectUtils::hasFlags( $flags, self::READ_LATEST )
1620 ? self::READ_LATEST_IMMUTABLE
1621 : 0;
1622
1623 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
1624 DBAccessObjectUtils::getDBOptions( $flags );
1625
1626 if ( !$row ) {
1627 // Text data is immutable; check replica DBs first.
1628 $row = wfGetDB( $index )->selectRow(
1629 'text',
1630 [ 'old_text', 'old_flags' ],
1631 [ 'old_id' => $textId ],
1632 __METHOD__,
1633 $options
1634 );
1635 }
1636
1637 // Fallback to DB_MASTER in some cases if the row was not found
1638 if ( !$row && $fallbackIndex !== null ) {
1639 // Use FOR UPDATE if it was used to fetch this revision. This avoids missing the row
1640 // due to REPEATABLE-READ. Also fallback to the master if READ_LATEST is provided.
1641 $row = wfGetDB( $fallbackIndex )->selectRow(
1642 'text',
1643 [ 'old_text', 'old_flags' ],
1644 [ 'old_id' => $textId ],
1645 __METHOD__,
1646 $fallbackOptions
1647 );
1648 }
1649
1650 if ( !$row ) {
1651 wfDebugLog( 'Revision', "No text row with ID '$textId' (revision {$this->getId()})." );
1652 }
1653
1654 $text = self::getRevisionText( $row );
1655 if ( $row && $text === false ) {
1656 wfDebugLog( 'Revision', "No blob for text row '$textId' (revision {$this->getId()})." );
1657 }
1658
1659 return is_string( $text ) ? $text : false;
1660 }
1661
1662 /**
1663 * Create a new null-revision for insertion into a page's
1664 * history. This will not re-save the text, but simply refer
1665 * to the text from the previous version.
1666 *
1667 * Such revisions can for instance identify page rename
1668 * operations and other such meta-modifications.
1669 *
1670 * @param IDatabase $dbw
1671 * @param int $pageId ID number of the page to read from
1672 * @param string $summary Revision's summary
1673 * @param bool $minor Whether the revision should be considered as minor
1674 * @param User|null $user User object to use or null for $wgUser
1675 * @return Revision|null Revision or null on error
1676 */
1677 public static function newNullRevision( $dbw, $pageId, $summary, $minor, $user = null ) {
1678 global $wgContentHandlerUseDB, $wgContLang;
1679
1680 $fields = [ 'page_latest', 'page_namespace', 'page_title',
1681 'rev_text_id', 'rev_len', 'rev_sha1' ];
1682
1683 if ( $wgContentHandlerUseDB ) {
1684 $fields[] = 'rev_content_model';
1685 $fields[] = 'rev_content_format';
1686 }
1687
1688 $current = $dbw->selectRow(
1689 [ 'page', 'revision' ],
1690 $fields,
1691 [
1692 'page_id' => $pageId,
1693 'page_latest=rev_id',
1694 ],
1695 __METHOD__,
1696 [ 'FOR UPDATE' ] // T51581
1697 );
1698
1699 if ( $current ) {
1700 if ( !$user ) {
1701 global $wgUser;
1702 $user = $wgUser;
1703 }
1704
1705 // Truncate for whole multibyte characters
1706 $summary = $wgContLang->truncate( $summary, 255 );
1707
1708 $row = [
1709 'page' => $pageId,
1710 'user_text' => $user->getName(),
1711 'user' => $user->getId(),
1712 'comment' => $summary,
1713 'minor_edit' => $minor,
1714 'text_id' => $current->rev_text_id,
1715 'parent_id' => $current->page_latest,
1716 'len' => $current->rev_len,
1717 'sha1' => $current->rev_sha1
1718 ];
1719
1720 if ( $wgContentHandlerUseDB ) {
1721 $row['content_model'] = $current->rev_content_model;
1722 $row['content_format'] = $current->rev_content_format;
1723 }
1724
1725 $row['title'] = Title::makeTitle( $current->page_namespace, $current->page_title );
1726
1727 $revision = new Revision( $row );
1728 } else {
1729 $revision = null;
1730 }
1731
1732 return $revision;
1733 }
1734
1735 /**
1736 * Determine if the current user is allowed to view a particular
1737 * field of this revision, if it's marked as deleted.
1738 *
1739 * @param int $field One of self::DELETED_TEXT,
1740 * self::DELETED_COMMENT,
1741 * self::DELETED_USER
1742 * @param User|null $user User object to check, or null to use $wgUser
1743 * @return bool
1744 */
1745 public function userCan( $field, User $user = null ) {
1746 return self::userCanBitfield( $this->getVisibility(), $field, $user );
1747 }
1748
1749 /**
1750 * Determine if the current user is allowed to view a particular
1751 * field of this revision, if it's marked as deleted. This is used
1752 * by various classes to avoid duplication.
1753 *
1754 * @param int $bitfield Current field
1755 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
1756 * self::DELETED_COMMENT = File::DELETED_COMMENT,
1757 * self::DELETED_USER = File::DELETED_USER
1758 * @param User|null $user User object to check, or null to use $wgUser
1759 * @param Title|null $title A Title object to check for per-page restrictions on,
1760 * instead of just plain userrights
1761 * @return bool
1762 */
1763 public static function userCanBitfield( $bitfield, $field, User $user = null,
1764 Title $title = null
1765 ) {
1766 if ( $bitfield & $field ) { // aspect is deleted
1767 if ( $user === null ) {
1768 global $wgUser;
1769 $user = $wgUser;
1770 }
1771 if ( $bitfield & self::DELETED_RESTRICTED ) {
1772 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
1773 } elseif ( $field & self::DELETED_TEXT ) {
1774 $permissions = [ 'deletedtext' ];
1775 } else {
1776 $permissions = [ 'deletedhistory' ];
1777 }
1778 $permissionlist = implode( ', ', $permissions );
1779 if ( $title === null ) {
1780 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
1781 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
1782 } else {
1783 $text = $title->getPrefixedText();
1784 wfDebug( "Checking for $permissionlist on $text due to $field match on $bitfield\n" );
1785 foreach ( $permissions as $perm ) {
1786 if ( $title->userCan( $perm, $user ) ) {
1787 return true;
1788 }
1789 }
1790 return false;
1791 }
1792 } else {
1793 return true;
1794 }
1795 }
1796
1797 /**
1798 * Get rev_timestamp from rev_id, without loading the rest of the row
1799 *
1800 * @param Title $title
1801 * @param int $id
1802 * @return string|bool False if not found
1803 */
1804 static function getTimestampFromId( $title, $id, $flags = 0 ) {
1805 $db = ( $flags & self::READ_LATEST )
1806 ? wfGetDB( DB_MASTER )
1807 : wfGetDB( DB_REPLICA );
1808 // Casting fix for databases that can't take '' for rev_id
1809 if ( $id == '' ) {
1810 $id = 0;
1811 }
1812 $conds = [ 'rev_id' => $id ];
1813 $conds['rev_page'] = $title->getArticleID();
1814 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1815
1816 return ( $timestamp !== false ) ? wfTimestamp( TS_MW, $timestamp ) : false;
1817 }
1818
1819 /**
1820 * Get count of revisions per page...not very efficient
1821 *
1822 * @param IDatabase $db
1823 * @param int $id Page id
1824 * @return int
1825 */
1826 static function countByPageId( $db, $id ) {
1827 $row = $db->selectRow( 'revision', [ 'revCount' => 'COUNT(*)' ],
1828 [ 'rev_page' => $id ], __METHOD__ );
1829 if ( $row ) {
1830 return $row->revCount;
1831 }
1832 return 0;
1833 }
1834
1835 /**
1836 * Get count of revisions per page...not very efficient
1837 *
1838 * @param IDatabase $db
1839 * @param Title $title
1840 * @return int
1841 */
1842 static function countByTitle( $db, $title ) {
1843 $id = $title->getArticleID();
1844 if ( $id ) {
1845 return self::countByPageId( $db, $id );
1846 }
1847 return 0;
1848 }
1849
1850 /**
1851 * Check if no edits were made by other users since
1852 * the time a user started editing the page. Limit to
1853 * 50 revisions for the sake of performance.
1854 *
1855 * @since 1.20
1856 * @deprecated since 1.24
1857 *
1858 * @param IDatabase|int $db The Database to perform the check on. May be given as a
1859 * Database object or a database identifier usable with wfGetDB.
1860 * @param int $pageId The ID of the page in question
1861 * @param int $userId The ID of the user in question
1862 * @param string $since Look at edits since this time
1863 *
1864 * @return bool True if the given user was the only one to edit since the given timestamp
1865 */
1866 public static function userWasLastToEdit( $db, $pageId, $userId, $since ) {
1867 if ( !$userId ) {
1868 return false;
1869 }
1870
1871 if ( is_int( $db ) ) {
1872 $db = wfGetDB( $db );
1873 }
1874
1875 $res = $db->select( 'revision',
1876 'rev_user',
1877 [
1878 'rev_page' => $pageId,
1879 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1880 ],
1881 __METHOD__,
1882 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ] );
1883 foreach ( $res as $row ) {
1884 if ( $row->rev_user != $userId ) {
1885 return false;
1886 }
1887 }
1888 return true;
1889 }
1890
1891 /**
1892 * Load a revision based on a known page ID and current revision ID from the DB
1893 *
1894 * This method allows for the use of caching, though accessing anything that normally
1895 * requires permission checks (aside from the text) will trigger a small DB lookup.
1896 * The title will also be lazy loaded, though setTitle() can be used to preload it.
1897 *
1898 * @param IDatabase $db
1899 * @param int $pageId Page ID
1900 * @param int $revId Known current revision of this page
1901 * @return Revision|bool Returns false if missing
1902 * @since 1.28
1903 */
1904 public static function newKnownCurrent( IDatabase $db, $pageId, $revId ) {
1905 $cache = ObjectCache::getMainWANInstance();
1906 return $cache->getWithSetCallback(
1907 // Page/rev IDs passed in from DB to reflect history merges
1908 $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ),
1909 $cache::TTL_WEEK,
1910 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1911 $setOpts += Database::getCacheSetOptions( $db );
1912
1913 $rev = Revision::loadFromPageId( $db, $pageId, $revId );
1914 // Reflect revision deletion and user renames
1915 if ( $rev ) {
1916 $rev->mTitle = null; // mutable; lazy-load
1917 $rev->mRefreshMutableFields = true;
1918 }
1919
1920 return $rev ?: false; // don't cache negatives
1921 }
1922 );
1923 }
1924
1925 /**
1926 * For cached revisions, make sure the user name and rev_deleted is up-to-date
1927 */
1928 private function loadMutableFields() {
1929 if ( !$this->mRefreshMutableFields ) {
1930 return; // not needed
1931 }
1932
1933 $this->mRefreshMutableFields = false;
1934 $dbr = wfGetLB( $this->mWiki )->getConnectionRef( DB_REPLICA, [], $this->mWiki );
1935 $row = $dbr->selectRow(
1936 [ 'revision', 'user' ],
1937 [ 'rev_deleted', 'user_name' ],
1938 [ 'rev_id' => $this->mId, 'user_id = rev_user' ],
1939 __METHOD__
1940 );
1941 if ( $row ) { // update values
1942 $this->mDeleted = (int)$row->rev_deleted;
1943 $this->mUserText = $row->user_name;
1944 }
1945 }
1946 }