Merge "rest: Return a 400 for invalid render IDs"
[mediawiki.git] / includes / content / AbstractContent.php
blobfd1ad4c5e58eb757c760c510f651bccd94ed1716
1 <?php
2 /**
3 * A content object represents page content, e.g. the text to show on a page.
4 * Content objects have no knowledge about how they relate to Wiki pages.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @since 1.21
23 * @file
24 * @ingroup Content
26 * @author Daniel Kinzler
29 namespace MediaWiki\Content;
31 use LogicException;
32 use MediaWiki\HookContainer\HookRunner;
33 use MediaWiki\MediaWikiServices;
34 use MediaWiki\Parser\MagicWord;
35 use MediaWiki\Title\Title;
36 use MWException;
38 /**
39 * Base implementation for content objects.
41 * @stable to extend
43 * @ingroup Content
45 abstract class AbstractContent implements Content {
46 /**
47 * Name of the content model this Content object represents.
48 * Use with CONTENT_MODEL_XXX constants
50 * @since 1.21
52 * @var string
54 protected $model_id;
56 /**
57 * @stable to call
59 * @param string|null $modelId
61 * @since 1.21
63 public function __construct( $modelId = null ) {
64 $this->model_id = $modelId;
67 /**
68 * @since 1.21
70 * @see Content::getModel
71 * @return string
73 public function getModel() {
74 return $this->model_id;
77 /**
78 * @since 1.21
80 * @param string $modelId The model to check
82 * @throws MWException If the provided ID is not the ID of the content model supported by this
83 * Content object.
85 protected function checkModelID( $modelId ) {
86 if ( $modelId !== $this->model_id ) {
87 throw new MWException(
88 "Bad content model: " .
89 "expected {$this->model_id} " .
90 "but got $modelId."
95 /**
96 * @since 1.21
98 * @see Content::getContentHandler
99 * @return ContentHandler
101 public function getContentHandler() {
102 return $this->getContentHandlerFactory()->getContentHandler( $this->getModel() );
105 protected function getContentHandlerFactory(): IContentHandlerFactory {
106 return MediaWikiServices::getInstance()->getContentHandlerFactory();
110 * @since 1.21
112 * @see Content::getDefaultFormat
113 * @return string
115 public function getDefaultFormat() {
116 return $this->getContentHandler()->getDefaultFormat();
120 * @since 1.21
122 * @see Content::getSupportedFormats
123 * @return string[]
125 public function getSupportedFormats() {
126 return $this->getContentHandler()->getSupportedFormats();
130 * @since 1.21
132 * @param string $format
134 * @return bool
136 * @see Content::isSupportedFormat
138 public function isSupportedFormat( $format ) {
139 if ( !$format ) {
140 return true; // this means "use the default"
143 return $this->getContentHandler()->isSupportedFormat( $format );
147 * @since 1.21
149 * @param string $format The serialization format to check.
151 * @throws MWException If the format is not supported by this content handler.
153 protected function checkFormat( $format ) {
154 if ( !$this->isSupportedFormat( $format ) ) {
155 throw new MWException(
156 "Format $format is not supported for content model " .
157 $this->getModel()
163 * @stable to override
164 * @since 1.21
166 * @param string|null $format
168 * @return string
170 * @see Content::serialize
172 public function serialize( $format = null ) {
173 return $this->getContentHandler()->serializeContent( $this, $format );
177 * Returns native representation of the data. Interpretation depends on
178 * the data model used, as given by getDataModel().
180 * @stable to override
181 * @since 1.21
183 * @deprecated since 1.33. Use getText() for TextContent instances.
184 * For other content models, use specialized getters.
185 * Emitting deprecation warnings since 1.41.
187 * @return mixed The native representation of the content. Could be a
188 * string, a nested array structure, an object, a binary blob...
189 * anything, really.
190 * @throws LogicException
192 * @note Caller must be aware of content model!
194 public function getNativeData() {
195 wfDeprecated( __METHOD__, '1.33' );
196 throw new LogicException( __METHOD__ . ': not implemented' );
200 * @stable to override
201 * @since 1.21
203 * @return bool
205 * @see Content::isEmpty
207 public function isEmpty() {
208 return $this->getSize() === 0;
212 * Subclasses may override this to implement (light weight) validation.
214 * @stable to override
215 * @since 1.21
217 * @return bool Always true.
219 * @see Content::isValid
221 public function isValid() {
222 return true;
226 * Decides whether two Content objects are equal.
227 * Two Content objects MUST not be considered equal if they do not share the same content model.
228 * Two Content objects that are equal SHOULD have the same serialization.
230 * This default implementation relies on equalsInternal() to determine whether the
231 * Content objects are logically equivalent. Subclasses that need to implement a custom
232 * equality check should consider overriding equalsInternal(). Subclasses that override
233 * equals() itself MUST make sure that the implementation returns false for $that === null,
234 * and true for $that === this. It MUST also return false if $that does not have the same
235 * content model.
237 * @stable to override
238 * @since 1.21
240 * @param Content|null $that
242 * @return bool
244 * @see Content::equals
246 public function equals( ?Content $that = null ) {
247 if ( $that === null ) {
248 return false;
251 if ( $that === $this ) {
252 return true;
255 if ( $that->getModel() !== $this->getModel() ) {
256 return false;
259 // For type safety. Needed for odd cases like non-TextContents using CONTENT_MODEL_WIKITEXT
260 if ( get_class( $that ) !== get_class( $this ) ) {
261 return false;
264 return $this->equalsInternal( $that );
268 * Checks whether $that is logically equal to this Content object.
270 * This method can be overwritten by subclasses that need to implement custom
271 * equality checks.
273 * This default implementation checks whether the serializations
274 * of $this and $that are the same: $this->serialize() === $that->serialize()
276 * Implementors can assume that $that is an instance of the same class
277 * as the present Content object, as long as equalsInternal() is only called
278 * by the standard implementation of equals().
280 * @note Do not call this method directly, call equals() instead.
282 * @stable to override
284 * @param Content $that
285 * @return bool
287 protected function equalsInternal( Content $that ) {
288 return $this->serialize() === $that->serialize();
292 * Subclasses that implement redirects should override this.
294 * @stable to override
295 * @since 1.21
297 * @return Title|null
299 * @see Content::getRedirectTarget
301 public function getRedirectTarget() {
302 return null;
306 * @since 1.21
308 * @return bool
310 * @see Content::isRedirect
312 public function isRedirect() {
313 return $this->getRedirectTarget() !== null;
317 * This default implementation always returns $this.
318 * Subclasses that implement redirects should override this.
320 * @stable to override
321 * @since 1.21
323 * @param Title $target
325 * @return Content $this
327 * @see Content::updateRedirect
329 public function updateRedirect( Title $target ) {
330 return $this;
334 * @stable to override
335 * @since 1.21
337 * @param string|int $sectionId
338 * @return null
340 * @see Content::getSection
342 public function getSection( $sectionId ) {
343 return null;
347 * @stable to override
348 * @since 1.21
350 * @param string|int|null|false $sectionId
351 * @param Content $with
352 * @param string $sectionTitle
353 * @return null
355 * @see Content::replaceSection
357 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
358 return null;
362 * @stable to override
363 * @since 1.21
365 * @param string $header
366 * @return Content $this
368 * @see Content::addSectionHeader
370 public function addSectionHeader( $header ) {
371 return $this;
375 * This default implementation always returns false. Subclasses may override
376 * this to supply matching logic.
378 * @stable to override
379 * @since 1.21
381 * @param MagicWord $word
383 * @return bool Always false.
385 * @see Content::matchMagicWord
387 public function matchMagicWord( MagicWord $word ) {
388 return false;
392 * This base implementation calls the hook ConvertContent to enable custom conversions.
393 * Subclasses may override this to implement conversion for "their" content model.
395 * @stable to override
397 * @param string $toModel
398 * @param string $lossy
400 * @return Content|false
402 * @see Content::convert()
404 public function convert( $toModel, $lossy = '' ) {
405 if ( $this->getModel() === $toModel ) {
406 // nothing to do, shorten out.
407 return $this;
410 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
411 $result = false;
413 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
414 ->onConvertContent( $this, $toModel, $lossy, $result );
416 return $result;
421 /** @deprecated class alias since 1.43 */
422 class_alias( AbstractContent::class, 'AbstractContent' );