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 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.
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 );
238 * @return Title[]|null
240 * @see Content::getRedirectChain
242 public function getRedirectChain() {
243 global $wgMaxRedirects;
244 $title = $this->getRedirectTarget();
245 if ( is_null( $title ) ) {
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();
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
263 $titles[] = $newtitle;
273 * Subclasses that implement redirects should override this.
279 * @see Content::getRedirectTarget
281 public function getRedirectTarget() {
286 * @note Migrated here from Title::newFromRedirectRecurse.
292 * @see Content::getUltimateRedirectTarget
294 public function getUltimateRedirectTarget() {
295 $titles = $this->getRedirectChain();
297 return $titles ?
array_pop( $titles ) : null;
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.
317 * @param Title $target
319 * @return Content $this
321 * @see Content::updateRedirect
323 public function updateRedirect( Title
$target ) {
332 * @see Content::getSection
334 public function getSection( $sectionId ) {
343 * @see Content::replaceSection
345 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
352 * @return Content $this
354 * @see Content::preSaveTransform
356 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
363 * @return Content $this
365 * @see Content::addSectionHeader
367 public function addSectionHeader( $header ) {
374 * @return Content $this
376 * @see Content::preloadTransform
378 public function preloadTransform( Title
$title, ParserOptions
$popts, $params = array() ) {
387 * @see Content::prepareSave
389 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
390 if ( $this->isValid() ) {
391 return Status
::newGood();
393 return Status
::newFatal( "invalid-content-data" );
400 * @param WikiPage $page
401 * @param ParserOutput $parserOutput
403 * @return LinksDeletionUpdate[]
405 * @see Content::getDeletionUpdates
407 public function getDeletionUpdates( WikiPage
$page, ParserOutput
$parserOutput = null ) {
409 new LinksDeletionUpdate( $page ),
414 * This default implementation always returns false. Subclasses may override
415 * this to supply matching logic.
419 * @param MagicWord $word
421 * @return bool Always false.
423 * @see Content::matchMagicWord
425 public function matchMagicWord( MagicWord
$word ) {
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.
446 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
449 Hooks
::run( 'ConvertContent', array( $this, $toModel, $lossy, &$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.
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 ) );
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.
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!' );