3 * Value object representing a content slot associated with a page revision.
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.
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.
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
23 namespace MediaWiki\Storage
;
26 use InvalidArgumentException
;
28 use OutOfBoundsException
;
29 use Wikimedia\Assert\Assert
;
32 * Value object representing a content slot associated with a page revision.
33 * SlotRecord provides direct access to a Content object.
34 * That access may be implemented through a callback.
43 * @var object database result row, as a raw object. Callbacks are supported for field values,
44 * to enable on-demand emulation of these values. This is primarily intended for use
45 * during schema migration.
50 * @var Content|callable
55 * Returns a new SlotRecord just like the given $slot, except that calling getContent()
56 * will fail with an exception.
58 * @param SlotRecord $slot
62 public static function newWithSuppressedContent( SlotRecord
$slot ) {
65 return new SlotRecord( $row, function () {
66 throw new SuppressedDataException( 'Content suppressed!' );
71 * Constructs a new SlotRecord from an existing SlotRecord, overriding some fields.
72 * The slot's content cannot be overwritten.
74 * @param SlotRecord $slot
75 * @param array $overrides
79 private static function newDerived( SlotRecord
$slot, array $overrides = [] ) {
80 $row = clone $slot->row
;
81 $row->slot_id
= null; // never copy the row ID!
83 foreach ( $overrides as $key => $value ) {
87 return new SlotRecord( $row, $slot->content
);
91 * Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord
92 * of a previous revision.
94 * Note that a SlotRecord constructed this way are intended as prototypes,
95 * to be used wit newSaved(). They are incomplete, so some getters such as
96 * getRevision() will fail.
98 * @param SlotRecord $slot
102 public static function newInherited( SlotRecord
$slot ) {
103 // Sanity check - we can't inherit from a Slot that's not attached to a revision.
104 $slot->getRevision();
108 // NOTE: slot_origin and content_address are copied from $slot.
109 return self
::newDerived( $slot, [
110 'slot_revision_id' => null,
115 * Constructs a new Slot from a Content object for a new revision.
116 * This is the preferred way to construct a slot for storing Content that
117 * resulted from a user edit. The slot is assumed to be not inherited.
119 * Note that a SlotRecord constructed this way are intended as prototypes,
120 * to be used wit newSaved(). They are incomplete, so some getters such as
121 * getAddress() will fail.
123 * @param string $role
124 * @param Content $content
126 * @return SlotRecord An incomplete proto-slot object, to be used with newSaved() later.
128 public static function newUnsaved( $role, Content
$content ) {
129 Assert
::parameterType( 'string', $role, '$role' );
132 'slot_id' => null, // not yet known
133 'slot_revision_id' => null, // not yet known
134 'slot_origin' => null, // not yet known, will be set in newSaved()
135 'content_size' => null, // compute later
136 'content_sha1' => null, // compute later
137 'slot_content_id' => null, // not yet known, will be set in newSaved()
138 'content_address' => null, // not yet known, will be set in newSaved()
139 'role_name' => $role,
140 'model_name' => $content->getModel(),
143 return new SlotRecord( (object)$row, $content );
147 * Constructs a complete SlotRecord for a newly saved revision, based on the incomplete
148 * proto-slot. This adds information that has only become available during saving,
149 * particularly the revision ID, content ID and content address.
151 * @param int $revisionId the revision the slot is to be associated with (field slot_revision_id).
152 * If $protoSlot already has a revision, it must be the same.
153 * @param int|null $contentId the ID of the row in the content table describing the content
154 * referenced by $contentAddress (field slot_content_id).
155 * If $protoSlot already has a content ID, it must be the same.
156 * @param string $contentAddress the slot's content address (field content_address).
157 * If $protoSlot already has an address, it must be the same.
158 * @param SlotRecord $protoSlot The proto-slot that was provided as input for creating a new
159 * revision. $protoSlot must have a content address if inherited.
161 * @return SlotRecord If the state of $protoSlot is inappropriate for saving a new revision.
163 public static function newSaved(
167 SlotRecord
$protoSlot
169 Assert
::parameterType( 'integer', $revisionId, '$revisionId' );
170 // TODO once migration is over $contentId must be an integer
171 Assert
::parameterType( 'integer|null', $contentId, '$contentId' );
172 Assert
::parameterType( 'string', $contentAddress, '$contentAddress' );
174 if ( $protoSlot->hasRevision() && $protoSlot->getRevision() !== $revisionId ) {
175 throw new LogicException(
176 "Mismatching revision ID $revisionId: "
177 . "The slot already belongs to revision {$protoSlot->getRevision()}. "
178 . "Use SlotRecord::newInherited() to re-use content between revisions."
182 if ( $protoSlot->hasAddress() && $protoSlot->getAddress() !== $contentAddress ) {
183 throw new LogicException(
184 "Mismatching blob address $contentAddress: "
185 . "The slot already has content at {$protoSlot->getAddress()}."
189 if ( $protoSlot->hasContentId() && $protoSlot->getContentId() !== $contentId ) {
190 throw new LogicException(
191 "Mismatching content ID $contentId: "
192 . "The slot already has content row {$protoSlot->getContentId()} associated."
196 if ( $protoSlot->isInherited() ) {
197 if ( !$protoSlot->hasAddress() ) {
198 throw new InvalidArgumentException(
199 "An inherited blob should have a content address!"
202 if ( !$protoSlot->hasField( 'slot_origin' ) ) {
203 throw new InvalidArgumentException(
204 "A saved inherited slot should have an origin set!"
207 $origin = $protoSlot->getOrigin();
209 $origin = $revisionId;
212 return self
::newDerived( $protoSlot, [
213 'slot_revision_id' => $revisionId,
214 'slot_content_id' => $contentId,
215 'slot_origin' => $origin,
216 'content_address' => $contentAddress,
221 * SlotRecord constructor.
223 * The following fields are supported by the $row parameter:
228 * @param object $row A database row composed of fields of the slot and content tables,
229 * as a raw object. Any field value can be a callback that produces the field value
230 * given this SlotRecord as a parameter. However, plain strings cannot be used as
231 * callbacks here, for security reasons.
232 * @param Content|callable $content The content object associated with the slot, or a
233 * callback that will return that Content object, given this SlotRecord as a parameter.
235 public function __construct( $row, $content ) {
236 Assert
::parameterType( 'object', $row, '$row' );
237 Assert
::parameterType( 'Content|callable', $content, '$content' );
240 property_exists( $row, 'slot_revision_id' ),
241 '$row->slot_revision_id',
245 property_exists( $row, 'slot_content_id' ),
246 '$row->slot_content_id',
250 property_exists( $row, 'content_address' ),
251 '$row->content_address',
255 property_exists( $row, 'model_name' ),
260 property_exists( $row, 'slot_origin' ),
265 !property_exists( $row, 'slot_inherited' ),
266 '$row->slot_inherited',
270 !property_exists( $row, 'slot_revision' ),
271 '$row->slot_revision',
276 $this->content
= $content;
280 * Implemented to defy serialization.
282 * @throws LogicException always
284 public function __sleep() {
285 throw new LogicException( __CLASS__
. ' is not serializable.' );
289 * Returns the Content of the given slot.
291 * @note This is free to load Content from whatever subsystem is necessary,
292 * performing potentially expensive operations and triggering I/O-related
295 * @note This method does not apply audience filtering.
297 * @throws SuppressedDataException if access to the content is not allowed according
298 * to the audience check performed by RevisionRecord::getSlot().
300 * @return Content The slot's content. This is a direct reference to the internal instance,
301 * copy before exposing to application logic!
303 public function getContent() {
304 if ( $this->content
instanceof Content
) {
305 return $this->content
;
308 $obj = call_user_func( $this->content
, $this );
310 Assert
::postcondition(
311 $obj instanceof Content
,
312 'Slot content callback should return a Content object'
315 $this->content
= $obj;
317 return $this->content
;
321 * Returns the string value of a data field from the database row supplied to the constructor.
322 * If the field was set to a callback, that callback is invoked and the result returned.
324 * @param string $name
326 * @throws OutOfBoundsException
327 * @throws IncompleteRevisionException
328 * @return mixed Returns the field's value, never null.
330 private function getField( $name ) {
331 if ( !isset( $this->row
->$name ) ) {
332 // distinguish between unknown and uninitialized fields
333 if ( property_exists( $this->row
, $name ) ) {
334 throw new IncompleteRevisionException( 'Uninitialized field: ' . $name );
336 throw new OutOfBoundsException( 'No such field: ' . $name );
340 $value = $this->row
->$name;
342 // NOTE: allow callbacks, but don't trust plain string callables from the database!
343 if ( !is_string( $value ) && is_callable( $value ) ) {
344 $value = call_user_func( $value, $this );
345 $this->setField( $name, $value );
352 * Returns the string value of a data field from the database row supplied to the constructor.
354 * @param string $name
356 * @throws OutOfBoundsException
357 * @throws IncompleteRevisionException
358 * @return string Returns the string value
360 private function getStringField( $name ) {
361 return strval( $this->getField( $name ) );
365 * Returns the int value of a data field from the database row supplied to the constructor.
367 * @param string $name
369 * @throws OutOfBoundsException
370 * @throws IncompleteRevisionException
371 * @return int Returns the int value
373 private function getIntField( $name ) {
374 return intval( $this->getField( $name ) );
378 * @param string $name
379 * @return bool whether this record contains the given field
381 private function hasField( $name ) {
382 if ( isset( $this->row
->$name ) ) {
383 // if the field is a callback, resolve first, then re-check
384 if ( !is_string( $this->row
->$name ) && is_callable( $this->row
->$name ) ) {
385 $this->getField( $name );
389 return isset( $this->row
->$name );
393 * Returns the ID of the revision this slot is associated with.
397 public function getRevision() {
398 return $this->getIntField( 'slot_revision_id' );
402 * Returns the revision ID of the revision that originated the slot's content.
406 public function getOrigin() {
407 return $this->getIntField( 'slot_origin' );
411 * Whether this slot was inherited from an older revision.
413 * If this SlotRecord is already attached to a revision, this returns true
414 * if the slot's revision of origin is the same as the revision it belongs to.
416 * If this SlotRecord is not yet attached to a revision, this returns true
417 * if the slot already has an address.
421 public function isInherited() {
422 if ( $this->hasRevision() ) {
423 return $this->getRevision() !== $this->getOrigin();
425 return $this->hasAddress();
430 * Whether this slot has an address. Slots will have an address if their
431 * content has been stored. While building a new revision,
432 * SlotRecords will not have an address associated.
436 public function hasAddress() {
437 return $this->hasField( 'content_address' );
441 * Whether this slot has an origin (revision ID that originated the slot's content.
447 public function hasOrigin() {
448 return $this->hasField( 'slot_origin' );
452 * Whether this slot has a content ID. Slots will have a content ID if their
453 * content has been stored in the content table. While building a new revision,
454 * SlotRecords will not have an ID associated.
456 * Also, during schema migration, hasContentId() may return false when encountering an
457 * un-migrated database entry in SCHEMA_COMPAT_WRITE_BOTH mode.
458 * It will however always return true for saved revisions on SCHEMA_COMPAT_READ_NEW mode,
459 * or without SCHEMA_COMPAT_WRITE_NEW mode. In the latter case, an emulated content ID
460 * is used, derived from the revision's text ID.
462 * Note that hasContentId() returning false while hasRevision() returns true always
463 * indicates an unmigrated row in SCHEMA_COMPAT_WRITE_BOTH mode, as described above.
464 * For an unsaved slot, both these methods would return false.
470 public function hasContentId() {
471 return $this->hasField( 'slot_content_id' );
475 * Whether this slot has revision ID associated. Slots will have a revision ID associated
476 * only if they were loaded as part of an existing revision. While building a new revision,
477 * Slotrecords will not have a revision ID associated.
481 public function hasRevision() {
482 return $this->hasField( 'slot_revision_id' );
486 * Returns the role of the slot.
490 public function getRole() {
491 return $this->getStringField( 'role_name' );
495 * Returns the address of this slot's content.
496 * This address can be used with BlobStore to load the Content object.
500 public function getAddress() {
501 return $this->getStringField( 'content_address' );
505 * Returns the ID of the content meta data row associated with the slot.
506 * This information should be irrelevant to application logic, it is here to allow
507 * the construction of a full row for the revision table.
509 * Note that this method may return an emulated value during schema migration in
510 * SCHEMA_COMPAT_WRITE_OLD mode. See RevisionStore::emulateContentId for more information.
514 public function getContentId() {
515 return $this->getIntField( 'slot_content_id' );
519 * Returns the content size
521 * @return int size of the content, in bogo-bytes, as reported by Content::getSize.
523 public function getSize() {
525 $size = $this->getIntField( 'content_size' );
526 } catch ( IncompleteRevisionException
$ex ) {
527 $size = $this->getContent()->getSize();
528 $this->setField( 'content_size', $size );
535 * Returns the content size
537 * @return string hash of the content.
539 public function getSha1() {
541 $sha1 = $this->getStringField( 'content_sha1' );
542 } catch ( IncompleteRevisionException
$ex ) {
543 $format = $this->hasField( 'format_name' )
544 ?
$this->getStringField( 'format_name' )
547 $data = $this->getContent()->serialize( $format );
548 $sha1 = self
::base36Sha1( $data );
549 $this->setField( 'content_sha1', $sha1 );
556 * Returns the content model. This is the model name that decides
557 * which ContentHandler is appropriate for interpreting the
558 * data of the blob referenced by the address returned by getAddress().
560 * @return string the content model of the content
562 public function getModel() {
564 $model = $this->getStringField( 'model_name' );
565 } catch ( IncompleteRevisionException
$ex ) {
566 $model = $this->getContent()->getModel();
567 $this->setField( 'model_name', $model );
574 * Returns the blob serialization format as a MIME type.
576 * @note When this method returns null, the caller is expected
577 * to auto-detect the serialization format, or to rely on
578 * the default format associated with the content model.
580 * @return string|null
582 public function getFormat() {
583 // XXX: we currently do not plan to store the format for each slot!
585 if ( $this->hasField( 'format_name' ) ) {
586 return $this->getStringField( 'format_name' );
593 * @param string $name
594 * @param string|int|null $value
596 private function setField( $name, $value ) {
597 $this->row
->$name = $value;
601 * Get the base 36 SHA-1 value for a string of text
603 * MCR migration note: this replaces Revision::base36Sha1
605 * @param string $blob
608 public static function base36Sha1( $blob ) {
609 return \Wikimedia\base_convert
( sha1( $blob ), 16, 36, 31 );
613 * Returns true if $other has the same content as this slot.
614 * The check is performed based on the model, address size, and hash.
615 * Two slots can have the same content if they use different content addresses,
616 * but if they have the same address and the same model, they have the same content.
617 * Two slots can have the same content if they belong to different
618 * revisions or pages.
620 * Note that hasSameContent() may return false even if Content::equals returns true for
621 * the content of two slots. This may happen if the two slots have different serializations
622 * representing equivalent Content. Such false negatives are considered acceptable. Code
623 * that has to be absolutely sure the Content is really not the same if hasSameContent()
624 * returns false should call getContent() and compare the Content objects directly.
628 * @param SlotRecord $other
631 public function hasSameContent( SlotRecord
$other ) {
632 if ( $other === $this ) {
636 if ( $this->getModel() !== $other->getModel() ) {
640 if ( $this->hasAddress()
641 && $other->hasAddress()
642 && $this->getAddress() == $other->getAddress()
647 if ( $this->getSize() !== $other->getSize() ) {
651 if ( $this->getSha1() !== $other->getSha1() ) {