4 * Content object implementation for representing flat text.
6 * TextContent instances are immutable
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 * @author Daniel Kinzler
30 class TextContent
extends AbstractContent
{
32 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
33 parent
::__construct( $model_id );
35 if ( $text === null ||
$text === false ) {
36 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
37 . "This may indicate an error in the caller's scope." );
42 if ( !is_string( $text ) ) {
43 throw new MWException( "TextContent expects a string in the constructor." );
49 public function copy() {
50 return $this; # NOTE: this is ok since TextContent are immutable.
53 public function getTextForSummary( $maxlength = 250 ) {
56 $text = $this->getNativeData();
58 $truncatedtext = $wgContLang->truncate(
59 preg_replace( "/[\n\r]/", ' ', $text ),
60 max( 0, $maxlength ) );
62 return $truncatedtext;
66 * returns the text's size in bytes.
68 * @return int The size
70 public function getSize( ) {
71 $text = $this->getNativeData( );
72 return strlen( $text );
76 * Returns true if this content is not a redirect, and $wgArticleCountMethod
79 * @param $hasLinks Bool: if it is known whether this content contains links,
80 * provide this information here, to avoid redundant parsing to find out.
82 * @return bool True if the content is countable
84 public function isCountable( $hasLinks = null ) {
85 global $wgArticleCountMethod;
87 if ( $this->isRedirect( ) ) {
91 if ( $wgArticleCountMethod === 'any' ) {
99 * Returns the text represented by this Content object, as a string.
101 * @return string: the raw text
103 public function getNativeData( ) {
104 $text = $this->mText
;
109 * Returns the text represented by this Content object, as a string.
111 * @return string: the raw text
113 public function getTextForSearchIndex( ) {
114 return $this->getNativeData();
118 * Returns the text represented by this Content object, as a string.
120 * @return string: the raw text
122 public function getWikitextForTransclusion( ) {
123 return $this->getNativeData();
127 * Returns a Content object with pre-save transformations applied.
128 * This implementation just trims trailing whitespace.
130 * @param $title Title
132 * @param $popts ParserOptions
135 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
136 $text = $this->getNativeData();
137 $pst = rtrim( $text );
139 return ( $text === $pst ) ?
$this : new WikitextContent( $pst );
143 * Diff this content object with another content object.
147 * @param $that Content: The other content object to compare this content
149 * @param $lang Language: The language object to use for text segmentation.
150 * If not given, $wgContentLang is used.
152 * @return DiffResult: A diff representing the changes that would have to be
153 * made to this content object to make it equal to $that.
155 public function diff( Content
$that, Language
$lang = null ) {
158 $this->checkModelID( $that->getModel() );
160 # @todo: could implement this in DifferenceEngine and just delegate here?
162 if ( !$lang ) $lang = $wgContLang;
164 $otext = $this->getNativeData();
165 $ntext = $this->getNativeData();
167 # Note: Use native PHP diff, external engines don't give us abstract output
168 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
169 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
171 $diff = new Diff( $ota, $nta );
177 * Returns a generic ParserOutput object, wrapping the HTML returned by
180 * @param $title Title Context title for parsing
181 * @param $revId int|null Revision ID (for {{REVISIONID}})
182 * @param $options ParserOptions|null Parser options
183 * @param $generateHtml bool Whether or not to generate HTML
185 * @return ParserOutput representing the HTML form of the text
187 public function getParserOutput( Title
$title,
189 ParserOptions
$options = null, $generateHtml = true
191 global $wgParser, $wgTextModelsToParse;
194 //NOTE: use canonical options per default to produce cacheable output
195 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
198 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
199 // parse just to get links etc into the database
200 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
202 $po = new ParserOutput();
205 if ( $generateHtml ) {
206 $html = $this->getHtml();
211 $po->setText( $html );
216 * Generates an HTML version of the content, for display. Used by
217 * getParserOutput() to construct a ParserOutput object.
219 * This default implementation just calls getHighlightHtml(). Content
220 * models that have another mapping to HTML (as is the case for markup
221 * languages like wikitext) should override this method to generate the
224 * @return string An HTML representation of the content
226 protected function getHtml() {
227 return $this->getHighlightHtml();
231 * Generates a syntax-highlighted version of the content, as HTML.
232 * Used by the default implementation of getHtml().
234 * @return string an HTML representation of the content's markup
236 protected function getHighlightHtml( ) {
237 # TODO: make Highlighter interface, use highlighter here, if available
238 return htmlspecialchars( $this->getNativeData() );