Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / content / TextContent.php
blob225522e92d1dee40d447a21e455e9f22fa5f56b2
1 <?php
2 /**
3 * Content object implementation for representing flat text.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @since 1.21
22 * @file
23 * @ingroup Content
25 * @author Daniel Kinzler
28 /**
29 * Content object implementation for representing flat text.
31 * TextContent instances are immutable
33 * @ingroup Content
35 class TextContent extends AbstractContent {
37 /**
38 * @param string $text
39 * @param string $model_id
40 * @throws MWException
42 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
43 parent::__construct( $model_id );
45 if ( $text === null || $text === false ) {
46 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
47 . "This may indicate an error in the caller's scope.", 2 );
49 $text = '';
52 if ( !is_string( $text ) ) {
53 throw new MWException( "TextContent expects a string in the constructor." );
56 $this->mText = $text;
59 /**
60 * @note Mutable subclasses MUST override this to return a copy!
62 * @return Content $this
64 public function copy() {
65 return $this; # NOTE: this is ok since TextContent are immutable.
68 public function getTextForSummary( $maxlength = 250 ) {
69 global $wgContLang;
71 $text = $this->getNativeData();
73 $truncatedtext = $wgContLang->truncate(
74 preg_replace( "/[\n\r]/", ' ', $text ),
75 max( 0, $maxlength ) );
77 return $truncatedtext;
80 /**
81 * Returns the text's size in bytes.
83 * @return int
85 public function getSize() {
86 $text = $this->getNativeData();
88 return strlen( $text );
91 /**
92 * Returns true if this content is not a redirect, and $wgArticleCountMethod
93 * is "any".
95 * @param bool|null $hasLinks If it is known whether this content contains links,
96 * provide this information here, to avoid redundant parsing to find out.
98 * @return bool
100 public function isCountable( $hasLinks = null ) {
101 global $wgArticleCountMethod;
103 if ( $this->isRedirect() ) {
104 return false;
107 if ( $wgArticleCountMethod === 'any' ) {
108 return true;
111 return false;
115 * Returns the text represented by this Content object, as a string.
117 * @return string The raw text.
119 public function getNativeData() {
120 return $this->mText;
124 * Returns the text represented by this Content object, as a string.
126 * @return string The raw text.
128 public function getTextForSearchIndex() {
129 return $this->getNativeData();
133 * Returns attempts to convert this content object to wikitext,
134 * and then returns the text string. The conversion may be lossy.
136 * @note this allows any text-based content to be transcluded as if it was wikitext.
138 * @return string|bool The raw text, or false if the conversion failed.
140 public function getWikitextForTransclusion() {
141 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
143 if ( $wikitext ) {
144 return $wikitext->getNativeData();
145 } else {
146 return false;
151 * Returns a Content object with pre-save transformations applied.
152 * This implementation just trims trailing whitespace.
154 * @param Title $title
155 * @param User $user
156 * @param ParserOptions $popts
158 * @return Content
160 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
161 $text = $this->getNativeData();
162 $pst = rtrim( $text );
164 return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
168 * Diff this content object with another content object.
170 * @since 1.21
172 * @param Content $that The other content object to compare this content object to.
173 * @param Language $lang The language object to use for text segmentation.
174 * If not given, $wgContentLang is used.
176 * @return Diff A diff representing the changes that would have to be
177 * made to this content object to make it equal to $that.
179 public function diff( Content $that, Language $lang = null ) {
180 global $wgContLang;
182 $this->checkModelID( $that->getModel() );
184 // @todo could implement this in DifferenceEngine and just delegate here?
186 if ( !$lang ) {
187 $lang = $wgContLang;
190 $otext = $this->getNativeData();
191 $ntext = $that->getNativeData();
193 # Note: Use native PHP diff, external engines don't give us abstract output
194 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
195 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
197 $diff = new Diff( $ota, $nta );
199 return $diff;
203 * Fills the provided ParserOutput object with information derived from the content.
204 * Unless $generateHtml was false, this includes an HTML representation of the content
205 * provided by getHtml().
207 * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
208 * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
210 * Subclasses may override this to provide custom content processing.
211 * For custom HTML generation alone, it is sufficient to override getHtml().
213 * @param Title $title Context title for parsing
214 * @param int $revId Revision ID (for {{REVISIONID}})
215 * @param ParserOptions $options Parser options
216 * @param bool $generateHtml Whether or not to generate HTML
217 * @param ParserOutput $output The output object to fill (reference).
219 protected function fillParserOutput( Title $title, $revId,
220 ParserOptions $options, $generateHtml, ParserOutput &$output
222 global $wgParser, $wgTextModelsToParse;
224 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
225 // parse just to get links etc into the database, HTML is replaced below.
226 $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
229 if ( $generateHtml ) {
230 $html = $this->getHtml();
231 } else {
232 $html = '';
235 $output->setText( $html );
239 * Generates an HTML version of the content, for display. Used by
240 * fillParserOutput() to provide HTML for the ParserOutput object.
242 * Subclasses may override this to provide a custom HTML rendering.
243 * If further information is to be derived from the content (such as
244 * categories), the fillParserOutput() method can be overridden instead.
246 * For backwards-compatibility, this default implementation just calls
247 * getHighlightHtml().
249 * @return string An HTML representation of the content
251 protected function getHtml() {
252 return $this->getHighlightHtml();
256 * Generates an HTML version of the content, for display.
258 * This default implementation returns an HTML-escaped version
259 * of the raw text content.
261 * @note The functionality of this method should really be implemented
262 * in getHtml(), and subclasses should override getHtml() if needed.
263 * getHighlightHtml() is kept around for backward compatibility with
264 * extensions that already override it.
266 * @deprecated since 1.24. Use getHtml() instead. In particular, subclasses overriding
267 * getHighlightHtml() should override getHtml() instead.
269 * @return string An HTML representation of the content
271 protected function getHighlightHtml() {
272 return htmlspecialchars( $this->getNativeData() );
276 * This implementation provides lossless conversion between content models based
277 * on TextContent.
279 * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags.
280 * @param string $lossy Flag, set to "lossy" to allow lossy conversion. If lossy conversion is not
281 * allowed, full round-trip conversion is expected to work without losing information.
283 * @return Content|bool A content object with the content model $toModel, or false if that
284 * conversion is not supported.
286 * @see Content::convert()
288 public function convert( $toModel, $lossy = '' ) {
289 $converted = parent::convert( $toModel, $lossy );
291 if ( $converted !== false ) {
292 return $converted;
295 $toHandler = ContentHandler::getForModelID( $toModel );
297 if ( $toHandler instanceof TextContentHandler ) {
298 // NOTE: ignore content serialization format - it's just text anyway.
299 $text = $this->getNativeData();
300 $converted = $toHandler->unserializeContent( $text );
303 return $converted;