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.
8 abstract class AbstractContent
implements Content
{
11 * Name of the content model this Content object represents.
12 * Use with CONTENT_MODEL_XXX constants
14 * @var string $model_id
19 * @param String $model_id
21 public function __construct( $model_id = null ) {
22 $this->model_id
= $model_id;
26 * @see Content::getModel()
28 public function getModel() {
29 return $this->model_id
;
33 * Throws an MWException if $model_id is not the id of the content model
34 * supported by this Content object.
36 * @param $model_id int the model to check
40 protected function checkModelID( $model_id ) {
41 if ( $model_id !== $this->model_id
) {
42 throw new MWException( "Bad content model: " .
43 "expected {$this->model_id} " .
44 "but got $model_id." );
49 * @see Content::getContentHandler()
51 public function getContentHandler() {
52 return ContentHandler
::getForContent( $this );
56 * @see Content::getDefaultFormat()
58 public function getDefaultFormat() {
59 return $this->getContentHandler()->getDefaultFormat();
63 * @see Content::getSupportedFormats()
65 public function getSupportedFormats() {
66 return $this->getContentHandler()->getSupportedFormats();
70 * @see Content::isSupportedFormat()
72 public function isSupportedFormat( $format ) {
74 return true; // this means "use the default"
77 return $this->getContentHandler()->isSupportedFormat( $format );
81 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
87 protected function checkFormat( $format ) {
88 if ( !$this->isSupportedFormat( $format ) ) {
89 throw new MWException( "Format $format is not supported for content model " .
95 * @see Content::serialize
97 public function serialize( $format = null ) {
98 return $this->getContentHandler()->serializeContent( $this, $format );
102 * @see Content::isEmpty()
104 public function isEmpty() {
105 return $this->getSize() === 0;
109 * @see Content::isValid()
111 public function isValid() {
116 * @see Content::equals()
118 public function equals( Content
$that = null ) {
119 if ( is_null( $that ) ) {
123 if ( $that === $this ) {
127 if ( $that->getModel() !== $this->getModel() ) {
131 return $this->getNativeData() === $that->getNativeData();
136 * Returns a list of DataUpdate objects for recording information about this
137 * Content in some secondary data store.
139 * This default implementation calls
140 * $this->getParserOutput( $content, $title, null, null, false ),
141 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
142 * resulting ParserOutput object.
144 * Subclasses may override this to determine the secondary data updates more
145 * efficiently, preferrably without the need to generate a parser output object.
147 * @see Content::getSecondaryDataUpdates()
149 * @param $title Title The context for determining the necessary updates
150 * @param $old Content|null An optional Content object representing the
151 * previous content, i.e. the content being replaced by this Content
153 * @param $recursive boolean Whether to include recursive updates (default:
155 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
156 * Provide if you have one handy, to avoid re-parsing of the content.
158 * @return Array. A list of DataUpdate objects for putting information
159 * about this content object somewhere.
163 public function getSecondaryDataUpdates( Title
$title,
165 $recursive = true, ParserOutput
$parserOutput = null
167 if ( !$parserOutput ) {
168 $parserOutput = $this->getParserOutput( $title, null, null, false );
171 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
176 * @see Content::getRedirectChain()
178 public function getRedirectChain() {
179 global $wgMaxRedirects;
180 $title = $this->getRedirectTarget();
181 if ( is_null( $title ) ) {
184 // recursive check to follow double redirects
185 $recurse = $wgMaxRedirects;
186 $titles = array( $title );
187 while ( --$recurse > 0 ) {
188 if ( $title->isRedirect() ) {
189 $page = WikiPage
::factory( $title );
190 $newtitle = $page->getRedirectTarget();
194 // Redirects to some special pages are not permitted
195 if ( $newtitle instanceOf Title
&& $newtitle->isValidRedirectTarget() ) {
196 // The new title passes the checks, so make that our current
197 // title so that further recursion can be checked
199 $titles[] = $newtitle;
208 * @see Content::getRedirectTarget()
210 public function getRedirectTarget() {
215 * @see Content::getUltimateRedirectTarget()
216 * @note: migrated here from Title::newFromRedirectRecurse
218 public function getUltimateRedirectTarget() {
219 $titles = $this->getRedirectChain();
220 return $titles ?
array_pop( $titles ) : null;
224 * @see Content::isRedirect()
230 public function isRedirect() {
231 return $this->getRedirectTarget() !== null;
235 * @see Content::updateRedirect()
237 * This default implementation always returns $this.
241 * @return Content $this
243 public function updateRedirect( Title
$target ) {
248 * @see Content::getSection()
250 public function getSection( $sectionId ) {
255 * @see Content::replaceSection()
257 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
262 * @see Content::preSaveTransform()
264 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
269 * @see Content::addSectionHeader()
271 public function addSectionHeader( $header ) {
276 * @see Content::preloadTransform()
278 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
283 * @see Content::prepareSave()
285 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
286 if ( $this->isValid() ) {
287 return Status
::newGood();
289 return Status
::newFatal( "invalid-content-data" );
294 * @see Content::getDeletionUpdates()
298 * @param $page \WikiPage the deleted page
299 * @param $parserOutput null|\ParserOutput optional parser output object
300 * for efficient access to meta-information about the content object.
301 * Provide if you have one handy.
303 * @return array A list of DataUpdate instances that will clean up the
304 * database after deletion.
306 public function getDeletionUpdates( WikiPage
$page,
307 ParserOutput
$parserOutput = null )
310 new LinksDeletionUpdate( $page ),
315 * @see Content::matchMagicWord()
317 * This default implementation always returns false. Subclasses may override this to supply matching logic.
319 * @param MagicWord $word
323 public function matchMagicWord( MagicWord
$word ) {