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|null $modelId
50 public function __construct( $modelId = null ) {
51 $this->model_id
= $modelId;
55 * @see Content::getModel
59 public function getModel() {
60 return $this->model_id
;
64 * Throws an MWException if $model_id is not the id of the content model
65 * supported by this Content object.
69 * @param string $modelId The model to check
73 protected function checkModelID( $modelId ) {
74 if ( $modelId !== $this->model_id
) {
75 throw new MWException(
76 "Bad content model: " .
77 "expected {$this->model_id} " .
84 * @see Content::getContentHandler
88 public function getContentHandler() {
89 return ContentHandler
::getForContent( $this );
93 * @see Content::getDefaultFormat
97 public function getDefaultFormat() {
98 return $this->getContentHandler()->getDefaultFormat();
102 * @see Content::getSupportedFormats
106 public function getSupportedFormats() {
107 return $this->getContentHandler()->getSupportedFormats();
111 * @see Content::isSupportedFormat
113 * @param string $format
119 public function isSupportedFormat( $format ) {
121 return true; // this means "use the default"
124 return $this->getContentHandler()->isSupportedFormat( $format );
128 * Throws an MWException if $this->isSupportedFormat( $format ) does not
133 * @param string $format
134 * @throws MWException
136 protected function checkFormat( $format ) {
137 if ( !$this->isSupportedFormat( $format ) ) {
138 throw new MWException(
139 "Format $format is not supported for content model " .
146 * @see Content::serialize
148 * @param string|null $format
154 public function serialize( $format = null ) {
155 return $this->getContentHandler()->serializeContent( $this, $format );
159 * @see Content::isEmpty
165 public function isEmpty() {
166 return $this->getSize() === 0;
170 * @see Content::isValid
176 public function isValid() {
181 * @see Content::equals
185 * @param Content|null $that
189 public function equals( Content
$that = null ) {
190 if ( is_null( $that ) ) {
194 if ( $that === $this ) {
198 if ( $that->getModel() !== $this->getModel() ) {
202 return $this->getNativeData() === $that->getNativeData();
206 * Returns a list of DataUpdate objects for recording information about this
207 * Content in some secondary data store.
209 * This default implementation calls
210 * $this->getParserOutput( $content, $title, null, null, false ),
211 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
212 * resulting ParserOutput object.
214 * Subclasses may override this to determine the secondary data updates more
215 * efficiently, preferably without the need to generate a parser output object.
217 * @see Content::getSecondaryDataUpdates()
219 * @param $title Title The context for determining the necessary updates
220 * @param $old Content|null An optional Content object representing the
221 * previous content, i.e. the content being replaced by this Content
223 * @param $recursive boolean Whether to include recursive updates (default:
225 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
226 * Provide if you have one handy, to avoid re-parsing of the content.
228 * @return Array. A list of DataUpdate objects for putting information
229 * about this content object somewhere.
233 public function getSecondaryDataUpdates( Title
$title,
235 $recursive = true, ParserOutput
$parserOutput = null
237 if ( $parserOutput === null ) {
238 $parserOutput = $this->getParserOutput( $title, null, null, false );
241 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
245 * @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 = array( $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 * @see Content::getRedirectTarget
284 public function getRedirectTarget() {
289 * @see Content::getUltimateRedirectTarget
290 * @note: migrated here from Title::newFromRedirectRecurse
294 public function getUltimateRedirectTarget() {
295 $titles = $this->getRedirectChain();
297 return $titles ?
array_pop( $titles ) : null;
301 * @see Content::isRedirect
307 public function isRedirect() {
308 return $this->getRedirectTarget() !== null;
312 * @see Content::updateRedirect
314 * This default implementation always returns $this.
316 * @param Title $target
320 * @return Content $this
322 public function updateRedirect( Title
$target ) {
327 * @see Content::getSection
331 public function getSection( $sectionId ) {
336 * @see Content::replaceSection
340 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
345 * @see Content::preSaveTransform
349 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
354 * @see Content::addSectionHeader
358 public function addSectionHeader( $header ) {
363 * @see Content::preloadTransform
367 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
372 * @see Content::prepareSave
376 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
377 if ( $this->isValid() ) {
378 return Status
::newGood();
380 return Status
::newFatal( "invalid-content-data" );
385 * @see Content::getDeletionUpdates
389 * @param $page WikiPage the deleted page
390 * @param $parserOutput null|ParserOutput optional parser output object
391 * for efficient access to meta-information about the content object.
392 * Provide if you have one handy.
394 * @return array A list of DataUpdate instances that will clean up the
395 * database after deletion.
397 public function getDeletionUpdates( WikiPage
$page,
398 ParserOutput
$parserOutput = null
401 new LinksDeletionUpdate( $page ),
406 * This default implementation always returns false. Subclasses may override
407 * this to supply matching logic.
409 * @see Content::matchMagicWord
413 * @param MagicWord $word
417 public function matchMagicWord( MagicWord
$word ) {
422 * @see Content::convert()
424 * This base implementation calls the hook ConvertContent to enable custom conversions.
425 * Subclasses may override this to implement conversion for "their" content model.
427 * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
428 * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
429 * not allowed, full round-trip conversion is expected to work without losing information.
431 * @return Content|bool A content object with the content model $toModel, or false if
432 * that conversion is not supported.
434 public function convert( $toModel, $lossy = '' ) {
435 if ( $this->getModel() === $toModel ) {
436 //nothing to do, shorten out.
440 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
443 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );