Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / content / AbstractContent.php
blob137efb8a49da5e39e9bfbaf459056abbc02e004d
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 {
36 /**
37 * Name of the content model this Content object represents.
38 * Use with CONTENT_MODEL_XXX constants
40 * @since 1.21
42 * @var string $model_id
44 protected $model_id;
46 /**
47 * @param string|null $modelId
49 * @since 1.21
51 public function __construct( $modelId = null ) {
52 $this->model_id = $modelId;
55 /**
56 * @see Content::getModel
58 * @since 1.21
60 public function getModel() {
61 return $this->model_id;
64 /**
65 * Throws an MWException if $model_id is not the id of the content model
66 * supported by this Content object.
68 * @since 1.21
70 * @param string $modelId The model to check
72 * @throws MWException
74 protected function checkModelID( $modelId ) {
75 if ( $modelId !== $this->model_id ) {
76 throw new MWException(
77 "Bad content model: " .
78 "expected {$this->model_id} " .
79 "but got $modelId."
84 /**
85 * @see Content::getContentHandler
87 * @since 1.21
89 public function getContentHandler() {
90 return ContentHandler::getForContent( $this );
93 /**
94 * @see Content::getDefaultFormat
96 * @since 1.21
98 public function getDefaultFormat() {
99 return $this->getContentHandler()->getDefaultFormat();
103 * @see Content::getSupportedFormats
105 * @since 1.21
107 public function getSupportedFormats() {
108 return $this->getContentHandler()->getSupportedFormats();
112 * @see Content::isSupportedFormat
114 * @param string $format
116 * @since 1.21
118 * @return boolean
120 public function isSupportedFormat( $format ) {
121 if ( !$format ) {
122 return true; // this means "use the default"
125 return $this->getContentHandler()->isSupportedFormat( $format );
129 * Throws an MWException if $this->isSupportedFormat( $format ) does not
130 * return true.
132 * @since 1.21
134 * @param string $format
135 * @throws MWException
137 protected function checkFormat( $format ) {
138 if ( !$this->isSupportedFormat( $format ) ) {
139 throw new MWException(
140 "Format $format is not supported for content model " .
141 $this->getModel()
147 * @see Content::serialize
149 * @param string|null $format
151 * @since 1.21
153 * @return string
155 public function serialize( $format = null ) {
156 return $this->getContentHandler()->serializeContent( $this, $format );
160 * @see Content::isEmpty
162 * @since 1.21
164 * @return boolean
166 public function isEmpty() {
167 return $this->getSize() === 0;
171 * @see Content::isValid
173 * @since 1.21
175 * @return boolean
177 public function isValid() {
178 return true;
182 * @see Content::equals
184 * @since 1.21
186 * @param Content|null $that
188 * @return boolean
190 public function equals( Content $that = null ) {
191 if ( is_null( $that ) ) {
192 return false;
195 if ( $that === $this ) {
196 return true;
199 if ( $that->getModel() !== $this->getModel() ) {
200 return false;
203 return $this->getNativeData() === $that->getNativeData();
207 * Returns a list of DataUpdate objects for recording information about this
208 * Content in some secondary data store.
210 * This default implementation calls
211 * $this->getParserOutput( $content, $title, null, null, false ),
212 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
213 * resulting ParserOutput object.
215 * Subclasses may override this to determine the secondary data updates more
216 * efficiently, preferably without the need to generate a parser output object.
218 * @see Content::getSecondaryDataUpdates()
220 * @param $title Title The context for determining the necessary updates
221 * @param $old Content|null An optional Content object representing the
222 * previous content, i.e. the content being replaced by this Content
223 * object.
224 * @param $recursive boolean Whether to include recursive updates (default:
225 * false).
226 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
227 * Provide if you have one handy, to avoid re-parsing of the content.
229 * @return Array. A list of DataUpdate objects for putting information
230 * about this content object somewhere.
232 * @since 1.21
234 public function getSecondaryDataUpdates( Title $title,
235 Content $old = null,
236 $recursive = true, ParserOutput $parserOutput = null
238 if ( $parserOutput === null ) {
239 $parserOutput = $this->getParserOutput( $title, null, null, false );
242 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
246 * @see Content::getRedirectChain
248 * @since 1.21
250 public function getRedirectChain() {
251 global $wgMaxRedirects;
252 $title = $this->getRedirectTarget();
253 if ( is_null( $title ) ) {
254 return null;
256 // recursive check to follow double redirects
257 $recurse = $wgMaxRedirects;
258 $titles = array( $title );
259 while ( --$recurse > 0 ) {
260 if ( $title->isRedirect() ) {
261 $page = WikiPage::factory( $title );
262 $newtitle = $page->getRedirectTarget();
263 } else {
264 break;
266 // Redirects to some special pages are not permitted
267 if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
268 // The new title passes the checks, so make that our current
269 // title so that further recursion can be checked
270 $title = $newtitle;
271 $titles[] = $newtitle;
272 } else {
273 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();
296 return $titles ? array_pop( $titles ) : null;
300 * @see Content::isRedirect
302 * @since 1.21
304 * @return bool
306 public function isRedirect() {
307 return $this->getRedirectTarget() !== null;
311 * @see Content::updateRedirect
313 * This default implementation always returns $this.
315 * @param Title $target
317 * @since 1.21
319 * @return Content $this
321 public function updateRedirect( Title $target ) {
322 return $this;
326 * @see Content::getSection
328 * @since 1.21
330 public function getSection( $sectionId ) {
331 return null;
335 * @see Content::replaceSection
337 * @since 1.21
339 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
340 return null;
344 * @see Content::preSaveTransform
346 * @since 1.21
348 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
349 return $this;
353 * @see Content::addSectionHeader
355 * @since 1.21
357 public function addSectionHeader( $header ) {
358 return $this;
362 * @see Content::preloadTransform
364 * @since 1.21
366 public function preloadTransform( Title $title, ParserOptions $popts ) {
367 return $this;
371 * @see Content::prepareSave
373 * @since 1.21
375 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
376 if ( $this->isValid() ) {
377 return Status::newGood();
378 } else {
379 return Status::newFatal( "invalid-content-data" );
384 * @see Content::getDeletionUpdates
386 * @since 1.21
388 * @param $page WikiPage the deleted page
389 * @param $parserOutput null|ParserOutput optional parser output object
390 * for efficient access to meta-information about the content object.
391 * Provide if you have one handy.
393 * @return array A list of DataUpdate instances that will clean up the
394 * database after deletion.
396 public function getDeletionUpdates( WikiPage $page,
397 ParserOutput $parserOutput = null )
399 return array(
400 new LinksDeletionUpdate( $page ),
405 * This default implementation always returns false. Subclasses may override this to supply matching logic.
407 * @see Content::matchMagicWord
409 * @since 1.21
411 * @param MagicWord $word
413 * @return bool
415 public function matchMagicWord( MagicWord $word ) {
416 return false;
420 * @see Content::convert()
422 * This base implementation calls the hook ConvertContent to enable custom conversions.
423 * Subclasses may override this to implement conversion for "their" content model.
425 * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
426 * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
427 * not allowed, full round-trip conversion is expected to work without losing information.
429 * @return Content|bool A content object with the content model $toModel, or false if
430 * that conversion is not supported.
432 public function convert( $toModel, $lossy = '' ) {
433 if ( $this->getModel() === $toModel ) {
434 //nothing to do, shorten out.
435 return $this;
438 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
439 $result = false;
441 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
442 return $result;