3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Content
;
23 use MediaWiki\Parser\MagicWord
;
24 use MediaWiki\Title\Title
;
27 * Base interface for representing page content.
29 * A content object represents page content, e.g. the text to show on a page.
30 * Content objects have no knowledge about how they relate to wiki pages.
32 * Must not be implemented directly by extensions, extend AbstractContent instead.
37 * @author Daniel Kinzler
44 * @return string A string representing the content in a way useful for
45 * building a full text search index. If no useful representation exists,
46 * this method returns an empty string.
48 * @todo Test that this actually works
49 * @todo Make sure this also works with LuceneSearch / WikiSearch
51 public function getTextForSearchIndex();
56 * @return string|false The wikitext to include when another page includes this
57 * content, or false if the content is not includable in a wikitext page.
59 * @todo Allow native handling, bypassing wikitext representation, like
60 * for includable special pages.
61 * @todo Allow transclusion into other content models than Wikitext!
62 * @todo Used in WikiPage and MessageCache to get message text. Not so
63 * nice. What should we use instead?!
65 public function getWikitextForTransclusion();
68 * Returns a textual representation of the content suitable for use in edit
69 * summaries and log messages.
73 * @param int $maxLength Maximum length of the summary text, in bytes.
74 * Usually implemented using {@link Language::truncateForDatabase()}.
76 * @return string The summary text.
78 public function getTextForSummary( $maxLength = 250 );
81 * Returns native representation of the data. Interpretation depends on
82 * the data model used, as given by getDataModel().
86 * @deprecated since 1.33 use getText() for TextContent instances.
87 * For other content models, use specialized getters.
89 * @return mixed The native representation of the content. Could be a
90 * string, a nested array structure, an object, a binary blob...
93 * @note Caller must be aware of content model!
95 public function getNativeData();
98 * Returns the content's nominal size in "bogo-bytes".
102 public function getSize();
105 * Returns the ID of the content model used by this Content object.
106 * Corresponds to the CONTENT_MODEL_XXX constants.
110 * @return string The model id
112 public function getModel();
115 * Convenience method that returns the ContentHandler singleton for handling
116 * the content model that this Content object uses.
118 * Shorthand for ContentHandler::getForContent( $this )
122 * @return ContentHandler
124 public function getContentHandler();
127 * Convenience method that returns the default serialization format for the
128 * content model that this Content object uses.
130 * Shorthand for $this->getContentHandler()->getDefaultFormat()
136 public function getDefaultFormat();
139 * Convenience method that returns the list of serialization formats
140 * supported for the content model that this Content object uses.
142 * Shorthand for $this->getContentHandler()->getSupportedFormats()
146 * @return string[] List of supported serialization formats
148 public function getSupportedFormats();
151 * Returns true if $format is a supported serialization format for this
152 * Content object, false if it isn't.
154 * Note that this should always return true if $format is null, because null
155 * stands for the default serialization.
157 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
161 * @param string $format The serialization format to check.
163 * @return bool Whether the format is supported
165 public function isSupportedFormat( $format );
168 * Convenience method for serializing this Content object.
170 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
174 * @param string|null $format The desired serialization format, or null for the default format.
176 * @return string Serialized form of this Content object.
178 public function serialize( $format = null );
181 * Returns true if this Content object represents empty content.
185 * @return bool Whether this Content object is empty
187 public function isEmpty();
190 * Returns whether the content is valid. This is intended for local validity
191 * checks, not considering global consistency.
193 * Content needs to be valid before it can be saved.
195 * This default implementation always returns true.
201 public function isValid();
204 * Returns true if this Content objects is conceptually equivalent to the
205 * given Content object.
209 * - Will return false if $that is null.
210 * - Will return true if $that === $this.
211 * - Will return false if $that->getModel() !== $this->getModel().
212 * - Will return false if get_class( $that ) !== get_class( $this )
213 * - Should return false if $that->getModel() == $this->getModel() and
214 * $that is not semantically equivalent to $this, according to
215 * the data model defined by $this->getModel().
217 * Implementations should be careful to make equals() transitive and reflexive:
219 * - $a->equals( $b ) <=> $b->equals( $a )
220 * - $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
224 * @param Content|null $that The Content object to compare to.
226 * @return bool True if this Content object is equal to $that, false otherwise.
228 public function equals( ?Content
$that = null );
231 * Return a copy of this Content object. The following must be true for the
234 * if $copy = $original->copy()
236 * - get_class($original) === get_class($copy)
237 * - $original->getModel() === $copy->getModel()
238 * - $original->equals( $copy )
240 * If and only if the Content object is immutable, the copy() method can and
241 * should return $this. That is, $copy === $original may be true, but only
242 * for immutable content objects.
246 * @return Content A copy of this object
248 public function copy();
251 * Returns true if this content is countable as a "real" wiki page, provided
252 * that it's also in a countable location (e.g. a current revision in the
255 * @see SlotRoleHandler::supportsArticleCount
259 * @param bool|null $hasLinks If it is known whether this content contains
260 * links, provide this information here, to avoid redundant parsing to
265 public function isCountable( $hasLinks = null );
268 * Construct the redirect destination from this content and return a Title,
269 * or null if this content doesn't represent a redirect.
275 public function getRedirectTarget();
278 * Returns whether this Content represents a redirect.
279 * Shorthand for getRedirectTarget() !== null.
281 * @see SlotRoleHandler::supportsRedirects
287 public function isRedirect();
290 * If this Content object is a redirect, this method updates the redirect target.
291 * Otherwise, it does nothing.
295 * @param Title $target The new redirect target
297 * @return Content A new Content object with the updated redirect (or $this
298 * if this Content object isn't a redirect)
300 public function updateRedirect( Title
$target );
303 * Returns the section with the given ID.
307 * @param string|int $sectionId Section identifier as a number or string
308 * (e.g. 0, 1 or 'T-1'). The ID "0" retrieves the section before the first heading, "1" the
309 * text between the first heading (included) and the second heading (excluded), etc.
311 * @return Content|false|null The section, or false if no such section
312 * exist, or null if sections are not supported.
314 public function getSection( $sectionId );
317 * Replaces a section of the content and returns a Content object with the
322 * @param string|int|null|false $sectionId Section identifier as a number or string
323 * (e.g. 0, 1 or 'T-1'), null/false or an empty string for the whole page
324 * or 'new' for a new section.
325 * @param Content $with New content of the section
326 * @param string $sectionTitle New section's subject, only if $section is 'new'
328 * @return Content|null New content of the entire page, or null if error
330 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' );
333 * Returns a new WikitextContent object with the given section heading
334 * prepended, if supported. The default implementation just returns this
335 * Content object unmodified, ignoring the section header.
339 * @param string $header
343 public function addSectionHeader( $header );
346 * Returns true if this Content object matches the given magic word.
350 * @param MagicWord $word The magic word to match
352 * @return bool Whether this Content object matches the given magic word.
354 public function matchMagicWord( MagicWord
$word );
357 * Converts this content object into another content object with the given content model,
358 * if that is possible.
360 * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags.
361 * @param string $lossy Optional flag, set to "lossy" to allow lossy conversion. If lossy
362 * conversion is not allowed, full round-trip conversion is expected to work without losing
365 * @return Content|false A content object with the content model $toModel, or false if
366 * that conversion is not supported.
368 public function convert( $toModel, $lossy = '' );
370 // @todo ImagePage and CategoryPage interfere with per-content action handlers
371 // @todo nice integration of GeSHi syntax highlighting
372 // [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a
373 // config to set the class which handles syntax highlighting
374 // [12:00] <vvv> And default it to a DummyHighlighter
378 /** @deprecated class alias since 1.43 */
379 class_alias( Content
::class, 'Content' );