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
59 public function getModel() {
60 return $this->model_id
;
66 * @param string $modelId The model to check
68 * @throws MWException If the provided ID is not the ID of the content model supported by this
71 protected function checkModelID( $modelId ) {
72 if ( $modelId !== $this->model_id
) {
73 throw new MWException(
74 "Bad content model: " .
75 "expected {$this->model_id} " .
84 * @see Content::getContentHandler
86 public function getContentHandler() {
87 return ContentHandler
::getForContent( $this );
93 * @see Content::getDefaultFormat
95 public function getDefaultFormat() {
96 return $this->getContentHandler()->getDefaultFormat();
102 * @see Content::getSupportedFormats
104 public function getSupportedFormats() {
105 return $this->getContentHandler()->getSupportedFormats();
111 * @param string $format
115 * @see Content::isSupportedFormat
117 public function isSupportedFormat( $format ) {
119 return true; // this means "use the default"
122 return $this->getContentHandler()->isSupportedFormat( $format );
128 * @param string $format The serialization format to check.
130 * @throws MWException If the format is not supported by this content handler.
132 protected function checkFormat( $format ) {
133 if ( !$this->isSupportedFormat( $format ) ) {
134 throw new MWException(
135 "Format $format is not supported for content model " .
144 * @param string $format
148 * @see Content::serialize
150 public function serialize( $format = null ) {
151 return $this->getContentHandler()->serializeContent( $this, $format );
159 * @see Content::isEmpty
161 public function isEmpty() {
162 return $this->getSize() === 0;
166 * Subclasses may override this to implement (light weight) validation.
170 * @return bool Always true.
172 * @see Content::isValid
174 public function isValid() {
181 * @param Content $that
185 * @see Content::equals
187 public function equals( Content
$that = null ) {
188 if ( is_null( $that ) ) {
192 if ( $that === $this ) {
196 if ( $that->getModel() !== $this->getModel() ) {
200 return $this->getNativeData() === $that->getNativeData();
204 * Returns a list of DataUpdate objects for recording information about this
205 * Content in some secondary data store.
207 * This default implementation returns a LinksUpdate object and calls the
208 * SecondaryDataUpdates hook.
210 * Subclasses may override this to determine the secondary data updates more
211 * efficiently, preferably without the need to generate a parser output object.
212 * They should however make sure to call SecondaryDataUpdates to give extensions
213 * a chance to inject additional updates.
217 * @param Title $title
218 * @param Content $old
219 * @param bool $recursive
220 * @param ParserOutput $parserOutput
222 * @return DataUpdate[]
224 * @see Content::getSecondaryDataUpdates()
226 public function getSecondaryDataUpdates( Title
$title, Content
$old = null,
227 $recursive = true, ParserOutput
$parserOutput = null
229 if ( $parserOutput === null ) {
230 $parserOutput = $this->getParserOutput( $title, null, null, false );
234 new LinksUpdate( $title, $parserOutput, $recursive )
237 Hooks
::run( 'SecondaryDataUpdates', [ $title, $old, $recursive, $parserOutput, &$updates ] );
245 * @return Title[]|null
247 * @see Content::getRedirectChain
249 public function getRedirectChain() {
250 global $wgMaxRedirects;
251 $title = $this->getRedirectTarget();
252 if ( is_null( $title ) ) {
255 // recursive check to follow double redirects
256 $recurse = $wgMaxRedirects;
257 $titles = [ $title ];
258 while ( --$recurse > 0 ) {
259 if ( $title->isRedirect() ) {
260 $page = WikiPage
::factory( $title );
261 $newtitle = $page->getRedirectTarget();
265 // Redirects to some special pages are not permitted
266 if ( $newtitle instanceof Title
&& $newtitle->isValidRedirectTarget() ) {
267 // The new title passes the checks, so make that our current
268 // title so that further recursion can be checked
270 $titles[] = $newtitle;
280 * Subclasses that implement redirects should override this.
286 * @see Content::getRedirectTarget
288 public function getRedirectTarget() {
293 * @note Migrated here from Title::newFromRedirectRecurse.
299 * @see Content::getUltimateRedirectTarget
301 public function getUltimateRedirectTarget() {
302 $titles = $this->getRedirectChain();
304 return $titles ?
array_pop( $titles ) : null;
312 * @see Content::isRedirect
314 public function isRedirect() {
315 return $this->getRedirectTarget() !== null;
319 * This default implementation always returns $this.
320 * Subclasses that implement redirects should override this.
324 * @param Title $target
326 * @return Content $this
328 * @see Content::updateRedirect
330 public function updateRedirect( Title
$target ) {
339 * @see Content::getSection
341 public function getSection( $sectionId ) {
350 * @see Content::replaceSection
352 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
359 * @return Content $this
361 * @see Content::preSaveTransform
363 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
370 * @return Content $this
372 * @see Content::addSectionHeader
374 public function addSectionHeader( $header ) {
381 * @return Content $this
383 * @see Content::preloadTransform
385 public function preloadTransform( Title
$title, ParserOptions
$popts, $params = [] ) {
394 * @see Content::prepareSave
396 public function prepareSave( WikiPage
$page, $flags, $parentRevId, User
$user ) {
397 if ( $this->isValid() ) {
398 return Status
::newGood();
400 return Status
::newFatal( "invalid-content-data" );
407 * @param WikiPage $page
408 * @param ParserOutput $parserOutput
410 * @return LinksDeletionUpdate[]
412 * @see Content::getDeletionUpdates
414 public function getDeletionUpdates( WikiPage
$page, ParserOutput
$parserOutput = null ) {
416 new LinksDeletionUpdate( $page ),
421 * This default implementation always returns false. Subclasses may override
422 * this to supply matching logic.
426 * @param MagicWord $word
428 * @return bool Always false.
430 * @see Content::matchMagicWord
432 public function matchMagicWord( MagicWord
$word ) {
437 * This base implementation calls the hook ConvertContent to enable custom conversions.
438 * Subclasses may override this to implement conversion for "their" content model.
440 * @param string $toModel
441 * @param string $lossy
443 * @return Content|bool
445 * @see Content::convert()
447 public function convert( $toModel, $lossy = '' ) {
448 if ( $this->getModel() === $toModel ) {
449 // nothing to do, shorten out.
453 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
456 Hooks
::run( 'ConvertContent', [ $this, $toModel, $lossy, &$result ] );
462 * Returns a ParserOutput object containing information derived from this content.
463 * Most importantly, unless $generateHtml was false, the return value contains an
464 * HTML representation of the content.
466 * Subclasses that want to control the parser output may override this, but it is
467 * preferred to override fillParserOutput() instead.
469 * Subclasses that override getParserOutput() itself should take care to call the
470 * ContentGetParserOutput hook.
474 * @param Title $title Context title for parsing
475 * @param int|null $revId Revision ID (for {{REVISIONID}})
476 * @param ParserOptions|null $options Parser options
477 * @param bool $generateHtml Whether or not to generate HTML
479 * @return ParserOutput Containing information derived from this content.
481 public function getParserOutput( Title
$title, $revId = null,
482 ParserOptions
$options = null, $generateHtml = true
484 if ( $options === null ) {
485 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
488 $po = new ParserOutput();
490 if ( Hooks
::run( 'ContentGetParserOutput',
491 [ $this, $title, $revId, $options, $generateHtml, &$po ] ) ) {
493 // Save and restore the old value, just in case something is reusing
494 // the ParserOptions object in some weird way.
495 $oldRedir = $options->getRedirectTarget();
496 $options->setRedirectTarget( $this->getRedirectTarget() );
497 $this->fillParserOutput( $title, $revId, $options, $generateHtml, $po );
498 $options->setRedirectTarget( $oldRedir );
501 Hooks
::run( 'ContentAlterParserOutput', [ $this, $title, $po ] );
507 * Fills the provided ParserOutput with information derived from the content.
508 * Unless $generateHtml was false, this includes an HTML representation of the content.
510 * This is called by getParserOutput() after consulting the ContentGetParserOutput hook.
511 * Subclasses are expected to override this method (or getParserOutput(), if need be).
512 * Subclasses of TextContent should generally override getHtml() instead.
514 * This placeholder implementation always throws an exception.
518 * @param Title $title Context title for parsing
519 * @param int|null $revId Revision ID (for {{REVISIONID}})
520 * @param ParserOptions $options Parser options
521 * @param bool $generateHtml Whether or not to generate HTML
522 * @param ParserOutput &$output The output object to fill (reference).
524 * @throws MWException
526 protected function fillParserOutput( Title
$title, $revId,
527 ParserOptions
$options, $generateHtml, ParserOutput
&$output
529 // Don't make abstract, so subclasses that override getParserOutput() directly don't fail.
530 throw new MWException( 'Subclasses of AbstractContent must override fillParserOutput!' );