Remove useless comments from search output
[mediawiki.git] / includes / content / TextContent.php
blobb0da62df3f6e3dd8a701fc50fb749a46cecfc653
1 <?php
2 /**
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
20 * @since 1.21
22 * @file
23 * @ingroup Content
25 * @author Daniel Kinzler
28 /**
29 * Content object implementation for representing flat text.
31 * TextContent instances are immutable
33 * @ingroup Content
35 class TextContent extends AbstractContent {
37 /**
38 * @param string $text
39 * @param string $model_id
41 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
42 parent::__construct( $model_id );
44 if ( $text === null || $text === false ) {
45 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
46 . "This may indicate an error in the caller's scope.", 2 );
48 $text = '';
51 if ( !is_string( $text ) ) {
52 throw new MWException( "TextContent expects a string in the constructor." );
55 $this->mText = $text;
58 /**
59 * @note Mutable subclasses MUST override this to return a copy!
61 * @return Content $this
63 public function copy() {
64 return $this; # NOTE: this is ok since TextContent are immutable.
67 public function getTextForSummary( $maxlength = 250 ) {
68 global $wgContLang;
70 $text = $this->getNativeData();
72 $truncatedtext = $wgContLang->truncate(
73 preg_replace( "/[\n\r]/", ' ', $text ),
74 max( 0, $maxlength ) );
76 return $truncatedtext;
79 /**
80 * returns the text's size in bytes.
82 * @return int The size
84 public function getSize() {
85 $text = $this->getNativeData();
87 return strlen( $text );
90 /**
91 * Returns true if this content is not a redirect, and $wgArticleCountMethod
92 * is "any".
94 * @param bool $hasLinks If it is known whether this content contains links,
95 * provide this information here, to avoid redundant parsing to find out.
97 * @return bool
99 public function isCountable( $hasLinks = null ) {
100 global $wgArticleCountMethod;
102 if ( $this->isRedirect() ) {
103 return false;
106 if ( $wgArticleCountMethod === 'any' ) {
107 return true;
110 return false;
114 * Returns the text represented by this Content object, as a string.
116 * @return string The raw text.
118 public function getNativeData() {
119 $text = $this->mText;
121 return $text;
125 * Returns the text represented by this Content object, as a string.
127 * @return string The raw text.
129 public function getTextForSearchIndex() {
130 return $this->getNativeData();
134 * Returns attempts to convert this content object to wikitext,
135 * and then returns the text string. The conversion may be lossy.
137 * @note: this allows any text-based content to be transcluded as if it was wikitext.
139 * @return string|false The raw text, or false if the conversion failed.
141 public function getWikitextForTransclusion() {
142 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
144 if ( $wikitext ) {
145 return $wikitext->getNativeData();
146 } else {
147 return false;
152 * Returns a Content object with pre-save transformations applied.
153 * This implementation just trims trailing whitespace.
155 * @param Title $title
156 * @param User $user
157 * @param ParserOptions $popts
159 * @return Content
161 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
162 $text = $this->getNativeData();
163 $pst = rtrim( $text );
165 return ( $text === $pst ) ? $this : new static( $pst );
169 * Diff this content object with another content object.
171 * @since 1.21
173 * @param Content $that The other content object to compare this content
174 * object to.
175 * @param Language $lang The language object to use for text segmentation.
176 * If not given, $wgContentLang is used.
178 * @return Diff A diff representing the changes that would have to be
179 * made to this content object to make it equal to $that.
181 public function diff( Content $that, Language $lang = null ) {
182 global $wgContLang;
184 $this->checkModelID( $that->getModel() );
186 // @todo could implement this in DifferenceEngine and just delegate here?
188 if ( !$lang ) {
189 $lang = $wgContLang;
192 $otext = $this->getNativeData();
193 $ntext = $that->getNativeData();
195 # Note: Use native PHP diff, external engines don't give us abstract output
196 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
197 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
199 $diff = new Diff( $ota, $nta );
201 return $diff;
205 * Returns a generic ParserOutput object, wrapping the HTML returned by
206 * getHtml().
208 * @param Title $title Context title for parsing
209 * @param int $revId Revision ID (for {{REVISIONID}})
210 * @param ParserOptions $options Parser options
211 * @param bool $generateHtml Whether or not to generate HTML
213 * @return ParserOutput Representing the HTML form of the text.
215 public function getParserOutput( Title $title, $revId = null,
216 ParserOptions $options = null, $generateHtml = true ) {
217 global $wgParser, $wgTextModelsToParse;
219 if ( !$options ) {
220 //NOTE: use canonical options per default to produce cacheable output
221 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
224 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
225 // parse just to get links etc into the database
226 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
227 } else {
228 $po = new ParserOutput();
231 if ( $generateHtml ) {
232 $html = $this->getHtml();
233 } else {
234 $html = '';
237 $po->setText( $html );
239 return $po;
243 * Generates an HTML version of the content, for display. Used by
244 * getParserOutput() to construct a ParserOutput object.
246 * This default implementation just calls getHighlightHtml(). Content
247 * models that have another mapping to HTML (as is the case for markup
248 * languages like wikitext) should override this method to generate the
249 * appropriate HTML.
251 * @return string An HTML representation of the content
253 protected function getHtml() {
254 return $this->getHighlightHtml();
258 * Generates a syntax-highlighted version of the content, as HTML.
259 * Used by the default implementation of getHtml().
261 * @return string A HTML representation of the content's markup
263 protected function getHighlightHtml() {
264 # TODO: make Highlighter interface, use highlighter here, if available
265 return htmlspecialchars( $this->getNativeData() );
269 * This implementation provides lossless conversion between content models based
270 * on TextContent.
272 * @param string $toModel
273 * @param string $lossy
275 * @return Content|bool
277 * @see Content::convert()
279 public function convert( $toModel, $lossy = '' ) {
280 $converted = parent::convert( $toModel, $lossy );
282 if ( $converted !== false ) {
283 return $converted;
286 $toHandler = ContentHandler::getForModelID( $toModel );
288 if ( $toHandler instanceof TextContentHandler ) {
289 //NOTE: ignore content serialization format - it's just text anyway.
290 $text = $this->getNativeData();
291 $converted = $toHandler->unserializeContent( $text );
294 return $converted;