e14fb56f7d0deca59d8700801815f067abbb8794
[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 /**
208 * Returns a list of DataUpdate objects for recording information about this
209 * Content in some secondary data store.
210 *
211 * This default implementation calls
212 * $this->getParserOutput( $content, $title, null, null, false ),
213 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
214 * resulting ParserOutput object.
215 *
216 * Subclasses may override this to determine the secondary data updates more
217 * efficiently, preferably without the need to generate a parser output object.
218 *
219 * @see Content::getSecondaryDataUpdates()
220 *
221 * @param $title Title The context for determining the necessary updates
222 * @param $old Content|null An optional Content object representing the
223 * previous content, i.e. the content being replaced by this Content
224 * object.
225 * @param $recursive boolean Whether to include recursive updates (default:
226 * false).
227 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
228 * Provide if you have one handy, to avoid re-parsing of the content.
229 *
230 * @return Array. A list of DataUpdate objects for putting information
231 * about this content object somewhere.
232 *
233 * @since 1.21
234 */
235 public function getSecondaryDataUpdates( Title $title,
236 Content $old = null,
237 $recursive = true, ParserOutput $parserOutput = null
238 ) {
239 if ( $parserOutput === null ) {
240 $parserOutput = $this->getParserOutput( $title, null, null, false );
241 }
242
243 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
244 }
245
246
247 /**
248 * @see Content::getRedirectChain
249 *
250 * @since 1.21
251 */
252 public function getRedirectChain() {
253 global $wgMaxRedirects;
254 $title = $this->getRedirectTarget();
255 if ( is_null( $title ) ) {
256 return null;
257 }
258 // recursive check to follow double redirects
259 $recurse = $wgMaxRedirects;
260 $titles = array( $title );
261 while ( --$recurse > 0 ) {
262 if ( $title->isRedirect() ) {
263 $page = WikiPage::factory( $title );
264 $newtitle = $page->getRedirectTarget();
265 } else {
266 break;
267 }
268 // Redirects to some special pages are not permitted
269 if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
270 // The new title passes the checks, so make that our current
271 // title so that further recursion can be checked
272 $title = $newtitle;
273 $titles[] = $newtitle;
274 } else {
275 break;
276 }
277 }
278 return $titles;
279 }
280
281 /**
282 * @see Content::getRedirectTarget
283 *
284 * @since 1.21
285 */
286 public function getRedirectTarget() {
287 return null;
288 }
289
290 /**
291 * @see Content::getUltimateRedirectTarget
292 * @note: migrated here from Title::newFromRedirectRecurse
293 *
294 * @since 1.21
295 */
296 public function getUltimateRedirectTarget() {
297 $titles = $this->getRedirectChain();
298 return $titles ? array_pop( $titles ) : null;
299 }
300
301 /**
302 * @see Content::isRedirect
303 *
304 * @since 1.21
305 *
306 * @return bool
307 */
308 public function isRedirect() {
309 return $this->getRedirectTarget() !== null;
310 }
311
312 /**
313 * @see Content::updateRedirect
314 *
315 * This default implementation always returns $this.
316 *
317 * @param Title $target
318 *
319 * @since 1.21
320 *
321 * @return Content $this
322 */
323 public function updateRedirect( Title $target ) {
324 return $this;
325 }
326
327 /**
328 * @see Content::getSection
329 *
330 * @since 1.21
331 */
332 public function getSection( $sectionId ) {
333 return null;
334 }
335
336 /**
337 * @see Content::replaceSection
338 *
339 * @since 1.21
340 */
341 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
342 return null;
343 }
344
345 /**
346 * @see Content::preSaveTransform
347 *
348 * @since 1.21
349 */
350 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
351 return $this;
352 }
353
354 /**
355 * @see Content::addSectionHeader
356 *
357 * @since 1.21
358 */
359 public function addSectionHeader( $header ) {
360 return $this;
361 }
362
363 /**
364 * @see Content::preloadTransform
365 *
366 * @since 1.21
367 */
368 public function preloadTransform( Title $title, ParserOptions $popts ) {
369 return $this;
370 }
371
372 /**
373 * @see Content::prepareSave
374 *
375 * @since 1.21
376 */
377 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
378 if ( $this->isValid() ) {
379 return Status::newGood();
380 } else {
381 return Status::newFatal( "invalid-content-data" );
382 }
383 }
384
385 /**
386 * @see Content::getDeletionUpdates
387 *
388 * @since 1.21
389 *
390 * @param $page WikiPage the deleted page
391 * @param $parserOutput null|ParserOutput optional parser output object
392 * for efficient access to meta-information about the content object.
393 * Provide if you have one handy.
394 *
395 * @return array A list of DataUpdate instances that will clean up the
396 * database after deletion.
397 */
398 public function getDeletionUpdates( WikiPage $page,
399 ParserOutput $parserOutput = null )
400 {
401 return array(
402 new LinksDeletionUpdate( $page ),
403 );
404 }
405
406 /**
407 * This default implementation always returns false. Subclasses may override this to supply matching logic.
408 *
409 * @see Content::matchMagicWord
410 *
411 * @since 1.21
412 *
413 * @param MagicWord $word
414 *
415 * @return bool
416 */
417 public function matchMagicWord( MagicWord $word ) {
418 return false;
419 }
420
421 /**
422 * @see Content::convert()
423 *
424 * This base implementation calls the hook ConvertContent to enable custom conversions.
425 * Subclasses may override this to implement conversion for "their" content model.
426 *
427 * @param String $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
428 * @param String $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
429 * not allowed, full round-trip conversion is expected to work without losing information.
430 *
431 * @return Content|bool A content object with the content model $toModel, or false if
432 * that conversion is not supported.
433 */
434 public function convert( $toModel, $lossy = '' ) {
435 if ( $this->getModel() === $toModel ) {
436 //nothing to do, shorten out.
437 return $this;
438 }
439
440 $lossy = ( $lossy === 'lossy' ); // string flag, convert to boolean for convenience
441 $result = false;
442
443 wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
444 return $result;
445 }
446 }