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
25 * @author Daniel Kinzler
29 * Content object implementation for representing flat text.
31 * TextContent instances are immutable
35 class TextContent
extends AbstractContent
{
44 * @param string $model_id
47 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
48 parent
::__construct( $model_id );
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 );
57 if ( !is_string( $text ) ) {
58 throw new MWException( "TextContent expects a string in the constructor." );
65 * @note Mutable subclasses MUST override this to return a copy!
67 * @return Content $this
69 public function copy() {
70 return $this; # NOTE: this is ok since TextContent are immutable.
73 public function getTextForSummary( $maxlength = 250 ) {
76 $text = $this->getNativeData();
78 $truncatedtext = $wgContLang->truncateForDatabase(
79 preg_replace( "/[\n\r]/", ' ', $text ),
80 max( 0, $maxlength ) );
82 return $truncatedtext;
86 * Returns the text's size in bytes.
90 public function getSize() {
91 $text = $this->getNativeData();
93 return strlen( $text );
97 * Returns true if this content is not a redirect, and $wgArticleCountMethod
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.
105 public function isCountable( $hasLinks = null ) {
106 global $wgArticleCountMethod;
108 if ( $this->isRedirect() ) {
112 if ( $wgArticleCountMethod === 'any' ) {
120 * Returns the text represented by this Content object, as a string.
122 * @return string The raw text.
124 public function getNativeData() {
129 * Returns the text represented by this Content object, as a string.
131 * @return string The raw text.
133 public function getTextForSearchIndex() {
134 return $this->getNativeData();
138 * Returns attempts to convert this content object to wikitext,
139 * and then returns the text string. The conversion may be lossy.
141 * @note this allows any text-based content to be transcluded as if it was wikitext.
143 * @return string|bool The raw text, or false if the conversion failed.
145 public function getWikitextForTransclusion() {
146 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT
, 'lossy' );
149 return $wikitext->getNativeData();
156 * Do a "\r\n" -> "\n" and "\r" -> "\n" transformation
157 * as well as trim trailing whitespace
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
165 * @param string $text
168 public static function normalizeLineEndings( $text ) {
169 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
173 * Returns a Content object with pre-save transformations applied.
175 * At a minimum, subclasses should make sure to call TextContent::normalizeLineEndings()
176 * either directly or part of Parser::preSaveTransform().
178 * @param Title $title
180 * @param ParserOptions $popts
184 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
185 $text = $this->getNativeData();
186 $pst = self
::normalizeLineEndings( $text );
188 return ( $text === $pst ) ?
$this : new static( $pst, $this->getModel() );
192 * Diff this content object with another content object.
196 * @param Content $that The other content object to compare this content object to.
197 * @param Language|null $lang The language object to use for text segmentation.
198 * If not given, $wgContentLang is used.
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.
203 public function diff( Content
$that, Language
$lang = null ) {
206 $this->checkModelID( $that->getModel() );
208 // @todo could implement this in DifferenceEngine and just delegate here?
214 $otext = $this->getNativeData();
215 $ntext = $that->getNativeData();
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 ) );
221 $diff = new Diff( $ota, $nta );
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().
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.
234 * Subclasses may override this to provide custom content processing.
235 * For custom HTML generation alone, it is sufficient to override getHtml().
237 * @param Title $title Context title for parsing
238 * @param int $revId Revision ID (for {{REVISIONID}})
239 * @param ParserOptions $options
240 * @param bool $generateHtml Whether or not to generate HTML
241 * @param ParserOutput &$output The output object to fill (reference).
243 protected function fillParserOutput( Title
$title, $revId,
244 ParserOptions
$options, $generateHtml, ParserOutput
&$output
246 global $wgParser, $wgTextModelsToParse;
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 );
253 if ( $generateHtml ) {
254 $html = $this->getHtml();
259 $output->setText( $html );
263 * Generates an HTML version of the content, for display. Used by
264 * fillParserOutput() to provide HTML for the ParserOutput object.
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.
270 * For backwards-compatibility, this default implementation just calls
271 * getHighlightHtml().
273 * @return string An HTML representation of the content
275 protected function getHtml() {
276 return $this->getHighlightHtml();
280 * Generates an HTML version of the content, for display.
282 * This default implementation returns an HTML-escaped version
283 * of the raw text content.
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.
290 * @deprecated since 1.24. Use getHtml() instead. In particular, subclasses overriding
291 * getHighlightHtml() should override getHtml() instead.
293 * @return string An HTML representation of the content
295 protected function getHighlightHtml() {
296 return htmlspecialchars( $this->getNativeData() );
300 * This implementation provides lossless conversion between content models based
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.
307 * @return Content|bool A content object with the content model $toModel, or false if that
308 * conversion is not supported.
310 * @see Content::convert()
312 public function convert( $toModel, $lossy = '' ) {
313 $converted = parent
::convert( $toModel, $lossy );
315 if ( $converted !== false ) {
319 $toHandler = ContentHandler
::getForModelID( $toModel );
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 );