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() );
106 * @return IContentHandlerFactory
108 protected function getContentHandlerFactory(): IContentHandlerFactory
{
109 return MediaWikiServices
::getInstance()->getContentHandlerFactory();
115 * @see Content::getDefaultFormat
118 public function getDefaultFormat() {
119 return $this->getContentHandler()->getDefaultFormat();
125 * @see Content::getSupportedFormats
128 public function getSupportedFormats() {
129 return $this->getContentHandler()->getSupportedFormats();
135 * @param string $format
139 * @see Content::isSupportedFormat
141 public function isSupportedFormat( $format ) {
143 return true; // this means "use the default"
146 return $this->getContentHandler()->isSupportedFormat( $format );
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 " .
166 * @stable to override
169 * @param string|null $format
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
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...
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
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
220 * @return bool Always true.
222 * @see Content::isValid
224 public function isValid() {
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
240 * @stable to override
243 * @param Content|null $that
247 * @see Content::equals
249 public function equals( ?Content
$that = null ) {
250 if ( $that === null ) {
254 if ( $that === $this ) {
258 if ( $that->getModel() !== $this->getModel() ) {
262 // For type safety. Needed for odd cases like non-TextContents using CONTENT_MODEL_WIKITEXT
263 if ( get_class( $that ) !== get_class( $this ) ) {
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
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
290 protected function equalsInternal( Content
$that ) {
291 return $this->serialize() === $that->serialize();
295 * Subclasses that implement redirects should override this.
297 * @stable to override
302 * @see Content::getRedirectTarget
304 public function getRedirectTarget() {
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
326 * @param Title $target
328 * @return Content $this
330 * @see Content::updateRedirect
332 public function updateRedirect( Title
$target ) {
337 * @stable to override
340 * @param string|int $sectionId
343 * @see Content::getSection
345 public function getSection( $sectionId ) {
350 * @stable to override
353 * @param string|int|null|false $sectionId
354 * @param Content $with
355 * @param string $sectionTitle
358 * @see Content::replaceSection
360 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
365 * @stable to override
368 * @param string $header
369 * @return Content $this
371 * @see Content::addSectionHeader
373 public function addSectionHeader( $header ) {
378 * This default implementation always returns false. Subclasses may override
379 * this to supply matching logic.
381 * @stable to override
384 * @param MagicWord $word
386 * @return bool Always false.
388 * @see Content::matchMagicWord
390 public function matchMagicWord( MagicWord
$word ) {
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.
413 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
416 ( new HookRunner( MediaWikiServices
::getInstance()->getHookContainer() ) )
417 ->onConvertContent( $this, $toModel, $lossy, $result );
424 /** @deprecated class alias since 1.43 */
425 class_alias( AbstractContent
::class, 'AbstractContent' );