Implement extension registration from an extension.json file
[mediawiki.git] / includes / content / AbstractContent.php
blob816572ca01aa734ab2c312536c7e9c94923ed892
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 /**
30 * Base implementation for content objects.
32 * @ingroup Content
34 abstract class AbstractContent implements Content {
35 /**
36 * Name of the content model this Content object represents.
37 * Use with CONTENT_MODEL_XXX constants
39 * @since 1.21
41 * @var string $model_id
43 protected $model_id;
45 /**
46 * @param string $modelId
48 * @since 1.21
50 public function __construct( $modelId = null ) {
51 $this->model_id = $modelId;
54 /**
55 * @since 1.21
57 * @see Content::getModel
59 public function getModel() {
60 return $this->model_id;
63 /**
64 * @since 1.21
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
69 * Content object.
71 protected function checkModelID( $modelId ) {
72 if ( $modelId !== $this->model_id ) {
73 throw new MWException(
74 "Bad content model: " .
75 "expected {$this->model_id} " .
76 "but got $modelId."
81 /**
82 * @since 1.21
84 * @see Content::getContentHandler
86 public function getContentHandler() {
87 return ContentHandler::getForContent( $this );
90 /**
91 * @since 1.21
93 * @see Content::getDefaultFormat
95 public function getDefaultFormat() {
96 return $this->getContentHandler()->getDefaultFormat();
99 /**
100 * @since 1.21
102 * @see Content::getSupportedFormats
104 public function getSupportedFormats() {
105 return $this->getContentHandler()->getSupportedFormats();
109 * @since 1.21
111 * @param string $format
113 * @return bool
115 * @see Content::isSupportedFormat
117 public function isSupportedFormat( $format ) {
118 if ( !$format ) {
119 return true; // this means "use the default"
122 return $this->getContentHandler()->isSupportedFormat( $format );
126 * @since 1.21
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 " .
136 $this->getModel()
142 * @since 1.21
144 * @param string $format
146 * @return string
148 * @see Content::serialize
150 public function serialize( $format = null ) {
151 return $this->getContentHandler()->serializeContent( $this, $format );
155 * @since 1.21
157 * @return bool
159 * @see Content::isEmpty
161 public function isEmpty() {
162 return $this->getSize() === 0;
166 * Subclasses may override this to implement (light weight) validation.
168 * @since 1.21
170 * @return bool Always true.
172 * @see Content::isValid
174 public function isValid() {
175 return true;
179 * @since 1.21
181 * @param Content $that
183 * @return bool
185 * @see Content::equals
187 public function equals( Content $that = null ) {
188 if ( is_null( $that ) ) {
189 return false;
192 if ( $that === $this ) {
193 return true;
196 if ( $that->getModel() !== $this->getModel() ) {
197 return false;
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 calls
208 * $this->getParserOutput( $content, $title, null, null, false ),
209 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
210 * resulting ParserOutput object.
212 * Subclasses may override this to determine the secondary data updates more
213 * efficiently, preferably without the need to generate a parser output object.
215 * @since 1.21
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 ) {
228 if ( $parserOutput === null ) {
229 $parserOutput = $this->getParserOutput( $title, null, null, false );
232 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
236 * @since 1.21
238 * @return Title[]|null
240 * @see Content::getRedirectChain
242 public function getRedirectChain() {
243 global $wgMaxRedirects;
244 $title = $this->getRedirectTarget();
245 if ( is_null( $title ) ) {
246 return null;
248 // recursive check to follow double redirects
249 $recurse = $wgMaxRedirects;
250 $titles = array( $title );
251 while ( --$recurse > 0 ) {
252 if ( $title->isRedirect() ) {
253 $page = WikiPage::factory( $title );
254 $newtitle = $page->getRedirectTarget();
255 } else {
256 break;
258 // Redirects to some special pages are not permitted
259 if ( $newtitle instanceof Title && $newtitle->isValidRedirectTarget() ) {
260 // The new title passes the checks, so make that our current
261 // title so that further recursion can be checked
262 $title = $newtitle;
263 $titles[] = $newtitle;
264 } else {
265 break;
269 return $titles;
273 * Subclasses that implement redirects should override this.
275 * @since 1.21
277 * @return null
279 * @see Content::getRedirectTarget
281 public function getRedirectTarget() {
282 return null;
286 * @note Migrated here from Title::newFromRedirectRecurse.
288 * @since 1.21
290 * @return Title|null
292 * @see Content::getUltimateRedirectTarget
294 public function getUltimateRedirectTarget() {
295 $titles = $this->getRedirectChain();
297 return $titles ? array_pop( $titles ) : null;
301 * @since 1.21
303 * @return bool
305 * @see Content::isRedirect
307 public function isRedirect() {
308 return $this->getRedirectTarget() !== null;
312 * This default implementation always returns $this.
313 * Subclasses that implement redirects should override this.
315 * @since 1.21
317 * @param Title $target
319 * @return Content $this
321 * @see Content::updateRedirect
323 public function updateRedirect( Title $target ) {
324 return $this;
328 * @since 1.21
330 * @return null
332 * @see Content::getSection
334 public function getSection( $sectionId ) {
335 return null;
339 * @since 1.21
341 * @return null
343 * @see Content::replaceSection
345 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
346 return null;
350 * @since 1.21
352 * @return Content $this
354 * @see Content::preSaveTransform
356 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
357 return $this;
361 * @since 1.21
363 * @return Content $this
365 * @see Content::addSectionHeader
367 public function addSectionHeader( $header ) {
368 return $this;
372 * @since 1.21
374 * @return Content $this
376 * @see Content::preloadTransform
378 public function preloadTransform( Title $title, ParserOptions $popts, $params = array() ) {
379 return $this;
383 * @since 1.21
385 * @return Status
387 * @see Content::prepareSave
389 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
390 if ( $this->isValid() ) {
391 return Status::newGood();
392 } else {
393 return Status::newFatal( "invalid-content-data" );
398 * @since 1.21
400 * @param WikiPage $page
401 * @param ParserOutput $parserOutput
403 * @return LinksDeletionUpdate[]
405 * @see Content::getDeletionUpdates
407 public function getDeletionUpdates( WikiPage $page, ParserOutput $parserOutput = null ) {
408 return array(
409 new LinksDeletionUpdate( $page ),
414 * This default implementation always returns false. Subclasses may override
415 * this to supply matching logic.
417 * @since 1.21
419 * @param MagicWord $word
421 * @return bool Always false.
423 * @see Content::matchMagicWord
425 public function matchMagicWord( MagicWord $word ) {
426 return false;
430 * This base implementation calls the hook ConvertContent to enable custom conversions.
431 * Subclasses may override this to implement conversion for "their" content model.
433 * @param string $toModel
434 * @param string $lossy
436 * @return Content|bool
438 * @see Content::convert()
440 public function convert( $toModel, $lossy = '' ) {
441 if ( $this->getModel() === $toModel ) {
442 //nothing to do, shorten out.
443 return $this;
446 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
447 $result = false;
449 Hooks::run( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
451 return $result;
455 * Returns a ParserOutput object containing information derived from this content.
456 * Most importantly, unless $generateHtml was false, the return value contains an
457 * HTML representation of the content.
459 * Subclasses that want to control the parser output may override this, but it is
460 * preferred to override fillParserOutput() instead.
462 * Subclasses that override getParserOutput() itself should take care to call the
463 * ContentGetParserOutput hook.
465 * @since 1.24
467 * @param Title $title Context title for parsing
468 * @param int|null $revId Revision ID (for {{REVISIONID}})
469 * @param ParserOptions|null $options Parser options
470 * @param bool $generateHtml Whether or not to generate HTML
472 * @return ParserOutput Containing information derived from this content.
474 public function getParserOutput( Title $title, $revId = null,
475 ParserOptions $options = null, $generateHtml = true
477 if ( $options === null ) {
478 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
481 $po = new ParserOutput();
483 if ( Hooks::run( 'ContentGetParserOutput',
484 array( $this, $title, $revId, $options, $generateHtml, &$po ) ) ) {
486 // Save and restore the old value, just in case something is reusing
487 // the ParserOptions object in some weird way.
488 $oldRedir = $options->getRedirectTarget();
489 $options->setRedirectTarget( $this->getRedirectTarget() );
490 $this->fillParserOutput( $title, $revId, $options, $generateHtml, $po );
491 $options->setRedirectTarget( $oldRedir );
494 Hooks::run( 'ContentAlterParserOutput', array( $this, $title, $po ) );
496 return $po;
500 * Fills the provided ParserOutput with information derived from the content.
501 * Unless $generateHtml was false, this includes an HTML representation of the content.
503 * This is called by getParserOutput() after consulting the ContentGetParserOutput hook.
504 * Subclasses are expected to override this method (or getParserOutput(), if need be).
505 * Subclasses of TextContent should generally override getHtml() instead.
507 * This placeholder implementation always throws an exception.
509 * @since 1.24
511 * @param Title $title Context title for parsing
512 * @param int|null $revId Revision ID (for {{REVISIONID}})
513 * @param ParserOptions $options Parser options
514 * @param bool $generateHtml Whether or not to generate HTML
515 * @param ParserOutput &$output The output object to fill (reference).
517 * @throws MWException
519 protected function fillParserOutput( Title $title, $revId,
520 ParserOptions $options, $generateHtml, ParserOutput &$output
522 // Don't make abstract, so subclasses that override getParserOutput() directly don't fail.
523 throw new MWException( 'Subclasses of AbstractContent must override fillParserOutput!' );