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
30 * Base implementation for content objects.
34 abstract class AbstractContent
implements Content
{
36 * Name of the content model this Content object represents.
37 * Use with CONTENT_MODEL_XXX constants
41 * @var string $model_id
46 * @param string $modelId
50 public function __construct( $modelId = null ) {
51 $this->model_id
= $modelId;
57 * @see Content::getModel
60 public function getModel() {
61 return $this->model_id
;
67 * @param string $modelId The model to check
69 * @throws MWException If the provided ID is not the ID of the content model supported by this
72 protected function checkModelID( $modelId ) {
73 if ( $modelId !== $this->model_id
) {
74 throw new MWException(
75 "Bad content model: " .
76 "expected {$this->model_id} " .
85 * @see Content::getContentHandler
86 * @return ContentHandler
88 public function getContentHandler() {
89 return ContentHandler
::getForContent( $this );
95 * @see Content::getDefaultFormat
98 public function getDefaultFormat() {
99 return $this->getContentHandler()->getDefaultFormat();
105 * @see Content::getSupportedFormats
108 public function getSupportedFormats() {
109 return $this->getContentHandler()->getSupportedFormats();
115 * @param string $format
119 * @see Content::isSupportedFormat
121 public function isSupportedFormat( $format ) {
123 return true; // this means "use the default"
126 return $this->getContentHandler()->isSupportedFormat( $format );
132 * @param string $format The serialization format to check.
134 * @throws MWException If the format is not supported by this content handler.
136 protected function checkFormat( $format ) {
137 if ( !$this->isSupportedFormat( $format ) ) {
138 throw new MWException(
139 "Format $format is not supported for content model " .
148 * @param string $format
152 * @see Content::serialize
154 public function serialize( $format = null ) {
155 return $this->getContentHandler()->serializeContent( $this, $format );
163 * @see Content::isEmpty
165 public function isEmpty() {
166 return $this->getSize() === 0;
170 * Subclasses may override this to implement (light weight) validation.
174 * @return bool Always true.
176 * @see Content::isValid
178 public function isValid() {
185 * @param Content $that
189 * @see Content::equals
191 public function equals( Content
$that = null ) {
192 if ( is_null( $that ) ) {
196 if ( $that === $this ) {
200 if ( $that->getModel() !== $this->getModel() ) {
204 return $this->getNativeData() === $that->getNativeData();
208 * Returns a list of DataUpdate objects for recording information about this
209 * Content in some secondary data store.
211 * This default implementation returns a LinksUpdate object and calls the
212 * SecondaryDataUpdates hook.
214 * Subclasses may override this to determine the secondary data updates more
215 * efficiently, preferably without the need to generate a parser output object.
216 * They should however make sure to call SecondaryDataUpdates to give extensions
217 * a chance to inject additional updates.
221 * @param Title $title
222 * @param Content $old
223 * @param bool $recursive
224 * @param ParserOutput $parserOutput
226 * @return DataUpdate[]
228 * @see Content::getSecondaryDataUpdates()
230 public function getSecondaryDataUpdates( Title
$title, Content
$old = null,
231 $recursive = true, ParserOutput
$parserOutput = null
233 if ( $parserOutput === null ) {
234 $parserOutput = $this->getParserOutput( $title, null, null, false );
238 new LinksUpdate( $title, $parserOutput, $recursive )
241 Hooks
::run( 'SecondaryDataUpdates', [ $title, $old, $recursive, $parserOutput, &$updates ] );
249 * @return Title[]|null
251 * @see Content::getRedirectChain
253 public function getRedirectChain() {
254 global $wgMaxRedirects;
255 $title = $this->getRedirectTarget();
256 if ( is_null( $title ) ) {
259 // recursive check to follow double redirects
260 $recurse = $wgMaxRedirects;
261 $titles = [ $title ];
262 while ( --$recurse > 0 ) {
263 if ( $title->isRedirect() ) {
264 $page = WikiPage
::factory( $title );
265 $newtitle = $page->getRedirectTarget();
269 // Redirects to some special pages are not permitted
270 if ( $newtitle instanceof Title
&& $newtitle->isValidRedirectTarget() ) {
271 // The new title passes the checks, so make that our current
272 // title so that further recursion can be checked
274 $titles[] = $newtitle;
284 * Subclasses that implement redirects should override this.
290 * @see Content::getRedirectTarget
292 public function getRedirectTarget() {
297 * @note Migrated here from Title::newFromRedirectRecurse.
303 * @see Content::getUltimateRedirectTarget
305 public function getUltimateRedirectTarget() {
306 $titles = $this->getRedirectChain();
308 return $titles ?
array_pop( $titles ) : null;
316 * @see Content::isRedirect
318 public function isRedirect() {
319 return $this->getRedirectTarget() !== null;
323 * This default implementation always returns $this.
324 * Subclasses that implement redirects should override this.
328 * @param Title $target
330 * @return Content $this
332 * @see Content::updateRedirect
334 public function updateRedirect( Title
$target ) {
341 * @param string|int $sectionId
344 * @see Content::getSection
346 public function getSection( $sectionId ) {
353 * @param string|int|null|bool $sectionId
354 * @param Content $with
355 * @param string $sectionTitle
358 * @see Content::replaceSection
360 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
367 * @param Title $title
369 * @param ParserOptions $popts
370 * @return Content $this
372 * @see Content::preSaveTransform
374 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
381 * @param string $header
382 * @return Content $this
384 * @see Content::addSectionHeader
386 public function addSectionHeader( $header ) {
393 * @param Title $title
394 * @param ParserOptions $popts
395 * @param array $params
396 * @return Content $this
398 * @see Content::preloadTransform
400 public function preloadTransform( Title
$title, ParserOptions
$popts, $params = [] ) {
407 * @param WikiPage $page
409 * @param int $parentRevId
413 * @see Content::prepareSave
415 public function prepareSave( WikiPage
$page, $flags, $parentRevId, User
$user ) {
416 if ( $this->isValid() ) {
417 return Status
::newGood();
419 return Status
::newFatal( "invalid-content-data" );
426 * @param WikiPage $page
427 * @param ParserOutput|null $parserOutput
429 * @return LinksDeletionUpdate[]
431 * @see Content::getDeletionUpdates
433 public function getDeletionUpdates( WikiPage
$page, ParserOutput
$parserOutput = null ) {
435 new LinksDeletionUpdate( $page ),
440 * This default implementation always returns false. Subclasses may override
441 * this to supply matching logic.
445 * @param MagicWord $word
447 * @return bool Always false.
449 * @see Content::matchMagicWord
451 public function matchMagicWord( MagicWord
$word ) {
456 * This base implementation calls the hook ConvertContent to enable custom conversions.
457 * Subclasses may override this to implement conversion for "their" content model.
459 * @param string $toModel
460 * @param string $lossy
462 * @return Content|bool
464 * @see Content::convert()
466 public function convert( $toModel, $lossy = '' ) {
467 if ( $this->getModel() === $toModel ) {
468 // nothing to do, shorten out.
472 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
475 Hooks
::run( 'ConvertContent', [ $this, $toModel, $lossy, &$result ] );
481 * Returns a ParserOutput object containing information derived from this content.
482 * Most importantly, unless $generateHtml was false, the return value contains an
483 * HTML representation of the content.
485 * Subclasses that want to control the parser output may override this, but it is
486 * preferred to override fillParserOutput() instead.
488 * Subclasses that override getParserOutput() itself should take care to call the
489 * ContentGetParserOutput hook.
493 * @param Title $title Context title for parsing
494 * @param int|null $revId Revision ID (for {{REVISIONID}})
495 * @param ParserOptions|null $options Parser options
496 * @param bool $generateHtml Whether or not to generate HTML
498 * @return ParserOutput Containing information derived from this content.
500 public function getParserOutput( Title
$title, $revId = null,
501 ParserOptions
$options = null, $generateHtml = true
503 if ( $options === null ) {
504 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
507 $po = new ParserOutput();
509 if ( Hooks
::run( 'ContentGetParserOutput',
510 [ $this, $title, $revId, $options, $generateHtml, &$po ] )
512 // Save and restore the old value, just in case something is reusing
513 // the ParserOptions object in some weird way.
514 $oldRedir = $options->getRedirectTarget();
515 $options->setRedirectTarget( $this->getRedirectTarget() );
516 $this->fillParserOutput( $title, $revId, $options, $generateHtml, $po );
517 $options->setRedirectTarget( $oldRedir );
520 Hooks
::run( 'ContentAlterParserOutput', [ $this, $title, $po ] );
526 * Fills the provided ParserOutput with information derived from the content.
527 * Unless $generateHtml was false, this includes an HTML representation of the content.
529 * This is called by getParserOutput() after consulting the ContentGetParserOutput hook.
530 * Subclasses are expected to override this method (or getParserOutput(), if need be).
531 * Subclasses of TextContent should generally override getHtml() instead.
533 * This placeholder implementation always throws an exception.
537 * @param Title $title Context title for parsing
538 * @param int|null $revId Revision ID (for {{REVISIONID}})
539 * @param ParserOptions $options Parser options
540 * @param bool $generateHtml Whether or not to generate HTML
541 * @param ParserOutput &$output The output object to fill (reference).
543 * @throws MWException
545 protected function fillParserOutput( Title
$title, $revId,
546 ParserOptions
$options, $generateHtml, ParserOutput
&$output
548 // Don't make abstract, so subclasses that override getParserOutput() directly don't fail.
549 throw new MWException( 'Subclasses of AbstractContent must override fillParserOutput!' );