Merge "Skin: Make skins aware of their registered skin name"
[lhc/web/wiklou.git] / includes / content / TextContent.php
1 <?php
2 /**
3 * Content object implementation for representing flat text.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
28 /**
29 * Content object implementation for representing flat text.
30 *
31 * TextContent instances are immutable
32 *
33 * @ingroup Content
34 */
35 class TextContent extends AbstractContent {
36
37 /**
38 * @var string
39 */
40 protected $mText;
41
42 /**
43 * @param string $text
44 * @param string $model_id
45 * @throws MWException
46 */
47 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
48 parent::__construct( $model_id );
49
50 if ( $text === null || $text === false ) {
51 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
52 . "This may indicate an error in the caller's scope.", 2 );
53
54 $text = '';
55 }
56
57 if ( !is_string( $text ) ) {
58 throw new MWException( "TextContent expects a string in the constructor." );
59 }
60
61 $this->mText = $text;
62 }
63
64 /**
65 * @note Mutable subclasses MUST override this to return a copy!
66 *
67 * @return Content $this
68 */
69 public function copy() {
70 return $this; # NOTE: this is ok since TextContent are immutable.
71 }
72
73 public function getTextForSummary( $maxlength = 250 ) {
74 global $wgContLang;
75
76 $text = $this->getNativeData();
77
78 $truncatedtext = $wgContLang->truncate(
79 preg_replace( "/[\n\r]/", ' ', $text ),
80 max( 0, $maxlength ) );
81
82 return $truncatedtext;
83 }
84
85 /**
86 * Returns the text's size in bytes.
87 *
88 * @return int
89 */
90 public function getSize() {
91 $text = $this->getNativeData();
92
93 return strlen( $text );
94 }
95
96 /**
97 * Returns true if this content is not a redirect, and $wgArticleCountMethod
98 * is "any".
99 *
100 * @param bool|null $hasLinks If it is known whether this content contains links,
101 * provide this information here, to avoid redundant parsing to find out.
102 *
103 * @return bool
104 */
105 public function isCountable( $hasLinks = null ) {
106 global $wgArticleCountMethod;
107
108 if ( $this->isRedirect() ) {
109 return false;
110 }
111
112 if ( $wgArticleCountMethod === 'any' ) {
113 return true;
114 }
115
116 return false;
117 }
118
119 /**
120 * Returns the text represented by this Content object, as a string.
121 *
122 * @return string The raw text.
123 */
124 public function getNativeData() {
125 return $this->mText;
126 }
127
128 /**
129 * Returns the text represented by this Content object, as a string.
130 *
131 * @return string The raw text.
132 */
133 public function getTextForSearchIndex() {
134 return $this->getNativeData();
135 }
136
137 /**
138 * Returns attempts to convert this content object to wikitext,
139 * and then returns the text string. The conversion may be lossy.
140 *
141 * @note this allows any text-based content to be transcluded as if it was wikitext.
142 *
143 * @return string|bool The raw text, or false if the conversion failed.
144 */
145 public function getWikitextForTransclusion() {
146 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
147
148 if ( $wikitext ) {
149 return $wikitext->getNativeData();
150 } else {
151 return false;
152 }
153 }
154
155 /**
156 * Do a "\r\n" -> "\n" and "\r" -> "\n" transformation
157 * as well as trim trailing whitespace
158 *
159 * This was formerly part of Parser::preSaveTransform, but
160 * for non-wikitext content models they probably still want
161 * to normalize line endings without all of the other PST
162 * changes.
163 *
164 * @since 1.28
165 * @param string $text
166 * @return string
167 */
168 public static function normalizeLineEndings( $text ) {
169 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
170 }
171
172 /**
173 * Returns a Content object with pre-save transformations applied.
174 *
175 * At a minimum, subclasses should make sure to call TextContent::normalizeLineEndings()
176 * either directly or part of Parser::preSaveTransform().
177 *
178 * @param Title $title
179 * @param User $user
180 * @param ParserOptions $popts
181 *
182 * @return Content
183 */
184 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
185 $text = $this->getNativeData();
186 $pst = self::normalizeLineEndings( $text );
187
188 return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
189 }
190
191 /**
192 * Diff this content object with another content object.
193 *
194 * @since 1.21
195 *
196 * @param Content $that The other content object to compare this content object to.
197 * @param Language $lang The language object to use for text segmentation.
198 * If not given, $wgContentLang is used.
199 *
200 * @return Diff A diff representing the changes that would have to be
201 * made to this content object to make it equal to $that.
202 */
203 public function diff( Content $that, Language $lang = null ) {
204 global $wgContLang;
205
206 $this->checkModelID( $that->getModel() );
207
208 // @todo could implement this in DifferenceEngine and just delegate here?
209
210 if ( !$lang ) {
211 $lang = $wgContLang;
212 }
213
214 $otext = $this->getNativeData();
215 $ntext = $that->getNativeData();
216
217 # Note: Use native PHP diff, external engines don't give us abstract output
218 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
219 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
220
221 $diff = new Diff( $ota, $nta );
222
223 return $diff;
224 }
225
226 /**
227 * Fills the provided ParserOutput object with information derived from the content.
228 * Unless $generateHtml was false, this includes an HTML representation of the content
229 * provided by getHtml().
230 *
231 * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
232 * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
233 *
234 * Subclasses may override this to provide custom content processing.
235 * For custom HTML generation alone, it is sufficient to override getHtml().
236 *
237 * @param Title $title Context title for parsing
238 * @param int $revId Revision ID (for {{REVISIONID}})
239 * @param ParserOptions $options Parser options
240 * @param bool $generateHtml Whether or not to generate HTML
241 * @param ParserOutput &$output The output object to fill (reference).
242 */
243 protected function fillParserOutput( Title $title, $revId,
244 ParserOptions $options, $generateHtml, ParserOutput &$output
245 ) {
246 global $wgParser, $wgTextModelsToParse;
247
248 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
249 // parse just to get links etc into the database, HTML is replaced below.
250 $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
251 }
252
253 if ( $generateHtml ) {
254 $html = $this->getHtml();
255 } else {
256 $html = '';
257 }
258
259 $output->setText( $html );
260 }
261
262 /**
263 * Generates an HTML version of the content, for display. Used by
264 * fillParserOutput() to provide HTML for the ParserOutput object.
265 *
266 * Subclasses may override this to provide a custom HTML rendering.
267 * If further information is to be derived from the content (such as
268 * categories), the fillParserOutput() method can be overridden instead.
269 *
270 * For backwards-compatibility, this default implementation just calls
271 * getHighlightHtml().
272 *
273 * @return string An HTML representation of the content
274 */
275 protected function getHtml() {
276 return $this->getHighlightHtml();
277 }
278
279 /**
280 * Generates an HTML version of the content, for display.
281 *
282 * This default implementation returns an HTML-escaped version
283 * of the raw text content.
284 *
285 * @note The functionality of this method should really be implemented
286 * in getHtml(), and subclasses should override getHtml() if needed.
287 * getHighlightHtml() is kept around for backward compatibility with
288 * extensions that already override it.
289 *
290 * @deprecated since 1.24. Use getHtml() instead. In particular, subclasses overriding
291 * getHighlightHtml() should override getHtml() instead.
292 *
293 * @return string An HTML representation of the content
294 */
295 protected function getHighlightHtml() {
296 return htmlspecialchars( $this->getNativeData() );
297 }
298
299 /**
300 * This implementation provides lossless conversion between content models based
301 * on TextContent.
302 *
303 * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags.
304 * @param string $lossy Flag, set to "lossy" to allow lossy conversion. If lossy conversion is not
305 * allowed, full round-trip conversion is expected to work without losing information.
306 *
307 * @return Content|bool A content object with the content model $toModel, or false if that
308 * conversion is not supported.
309 *
310 * @see Content::convert()
311 */
312 public function convert( $toModel, $lossy = '' ) {
313 $converted = parent::convert( $toModel, $lossy );
314
315 if ( $converted !== false ) {
316 return $converted;
317 }
318
319 $toHandler = ContentHandler::getForModelID( $toModel );
320
321 if ( $toHandler instanceof TextContentHandler ) {
322 // NOTE: ignore content serialization format - it's just text anyway.
323 $text = $this->getNativeData();
324 $converted = $toHandler->unserializeContent( $text );
325 }
326
327 return $converted;
328 }
329
330 }