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
26 * @author Daniel Kinzler
29 namespace MediaWiki\Content
;
32 use MediaWiki\HookContainer\HookRunner
;
33 use MediaWiki\MediaWikiServices
;
34 use MediaWiki\Parser\MagicWord
;
35 use MediaWiki\Title\Title
;
39 * Base implementation for content objects.
45 abstract class AbstractContent
implements Content
{
47 * Name of the content model this Content object represents.
48 * Use with CONTENT_MODEL_XXX constants
59 * @param string|null $modelId
63 public function __construct( $modelId = null ) {
64 $this->model_id
= $modelId;
70 * @see Content::getModel
73 public function getModel() {
74 return $this->model_id
;
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
85 protected function checkModelID( $modelId ) {
86 if ( $modelId !== $this->model_id
) {
87 throw new MWException(
88 "Bad content model: " .
89 "expected {$this->model_id} " .
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();
112 * @see Content::getDefaultFormat
115 public function getDefaultFormat() {
116 return $this->getContentHandler()->getDefaultFormat();
122 * @see Content::getSupportedFormats
125 public function getSupportedFormats() {
126 return $this->getContentHandler()->getSupportedFormats();
132 * @param string $format
136 * @see Content::isSupportedFormat
138 public function isSupportedFormat( $format ) {
140 return true; // this means "use the default"
143 return $this->getContentHandler()->isSupportedFormat( $format );
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 " .
163 * @stable to override
166 * @param string|null $format
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
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...
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
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
217 * @return bool Always true.
219 * @see Content::isValid
221 public function isValid() {
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
237 * @stable to override
240 * @param Content|null $that
244 * @see Content::equals
246 public function equals( ?Content
$that = null ) {
247 if ( $that === null ) {
251 if ( $that === $this ) {
255 if ( $that->getModel() !== $this->getModel() ) {
259 // For type safety. Needed for odd cases like non-TextContents using CONTENT_MODEL_WIKITEXT
260 if ( get_class( $that ) !== get_class( $this ) ) {
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
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
287 protected function equalsInternal( Content
$that ) {
288 return $this->serialize() === $that->serialize();
292 * Subclasses that implement redirects should override this.
294 * @stable to override
299 * @see Content::getRedirectTarget
301 public function getRedirectTarget() {
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
323 * @param Title $target
325 * @return Content $this
327 * @see Content::updateRedirect
329 public function updateRedirect( Title
$target ) {
334 * @stable to override
337 * @param string|int $sectionId
340 * @see Content::getSection
342 public function getSection( $sectionId ) {
347 * @stable to override
350 * @param string|int|null|false $sectionId
351 * @param Content $with
352 * @param string $sectionTitle
355 * @see Content::replaceSection
357 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
362 * @stable to override
365 * @param string $header
366 * @return Content $this
368 * @see Content::addSectionHeader
370 public function addSectionHeader( $header ) {
375 * This default implementation always returns false. Subclasses may override
376 * this to supply matching logic.
378 * @stable to override
381 * @param MagicWord $word
383 * @return bool Always false.
385 * @see Content::matchMagicWord
387 public function matchMagicWord( MagicWord
$word ) {
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.
410 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
413 ( new HookRunner( MediaWikiServices
::getInstance()->getHookContainer() ) )
414 ->onConvertContent( $this, $toModel, $lossy, $result );
421 /** @deprecated class alias since 1.43 */
422 class_alias( AbstractContent
::class, 'AbstractContent' );