Merge "Removed READ_LATEST default from Revision::newFromPageId()."
[lhc/web/wiklou.git] / includes / content / AbstractContent.php
1 <?php
2 /**
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.
5 *
6 * @since 1.21
7 */
8 abstract class AbstractContent implements Content {
9
10 /**
11 * Name of the content model this Content object represents.
12 * Use with CONTENT_MODEL_XXX constants
13 *
14 * @var string $model_id
15 */
16 protected $model_id;
17
18 /**
19 * @param String $model_id
20 */
21 public function __construct( $model_id = null ) {
22 $this->model_id = $model_id;
23 }
24
25 /**
26 * @see Content::getModel()
27 */
28 public function getModel() {
29 return $this->model_id;
30 }
31
32 /**
33 * Throws an MWException if $model_id is not the id of the content model
34 * supported by this Content object.
35 *
36 * @param $model_id int the model to check
37 *
38 * @throws MWException
39 */
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." );
45 }
46 }
47
48 /**
49 * @see Content::getContentHandler()
50 */
51 public function getContentHandler() {
52 return ContentHandler::getForContent( $this );
53 }
54
55 /**
56 * @see Content::getDefaultFormat()
57 */
58 public function getDefaultFormat() {
59 return $this->getContentHandler()->getDefaultFormat();
60 }
61
62 /**
63 * @see Content::getSupportedFormats()
64 */
65 public function getSupportedFormats() {
66 return $this->getContentHandler()->getSupportedFormats();
67 }
68
69 /**
70 * @see Content::isSupportedFormat()
71 */
72 public function isSupportedFormat( $format ) {
73 if ( !$format ) {
74 return true; // this means "use the default"
75 }
76
77 return $this->getContentHandler()->isSupportedFormat( $format );
78 }
79
80 /**
81 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
82 * return true.
83 *
84 * @param $format
85 * @throws MWException
86 */
87 protected function checkFormat( $format ) {
88 if ( !$this->isSupportedFormat( $format ) ) {
89 throw new MWException( "Format $format is not supported for content model " .
90 $this->getModel() );
91 }
92 }
93
94 /**
95 * @see Content::serialize
96 */
97 public function serialize( $format = null ) {
98 return $this->getContentHandler()->serializeContent( $this, $format );
99 }
100
101 /**
102 * @see Content::isEmpty()
103 */
104 public function isEmpty() {
105 return $this->getSize() === 0;
106 }
107
108 /**
109 * @see Content::isValid()
110 */
111 public function isValid() {
112 return true;
113 }
114
115 /**
116 * @see Content::equals()
117 */
118 public function equals( Content $that = null ) {
119 if ( is_null( $that ) ) {
120 return false;
121 }
122
123 if ( $that === $this ) {
124 return true;
125 }
126
127 if ( $that->getModel() !== $this->getModel() ) {
128 return false;
129 }
130
131 return $this->getNativeData() === $that->getNativeData();
132 }
133
134
135 /**
136 * Returns a list of DataUpdate objects for recording information about this
137 * Content in some secondary data store.
138 *
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.
143 *
144 * Subclasses may override this to determine the secondary data updates more
145 * efficiently, preferrably without the need to generate a parser output object.
146 *
147 * @see Content::getSecondaryDataUpdates()
148 *
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
152 * object.
153 * @param $recursive boolean Whether to include recursive updates (default:
154 * false).
155 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
156 * Provide if you have one handy, to avoid re-parsing of the content.
157 *
158 * @return Array. A list of DataUpdate objects for putting information
159 * about this content object somewhere.
160 *
161 * @since 1.21
162 */
163 public function getSecondaryDataUpdates( Title $title,
164 Content $old = null,
165 $recursive = true, ParserOutput $parserOutput = null
166 ) {
167 if ( !$parserOutput ) {
168 $parserOutput = $this->getParserOutput( $title, null, null, false );
169 }
170
171 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
172 }
173
174
175 /**
176 * @see Content::getRedirectChain()
177 */
178 public function getRedirectChain() {
179 global $wgMaxRedirects;
180 $title = $this->getRedirectTarget();
181 if ( is_null( $title ) ) {
182 return null;
183 }
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();
191 } else {
192 break;
193 }
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
198 $title = $newtitle;
199 $titles[] = $newtitle;
200 } else {
201 break;
202 }
203 }
204 return $titles;
205 }
206
207 /**
208 * @see Content::getRedirectTarget()
209 */
210 public function getRedirectTarget() {
211 return null;
212 }
213
214 /**
215 * @see Content::getUltimateRedirectTarget()
216 * @note: migrated here from Title::newFromRedirectRecurse
217 */
218 public function getUltimateRedirectTarget() {
219 $titles = $this->getRedirectChain();
220 return $titles ? array_pop( $titles ) : null;
221 }
222
223 /**
224 * @see Content::isRedirect()
225 *
226 * @since 1.21
227 *
228 * @return bool
229 */
230 public function isRedirect() {
231 return $this->getRedirectTarget() !== null;
232 }
233
234 /**
235 * @see Content::updateRedirect()
236 *
237 * This default implementation always returns $this.
238 *
239 * @since 1.21
240 *
241 * @return Content $this
242 */
243 public function updateRedirect( Title $target ) {
244 return $this;
245 }
246
247 /**
248 * @see Content::getSection()
249 */
250 public function getSection( $sectionId ) {
251 return null;
252 }
253
254 /**
255 * @see Content::replaceSection()
256 */
257 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
258 return null;
259 }
260
261 /**
262 * @see Content::preSaveTransform()
263 */
264 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
265 return $this;
266 }
267
268 /**
269 * @see Content::addSectionHeader()
270 */
271 public function addSectionHeader( $header ) {
272 return $this;
273 }
274
275 /**
276 * @see Content::preloadTransform()
277 */
278 public function preloadTransform( Title $title, ParserOptions $popts ) {
279 return $this;
280 }
281
282 /**
283 * @see Content::prepareSave()
284 */
285 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
286 if ( $this->isValid() ) {
287 return Status::newGood();
288 } else {
289 return Status::newFatal( "invalid-content-data" );
290 }
291 }
292
293 /**
294 * @see Content::getDeletionUpdates()
295 *
296 * @since 1.21
297 *
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.
302 *
303 * @return array A list of DataUpdate instances that will clean up the
304 * database after deletion.
305 */
306 public function getDeletionUpdates( WikiPage $page,
307 ParserOutput $parserOutput = null )
308 {
309 return array(
310 new LinksDeletionUpdate( $page ),
311 );
312 }
313
314 /**
315 * @see Content::matchMagicWord()
316 *
317 * This default implementation always returns false. Subclasses may override this to supply matching logic.
318 *
319 * @param MagicWord $word
320 *
321 * @return bool
322 */
323 public function matchMagicWord( MagicWord $word ) {
324 return false;
325 }
326 }