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
{
37 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
38 parent
::__construct( $model_id );
40 if ( $text === null ||
$text === false ) {
41 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
42 . "This may indicate an error in the caller's scope." );
47 if ( !is_string( $text ) ) {
48 throw new MWException( "TextContent expects a string in the constructor." );
54 public function copy() {
55 return $this; # NOTE: this is ok since TextContent are immutable.
58 public function getTextForSummary( $maxlength = 250 ) {
61 $text = $this->getNativeData();
63 $truncatedtext = $wgContLang->truncate(
64 preg_replace( "/[\n\r]/", ' ', $text ),
65 max( 0, $maxlength ) );
67 return $truncatedtext;
71 * returns the text's size in bytes.
73 * @return int The size
75 public function getSize() {
76 $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
;
114 * Returns the text represented by this Content object, as a string.
116 * @return string: the raw text
118 public function getTextForSearchIndex() {
119 return $this->getNativeData();
123 * Returns attempts to convert this content object to wikitext,
124 * and then returns the text string. The conversion may be lossy.
126 * @note: this allows any text-based content to be transcluded as if it was wikitext.
128 * @return string|false: the raw text, or null if the conversion failed
130 public function getWikitextForTransclusion() {
131 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT
, 'lossy' );
134 return $wikitext->getNativeData();
141 * Returns a Content object with pre-save transformations applied.
142 * This implementation just trims trailing whitespace.
144 * @param $title Title
146 * @param $popts ParserOptions
149 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
150 $text = $this->getNativeData();
151 $pst = rtrim( $text );
153 return ( $text === $pst ) ?
$this : new WikitextContent( $pst );
157 * Diff this content object with another content object.
161 * @param $that Content: The other content object to compare this content
163 * @param $lang Language: The language object to use for text segmentation.
164 * If not given, $wgContentLang is used.
166 * @return DiffResult: A diff representing the changes that would have to be
167 * made to this content object to make it equal to $that.
169 public function diff( Content
$that, Language
$lang = null ) {
172 $this->checkModelID( $that->getModel() );
174 // @todo could implement this in DifferenceEngine and just delegate here?
180 $otext = $this->getNativeData();
181 $ntext = $this->getNativeData();
183 # Note: Use native PHP diff, external engines don't give us abstract output
184 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
185 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
187 $diff = new Diff( $ota, $nta );
192 * Returns a generic ParserOutput object, wrapping the HTML returned by
195 * @param $title Title Context title for parsing
196 * @param int|null $revId Revision ID (for {{REVISIONID}})
197 * @param $options ParserOptions|null Parser options
198 * @param bool $generateHtml Whether or not to generate HTML
200 * @return ParserOutput representing the HTML form of the text
202 public function getParserOutput( Title
$title,
204 ParserOptions
$options = null, $generateHtml = true
206 global $wgParser, $wgTextModelsToParse;
209 //NOTE: use canonical options per default to produce cacheable output
210 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
213 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
214 // parse just to get links etc into the database
215 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
217 $po = new ParserOutput();
220 if ( $generateHtml ) {
221 $html = $this->getHtml();
226 $po->setText( $html );
231 * Generates an HTML version of the content, for display. Used by
232 * getParserOutput() to construct a ParserOutput object.
234 * This default implementation just calls getHighlightHtml(). Content
235 * models that have another mapping to HTML (as is the case for markup
236 * languages like wikitext) should override this method to generate the
239 * @return string An HTML representation of the content
241 protected function getHtml() {
242 return $this->getHighlightHtml();
246 * Generates a syntax-highlighted version of the content, as HTML.
247 * Used by the default implementation of getHtml().
249 * @return string an HTML representation of the content's markup
251 protected function getHighlightHtml() {
252 # TODO: make Highlighter interface, use highlighter here, if available
253 return htmlspecialchars( $this->getNativeData() );
257 * @see Content::convert()
259 * This implementation provides lossless conversion between content models based
262 * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
263 * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
264 * not allowed, full round-trip conversion is expected to work without losing information.
266 * @return Content|bool A content object with the content model $toModel, or false if
267 * that conversion is not supported.
269 public function convert( $toModel, $lossy = '' ) {
270 $converted = parent
::convert( $toModel, $lossy );
272 if ( $converted !== false ) {
276 $toHandler = ContentHandler
::getForModelID( $toModel );
278 if ( $toHandler instanceof TextContentHandler
) {
279 //NOTE: ignore content serialization format - it's just text anyway.
280 $text = $this->getNativeData();
281 $converted = $toHandler->unserializeContent( $text );