Merge changes Ic13414f0,I26085bfc
[lhc/web/wiklou.git] / includes / content / Content.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 interface for content objects.
31 *
32 * @ingroup Content
33 */
34 interface Content {
35 /**
36 * @since 1.21
37 *
38 * @return string A string representing the content in a way useful for
39 * building a full text search index. If no useful representation exists,
40 * this method returns an empty string.
41 *
42 * @todo Test that this actually works
43 * @todo Make sure this also works with LuceneSearch / WikiSearch
44 */
45 public function getTextForSearchIndex();
46
47 /**
48 * @since 1.21
49 *
50 * @return string|false The wikitext to include when another page includes this
51 * content, or false if the content is not includable in a wikitext page.
52 *
53 * @todo Allow native handling, bypassing wikitext representation, like
54 * for includable special pages.
55 * @todo Allow transclusion into other content models than Wikitext!
56 * @todo Used in WikiPage and MessageCache to get message text. Not so
57 * nice. What should we use instead?!
58 */
59 public function getWikitextForTransclusion();
60
61 /**
62 * Returns a textual representation of the content suitable for use in edit
63 * summaries and log messages.
64 *
65 * @since 1.21
66 *
67 * @param int $maxLength Maximum length of the summary text
68 * @return string The summary text
69 */
70 public function getTextForSummary( $maxLength = 250 );
71
72 /**
73 * Returns native representation of the data. Interpretation depends on
74 * the data model used, as given by getDataModel().
75 *
76 * @since 1.21
77 *
78 * @return mixed The native representation of the content. Could be a
79 * string, a nested array structure, an object, a binary blob...
80 * anything, really.
81 *
82 * @note Caller must be aware of content model!
83 */
84 public function getNativeData();
85
86 /**
87 * Returns the content's nominal size in bogo-bytes.
88 *
89 * @return int
90 */
91 public function getSize();
92
93 /**
94 * Returns the ID of the content model used by this Content object.
95 * Corresponds to the CONTENT_MODEL_XXX constants.
96 *
97 * @since 1.21
98 *
99 * @return String The model id
100 */
101 public function getModel();
102
103 /**
104 * Convenience method that returns the ContentHandler singleton for handling
105 * the content model that this Content object uses.
106 *
107 * Shorthand for ContentHandler::getForContent( $this )
108 *
109 * @since 1.21
110 *
111 * @return ContentHandler
112 */
113 public function getContentHandler();
114
115 /**
116 * Convenience method that returns the default serialization format for the
117 * content model that this Content object uses.
118 *
119 * Shorthand for $this->getContentHandler()->getDefaultFormat()
120 *
121 * @since 1.21
122 *
123 * @return String
124 */
125 public function getDefaultFormat();
126
127 /**
128 * Convenience method that returns the list of serialization formats
129 * supported for the content model that this Content object uses.
130 *
131 * Shorthand for $this->getContentHandler()->getSupportedFormats()
132 *
133 * @since 1.21
134 *
135 * @return Array of supported serialization formats
136 */
137 public function getSupportedFormats();
138
139 /**
140 * Returns true if $format is a supported serialization format for this
141 * Content object, false if it isn't.
142 *
143 * Note that this should always return true if $format is null, because null
144 * stands for the default serialization.
145 *
146 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
147 *
148 * @since 1.21
149 *
150 * @param string $format The format to check
151 * @return bool Whether the format is supported
152 */
153 public function isSupportedFormat( $format );
154
155 /**
156 * Convenience method for serializing this Content object.
157 *
158 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
159 *
160 * @since 1.21
161 *
162 * @param $format null|string The desired serialization format (or null for
163 * the default format).
164 * @return string Serialized form of this Content object
165 */
166 public function serialize( $format = null );
167
168 /**
169 * Returns true if this Content object represents empty content.
170 *
171 * @since 1.21
172 *
173 * @return bool Whether this Content object is empty
174 */
175 public function isEmpty();
176
177 /**
178 * Returns whether the content is valid. This is intended for local validity
179 * checks, not considering global consistency.
180 *
181 * Content needs to be valid before it can be saved.
182 *
183 * This default implementation always returns true.
184 *
185 * @since 1.21
186 *
187 * @return boolean
188 */
189 public function isValid();
190
191 /**
192 * Returns true if this Content objects is conceptually equivalent to the
193 * given Content object.
194 *
195 * Contract:
196 *
197 * - Will return false if $that is null.
198 * - Will return true if $that === $this.
199 * - Will return false if $that->getModel() != $this->getModel().
200 * - Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
201 * where the meaning of "equal" depends on the actual data model.
202 *
203 * Implementations should be careful to make equals() transitive and reflexive:
204 *
205 * - $a->equals( $b ) <=> $b->equals( $a )
206 * - $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
207 *
208 * @since 1.21
209 *
210 * @param $that Content The Content object to compare to
211 * @return bool True if this Content object is equal to $that, false otherwise.
212 */
213 public function equals( Content $that = null );
214
215 /**
216 * Return a copy of this Content object. The following must be true for the
217 * object returned:
218 *
219 * if $copy = $original->copy()
220 *
221 * - get_class($original) === get_class($copy)
222 * - $original->getModel() === $copy->getModel()
223 * - $original->equals( $copy )
224 *
225 * If and only if the Content object is immutable, the copy() method can and
226 * should return $this. That is, $copy === $original may be true, but only
227 * for immutable content objects.
228 *
229 * @since 1.21
230 *
231 * @return Content. A copy of this object
232 */
233 public function copy();
234
235 /**
236 * Returns true if this content is countable as a "real" wiki page, provided
237 * that it's also in a countable location (e.g. a current revision in the
238 * main namespace).
239 *
240 * @since 1.21
241 *
242 * @param bool $hasLinks If it is known whether this content contains
243 * links, provide this information here, to avoid redundant parsing to
244 * find out.
245 * @return boolean
246 */
247 public function isCountable( $hasLinks = null );
248
249 /**
250 * Parse the Content object and generate a ParserOutput from the result.
251 * $result->getText() can be used to obtain the generated HTML. If no HTML
252 * is needed, $generateHtml can be set to false; in that case,
253 * $result->getText() may return null.
254 *
255 * @param $title Title The page title to use as a context for rendering
256 * @param $revId null|int The revision being rendered (optional)
257 * @param $options null|ParserOptions Any parser options
258 * @param $generateHtml Boolean Whether to generate HTML (default: true). If false,
259 * the result of calling getText() on the ParserOutput object returned by
260 * this method is undefined.
261 *
262 * @since 1.21
263 *
264 * @return ParserOutput
265 */
266 public function getParserOutput( Title $title,
267 $revId = null,
268 ParserOptions $options = null, $generateHtml = true );
269
270 // TODO: make RenderOutput and RenderOptions base classes
271
272 /**
273 * Returns a list of DataUpdate objects for recording information about this
274 * Content in some secondary data store. If the optional second argument,
275 * $old, is given, the updates may model only the changes that need to be
276 * made to replace information about the old content with information about
277 * the new content.
278 *
279 * This default implementation calls
280 * $this->getParserOutput( $content, $title, null, null, false ),
281 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
282 * resulting ParserOutput object.
283 *
284 * Subclasses may implement this to determine the necessary updates more
285 * efficiently, or make use of information about the old content.
286 *
287 * @param $title Title The context for determining the necessary updates
288 * @param $old Content|null An optional Content object representing the
289 * previous content, i.e. the content being replaced by this Content
290 * object.
291 * @param $recursive boolean Whether to include recursive updates (default:
292 * false).
293 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
294 * Provide if you have one handy, to avoid re-parsing of the content.
295 *
296 * @return Array. A list of DataUpdate objects for putting information
297 * about this content object somewhere.
298 *
299 * @since 1.21
300 */
301 public function getSecondaryDataUpdates( Title $title,
302 Content $old = null,
303 $recursive = true, ParserOutput $parserOutput = null
304 );
305
306 /**
307 * Construct the redirect destination from this content and return an
308 * array of Titles, or null if this content doesn't represent a redirect.
309 * The last element in the array is the final destination after all redirects
310 * have been resolved (up to $wgMaxRedirects times).
311 *
312 * @since 1.21
313 *
314 * @return Array of Titles, with the destination last
315 */
316 public function getRedirectChain();
317
318 /**
319 * Construct the redirect destination from this content and return a Title,
320 * or null if this content doesn't represent a redirect.
321 * This will only return the immediate redirect target, useful for
322 * the redirect table and other checks that don't need full recursion.
323 *
324 * @since 1.21
325 *
326 * @return Title: The corresponding Title
327 */
328 public function getRedirectTarget();
329
330 /**
331 * Construct the redirect destination from this content and return the
332 * Title, or null if this content doesn't represent a redirect.
333 *
334 * This will recurse down $wgMaxRedirects times or until a non-redirect
335 * target is hit in order to provide (hopefully) the Title of the final
336 * destination instead of another redirect.
337 *
338 * There is usually no need to override the default behavior, subclasses that
339 * want to implement redirects should override getRedirectTarget().
340 *
341 * @since 1.21
342 *
343 * @return Title
344 */
345 public function getUltimateRedirectTarget();
346
347 /**
348 * Returns whether this Content represents a redirect.
349 * Shorthand for getRedirectTarget() !== null.
350 *
351 * @since 1.21
352 *
353 * @return bool
354 */
355 public function isRedirect();
356
357 /**
358 * If this Content object is a redirect, this method updates the redirect target.
359 * Otherwise, it does nothing.
360 *
361 * @since 1.21
362 *
363 * @param Title $target the new redirect target
364 *
365 * @return Content a new Content object with the updated redirect (or $this
366 * if this Content object isn't a redirect)
367 */
368 public function updateRedirect( Title $target );
369
370 /**
371 * Returns the section with the given ID.
372 *
373 * @since 1.21
374 *
375 * @param string $sectionId The section's ID, given as a numeric string.
376 * The ID "0" retrieves the section before the first heading, "1" the
377 * text between the first heading (included) and the second heading
378 * (excluded), etc.
379 * @return Content|Boolean|null The section, or false if no such section
380 * exist, or null if sections are not supported.
381 */
382 public function getSection( $sectionId );
383
384 /**
385 * Replaces a section of the content and returns a Content object with the
386 * section replaced.
387 *
388 * @since 1.21
389 *
390 * @param $section null/false or a section number (0, 1, 2, T1, T2...), or "new"
391 * @param $with Content: new content of the section
392 * @param string $sectionTitle new section's subject, only if $section is 'new'
393 * @return string Complete article text, or null if error
394 */
395 public function replaceSection( $section, Content $with, $sectionTitle = '' );
396
397 /**
398 * Returns a Content object with pre-save transformations applied (or this
399 * object if no transformations apply).
400 *
401 * @since 1.21
402 *
403 * @param $title Title
404 * @param $user User
405 * @param $parserOptions null|ParserOptions
406 * @return Content
407 */
408 public function preSaveTransform( Title $title, User $user, ParserOptions $parserOptions );
409
410 /**
411 * Returns a new WikitextContent object with the given section heading
412 * prepended, if supported. The default implementation just returns this
413 * Content object unmodified, ignoring the section header.
414 *
415 * @since 1.21
416 *
417 * @param $header string
418 * @return Content
419 */
420 public function addSectionHeader( $header );
421
422 /**
423 * Returns a Content object with preload transformations applied (or this
424 * object if no transformations apply).
425 *
426 * @since 1.21
427 *
428 * @param $title Title
429 * @param $parserOptions null|ParserOptions
430 * @return Content
431 */
432 public function preloadTransform( Title $title, ParserOptions $parserOptions );
433
434 /**
435 * Prepare Content for saving. Called before Content is saved by WikiPage::doEditContent() and in
436 * similar places.
437 *
438 * This may be used to check the content's consistency with global state. This function should
439 * NOT write any information to the database.
440 *
441 * Note that this method will usually be called inside the same transaction
442 * bracket that will be used to save the new revision.
443 *
444 * Note that this method is called before any update to the page table is
445 * performed. This means that $page may not yet know a page ID.
446 *
447 * @since 1.21
448 *
449 * @param WikiPage $page The page to be saved.
450 * @param int $flags bitfield for use with EDIT_XXX constants, see WikiPage::doEditContent()
451 * @param int $baseRevId the ID of the current revision
452 * @param User $user
453 *
454 * @return Status A status object indicating whether the content was
455 * successfully prepared for saving. If the returned status indicates
456 * an error, a rollback will be performed and the transaction aborted.
457 *
458 * @see see WikiPage::doEditContent()
459 */
460 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user );
461
462 /**
463 * Returns a list of updates to perform when this content is deleted.
464 * The necessary updates may be taken from the Content object, or depend on
465 * the current state of the database.
466 *
467 * @since 1.21
468 *
469 * @param $page WikiPage the deleted page
470 * @param $parserOutput null|ParserOutput optional parser output object
471 * for efficient access to meta-information about the content object.
472 * Provide if you have one handy.
473 *
474 * @return array A list of DataUpdate instances that will clean up the
475 * database after deletion.
476 */
477 public function getDeletionUpdates( WikiPage $page,
478 ParserOutput $parserOutput = null );
479
480 /**
481 * Returns true if this Content object matches the given magic word.
482 *
483 * @since 1.21
484 *
485 * @param MagicWord $word the magic word to match
486 *
487 * @return bool whether this Content object matches the given magic word.
488 */
489 public function matchMagicWord( MagicWord $word );
490
491 /**
492 * Converts this content object into another content object with the given content model,
493 * if that is possible.
494 *
495 * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
496 * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
497 * not allowed, full round-trip conversion is expected to work without losing information.
498 *
499 * @return Content|bool A content object with the content model $toModel, or false if
500 * that conversion is not supported.
501 */
502 public function convert( $toModel, $lossy = '' );
503 // @todo ImagePage and CategoryPage interfere with per-content action handlers
504 // @todo nice&sane integration of GeSHi syntax highlighting
505 // [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a
506 // config to set the class which handles syntax highlighting
507 // [12:00] <vvv> And default it to a DummyHighlighter
508 }