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
28 abstract class AbstractContent
implements Content
{
31 * Name of the content model this Content object represents.
32 * Use with CONTENT_MODEL_XXX constants
36 * @var string $model_id
41 * @param string|null $modelId
45 public function __construct( $modelId = null ) {
46 $this->model_id
= $modelId;
50 * @see Content::getModel
54 public function getModel() {
55 return $this->model_id
;
59 * Throws an MWException if $model_id is not the id of the content model
60 * supported by this Content object.
64 * @param string $modelId The model to check
68 protected function checkModelID( $modelId ) {
69 if ( $modelId !== $this->model_id
) {
70 throw new MWException(
71 "Bad content model: " .
72 "expected {$this->model_id} " .
79 * @see Content::getContentHandler
83 public function getContentHandler() {
84 return ContentHandler
::getForContent( $this );
88 * @see Content::getDefaultFormat
92 public function getDefaultFormat() {
93 return $this->getContentHandler()->getDefaultFormat();
97 * @see Content::getSupportedFormats
101 public function getSupportedFormats() {
102 return $this->getContentHandler()->getSupportedFormats();
106 * @see Content::isSupportedFormat
108 * @param string $format
114 public function isSupportedFormat( $format ) {
116 return true; // this means "use the default"
119 return $this->getContentHandler()->isSupportedFormat( $format );
123 * Throws an MWException if $this->isSupportedFormat( $format ) does not
128 * @param string $format
129 * @throws MWException
131 protected function checkFormat( $format ) {
132 if ( !$this->isSupportedFormat( $format ) ) {
133 throw new MWException(
134 "Format $format is not supported for content model " .
141 * @see Content::serialize
143 * @param string|null $format
149 public function serialize( $format = null ) {
150 return $this->getContentHandler()->serializeContent( $this, $format );
154 * @see Content::isEmpty
160 public function isEmpty() {
161 return $this->getSize() === 0;
165 * @see Content::isValid
171 public function isValid() {
176 * @see Content::equals
180 * @param Content|null $that
184 public function equals( Content
$that = null ) {
185 if ( is_null( $that ) ) {
189 if ( $that === $this ) {
193 if ( $that->getModel() !== $this->getModel() ) {
197 return $this->getNativeData() === $that->getNativeData();
202 * Returns a list of DataUpdate objects for recording information about this
203 * Content in some secondary data store.
205 * This default implementation calls
206 * $this->getParserOutput( $content, $title, null, null, false ),
207 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
208 * resulting ParserOutput object.
210 * Subclasses may override this to determine the secondary data updates more
211 * efficiently, preferrably without the need to generate a parser output object.
213 * @see Content::getSecondaryDataUpdates()
215 * @param $title Title The context for determining the necessary updates
216 * @param $old Content|null An optional Content object representing the
217 * previous content, i.e. the content being replaced by this Content
219 * @param $recursive boolean Whether to include recursive updates (default:
221 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
222 * Provide if you have one handy, to avoid re-parsing of the content.
224 * @return Array. A list of DataUpdate objects for putting information
225 * about this content object somewhere.
229 public function getSecondaryDataUpdates( Title
$title,
231 $recursive = true, ParserOutput
$parserOutput = null
233 if ( !$parserOutput ) {
234 $parserOutput = $this->getParserOutput( $title, null, null, false );
237 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
242 * @see Content::getRedirectChain
246 public function getRedirectChain() {
247 global $wgMaxRedirects;
248 $title = $this->getRedirectTarget();
249 if ( is_null( $title ) ) {
252 // recursive check to follow double redirects
253 $recurse = $wgMaxRedirects;
254 $titles = array( $title );
255 while ( --$recurse > 0 ) {
256 if ( $title->isRedirect() ) {
257 $page = WikiPage
::factory( $title );
258 $newtitle = $page->getRedirectTarget();
262 // Redirects to some special pages are not permitted
263 if ( $newtitle instanceOf Title
&& $newtitle->isValidRedirectTarget() ) {
264 // The new title passes the checks, so make that our current
265 // title so that further recursion can be checked
267 $titles[] = $newtitle;
276 * @see Content::getRedirectTarget
280 public function getRedirectTarget() {
285 * @see Content::getUltimateRedirectTarget
286 * @note: migrated here from Title::newFromRedirectRecurse
290 public function getUltimateRedirectTarget() {
291 $titles = $this->getRedirectChain();
292 return $titles ?
array_pop( $titles ) : null;
296 * @see Content::isRedirect
302 public function isRedirect() {
303 return $this->getRedirectTarget() !== null;
307 * @see Content::updateRedirect
309 * This default implementation always returns $this.
311 * @param Title $target
315 * @return Content $this
317 public function updateRedirect( Title
$target ) {
322 * @see Content::getSection
326 public function getSection( $sectionId ) {
331 * @see Content::replaceSection
335 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
340 * @see Content::preSaveTransform
344 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
349 * @see Content::addSectionHeader
353 public function addSectionHeader( $header ) {
358 * @see Content::preloadTransform
362 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
367 * @see Content::prepareSave
371 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
372 if ( $this->isValid() ) {
373 return Status
::newGood();
375 return Status
::newFatal( "invalid-content-data" );
380 * @see Content::getDeletionUpdates
384 * @param $page \WikiPage the deleted page
385 * @param $parserOutput null|\ParserOutput optional parser output object
386 * for efficient access to meta-information about the content object.
387 * Provide if you have one handy.
389 * @return array A list of DataUpdate instances that will clean up the
390 * database after deletion.
392 public function getDeletionUpdates( WikiPage
$page,
393 ParserOutput
$parserOutput = null )
396 new LinksDeletionUpdate( $page ),
401 * This default implementation always returns false. Subclasses may override this to supply matching logic.
403 * @see Content::matchMagicWord
407 * @param MagicWord $word
411 public function matchMagicWord( MagicWord
$word ) {