Merge "Fix method declaration in UploadFromStash"
[mediawiki.git] / includes / content / Content.php
blob35b51c346f9beb9d2464cc4ed42e8b6acb9957d6
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.
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
21 * @since 1.21
23 * @file
24 * @ingroup Content
26 * @author Daniel Kinzler
28 interface Content {
30 /**
31 * @since 1.21
33 * @return string A string representing the content in a way useful for
34 * building a full text search index. If no useful representation exists,
35 * this method returns an empty string.
37 * @todo: test that this actually works
38 * @todo: make sure this also works with LuceneSearch / WikiSearch
40 public function getTextForSearchIndex();
42 /**
43 * @since 1.21
45 * @return string|false The wikitext to include when another page includes this
46 * content, or false if the content is not includable in a wikitext page.
48 * @todo allow native handling, bypassing wikitext representation, like
49 * for includable special pages.
50 * @todo allow transclusion into other content models than Wikitext!
51 * @todo used in WikiPage and MessageCache to get message text. Not so
52 * nice. What should we use instead?!
54 public function getWikitextForTransclusion();
56 /**
57 * Returns a textual representation of the content suitable for use in edit
58 * summaries and log messages.
60 * @since 1.21
62 * @param $maxLength int Maximum length of the summary text
63 * @return string The summary text
65 public function getTextForSummary( $maxLength = 250 );
67 /**
68 * Returns native representation of the data. Interpretation depends on
69 * the data model used, as given by getDataModel().
71 * @since 1.21
73 * @return mixed The native representation of the content. Could be a
74 * string, a nested array structure, an object, a binary blob...
75 * anything, really.
77 * @note Caller must be aware of content model!
79 public function getNativeData();
81 /**
82 * Returns the content's nominal size in bogo-bytes.
84 * @return int
86 public function getSize();
88 /**
89 * Returns the ID of the content model used by this Content object.
90 * Corresponds to the CONTENT_MODEL_XXX constants.
92 * @since 1.21
94 * @return String The model id
96 public function getModel();
98 /**
99 * Convenience method that returns the ContentHandler singleton for handling
100 * the content model that this Content object uses.
102 * Shorthand for ContentHandler::getForContent( $this )
104 * @since 1.21
106 * @return ContentHandler
108 public function getContentHandler();
111 * Convenience method that returns the default serialization format for the
112 * content model that this Content object uses.
114 * Shorthand for $this->getContentHandler()->getDefaultFormat()
116 * @since 1.21
118 * @return String
120 public function getDefaultFormat();
123 * Convenience method that returns the list of serialization formats
124 * supported for the content model that this Content object uses.
126 * Shorthand for $this->getContentHandler()->getSupportedFormats()
128 * @since 1.21
130 * @return Array of supported serialization formats
132 public function getSupportedFormats();
135 * Returns true if $format is a supported serialization format for this
136 * Content object, false if it isn't.
138 * Note that this should always return true if $format is null, because null
139 * stands for the default serialization.
141 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
143 * @since 1.21
145 * @param $format string The format to check
146 * @return bool Whether the format is supported
148 public function isSupportedFormat( $format );
151 * Convenience method for serializing this Content object.
153 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
155 * @since 1.21
157 * @param $format null|string The desired serialization format (or null for
158 * the default format).
159 * @return string Serialized form of this Content object
161 public function serialize( $format = null );
164 * Returns true if this Content object represents empty content.
166 * @since 1.21
168 * @return bool Whether this Content object is empty
170 public function isEmpty();
173 * Returns whether the content is valid. This is intended for local validity
174 * checks, not considering global consistency.
176 * Content needs to be valid before it can be saved.
178 * This default implementation always returns true.
180 * @since 1.21
182 * @return boolean
184 public function isValid();
187 * Returns true if this Content objects is conceptually equivalent to the
188 * given Content object.
190 * Contract:
192 * - Will return false if $that is null.
193 * - Will return true if $that === $this.
194 * - Will return false if $that->getModel() != $this->getModel().
195 * - Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
196 * where the meaning of "equal" depends on the actual data model.
198 * Implementations should be careful to make equals() transitive and reflexive:
200 * - $a->equals( $b ) <=> $b->equals( $a )
201 * - $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
203 * @since 1.21
205 * @param $that Content The Content object to compare to
206 * @return bool True if this Content object is equal to $that, false otherwise.
208 public function equals( Content $that = null );
211 * Return a copy of this Content object. The following must be true for the
212 * object returned:
214 * if $copy = $original->copy()
216 * - get_class($original) === get_class($copy)
217 * - $original->getModel() === $copy->getModel()
218 * - $original->equals( $copy )
220 * If and only if the Content object is immutable, the copy() method can and
221 * should return $this. That is, $copy === $original may be true, but only
222 * for immutable content objects.
224 * @since 1.21
226 * @return Content. A copy of this object
228 public function copy( );
231 * Returns true if this content is countable as a "real" wiki page, provided
232 * that it's also in a countable location (e.g. a current revision in the
233 * main namespace).
235 * @since 1.21
237 * @param $hasLinks Bool: If it is known whether this content contains
238 * links, provide this information here, to avoid redundant parsing to
239 * find out.
240 * @return boolean
242 public function isCountable( $hasLinks = null );
245 * Parse the Content object and generate a ParserOutput from the result.
246 * $result->getText() can be used to obtain the generated HTML. If no HTML
247 * is needed, $generateHtml can be set to false; in that case,
248 * $result->getText() may return null.
250 * @param $title Title The page title to use as a context for rendering
251 * @param $revId null|int The revision being rendered (optional)
252 * @param $options null|ParserOptions Any parser options
253 * @param $generateHtml Boolean Whether to generate HTML (default: true). If false,
254 * the result of calling getText() on the ParserOutput object returned by
255 * this method is undefined.
257 * @since 1.21
259 * @return ParserOutput
261 public function getParserOutput( Title $title,
262 $revId = null,
263 ParserOptions $options = null, $generateHtml = true );
264 // TODO: make RenderOutput and RenderOptions base classes
267 * Returns a list of DataUpdate objects for recording information about this
268 * Content in some secondary data store. If the optional second argument,
269 * $old, is given, the updates may model only the changes that need to be
270 * made to replace information about the old content with information about
271 * the new content.
273 * This default implementation calls
274 * $this->getParserOutput( $content, $title, null, null, false ),
275 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
276 * resulting ParserOutput object.
278 * Subclasses may implement this to determine the necessary updates more
279 * efficiently, or make use of information about the old content.
281 * @param $title Title The context for determining the necessary updates
282 * @param $old Content|null An optional Content object representing the
283 * previous content, i.e. the content being replaced by this Content
284 * object.
285 * @param $recursive boolean Whether to include recursive updates (default:
286 * false).
287 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
288 * Provide if you have one handy, to avoid re-parsing of the content.
290 * @return Array. A list of DataUpdate objects for putting information
291 * about this content object somewhere.
293 * @since 1.21
295 public function getSecondaryDataUpdates( Title $title,
296 Content $old = null,
297 $recursive = true, ParserOutput $parserOutput = null
301 * Construct the redirect destination from this content and return an
302 * array of Titles, or null if this content doesn't represent a redirect.
303 * The last element in the array is the final destination after all redirects
304 * have been resolved (up to $wgMaxRedirects times).
306 * @since 1.21
308 * @return Array of Titles, with the destination last
310 public function getRedirectChain();
313 * Construct the redirect destination from this content and return a Title,
314 * or null if this content doesn't represent a redirect.
315 * This will only return the immediate redirect target, useful for
316 * the redirect table and other checks that don't need full recursion.
318 * @since 1.21
320 * @return Title: The corresponding Title
322 public function getRedirectTarget();
325 * Construct the redirect destination from this content and return the
326 * Title, or null if this content doesn't represent a redirect.
328 * This will recurse down $wgMaxRedirects times or until a non-redirect
329 * target is hit in order to provide (hopefully) the Title of the final
330 * destination instead of another redirect.
332 * There is usually no need to override the default behaviour, subclasses that
333 * want to implement redirects should override getRedirectTarget().
335 * @since 1.21
337 * @return Title
339 public function getUltimateRedirectTarget();
342 * Returns whether this Content represents a redirect.
343 * Shorthand for getRedirectTarget() !== null.
345 * @since 1.21
347 * @return bool
349 public function isRedirect();
352 * If this Content object is a redirect, this method updates the redirect target.
353 * Otherwise, it does nothing.
355 * @since 1.21
357 * @param Title $target the new redirect target
359 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
361 public function updateRedirect( Title $target );
364 * Returns the section with the given ID.
366 * @since 1.21
368 * @param $sectionId string The section's ID, given as a numeric string.
369 * The ID "0" retrieves the section before the first heading, "1" the
370 * text between the first heading (included) and the second heading
371 * (excluded), etc.
372 * @return Content|Boolean|null The section, or false if no such section
373 * exist, or null if sections are not supported.
375 public function getSection( $sectionId );
378 * Replaces a section of the content and returns a Content object with the
379 * section replaced.
381 * @since 1.21
383 * @param $section null/false or a section number (0, 1, 2, T1, T2...), or "new"
384 * @param $with Content: new content of the section
385 * @param $sectionTitle String: new section's subject, only if $section is 'new'
386 * @return string Complete article text, or null if error
388 public function replaceSection( $section, Content $with, $sectionTitle = '' );
391 * Returns a Content object with pre-save transformations applied (or this
392 * object if no transformations apply).
394 * @since 1.21
396 * @param $title Title
397 * @param $user User
398 * @param $parserOptions null|ParserOptions
399 * @return Content
401 public function preSaveTransform( Title $title, User $user, ParserOptions $parserOptions );
404 * Returns a new WikitextContent object with the given section heading
405 * prepended, if supported. The default implementation just returns this
406 * Content object unmodified, ignoring the section header.
408 * @since 1.21
410 * @param $header string
411 * @return Content
413 public function addSectionHeader( $header );
416 * Returns a Content object with preload transformations applied (or this
417 * object if no transformations apply).
419 * @since 1.21
421 * @param $title Title
422 * @param $parserOptions null|ParserOptions
423 * @return Content
425 public function preloadTransform( Title $title, ParserOptions $parserOptions );
428 * Prepare Content for saving. Called before Content is saved by WikiPage::doEditContent() and in
429 * similar places.
431 * This may be used to check the content's consistency with global state. This function should
432 * NOT write any information to the database.
434 * Note that this method will usually be called inside the same transaction bracket that will be used
435 * to save the new revision.
437 * Note that this method is called before any update to the page table is performed. This means that
438 * $page may not yet know a page ID.
440 * @since 1.21
442 * @param WikiPage $page The page to be saved.
443 * @param int $flags bitfield for use with EDIT_XXX constants, see WikiPage::doEditContent()
444 * @param int $baseRevId the ID of the current revision
445 * @param User $user
447 * @return Status A status object indicating whether the content was successfully prepared for saving.
448 * If the returned status indicates an error, a rollback will be performed and the
449 * transaction aborted.
451 * @see see WikiPage::doEditContent()
453 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user );
456 * Returns a list of updates to perform when this content is deleted.
457 * The necessary updates may be taken from the Content object, or depend on
458 * the current state of the database.
460 * @since 1.21
462 * @param $page WikiPage the deleted page
463 * @param $parserOutput null|ParserOutput optional parser output object
464 * for efficient access to meta-information about the content object.
465 * Provide if you have one handy.
467 * @return array A list of DataUpdate instances that will clean up the
468 * database after deletion.
470 public function getDeletionUpdates( WikiPage $page,
471 ParserOutput $parserOutput = null );
474 * Returns true if this Content object matches the given magic word.
476 * @since 1.21
478 * @param MagicWord $word the magic word to match
480 * @return bool whether this Content object matches the given magic word.
482 public function matchMagicWord( MagicWord $word );
485 * Converts this content object into another content object with the given content model,
486 * if that is possible.
488 * @param String $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
489 * @param String $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
490 * not allowed, full round-trip conversion is expected to work without losing information.
492 * @return Content|bool A content object with the content model $toModel, or false if
493 * that conversion is not supported.
495 public function convert( $toModel, $lossy = '' );
497 // TODO: ImagePage and CategoryPage interfere with per-content action handlers
498 // TODO: nice&sane integration of GeSHi syntax highlighting
499 // [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a
500 // config to set the class which handles syntax highlighting
501 // [12:00] <vvv> And default it to a DummyHighlighter