e14fb56f7d0deca59d8700801815f067abbb8794
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();
208 * Returns a list of DataUpdate objects for recording information about this
209 * Content in some secondary data store.
211 * This default implementation calls
212 * $this->getParserOutput( $content, $title, null, null, false ),
213 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
214 * resulting ParserOutput object.
216 * Subclasses may override this to determine the secondary data updates more
217 * efficiently, preferably without the need to generate a parser output object.
219 * @see Content::getSecondaryDataUpdates()
221 * @param $title Title The context for determining the necessary updates
222 * @param $old Content|null An optional Content object representing the
223 * previous content, i.e. the content being replaced by this Content
225 * @param $recursive boolean Whether to include recursive updates (default:
227 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
228 * Provide if you have one handy, to avoid re-parsing of the content.
230 * @return Array. A list of DataUpdate objects for putting information
231 * about this content object somewhere.
235 public function getSecondaryDataUpdates( Title
$title,
237 $recursive = true, ParserOutput
$parserOutput = null
239 if ( $parserOutput === null ) {
240 $parserOutput = $this->getParserOutput( $title, null, null, false );
243 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
248 * @see Content::getRedirectChain
252 public function getRedirectChain() {
253 global $wgMaxRedirects;
254 $title = $this->getRedirectTarget();
255 if ( is_null( $title ) ) {
258 // recursive check to follow double redirects
259 $recurse = $wgMaxRedirects;
260 $titles = array( $title );
261 while ( --$recurse > 0 ) {
262 if ( $title->isRedirect() ) {
263 $page = WikiPage
::factory( $title );
264 $newtitle = $page->getRedirectTarget();
268 // Redirects to some special pages are not permitted
269 if ( $newtitle instanceOf Title
&& $newtitle->isValidRedirectTarget() ) {
270 // The new title passes the checks, so make that our current
271 // title so that further recursion can be checked
273 $titles[] = $newtitle;
282 * @see Content::getRedirectTarget
286 public function getRedirectTarget() {
291 * @see Content::getUltimateRedirectTarget
292 * @note: migrated here from Title::newFromRedirectRecurse
296 public function getUltimateRedirectTarget() {
297 $titles = $this->getRedirectChain();
298 return $titles ?
array_pop( $titles ) : null;
302 * @see Content::isRedirect
308 public function isRedirect() {
309 return $this->getRedirectTarget() !== null;
313 * @see Content::updateRedirect
315 * This default implementation always returns $this.
317 * @param Title $target
321 * @return Content $this
323 public function updateRedirect( Title
$target ) {
328 * @see Content::getSection
332 public function getSection( $sectionId ) {
337 * @see Content::replaceSection
341 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
346 * @see Content::preSaveTransform
350 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
355 * @see Content::addSectionHeader
359 public function addSectionHeader( $header ) {
364 * @see Content::preloadTransform
368 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
373 * @see Content::prepareSave
377 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
378 if ( $this->isValid() ) {
379 return Status
::newGood();
381 return Status
::newFatal( "invalid-content-data" );
386 * @see Content::getDeletionUpdates
390 * @param $page WikiPage the deleted page
391 * @param $parserOutput null|ParserOutput optional parser output object
392 * for efficient access to meta-information about the content object.
393 * Provide if you have one handy.
395 * @return array A list of DataUpdate instances that will clean up the
396 * database after deletion.
398 public function getDeletionUpdates( WikiPage
$page,
399 ParserOutput
$parserOutput = null )
402 new LinksDeletionUpdate( $page ),
407 * This default implementation always returns false. Subclasses may override 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 ) );