1a730bc47ed516487a88783785cdf4bafe2e78ac
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
34 * @var string $model_id
39 * @param String $model_id
41 public function __construct( $model_id = null ) {
42 $this->model_id
= $model_id;
46 * @see Content::getModel()
48 public function getModel() {
49 return $this->model_id
;
53 * Throws an MWException if $model_id is not the id of the content model
54 * supported by this Content object.
56 * @param $model_id int the model to check
60 protected function checkModelID( $model_id ) {
61 if ( $model_id !== $this->model_id
) {
62 throw new MWException( "Bad content model: " .
63 "expected {$this->model_id} " .
64 "but got $model_id." );
69 * @see Content::getContentHandler()
71 public function getContentHandler() {
72 return ContentHandler
::getForContent( $this );
76 * @see Content::getDefaultFormat()
78 public function getDefaultFormat() {
79 return $this->getContentHandler()->getDefaultFormat();
83 * @see Content::getSupportedFormats()
85 public function getSupportedFormats() {
86 return $this->getContentHandler()->getSupportedFormats();
90 * @see Content::isSupportedFormat()
92 public function isSupportedFormat( $format ) {
94 return true; // this means "use the default"
97 return $this->getContentHandler()->isSupportedFormat( $format );
101 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
105 * @throws MWException
107 protected function checkFormat( $format ) {
108 if ( !$this->isSupportedFormat( $format ) ) {
109 throw new MWException( "Format $format is not supported for content model " .
115 * @see Content::serialize
117 public function serialize( $format = null ) {
118 return $this->getContentHandler()->serializeContent( $this, $format );
122 * @see Content::isEmpty()
124 public function isEmpty() {
125 return $this->getSize() === 0;
129 * @see Content::isValid()
131 public function isValid() {
136 * @see Content::equals()
138 public function equals( Content
$that = null ) {
139 if ( is_null( $that ) ) {
143 if ( $that === $this ) {
147 if ( $that->getModel() !== $this->getModel() ) {
151 return $this->getNativeData() === $that->getNativeData();
156 * Returns a list of DataUpdate objects for recording information about this
157 * Content in some secondary data store.
159 * This default implementation calls
160 * $this->getParserOutput( $content, $title, null, null, false ),
161 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
162 * resulting ParserOutput object.
164 * Subclasses may override this to determine the secondary data updates more
165 * efficiently, preferrably without the need to generate a parser output object.
167 * @see Content::getSecondaryDataUpdates()
169 * @param $title Title The context for determining the necessary updates
170 * @param $old Content|null An optional Content object representing the
171 * previous content, i.e. the content being replaced by this Content
173 * @param $recursive boolean Whether to include recursive updates (default:
175 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
176 * Provide if you have one handy, to avoid re-parsing of the content.
178 * @return Array. A list of DataUpdate objects for putting information
179 * about this content object somewhere.
183 public function getSecondaryDataUpdates( Title
$title,
185 $recursive = true, ParserOutput
$parserOutput = null
187 if ( !$parserOutput ) {
188 $parserOutput = $this->getParserOutput( $title, null, null, false );
191 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
196 * @see Content::getRedirectChain()
198 public function getRedirectChain() {
199 global $wgMaxRedirects;
200 $title = $this->getRedirectTarget();
201 if ( is_null( $title ) ) {
204 // recursive check to follow double redirects
205 $recurse = $wgMaxRedirects;
206 $titles = array( $title );
207 while ( --$recurse > 0 ) {
208 if ( $title->isRedirect() ) {
209 $page = WikiPage
::factory( $title );
210 $newtitle = $page->getRedirectTarget();
214 // Redirects to some special pages are not permitted
215 if ( $newtitle instanceOf Title
&& $newtitle->isValidRedirectTarget() ) {
216 // The new title passes the checks, so make that our current
217 // title so that further recursion can be checked
219 $titles[] = $newtitle;
228 * @see Content::getRedirectTarget()
230 public function getRedirectTarget() {
235 * @see Content::getUltimateRedirectTarget()
236 * @note: migrated here from Title::newFromRedirectRecurse
238 public function getUltimateRedirectTarget() {
239 $titles = $this->getRedirectChain();
240 return $titles ?
array_pop( $titles ) : null;
244 * @see Content::isRedirect()
250 public function isRedirect() {
251 return $this->getRedirectTarget() !== null;
255 * @see Content::updateRedirect()
257 * This default implementation always returns $this.
261 * @return Content $this
263 public function updateRedirect( Title
$target ) {
268 * @see Content::getSection()
270 public function getSection( $sectionId ) {
275 * @see Content::replaceSection()
277 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
282 * @see Content::preSaveTransform()
284 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
289 * @see Content::addSectionHeader()
291 public function addSectionHeader( $header ) {
296 * @see Content::preloadTransform()
298 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
303 * @see Content::prepareSave()
305 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
306 if ( $this->isValid() ) {
307 return Status
::newGood();
309 return Status
::newFatal( "invalid-content-data" );
314 * @see Content::getDeletionUpdates()
318 * @param $page \WikiPage the deleted page
319 * @param $parserOutput null|\ParserOutput optional parser output object
320 * for efficient access to meta-information about the content object.
321 * Provide if you have one handy.
323 * @return array A list of DataUpdate instances that will clean up the
324 * database after deletion.
326 public function getDeletionUpdates( WikiPage
$page,
327 ParserOutput
$parserOutput = null )
330 new LinksDeletionUpdate( $page ),
335 * @see Content::matchMagicWord()
337 * This default implementation always returns false. Subclasses may override this to supply matching logic.
339 * @param MagicWord $word
343 public function matchMagicWord( MagicWord
$word ) {