fix some spacing
[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 * 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.
10 *
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.
15 *
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
20 *
21 * @since 1.21
22 *
23 * @file
24 * @ingroup Content
25 *
26 * @author Daniel Kinzler
27 */
28
29 /**
30 * Base implementation for content objects.
31 *
32 * @ingroup Content
33 */
34 abstract class AbstractContent implements Content {
35
36 /**
37 * Name of the content model this Content object represents.
38 * Use with CONTENT_MODEL_XXX constants
39 *
40 * @since 1.21
41 *
42 * @var string $model_id
43 */
44 protected $model_id;
45
46 /**
47 * @param string|null $modelId
48 *
49 * @since 1.21
50 */
51 public function __construct( $modelId = null ) {
52 $this->model_id = $modelId;
53 }
54
55 /**
56 * @see Content::getModel
57 *
58 * @since 1.21
59 */
60 public function getModel() {
61 return $this->model_id;
62 }
63
64 /**
65 * Throws an MWException if $model_id is not the id of the content model
66 * supported by this Content object.
67 *
68 * @since 1.21
69 *
70 * @param string $modelId The model to check
71 *
72 * @throws MWException
73 */
74 protected function checkModelID( $modelId ) {
75 if ( $modelId !== $this->model_id ) {
76 throw new MWException(
77 "Bad content model: " .
78 "expected {$this->model_id} " .
79 "but got $modelId."
80 );
81 }
82 }
83
84 /**
85 * @see Content::getContentHandler
86 *
87 * @since 1.21
88 */
89 public function getContentHandler() {
90 return ContentHandler::getForContent( $this );
91 }
92
93 /**
94 * @see Content::getDefaultFormat
95 *
96 * @since 1.21
97 */
98 public function getDefaultFormat() {
99 return $this->getContentHandler()->getDefaultFormat();
100 }
101
102 /**
103 * @see Content::getSupportedFormats
104 *
105 * @since 1.21
106 */
107 public function getSupportedFormats() {
108 return $this->getContentHandler()->getSupportedFormats();
109 }
110
111 /**
112 * @see Content::isSupportedFormat
113 *
114 * @param string $format
115 *
116 * @since 1.21
117 *
118 * @return boolean
119 */
120 public function isSupportedFormat( $format ) {
121 if ( !$format ) {
122 return true; // this means "use the default"
123 }
124
125 return $this->getContentHandler()->isSupportedFormat( $format );
126 }
127
128 /**
129 * Throws an MWException if $this->isSupportedFormat( $format ) does not
130 * return true.
131 *
132 * @since 1.21
133 *
134 * @param string $format
135 * @throws MWException
136 */
137 protected function checkFormat( $format ) {
138 if ( !$this->isSupportedFormat( $format ) ) {
139 throw new MWException(
140 "Format $format is not supported for content model " .
141 $this->getModel()
142 );
143 }
144 }
145
146 /**
147 * @see Content::serialize
148 *
149 * @param string|null $format
150 *
151 * @since 1.21
152 *
153 * @return string
154 */
155 public function serialize( $format = null ) {
156 return $this->getContentHandler()->serializeContent( $this, $format );
157 }
158
159 /**
160 * @see Content::isEmpty
161 *
162 * @since 1.21
163 *
164 * @return boolean
165 */
166 public function isEmpty() {
167 return $this->getSize() === 0;
168 }
169
170 /**
171 * @see Content::isValid
172 *
173 * @since 1.21
174 *
175 * @return boolean
176 */
177 public function isValid() {
178 return true;
179 }
180
181 /**
182 * @see Content::equals
183 *
184 * @since 1.21
185 *
186 * @param Content|null $that
187 *
188 * @return boolean
189 */
190 public function equals( Content $that = null ) {
191 if ( is_null( $that ) ) {
192 return false;
193 }
194
195 if ( $that === $this ) {
196 return true;
197 }
198
199 if ( $that->getModel() !== $this->getModel() ) {
200 return false;
201 }
202
203 return $this->getNativeData() === $that->getNativeData();
204 }
205
206 /**
207 * Returns a list of DataUpdate objects for recording information about this
208 * Content in some secondary data store.
209 *
210 * This default implementation calls
211 * $this->getParserOutput( $content, $title, null, null, false ),
212 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
213 * resulting ParserOutput object.
214 *
215 * Subclasses may override this to determine the secondary data updates more
216 * efficiently, preferably without the need to generate a parser output object.
217 *
218 * @see Content::getSecondaryDataUpdates()
219 *
220 * @param $title Title The context for determining the necessary updates
221 * @param $old Content|null An optional Content object representing the
222 * previous content, i.e. the content being replaced by this Content
223 * object.
224 * @param $recursive boolean Whether to include recursive updates (default:
225 * false).
226 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
227 * Provide if you have one handy, to avoid re-parsing of the content.
228 *
229 * @return Array. A list of DataUpdate objects for putting information
230 * about this content object somewhere.
231 *
232 * @since 1.21
233 */
234 public function getSecondaryDataUpdates( Title $title,
235 Content $old = null,
236 $recursive = true, ParserOutput $parserOutput = null
237 ) {
238 if ( $parserOutput === null ) {
239 $parserOutput = $this->getParserOutput( $title, null, null, false );
240 }
241
242 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
243 }
244
245 /**
246 * @see Content::getRedirectChain
247 *
248 * @since 1.21
249 */
250 public function getRedirectChain() {
251 global $wgMaxRedirects;
252 $title = $this->getRedirectTarget();
253 if ( is_null( $title ) ) {
254 return null;
255 }
256 // recursive check to follow double redirects
257 $recurse = $wgMaxRedirects;
258 $titles = array( $title );
259 while ( --$recurse > 0 ) {
260 if ( $title->isRedirect() ) {
261 $page = WikiPage::factory( $title );
262 $newtitle = $page->getRedirectTarget();
263 } else {
264 break;
265 }
266 // Redirects to some special pages are not permitted
267 if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
268 // The new title passes the checks, so make that our current
269 // title so that further recursion can be checked
270 $title = $newtitle;
271 $titles[] = $newtitle;
272 } else {
273 break;
274 }
275 }
276 return $titles;
277 }
278
279 /**
280 * @see Content::getRedirectTarget
281 *
282 * @since 1.21
283 */
284 public function getRedirectTarget() {
285 return null;
286 }
287
288 /**
289 * @see Content::getUltimateRedirectTarget
290 * @note: migrated here from Title::newFromRedirectRecurse
291 *
292 * @since 1.21
293 */
294 public function getUltimateRedirectTarget() {
295 $titles = $this->getRedirectChain();
296 return $titles ? array_pop( $titles ) : null;
297 }
298
299 /**
300 * @see Content::isRedirect
301 *
302 * @since 1.21
303 *
304 * @return bool
305 */
306 public function isRedirect() {
307 return $this->getRedirectTarget() !== null;
308 }
309
310 /**
311 * @see Content::updateRedirect
312 *
313 * This default implementation always returns $this.
314 *
315 * @param Title $target
316 *
317 * @since 1.21
318 *
319 * @return Content $this
320 */
321 public function updateRedirect( Title $target ) {
322 return $this;
323 }
324
325 /**
326 * @see Content::getSection
327 *
328 * @since 1.21
329 */
330 public function getSection( $sectionId ) {
331 return null;
332 }
333
334 /**
335 * @see Content::replaceSection
336 *
337 * @since 1.21
338 */
339 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
340 return null;
341 }
342
343 /**
344 * @see Content::preSaveTransform
345 *
346 * @since 1.21
347 */
348 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
349 return $this;
350 }
351
352 /**
353 * @see Content::addSectionHeader
354 *
355 * @since 1.21
356 */
357 public function addSectionHeader( $header ) {
358 return $this;
359 }
360
361 /**
362 * @see Content::preloadTransform
363 *
364 * @since 1.21
365 */
366 public function preloadTransform( Title $title, ParserOptions $popts ) {
367 return $this;
368 }
369
370 /**
371 * @see Content::prepareSave
372 *
373 * @since 1.21
374 */
375 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
376 if ( $this->isValid() ) {
377 return Status::newGood();
378 } else {
379 return Status::newFatal( "invalid-content-data" );
380 }
381 }
382
383 /**
384 * @see Content::getDeletionUpdates
385 *
386 * @since 1.21
387 *
388 * @param $page WikiPage the deleted page
389 * @param $parserOutput null|ParserOutput optional parser output object
390 * for efficient access to meta-information about the content object.
391 * Provide if you have one handy.
392 *
393 * @return array A list of DataUpdate instances that will clean up the
394 * database after deletion.
395 */
396 public function getDeletionUpdates( WikiPage $page,
397 ParserOutput $parserOutput = null )
398 {
399 return array(
400 new LinksDeletionUpdate( $page ),
401 );
402 }
403
404 /**
405 * This default implementation always returns false. Subclasses may override this to supply matching logic.
406 *
407 * @see Content::matchMagicWord
408 *
409 * @since 1.21
410 *
411 * @param MagicWord $word
412 *
413 * @return bool
414 */
415 public function matchMagicWord( MagicWord $word ) {
416 return false;
417 }
418
419 /**
420 * @see Content::convert()
421 *
422 * This base implementation calls the hook ConvertContent to enable custom conversions.
423 * Subclasses may override this to implement conversion for "their" content model.
424 *
425 * @param String $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
426 * @param String $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
427 * not allowed, full round-trip conversion is expected to work without losing information.
428 *
429 * @return Content|bool A content object with the content model $toModel, or false if
430 * that conversion is not supported.
431 */
432 public function convert( $toModel, $lossy = '' ) {
433 if ( $this->getModel() === $toModel ) {
434 //nothing to do, shorten out.
435 return $this;
436 }
437
438 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
439 $result = false;
440
441 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
442 return $result;
443 }
444 }