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.
41 * @var object database result row, as a raw object. Callbacks are supported for field values,
42 * to enable on-demand emulation of these values. This is primarily intended for use
43 * during schema migration.
48 * @var Content|callable
53 * Returns a new SlotRecord just like the given $slot, except that calling getContent()
54 * will fail with an exception.
56 * @param SlotRecord $slot
60 public static function newWithSuppressedContent( SlotRecord
$slot ) {
63 return new SlotRecord( $row, function () {
64 throw new SuppressedDataException( 'Content suppressed!' );
69 * Constructs a new SlotRecord from an existing SlotRecord, overriding some fields.
70 * The slot's content cannot be overwritten.
72 * @param SlotRecord $slot
73 * @param array $overrides
77 private static function newDerived( SlotRecord
$slot, array $overrides = [] ) {
78 $row = clone $slot->row
;
79 $row->slot_id
= null; // never copy the row ID!
81 foreach ( $overrides as $key => $value ) {
85 return new SlotRecord( $row, $slot->content
);
89 * Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord
90 * of a previous revision.
92 * Note that a SlotRecord constructed this way are intended as prototypes,
93 * to be used wit newSaved(). They are incomplete, so some getters such as
94 * getRevision() will fail.
96 * @param SlotRecord $slot
100 public static function newInherited( SlotRecord
$slot ) {
101 // Sanity check - we can't inherit from a Slot that's not attached to a revision.
102 $slot->getRevision();
106 // NOTE: slot_origin and content_address are copied from $slot.
107 return self
::newDerived( $slot, [
108 'slot_revision_id' => null,
113 * Constructs a new Slot from a Content object for a new revision.
114 * This is the preferred way to construct a slot for storing Content that
115 * resulted from a user edit. The slot is assumed to be not inherited.
117 * Note that a SlotRecord constructed this way are intended as prototypes,
118 * to be used wit newSaved(). They are incomplete, so some getters such as
119 * getAddress() will fail.
121 * @param string $role
122 * @param Content $content
124 * @return SlotRecord An incomplete proto-slot object, to be used with newSaved() later.
126 public static function newUnsaved( $role, Content
$content ) {
127 Assert
::parameterType( 'string', $role, '$role' );
130 'slot_id' => null, // not yet known
131 'slot_revision_id' => null, // not yet known
132 'slot_origin' => null, // not yet known, will be set in newSaved()
133 'content_size' => null, // compute later
134 'content_sha1' => null, // compute later
135 'slot_content_id' => null, // not yet known, will be set in newSaved()
136 'content_address' => null, // not yet known, will be set in newSaved()
137 'role_name' => $role,
138 'model_name' => $content->getModel(),
141 return new SlotRecord( (object)$row, $content );
145 * Constructs a complete SlotRecord for a newly saved revision, based on the incomplete
146 * proto-slot. This adds information that has only become available during saving,
147 * particularly the revision ID, content ID and content address.
149 * @param int $revisionId the revision the slot is to be associated with (field slot_revision_id).
150 * If $protoSlot already has a revision, it must be the same.
151 * @param int|null $contentId the ID of the row in the content table describing the content
152 * referenced by $contentAddress (field slot_content_id).
153 * If $protoSlot already has a content ID, it must be the same.
154 * @param string $contentAddress the slot's content address (field content_address).
155 * If $protoSlot already has an address, it must be the same.
156 * @param SlotRecord $protoSlot The proto-slot that was provided as input for creating a new
157 * revision. $protoSlot must have a content address if inherited.
159 * @return SlotRecord If the state of $protoSlot is inappropriate for saving a new revision.
161 public static function newSaved(
165 SlotRecord
$protoSlot
167 Assert
::parameterType( 'integer', $revisionId, '$revisionId' );
168 // TODO once migration is over $contentId must be an integer
169 Assert
::parameterType( 'integer|null', $contentId, '$contentId' );
170 Assert
::parameterType( 'string', $contentAddress, '$contentAddress' );
172 if ( $protoSlot->hasRevision() && $protoSlot->getRevision() !== $revisionId ) {
173 throw new LogicException(
174 "Mismatching revision ID $revisionId: "
175 . "The slot already belongs to revision {$protoSlot->getRevision()}. "
176 . "Use SlotRecord::newInherited() to re-use content between revisions."
180 if ( $protoSlot->hasAddress() && $protoSlot->getAddress() !== $contentAddress ) {
181 throw new LogicException(
182 "Mismatching blob address $contentAddress: "
183 . "The slot already has content at {$protoSlot->getAddress()}."
187 if ( $protoSlot->hasContentId() && $protoSlot->getContentId() !== $contentId ) {
188 throw new LogicException(
189 "Mismatching content ID $contentId: "
190 . "The slot already has content row {$protoSlot->getContentId()} associated."
194 if ( $protoSlot->isInherited() ) {
195 if ( !$protoSlot->hasAddress() ) {
196 throw new InvalidArgumentException(
197 "An inherited blob should have a content address!"
200 if ( !$protoSlot->hasField( 'slot_origin' ) ) {
201 throw new InvalidArgumentException(
202 "A saved inherited slot should have an origin set!"
205 $origin = $protoSlot->getOrigin();
207 $origin = $revisionId;
210 return self
::newDerived( $protoSlot, [
211 'slot_revision_id' => $revisionId,
212 'slot_content_id' => $contentId,
213 'slot_origin' => $origin,
214 'content_address' => $contentAddress,
219 * SlotRecord constructor.
221 * The following fields are supported by the $row parameter:
226 * @param object $row A database row composed of fields of the slot and content tables,
227 * as a raw object. Any field value can be a callback that produces the field value
228 * given this SlotRecord as a parameter. However, plain strings cannot be used as
229 * callbacks here, for security reasons.
230 * @param Content|callable $content The content object associated with the slot, or a
231 * callback that will return that Content object, given this SlotRecord as a parameter.
233 public function __construct( $row, $content ) {
234 Assert
::parameterType( 'object', $row, '$row' );
235 Assert
::parameterType( 'Content|callable', $content, '$content' );
238 property_exists( $row, 'slot_revision_id' ),
239 '$row->slot_revision_id',
243 property_exists( $row, 'slot_content_id' ),
244 '$row->slot_content_id',
248 property_exists( $row, 'content_address' ),
249 '$row->content_address',
253 property_exists( $row, 'model_name' ),
258 property_exists( $row, 'slot_origin' ),
263 !property_exists( $row, 'slot_inherited' ),
264 '$row->slot_inherited',
268 !property_exists( $row, 'slot_revision' ),
269 '$row->slot_revision',
274 $this->content
= $content;
278 * Implemented to defy serialization.
280 * @throws LogicException always
282 public function __sleep() {
283 throw new LogicException( __CLASS__
. ' is not serializable.' );
287 * Returns the Content of the given slot.
289 * @note This is free to load Content from whatever subsystem is necessary,
290 * performing potentially expensive operations and triggering I/O-related
293 * @note This method does not apply audience filtering.
295 * @throws SuppressedDataException if access to the content is not allowed according
296 * to the audience check performed by RevisionRecord::getSlot().
298 * @return Content The slot's content. This is a direct reference to the internal instance,
299 * copy before exposing to application logic!
301 public function getContent() {
302 if ( $this->content
instanceof Content
) {
303 return $this->content
;
306 $obj = call_user_func( $this->content
, $this );
308 Assert
::postcondition(
309 $obj instanceof Content
,
310 'Slot content callback should return a Content object'
313 $this->content
= $obj;
315 return $this->content
;
319 * Returns the string value of a data field from the database row supplied to the constructor.
320 * If the field was set to a callback, that callback is invoked and the result returned.
322 * @param string $name
324 * @throws OutOfBoundsException
325 * @throws IncompleteRevisionException
326 * @return mixed Returns the field's value, never null.
328 private function getField( $name ) {
329 if ( !isset( $this->row
->$name ) ) {
330 // distinguish between unknown and uninitialized fields
331 if ( property_exists( $this->row
, $name ) ) {
332 throw new IncompleteRevisionException( 'Uninitialized field: ' . $name );
334 throw new OutOfBoundsException( 'No such field: ' . $name );
338 $value = $this->row
->$name;
340 // NOTE: allow callbacks, but don't trust plain string callables from the database!
341 if ( !is_string( $value ) && is_callable( $value ) ) {
342 $value = call_user_func( $value, $this );
343 $this->setField( $name, $value );
350 * Returns the string value of a data field from the database row supplied to the constructor.
352 * @param string $name
354 * @throws OutOfBoundsException
355 * @throws IncompleteRevisionException
356 * @return string Returns the string value
358 private function getStringField( $name ) {
359 return strval( $this->getField( $name ) );
363 * Returns the int value of a data field from the database row supplied to the constructor.
365 * @param string $name
367 * @throws OutOfBoundsException
368 * @throws IncompleteRevisionException
369 * @return int Returns the int value
371 private function getIntField( $name ) {
372 return intval( $this->getField( $name ) );
376 * @param string $name
377 * @return bool whether this record contains the given field
379 private function hasField( $name ) {
380 if ( isset( $this->row
->$name ) ) {
381 // if the field is a callback, resolve first, then re-check
382 if ( !is_string( $this->row
->$name ) && is_callable( $this->row
->$name ) ) {
383 $this->getField( $name );
387 return isset( $this->row
->$name );
391 * Returns the ID of the revision this slot is associated with.
395 public function getRevision() {
396 return $this->getIntField( 'slot_revision_id' );
400 * Returns the revision ID of the revision that originated the slot's content.
404 public function getOrigin() {
405 return $this->getIntField( 'slot_origin' );
409 * Whether this slot was inherited from an older revision.
411 * If this SlotRecord is already attached to a revision, this returns true
412 * if the slot's revision of origin is the same as the revision it belongs to.
414 * If this SlotRecord is not yet attached to a revision, this returns true
415 * if the slot already has an address.
419 public function isInherited() {
420 if ( $this->hasRevision() ) {
421 return $this->getRevision() !== $this->getOrigin();
423 return $this->hasAddress();
428 * Whether this slot has an address. Slots will have an address if their
429 * content has been stored. While building a new revision,
430 * SlotRecords will not have an address associated.
434 public function hasAddress() {
435 return $this->hasField( 'content_address' );
439 * Whether this slot has an origin (revision ID that originated the slot's content.
445 public function hasOrigin() {
446 return $this->hasField( 'slot_origin' );
450 * Whether this slot has a content ID. Slots will have a content ID if their
451 * content has been stored in the content table. While building a new revision,
452 * SlotRecords will not have an ID associated.
458 public function hasContentId() {
459 return $this->hasField( 'slot_content_id' );
463 * Whether this slot has revision ID associated. Slots will have a revision ID associated
464 * only if they were loaded as part of an existing revision. While building a new revision,
465 * Slotrecords will not have a revision ID associated.
469 public function hasRevision() {
470 return $this->hasField( 'slot_revision_id' );
474 * Returns the role of the slot.
478 public function getRole() {
479 return $this->getStringField( 'role_name' );
483 * Returns the address of this slot's content.
484 * This address can be used with BlobStore to load the Content object.
488 public function getAddress() {
489 return $this->getStringField( 'content_address' );
493 * Returns the ID of the content meta data row associated with the slot.
494 * This information should be irrelevant to application logic, it is here to allow
495 * the construction of a full row for the revision table.
499 public function getContentId() {
500 return $this->getIntField( 'slot_content_id' );
504 * Returns the content size
506 * @return int size of the content, in bogo-bytes, as reported by Content::getSize.
508 public function getSize() {
510 $size = $this->getIntField( 'content_size' );
511 } catch ( IncompleteRevisionException
$ex ) {
512 $size = $this->getContent()->getSize();
513 $this->setField( 'content_size', $size );
520 * Returns the content size
522 * @return string hash of the content.
524 public function getSha1() {
526 $sha1 = $this->getStringField( 'content_sha1' );
527 } catch ( IncompleteRevisionException
$ex ) {
528 $format = $this->hasField( 'format_name' )
529 ?
$this->getStringField( 'format_name' )
532 $data = $this->getContent()->serialize( $format );
533 $sha1 = self
::base36Sha1( $data );
534 $this->setField( 'content_sha1', $sha1 );
541 * Returns the content model. This is the model name that decides
542 * which ContentHandler is appropriate for interpreting the
543 * data of the blob referenced by the address returned by getAddress().
545 * @return string the content model of the content
547 public function getModel() {
549 $model = $this->getStringField( 'model_name' );
550 } catch ( IncompleteRevisionException
$ex ) {
551 $model = $this->getContent()->getModel();
552 $this->setField( 'model_name', $model );
559 * Returns the blob serialization format as a MIME type.
561 * @note When this method returns null, the caller is expected
562 * to auto-detect the serialization format, or to rely on
563 * the default format associated with the content model.
565 * @return string|null
567 public function getFormat() {
568 // XXX: we currently do not plan to store the format for each slot!
570 if ( $this->hasField( 'format_name' ) ) {
571 return $this->getStringField( 'format_name' );
578 * @param string $name
579 * @param string|int|null $value
581 private function setField( $name, $value ) {
582 $this->row
->$name = $value;
586 * Get the base 36 SHA-1 value for a string of text
588 * MCR migration note: this replaces Revision::base36Sha1
590 * @param string $blob
593 public static function base36Sha1( $blob ) {
594 return \Wikimedia\base_convert
( sha1( $blob ), 16, 36, 31 );
598 * Returns true if $other has the same content as this slot.
599 * The check is performed based on the model, address size, and hash.
600 * Two slots can have the same content if they use different content addresses,
601 * but if they have the same address and the same model, they have the same content.
602 * Two slots can have the same content if they belong to different
603 * revisions or pages.
605 * Note that hasSameContent() may return false even if Content::equals returns true for
606 * the content of two slots. This may happen if the two slots have different serializations
607 * representing equivalent Content. Such false negatives are considered acceptable. Code
608 * that has to be absolutely sure the Content is really not the same if hasSameContent()
609 * returns false should call getContent() and compare the Content objects directly.
613 * @param SlotRecord $other
616 public function hasSameContent( SlotRecord
$other ) {
617 if ( $other === $this ) {
621 if ( $this->getModel() !== $other->getModel() ) {
625 if ( $this->hasAddress()
626 && $other->hasAddress()
627 && $this->getAddress() == $other->getAddress()
632 if ( $this->getSize() !== $other->getSize() ) {
636 if ( $this->getSha1() !== $other->getSha1() ) {