Release notes for Iabf4873f
[mediawiki.git] / includes / content / AbstractContent.php
blobe1b1f019268ca26222b14478501ab970d01d1935
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|null $modelId
48 * @since 1.21
50 public function __construct( $modelId = null ) {
51 $this->model_id = $modelId;
54 /**
55 * @see Content::getModel
57 * @since 1.21
59 public function getModel() {
60 return $this->model_id;
63 /**
64 * Throws an MWException if $model_id is not the id of the content model
65 * supported by this Content object.
67 * @since 1.21
69 * @param string $modelId The model to check
71 * @throws MWException
73 protected function checkModelID( $modelId ) {
74 if ( $modelId !== $this->model_id ) {
75 throw new MWException(
76 "Bad content model: " .
77 "expected {$this->model_id} " .
78 "but got $modelId."
83 /**
84 * @see Content::getContentHandler
86 * @since 1.21
88 public function getContentHandler() {
89 return ContentHandler::getForContent( $this );
92 /**
93 * @see Content::getDefaultFormat
95 * @since 1.21
97 public function getDefaultFormat() {
98 return $this->getContentHandler()->getDefaultFormat();
102 * @see Content::getSupportedFormats
104 * @since 1.21
106 public function getSupportedFormats() {
107 return $this->getContentHandler()->getSupportedFormats();
111 * @see Content::isSupportedFormat
113 * @param string $format
115 * @since 1.21
117 * @return boolean
119 public function isSupportedFormat( $format ) {
120 if ( !$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
129 * return true.
131 * @since 1.21
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 " .
140 $this->getModel()
146 * @see Content::serialize
148 * @param string|null $format
150 * @since 1.21
152 * @return string
154 public function serialize( $format = null ) {
155 return $this->getContentHandler()->serializeContent( $this, $format );
159 * @see Content::isEmpty
161 * @since 1.21
163 * @return boolean
165 public function isEmpty() {
166 return $this->getSize() === 0;
170 * @see Content::isValid
172 * @since 1.21
174 * @return boolean
176 public function isValid() {
177 return true;
181 * @see Content::equals
183 * @since 1.21
185 * @param Content|null $that
187 * @return boolean
189 public function equals( Content $that = null ) {
190 if ( is_null( $that ) ) {
191 return false;
194 if ( $that === $this ) {
195 return true;
198 if ( $that->getModel() !== $this->getModel() ) {
199 return false;
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
222 * object.
223 * @param $recursive boolean Whether to include recursive updates (default:
224 * false).
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.
231 * @since 1.21
233 public function getSecondaryDataUpdates( Title $title,
234 Content $old = null,
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
247 * @since 1.21
249 public function getRedirectChain() {
250 global $wgMaxRedirects;
251 $title = $this->getRedirectTarget();
252 if ( is_null( $title ) ) {
253 return null;
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();
262 } else {
263 break;
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
269 $title = $newtitle;
270 $titles[] = $newtitle;
271 } else {
272 break;
276 return $titles;
280 * @see Content::getRedirectTarget
282 * @since 1.21
284 public function getRedirectTarget() {
285 return null;
289 * @see Content::getUltimateRedirectTarget
290 * @note: migrated here from Title::newFromRedirectRecurse
292 * @since 1.21
294 public function getUltimateRedirectTarget() {
295 $titles = $this->getRedirectChain();
297 return $titles ? array_pop( $titles ) : null;
301 * @see Content::isRedirect
303 * @since 1.21
305 * @return bool
307 public function isRedirect() {
308 return $this->getRedirectTarget() !== null;
312 * @see Content::updateRedirect
314 * This default implementation always returns $this.
316 * @param Title $target
318 * @since 1.21
320 * @return Content $this
322 public function updateRedirect( Title $target ) {
323 return $this;
327 * @see Content::getSection
329 * @since 1.21
331 public function getSection( $sectionId ) {
332 return null;
336 * @see Content::replaceSection
338 * @since 1.21
340 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
341 return null;
345 * @see Content::preSaveTransform
347 * @since 1.21
349 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
350 return $this;
354 * @see Content::addSectionHeader
356 * @since 1.21
358 public function addSectionHeader( $header ) {
359 return $this;
363 * @see Content::preloadTransform
365 * @since 1.21
367 public function preloadTransform( Title $title, ParserOptions $popts ) {
368 return $this;
372 * @see Content::prepareSave
374 * @since 1.21
376 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
377 if ( $this->isValid() ) {
378 return Status::newGood();
379 } else {
380 return Status::newFatal( "invalid-content-data" );
385 * @see Content::getDeletionUpdates
387 * @since 1.21
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
400 return array(
401 new LinksDeletionUpdate( $page ),
406 * This default implementation always returns false. Subclasses may override
407 * this to supply matching logic.
409 * @see Content::matchMagicWord
411 * @since 1.21
413 * @param MagicWord $word
415 * @return bool
417 public function matchMagicWord( MagicWord $word ) {
418 return false;
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.
437 return $this;
440 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
441 $result = false;
443 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
445 return $result;