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
{
37 * Name of the content model this Content object represents.
38 * Use with CONTENT_MODEL_XXX constants
42 * @var string $model_id
47 * @param string|null $modelId
51 public function __construct( $modelId = null ) {
52 $this->model_id
= $modelId;
56 * @see Content::getModel
60 public function getModel() {
61 return $this->model_id
;
65 * Throws an MWException if $model_id is not the id of the content model
66 * supported by this Content object.
70 * @param string $modelId The model to check
74 protected function checkModelID( $modelId ) {
75 if ( $modelId !== $this->model_id
) {
76 throw new MWException(
77 "Bad content model: " .
78 "expected {$this->model_id} " .
85 * @see Content::getContentHandler
89 public function getContentHandler() {
90 return ContentHandler
::getForContent( $this );
94 * @see Content::getDefaultFormat
98 public function getDefaultFormat() {
99 return $this->getContentHandler()->getDefaultFormat();
103 * @see Content::getSupportedFormats
107 public function getSupportedFormats() {
108 return $this->getContentHandler()->getSupportedFormats();
112 * @see Content::isSupportedFormat
114 * @param string $format
120 public function isSupportedFormat( $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
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 " .
147 * @see Content::serialize
149 * @param string|null $format
155 public function serialize( $format = null ) {
156 return $this->getContentHandler()->serializeContent( $this, $format );
160 * @see Content::isEmpty
166 public function isEmpty() {
167 return $this->getSize() === 0;
171 * @see Content::isValid
177 public function isValid() {
182 * @see Content::equals
186 * @param Content|null $that
190 public function equals( Content
$that = null ) {
191 if ( is_null( $that ) ) {
195 if ( $that === $this ) {
199 if ( $that->getModel() !== $this->getModel() ) {
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
224 * @param $recursive boolean Whether to include recursive updates (default:
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.
234 public function getSecondaryDataUpdates( Title
$title,
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
250 public function getRedirectChain() {
251 global $wgMaxRedirects;
252 $title = $this->getRedirectTarget();
253 if ( is_null( $title ) ) {
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();
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
271 $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();
296 return $titles ?
array_pop( $titles ) : null;
300 * @see Content::isRedirect
306 public function isRedirect() {
307 return $this->getRedirectTarget() !== null;
311 * @see Content::updateRedirect
313 * This default implementation always returns $this.
315 * @param Title $target
319 * @return Content $this
321 public function updateRedirect( Title
$target ) {
326 * @see Content::getSection
330 public function getSection( $sectionId ) {
335 * @see Content::replaceSection
339 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
344 * @see Content::preSaveTransform
348 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
353 * @see Content::addSectionHeader
357 public function addSectionHeader( $header ) {
362 * @see Content::preloadTransform
366 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
371 * @see Content::prepareSave
375 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
376 if ( $this->isValid() ) {
377 return Status
::newGood();
379 return Status
::newFatal( "invalid-content-data" );
384 * @see Content::getDeletionUpdates
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 )
400 new LinksDeletionUpdate( $page ),
405 * This default implementation always returns false. Subclasses may override this to supply matching logic.
407 * @see Content::matchMagicWord
411 * @param MagicWord $word
415 public function matchMagicWord( MagicWord
$word ) {
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.
438 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
441 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );