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
{
36 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
37 parent
::__construct( $model_id );
39 if ( $text === null ||
$text === false ) {
40 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
41 . "This may indicate an error in the caller's scope.", 2 );
46 if ( !is_string( $text ) ) {
47 throw new MWException( "TextContent expects a string in the constructor." );
53 public function copy() {
54 return $this; # NOTE: this is ok since TextContent are immutable.
57 public function getTextForSummary( $maxlength = 250 ) {
60 $text = $this->getNativeData();
62 $truncatedtext = $wgContLang->truncate(
63 preg_replace( "/[\n\r]/", ' ', $text ),
64 max( 0, $maxlength ) );
66 return $truncatedtext;
70 * returns the text's size in bytes.
72 * @return int The size
74 public function getSize() {
75 $text = $this->getNativeData();
77 return strlen( $text );
81 * Returns true if this content is not a redirect, and $wgArticleCountMethod
84 * @param bool $hasLinks if it is known whether this content contains links,
85 * provide this information here, to avoid redundant parsing to find out.
87 * @return bool True if the content is countable
89 public function isCountable( $hasLinks = null ) {
90 global $wgArticleCountMethod;
92 if ( $this->isRedirect() ) {
96 if ( $wgArticleCountMethod === 'any' ) {
104 * Returns the text represented by this Content object, as a string.
106 * @return string: the raw text
108 public function getNativeData() {
109 $text = $this->mText
;
115 * Returns the text represented by this Content object, as a string.
117 * @return string: the raw text
119 public function getTextForSearchIndex() {
120 return $this->getNativeData();
124 * Returns attempts to convert this content object to wikitext,
125 * and then returns the text string. The conversion may be lossy.
127 * @note: this allows any text-based content to be transcluded as if it was wikitext.
129 * @return string|false: the raw text, or null if the conversion failed
131 public function getWikitextForTransclusion() {
132 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT
, 'lossy' );
135 return $wikitext->getNativeData();
142 * Returns a Content object with pre-save transformations applied.
143 * This implementation just trims trailing whitespace.
145 * @param $title Title
147 * @param $popts ParserOptions
150 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
151 $text = $this->getNativeData();
152 $pst = rtrim( $text );
154 return ( $text === $pst ) ?
$this : new static( $pst );
158 * Diff this content object with another content object.
162 * @param $that Content: The other content object to compare this content
164 * @param $lang Language: The language object to use for text segmentation.
165 * If not given, $wgContentLang is used.
167 * @return Diff A diff representing the changes that would have to be
168 * made to this content object to make it equal to $that.
170 public function diff( Content
$that, Language
$lang = null ) {
173 $this->checkModelID( $that->getModel() );
175 // @todo could implement this in DifferenceEngine and just delegate here?
181 $otext = $this->getNativeData();
182 $ntext = $that->getNativeData();
184 # Note: Use native PHP diff, external engines don't give us abstract output
185 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
186 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
188 $diff = new Diff( $ota, $nta );
194 * Returns a generic ParserOutput object, wrapping the HTML returned by
197 * @param $title Title Context title for parsing
198 * @param int|null $revId Revision ID (for {{REVISIONID}})
199 * @param $options ParserOptions|null Parser options
200 * @param bool $generateHtml Whether or not to generate HTML
202 * @return ParserOutput representing the HTML form of the text
204 public function getParserOutput( Title
$title,
206 ParserOptions
$options = null, $generateHtml = true
208 global $wgParser, $wgTextModelsToParse;
211 //NOTE: use canonical options per default to produce cacheable output
212 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
215 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
216 // parse just to get links etc into the database
217 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
219 $po = new ParserOutput();
222 if ( $generateHtml ) {
223 $html = $this->getHtml();
228 $po->setText( $html );
234 * Generates an HTML version of the content, for display. Used by
235 * getParserOutput() to construct a ParserOutput object.
237 * This default implementation just calls getHighlightHtml(). Content
238 * models that have another mapping to HTML (as is the case for markup
239 * languages like wikitext) should override this method to generate the
242 * @return string An HTML representation of the content
244 protected function getHtml() {
245 return $this->getHighlightHtml();
249 * Generates a syntax-highlighted version of the content, as HTML.
250 * Used by the default implementation of getHtml().
252 * @return string an HTML representation of the content's markup
254 protected function getHighlightHtml() {
255 # TODO: make Highlighter interface, use highlighter here, if available
256 return htmlspecialchars( $this->getNativeData() );
260 * @see Content::convert()
262 * This implementation provides lossless conversion between content models based
265 * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
266 * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
267 * not allowed, full round-trip conversion is expected to work without losing information.
269 * @return Content|bool A content object with the content model $toModel, or false if
270 * that conversion is not supported.
272 public function convert( $toModel, $lossy = '' ) {
273 $converted = parent
::convert( $toModel, $lossy );
275 if ( $converted !== false ) {
279 $toHandler = ContentHandler
::getForModelID( $toModel );
281 if ( $toHandler instanceof TextContentHandler
) {
282 //NOTE: ignore content serialization format - it's just text anyway.
283 $text = $this->getNativeData();
284 $converted = $toHandler->unserializeContent( $text );