Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / AbstractContent.php
blob2535ba92b89ac4589ff85640f3b39b0702dcb055
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() );
106 * @return IContentHandlerFactory
108 protected function getContentHandlerFactory(): IContentHandlerFactory {
109 return MediaWikiServices::getInstance()->getContentHandlerFactory();
113 * @since 1.21
115 * @see Content::getDefaultFormat
116 * @return string
118 public function getDefaultFormat() {
119 return $this->getContentHandler()->getDefaultFormat();
123 * @since 1.21
125 * @see Content::getSupportedFormats
126 * @return string[]
128 public function getSupportedFormats() {
129 return $this->getContentHandler()->getSupportedFormats();
133 * @since 1.21
135 * @param string $format
137 * @return bool
139 * @see Content::isSupportedFormat
141 public function isSupportedFormat( $format ) {
142 if ( !$format ) {
143 return true; // this means "use the default"
146 return $this->getContentHandler()->isSupportedFormat( $format );
150 * @since 1.21
152 * @param string $format The serialization format to check.
154 * @throws MWException If the format is not supported by this content handler.
156 protected function checkFormat( $format ) {
157 if ( !$this->isSupportedFormat( $format ) ) {
158 throw new MWException(
159 "Format $format is not supported for content model " .
160 $this->getModel()
166 * @stable to override
167 * @since 1.21
169 * @param string|null $format
171 * @return string
173 * @see Content::serialize
175 public function serialize( $format = null ) {
176 return $this->getContentHandler()->serializeContent( $this, $format );
180 * Returns native representation of the data. Interpretation depends on
181 * the data model used, as given by getDataModel().
183 * @stable to override
184 * @since 1.21
186 * @deprecated since 1.33. Use getText() for TextContent instances.
187 * For other content models, use specialized getters.
188 * Emitting deprecation warnings since 1.41.
190 * @return mixed The native representation of the content. Could be a
191 * string, a nested array structure, an object, a binary blob...
192 * anything, really.
193 * @throws LogicException
195 * @note Caller must be aware of content model!
197 public function getNativeData() {
198 wfDeprecated( __METHOD__, '1.33' );
199 throw new LogicException( __METHOD__ . ': not implemented' );
203 * @stable to override
204 * @since 1.21
206 * @return bool
208 * @see Content::isEmpty
210 public function isEmpty() {
211 return $this->getSize() === 0;
215 * Subclasses may override this to implement (light weight) validation.
217 * @stable to override
218 * @since 1.21
220 * @return bool Always true.
222 * @see Content::isValid
224 public function isValid() {
225 return true;
229 * Decides whether two Content objects are equal.
230 * Two Content objects MUST not be considered equal if they do not share the same content model.
231 * Two Content objects that are equal SHOULD have the same serialization.
233 * This default implementation relies on equalsInternal() to determine whether the
234 * Content objects are logically equivalent. Subclasses that need to implement a custom
235 * equality check should consider overriding equalsInternal(). Subclasses that override
236 * equals() itself MUST make sure that the implementation returns false for $that === null,
237 * and true for $that === this. It MUST also return false if $that does not have the same
238 * content model.
240 * @stable to override
241 * @since 1.21
243 * @param Content|null $that
245 * @return bool
247 * @see Content::equals
249 public function equals( ?Content $that = null ) {
250 if ( $that === null ) {
251 return false;
254 if ( $that === $this ) {
255 return true;
258 if ( $that->getModel() !== $this->getModel() ) {
259 return false;
262 // For type safety. Needed for odd cases like non-TextContents using CONTENT_MODEL_WIKITEXT
263 if ( get_class( $that ) !== get_class( $this ) ) {
264 return false;
267 return $this->equalsInternal( $that );
271 * Checks whether $that is logically equal to this Content object.
273 * This method can be overwritten by subclasses that need to implement custom
274 * equality checks.
276 * This default implementation checks whether the serializations
277 * of $this and $that are the same: $this->serialize() === $that->serialize()
279 * Implementors can assume that $that is an instance of the same class
280 * as the present Content object, as long as equalsInternal() is only called
281 * by the standard implementation of equals().
283 * @note Do not call this method directly, call equals() instead.
285 * @stable to override
287 * @param Content $that
288 * @return bool
290 protected function equalsInternal( Content $that ) {
291 return $this->serialize() === $that->serialize();
295 * Subclasses that implement redirects should override this.
297 * @stable to override
298 * @since 1.21
300 * @return Title|null
302 * @see Content::getRedirectTarget
304 public function getRedirectTarget() {
305 return null;
309 * @since 1.21
311 * @return bool
313 * @see Content::isRedirect
315 public function isRedirect() {
316 return $this->getRedirectTarget() !== null;
320 * This default implementation always returns $this.
321 * Subclasses that implement redirects should override this.
323 * @stable to override
324 * @since 1.21
326 * @param Title $target
328 * @return Content $this
330 * @see Content::updateRedirect
332 public function updateRedirect( Title $target ) {
333 return $this;
337 * @stable to override
338 * @since 1.21
340 * @param string|int $sectionId
341 * @return null
343 * @see Content::getSection
345 public function getSection( $sectionId ) {
346 return null;
350 * @stable to override
351 * @since 1.21
353 * @param string|int|null|false $sectionId
354 * @param Content $with
355 * @param string $sectionTitle
356 * @return null
358 * @see Content::replaceSection
360 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
361 return null;
365 * @stable to override
366 * @since 1.21
368 * @param string $header
369 * @return Content $this
371 * @see Content::addSectionHeader
373 public function addSectionHeader( $header ) {
374 return $this;
378 * This default implementation always returns false. Subclasses may override
379 * this to supply matching logic.
381 * @stable to override
382 * @since 1.21
384 * @param MagicWord $word
386 * @return bool Always false.
388 * @see Content::matchMagicWord
390 public function matchMagicWord( MagicWord $word ) {
391 return false;
395 * This base implementation calls the hook ConvertContent to enable custom conversions.
396 * Subclasses may override this to implement conversion for "their" content model.
398 * @stable to override
400 * @param string $toModel
401 * @param string $lossy
403 * @return Content|false
405 * @see Content::convert()
407 public function convert( $toModel, $lossy = '' ) {
408 if ( $this->getModel() === $toModel ) {
409 // nothing to do, shorten out.
410 return $this;
413 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
414 $result = false;
416 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
417 ->onConvertContent( $this, $toModel, $lossy, $result );
419 return $result;
424 /** @deprecated class alias since 1.43 */
425 class_alias( AbstractContent::class, 'AbstractContent' );