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
30 * Base interface for content objects.
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.
42 * @todo Test that this actually works
43 * @todo Make sure this also works with LuceneSearch / WikiSearch
45 public function getTextForSearchIndex();
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.
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?!
59 public function getWikitextForTransclusion();
62 * Returns a textual representation of the content suitable for use in edit
63 * summaries and log messages.
67 * @param int $maxLength Maximum length of the summary text
68 * @return string The summary text
70 public function getTextForSummary( $maxLength = 250 );
73 * Returns native representation of the data. Interpretation depends on
74 * the data model used, as given by getDataModel().
78 * @return mixed The native representation of the content. Could be a
79 * string, a nested array structure, an object, a binary blob...
82 * @note Caller must be aware of content model!
84 public function getNativeData();
87 * Returns the content's nominal size in bogo-bytes.
91 public function getSize();
94 * Returns the ID of the content model used by this Content object.
95 * Corresponds to the CONTENT_MODEL_XXX constants.
99 * @return String The model id
101 public function getModel();
104 * Convenience method that returns the ContentHandler singleton for handling
105 * the content model that this Content object uses.
107 * Shorthand for ContentHandler::getForContent( $this )
111 * @return ContentHandler
113 public function getContentHandler();
116 * Convenience method that returns the default serialization format for the
117 * content model that this Content object uses.
119 * Shorthand for $this->getContentHandler()->getDefaultFormat()
125 public function getDefaultFormat();
128 * Convenience method that returns the list of serialization formats
129 * supported for the content model that this Content object uses.
131 * Shorthand for $this->getContentHandler()->getSupportedFormats()
135 * @return Array of supported serialization formats
137 public function getSupportedFormats();
140 * Returns true if $format is a supported serialization format for this
141 * Content object, false if it isn't.
143 * Note that this should always return true if $format is null, because null
144 * stands for the default serialization.
146 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
150 * @param string $format The format to check
151 * @return bool Whether the format is supported
153 public function isSupportedFormat( $format );
156 * Convenience method for serializing this Content object.
158 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
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
166 public function serialize( $format = null );
169 * Returns true if this Content object represents empty content.
173 * @return bool Whether this Content object is empty
175 public function isEmpty();
178 * Returns whether the content is valid. This is intended for local validity
179 * checks, not considering global consistency.
181 * Content needs to be valid before it can be saved.
183 * This default implementation always returns true.
189 public function isValid();
192 * Returns true if this Content objects is conceptually equivalent to the
193 * given Content object.
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.
203 * Implementations should be careful to make equals() transitive and reflexive:
205 * - $a->equals( $b ) <=> $b->equals( $a )
206 * - $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
210 * @param $that Content The Content object to compare to
211 * @return bool True if this Content object is equal to $that, false otherwise.
213 public function equals( Content
$that = null );
216 * Return a copy of this Content object. The following must be true for the
219 * if $copy = $original->copy()
221 * - get_class($original) === get_class($copy)
222 * - $original->getModel() === $copy->getModel()
223 * - $original->equals( $copy )
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.
231 * @return Content. A copy of this object
233 public function copy();
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
242 * @param bool $hasLinks If it is known whether this content contains
243 * links, provide this information here, to avoid redundant parsing to
247 public function isCountable( $hasLinks = null );
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.
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.
264 * @return ParserOutput
266 public function getParserOutput( Title
$title,
268 ParserOptions
$options = null, $generateHtml = true );
270 // TODO: make RenderOutput and RenderOptions base classes
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
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.
284 * Subclasses may implement this to determine the necessary updates more
285 * efficiently, or make use of information about the old content.
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
291 * @param $recursive boolean Whether to include recursive updates (default:
293 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
294 * Provide if you have one handy, to avoid re-parsing of the content.
296 * @return Array. A list of DataUpdate objects for putting information
297 * about this content object somewhere.
301 public function getSecondaryDataUpdates( Title
$title,
303 $recursive = true, ParserOutput
$parserOutput = null
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).
314 * @return Array of Titles, with the destination last
316 public function getRedirectChain();
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.
326 * @return Title: The corresponding Title
328 public function getRedirectTarget();
331 * Construct the redirect destination from this content and return the
332 * Title, or null if this content doesn't represent a redirect.
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.
338 * There is usually no need to override the default behavior, subclasses that
339 * want to implement redirects should override getRedirectTarget().
345 public function getUltimateRedirectTarget();
348 * Returns whether this Content represents a redirect.
349 * Shorthand for getRedirectTarget() !== null.
355 public function isRedirect();
358 * If this Content object is a redirect, this method updates the redirect target.
359 * Otherwise, it does nothing.
363 * @param Title $target the new redirect target
365 * @return Content a new Content object with the updated redirect (or $this
366 * if this Content object isn't a redirect)
368 public function updateRedirect( Title
$target );
371 * Returns the section with the given ID.
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
379 * @return Content|Boolean|null The section, or false if no such section
380 * exist, or null if sections are not supported.
382 public function getSection( $sectionId );
385 * Replaces a section of the content and returns a Content object with the
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
395 public function replaceSection( $section, Content
$with, $sectionTitle = '' );
398 * Returns a Content object with pre-save transformations applied (or this
399 * object if no transformations apply).
403 * @param $title Title
405 * @param $parserOptions null|ParserOptions
408 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$parserOptions );
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.
417 * @param $header string
420 public function addSectionHeader( $header );
423 * Returns a Content object with preload transformations applied (or this
424 * object if no transformations apply).
428 * @param $title Title
429 * @param $parserOptions null|ParserOptions
432 public function preloadTransform( Title
$title, ParserOptions
$parserOptions );
435 * Prepare Content for saving. Called before Content is saved by WikiPage::doEditContent() and in
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.
441 * Note that this method will usually be called inside the same transaction
442 * bracket that will be used to save the new revision.
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.
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
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.
458 * @see see WikiPage::doEditContent()
460 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user );
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.
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.
474 * @return array A list of DataUpdate instances that will clean up the
475 * database after deletion.
477 public function getDeletionUpdates( WikiPage
$page,
478 ParserOutput
$parserOutput = null );
481 * Returns true if this Content object matches the given magic word.
485 * @param MagicWord $word the magic word to match
487 * @return bool whether this Content object matches the given magic word.
489 public function matchMagicWord( MagicWord
$word );
492 * Converts this content object into another content object with the given content model,
493 * if that is possible.
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.
499 * @return Content|bool A content object with the content model $toModel, or false if
500 * that conversion is not supported.
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