Debian Codegraph @

includes/Revision/RevisionRecord.php

raw symbols SCIP
1 <?php
2 /**
3 * Page revision base class.
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 namespace MediaWiki\Revision;
24
25 use InvalidArgumentException;
26 use MediaWiki\CommentStore\CommentStoreComment;
27 use MediaWiki\Content\Content;
28 use MediaWiki\DAO\WikiAwareEntity;
29 use MediaWiki\DAO\WikiAwareEntityTrait;
30 use MediaWiki\Linker\LinkTarget;
31 use MediaWiki\Page\LegacyArticleIdAccess;
32 use MediaWiki\Page\PageIdentity;
33 use MediaWiki\Permissions\Authority;
34 use MediaWiki\Title\Title;
35 use MediaWiki\User\UserIdentity;
36 use Wikimedia\NonSerializable\NonSerializableTrait;
37
38 /**
39 * Page revision base class.
40 *
41 * RevisionRecords are considered value objects, but they may use callbacks for lazy loading.
42 * Note that while the base class has no setters, subclasses may offer a mutable interface.
43 *
44 * @since 1.31
45 * @since 1.32 Renamed from MediaWiki\Storage\RevisionRecord
46 */
47 abstract class RevisionRecord implements WikiAwareEntity {
48 use LegacyArticleIdAccess;
49 use NonSerializableTrait;
50 use WikiAwareEntityTrait;
51
52 // RevisionRecord deletion constants
53 public const DELETED_TEXT = 1;
54 public const DELETED_COMMENT = 2;
55 public const DELETED_USER = 4;
56 public const DELETED_RESTRICTED = 8;
57 public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED; // convenience
58 public const SUPPRESSED_ALL = self::DELETED_TEXT | self::DELETED_COMMENT | self::DELETED_USER |
59 self::DELETED_RESTRICTED; // convenience
60
61 // Audience options for accessors
62 public const FOR_PUBLIC = 1;
63 public const FOR_THIS_USER = 2;
64 public const RAW = 3;
65
66 /** @var string|false Wiki ID; false means the current wiki */
67 protected $wikiId = false;
68 /** @var int|null */
69 protected $mId;
70 /** @var int */
71 protected $mPageId;
72 /** @var UserIdentity|null */
73 protected $mUser;
74 /** @var bool */
75 protected $mMinorEdit = false;
76 /** @var string|null */
77 protected $mTimestamp;
78 /** @var int using the DELETED_XXX and SUPPRESSED_XXX flags */
79 protected $mDeleted = 0;
80 /** @var int|null */
81 protected $mSize;
82 /** @var string|null */
83 protected $mSha1;
84 /** @var int|null */
85 protected $mParentId;
86 /** @var CommentStoreComment|null */
87 protected $mComment;
88
89 /** @var PageIdentity */
90 protected $mPage;
91
92 /** @var RevisionSlots */
93 protected $mSlots;
94
95 /**
96 * @note Avoid calling this constructor directly. Use the appropriate methods
97 * in RevisionStore instead.
98 *
99 * @param PageIdentity $page The page this RevisionRecord is associated with.
100 * @param RevisionSlots $slots The slots of this revision.
101 * @param false|string $wikiId Relevant wiki id or self::LOCAL for the current one.
102 */
103 public function __construct( PageIdentity $page, RevisionSlots $slots, $wikiId = self::LOCAL ) {
104 $this->assertWikiIdParam( $wikiId );
105
106 $this->mPage = $page;
107 $this->mSlots = $slots;
108 $this->wikiId = $wikiId;
109 $this->mPageId = $this->getArticleId( $page );
110 }
111
112 /**
113 * @param RevisionRecord $rec
114 *
115 * @return bool True if this RevisionRecord is known to have same content as $rec.
116 * False if the content is different (or not known to be the same).
117 */
118 public function hasSameContent( RevisionRecord $rec ): bool {
119 if ( $rec === $this ) {
120 return true;
121 }
122
123 if ( $this->getId() !== null && $this->getId() === $rec->getId() ) {
124 return true;
125 }
126
127 // check size before hash, since size is quicker to compute
128 if ( $this->getSize() !== $rec->getSize() ) {
129 return false;
130 }
131
132 // instead of checking the hash, we could also check the content addresses of all slots.
133
134 if ( $this->getSha1() === $rec->getSha1() ) {
135 return true;
136 }
137
138 return false;
139 }
140
141 /**
142 * Returns the Content of the given slot of this revision.
143 * Call getSlotNames() to get a list of available slots.
144 *
145 * Note that for mutable Content objects, each call to this method will return a
146 * fresh clone.
147 *
148 * Use getContentOrThrow() for more specific error information.
149 *
150 * @param string $role The role name of the desired slot
151 * @param int $audience
152 * @param Authority|null $performer user on whose behalf to check
153 *
154 * @return Content|null The content of the given slot, or null on error
155 * @throws RevisionAccessException
156 */
157 public function getContent( $role, $audience = self::FOR_PUBLIC, ?Authority $performer = null ): ?Content {
158 try {
159 $content = $this->getSlot( $role, $audience, $performer )->getContent();
160 } catch ( BadRevisionException | SuppressedDataException $e ) {
161 return null;
162 }
163 return $content->copy();
164 }
165
166 /**
167 * Get the Content of the given slot of this revision.
168 *
169 * @param string $role The role name of the desired slot
170 * @param int $audience
171 * @param Authority|null $performer user on whose behalf to check
172 *
173 * @return Content
174 * @throws SuppressedDataException if the content is not viewable by the given audience
175 * @throws BadRevisionException if the content is missing or corrupted
176 * @throws RevisionAccessException
177 */
178 public function getContentOrThrow( $role, $audience = self::FOR_PUBLIC, ?Authority $performer = null ): Content {
179 if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
180 throw new SuppressedDataException(
181 'Access to the content has been suppressed for this audience' );
182 }
183
184 $content = $this->getSlot( $role, $audience, $performer )->getContent();
185 return $content->copy();
186 }
187
188 /**
189 * Returns meta-data for the given slot.
190 *
191 * @param string $role The role name of the desired slot
192 * @param int $audience
193 * @param Authority|null $performer user on whose behalf to check
194 *
195 * @throws RevisionAccessException if the slot does not exist or slot data
196 * could not be lazy-loaded.
197 * @return SlotRecord The slot meta-data. If access to the slot's content is forbidden,
198 * calling getContent() on the SlotRecord will throw an exception.
199 */
200 public function getSlot( $role, $audience = self::FOR_PUBLIC, ?Authority $performer = null ): SlotRecord {
201 $slot = $this->mSlots->getSlot( $role );
202
203 if ( !$this->audienceCan( self::DELETED_TEXT, $audience, $performer ) ) {
204 return SlotRecord::newWithSuppressedContent( $slot );
205 }
206
207 return $slot;
208 }
209
210 /**
211 * Returns whether the given slot is defined in this revision.
212 *
213 * @param string $role The role name of the desired slot
214 *
215 * @return bool
216 */
217 public function hasSlot( $role ): bool {
218 return $this->mSlots->hasSlot( $role );
219 }
220
221 /**
222 * Returns the slot names (roles) of all slots present in this revision.
223 * getContent() will succeed only for the names returned by this method.
224 *
225 * @return string[]
226 */
227 public function getSlotRoles(): array {
228 return $this->mSlots->getSlotRoles();
229 }
230
231 /**
232 * Returns the slots defined for this revision.
233 *
234 * @note This provides access to slot content with no audience checks applied.
235 * Calling getContent() on the RevisionSlots object returned here, or on any
236 * SlotRecord it returns from getSlot(), will not fail due to access restrictions.
237 * If audience checks are desired, use getSlot( $role, $audience, $performer )
238 * or getContent( $role, $audience, $performer ) instead.
239 *
240 * @return RevisionSlots
241 */
242 public function getSlots(): RevisionSlots {
243 return $this->mSlots;
244 }
245
246 /**
247 * Returns the slots that originate in this revision.
248 *
249 * Note that this does not include any slots inherited from some earlier revision,
250 * even if they are different from the slots in the immediate parent revision.
251 * This is the case for rollbacks: slots of a rollback revision are inherited from
252 * the rollback target, and are different from the slots in the parent revision,
253 * which was rolled back.
254 *
255 * To find all slots modified by this revision against its immediate parent
256 * revision, use RevisionSlotsUpdate::newFromRevisionSlots().
257 *
258 * @return RevisionSlots
259 */
260 public function getOriginalSlots(): RevisionSlots {
261 return new RevisionSlots( $this->mSlots->getOriginalSlots() );
262 }
263
264 /**
265 * Returns slots inherited from some previous revision.
266 *
267 * "Inherited" slots are all slots that do not originate in this revision.
268 * Note that these slots may still differ from the one in the parent revision.
269 * This is the case for rollbacks: slots of a rollback revision are inherited from
270 * the rollback target, and are different from the slots in the parent revision,
271 * which was rolled back.
272 *
273 * @return RevisionSlots
274 */
275 public function getInheritedSlots(): RevisionSlots {
276 return new RevisionSlots( $this->mSlots->getInheritedSlots() );
277 }
278
279 /**
280 * Returns primary slots (those that are not derived).
281 *
282 * @return RevisionSlots
283 * @since 1.36
284 */
285 public function getPrimarySlots(): RevisionSlots {
286 return new RevisionSlots( $this->mSlots->getPrimarySlots() );
287 }
288
289 /**
290 * Get revision ID. Depending on the concrete subclass, this may return null if
291 * the revision ID is not known (e.g. because the revision does not yet exist
292 * in the database).
293 *
294 * MCR migration note: this replaced Revision::getId
295 *
296 * @param string|false $wikiId The wiki ID expected by the caller.
297 * @return int|null
298 */
299 public function getId( $wikiId = self::LOCAL ) {
300 $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
301 return $this->mId;
302 }
303
304 /**
305 * Get parent revision ID (the original previous page revision).
306 * If there is no parent revision, this returns 0.
307 * If the parent revision is undefined or unknown, this returns null.
308 *
309 * @note As of MW 1.31, the database schema allows the parent ID to be
310 * NULL to indicate that it is unknown.
311 *
312 * MCR migration note: this replaced Revision::getParentId
313 *
314 * @param string|false $wikiId The wiki ID expected by the caller.
315 * @return int|null
316 */
317 public function getParentId( $wikiId = self::LOCAL ) {
318 $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
319 return $this->mParentId;
320 }
321
322 /**
323 * Returns the nominal size of this revision, in bogo-bytes.
324 * May be calculated on the fly if not known, which may in the worst
325 * case may involve loading all content.
326 *
327 * MCR migration note: this replaced Revision::getSize
328 *
329 * @throws RevisionAccessException if the size was unknown and could not be calculated.
330 * @return int
331 */
332 abstract public function getSize();
333
334 /**
335 * Returns the base36 sha1 of this revision. This hash is derived from the
336 * hashes of all slots associated with the revision.
337 * May be calculated on the fly if not known, which may in the worst
338 * case may involve loading all content.
339 *
340 * MCR migration note: this replaced Revision::getSha1
341 *
342 * @throws RevisionAccessException if the hash was unknown and could not be calculated.
343 * @return string
344 */
345 abstract public function getSha1();
346
347 /**
348 * Get the page ID. If the page does not yet exist, the page ID is 0.
349 *
350 * MCR migration note: this replaced Revision::getPage
351 *
352 * @param string|false $wikiId The wiki ID expected by the caller.
353 * @return int
354 */
355 public function getPageId( $wikiId = self::LOCAL ) {
356 $this->deprecateInvalidCrossWiki( $wikiId, '1.36' );
357 return $this->mPageId;
358 }
359
360 /**
361 * Get the ID of the wiki this revision belongs to.
362 *
363 * @return string|false The wiki's logical name, of false to indicate the local wiki.
364 */
365 public function getWikiId() {
366 return $this->wikiId;
367 }
368
369 /**
370 * Returns the title of the page this revision is associated with as a LinkTarget object.
371 *
372 * @throws InvalidArgumentException if this revision does not belong to a local wiki
373 * @return LinkTarget
374 */
375 public function getPageAsLinkTarget() {
376 // TODO: Should be TitleValue::newFromPage( $this->mPage ),
377 // but Title is used too much still, so let's keep propagating it
378 return Title::newFromPageIdentity( $this->mPage );
379 }
380
381 /**
382 * Returns the page this revision belongs to.
383 *
384 * MCR migration note: this replaced Revision::getTitle
385 *
386 * @since 1.36
387 *
388 * @return PageIdentity
389 */
390 public function getPage(): PageIdentity {
391 return $this->mPage;
392 }
393
394 /**
395 * Fetch revision's author's user identity, if it's available to the specified audience.
396 * If the specified audience does not have access to it, null will be
397 * returned. Depending on the concrete subclass, null may also be returned if the user is
398 * not yet specified.
399 *
400 * MCR migration note: this replaced Revision::getUser
401 *
402 * @param int $audience One of:
403 * RevisionRecord::FOR_PUBLIC to be displayed to all users
404 * RevisionRecord::FOR_THIS_USER to be displayed to the given user
405 * RevisionRecord::RAW get the ID regardless of permissions
406 * @param Authority|null $performer user on whose behalf to check
407 * @return UserIdentity|null
408 */
409 public function getUser( $audience = self::FOR_PUBLIC, ?Authority $performer = null ) {
410 if ( !$this->audienceCan( self::DELETED_USER, $audience, $performer ) ) {
411 return null;
412 } else {
413 return $this->mUser;
414 }
415 }
416
417 /**
418 * Fetch revision comment, if it's available to the specified audience.
419 * If the specified audience does not have access to the comment,
420 * this will return null. Depending on the concrete subclass, null may also be returned
421 * if the comment is not yet specified.
422 *
423 * MCR migration note: this replaced Revision::getComment
424 *
425 * @param int $audience One of:
426 * RevisionRecord::FOR_PUBLIC to be displayed to all users
427 * RevisionRecord::FOR_THIS_USER to be displayed to the given user
428 * RevisionRecord::RAW get the text regardless of permissions
429 * @param Authority|null $performer user on whose behalf to check
430 *
431 * @return CommentStoreComment|null
432 */
433 public function getComment( $audience = self::FOR_PUBLIC, ?Authority $performer = null ) {
434 if ( !$this->audienceCan( self::DELETED_COMMENT, $audience, $performer ) ) {
435 return null;
436 } else {
437 return $this->mComment;
438 }
439 }
440
441 /**
442 * MCR migration note: this replaced Revision::isMinor
443 *
444 * @return bool
445 */
446 public function isMinor() {
447 return (bool)$this->mMinorEdit;
448 }
449
450 /**
451 * MCR migration note: this replaced Revision::isDeleted
452 *
453 * @param int $field One of DELETED_* bitfield constants
454 *
455 * @return bool
456 */
457 public function isDeleted( $field ) {
458 return ( $this->getVisibility() & $field ) == $field;
459 }
460
461 /**
462 * Get the deletion bitfield of the revision
463 *
464 * MCR migration note: this replaced Revision::getVisibility
465 *
466 * @return int
467 */
468 public function getVisibility() {
469 return (int)$this->mDeleted;
470 }
471
472 /**
473 * MCR migration note: this replaced Revision::getTimestamp.
474 *
475 * May return null if the timestamp was not specified.
476 *
477 * @return string|null
478 */
479 public function getTimestamp() {
480 return $this->mTimestamp;
481 }
482
483 /**
484 * Check that the given audience has access to the given field.
485 *
486 * MCR migration note: this corresponded to Revision::userCan
487 *
488 * @param int $field One of self::DELETED_TEXT,
489 * self::DELETED_COMMENT,
490 * self::DELETED_USER
491 * @param int $audience One of:
492 * RevisionRecord::FOR_PUBLIC to be displayed to all users
493 * RevisionRecord::FOR_THIS_USER to be displayed to the given user
494 * RevisionRecord::RAW get the text regardless of permissions
495 * @param Authority|null $performer user on whose behalf to check
496 *
497 * @return bool
498 */
499 public function audienceCan( $field, $audience, ?Authority $performer = null ) {
500 if ( $audience == self::FOR_PUBLIC && $this->isDeleted( $field ) ) {
501 return false;
502 } elseif ( $audience == self::FOR_THIS_USER ) {
503 if ( !$performer ) {
504 throw new InvalidArgumentException(
505 'An Authority object must be given when checking FOR_THIS_USER audience.'
506 );
507 }
508
509 if ( !$this->userCan( $field, $performer ) ) {
510 return false;
511 }
512 }
513
514 return true;
515 }
516
517 /**
518 * Determine if the give authority is allowed to view a particular
519 * field of this revision, if it's marked as deleted.
520 *
521 * MCR migration note: this corresponded to Revision::userCan
522 *
523 * @param int $field One of self::DELETED_TEXT,
524 * self::DELETED_COMMENT,
525 * self::DELETED_USER
526 * @param Authority $performer user on whose behalf to check
527 * @return bool
528 */
529 public function userCan( $field, Authority $performer ) {
530 return self::userCanBitfield( $this->getVisibility(), $field, $performer, $this->mPage );
531 }
532
533 /**
534 * Determine if the current user is allowed to view a particular
535 * field of this revision, if it's marked as deleted. This is used
536 * by various classes to avoid duplication.
537 *
538 * MCR migration note: this replaced Revision::userCanBitfield
539 *
540 * @param int $bitfield Current field
541 * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
542 * self::DELETED_COMMENT = File::DELETED_COMMENT,
543 * self::DELETED_USER = File::DELETED_USER
544 * @param Authority $performer user on whose behalf to check
545 * @param PageIdentity|null $page A PageIdentity object to check for per-page restrictions on,
546 * instead of just plain user rights
547 * @return bool
548 */
549 public static function userCanBitfield( $bitfield, $field, Authority $performer, ?PageIdentity $page = null ) {
550 if ( $bitfield & $field ) { // aspect is deleted
551 if ( $bitfield & self::DELETED_RESTRICTED ) {
552 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
553 } elseif ( $field & self::DELETED_TEXT ) {
554 $permissions = [ 'deletedtext' ];
555 } else {
556 $permissions = [ 'deletedhistory' ];
557 }
558
559 $permissionlist = implode( ', ', $permissions );
560 if ( $page === null ) {
561 wfDebug( "Checking for $permissionlist due to $field match on $bitfield" );
562 return $performer->isAllowedAny( ...$permissions );
563 } else {
564 wfDebug( "Checking for $permissionlist on $page due to $field match on $bitfield" );
565 foreach ( $permissions as $perm ) {
566 if ( $performer->authorizeRead( $perm, $page ) ) {
567 return true;
568 }
569 }
570 return false;
571 }
572 } else {
573 return true;
574 }
575 }
576
577 /**
578 * Returns whether this RevisionRecord is ready for insertion, that is, whether it contains all
579 * information needed to save it to the database. This should trivially be true for
580 * RevisionRecords loaded from the database.
581 *
582 * Note that this may return true even if getId() or getPage() return null or 0, since these
583 * are generally assigned while the revision is saved to the database, and may not be available
584 * before.
585 *
586 * @return bool
587 */
588 public function isReadyForInsertion() {
589 // NOTE: don't check getSize() and getSha1(), since that may cause the full content to
590 // be loaded in order to calculate the values. Just assume these methods will not return
591 // null if mSlots is not empty.
592
593 // NOTE: getId() and getPageId() may return null before a revision is saved, so don't
594 // check them.
595
596 return $this->getTimestamp() !== null
597 && $this->getComment( self::RAW ) !== null
598 && $this->getUser( self::RAW ) !== null
599 && $this->mSlots->getSlotRoles() !== [];
600 }
601
602 /**
603 * Checks whether the revision record is a stored current revision.
604 * @since 1.35
605 * @return bool
606 */
607 public function isCurrent() {
608 return false;
609 }
610 }