Merge "mediawiki.searchSuggest: Unbreak browser blacklist"
[lhc/web/wiklou.git] / includes / content / TextContent.php
1 <?php
2 /**
3 * Content object implementation for representing flat text.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
28 /**
29 * Content object implementation for representing flat text.
30 *
31 * TextContent instances are immutable
32 *
33 * @ingroup Content
34 */
35 class TextContent extends AbstractContent {
36 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
37 parent::__construct( $model_id );
38
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." );
42
43 $text = '';
44 }
45
46 if ( !is_string( $text ) ) {
47 throw new MWException( "TextContent expects a string in the constructor." );
48 }
49
50 $this->mText = $text;
51 }
52
53 public function copy() {
54 return $this; # NOTE: this is ok since TextContent are immutable.
55 }
56
57 public function getTextForSummary( $maxlength = 250 ) {
58 global $wgContLang;
59
60 $text = $this->getNativeData();
61
62 $truncatedtext = $wgContLang->truncate(
63 preg_replace( "/[\n\r]/", ' ', $text ),
64 max( 0, $maxlength ) );
65
66 return $truncatedtext;
67 }
68
69 /**
70 * returns the text's size in bytes.
71 *
72 * @return int The size
73 */
74 public function getSize() {
75 $text = $this->getNativeData();
76
77 return strlen( $text );
78 }
79
80 /**
81 * Returns true if this content is not a redirect, and $wgArticleCountMethod
82 * is "any".
83 *
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.
86 *
87 * @return bool True if the content is countable
88 */
89 public function isCountable( $hasLinks = null ) {
90 global $wgArticleCountMethod;
91
92 if ( $this->isRedirect() ) {
93 return false;
94 }
95
96 if ( $wgArticleCountMethod === 'any' ) {
97 return true;
98 }
99
100 return false;
101 }
102
103 /**
104 * Returns the text represented by this Content object, as a string.
105 *
106 * @return string: the raw text
107 */
108 public function getNativeData() {
109 $text = $this->mText;
110
111 return $text;
112 }
113
114 /**
115 * Returns the text represented by this Content object, as a string.
116 *
117 * @return string: the raw text
118 */
119 public function getTextForSearchIndex() {
120 return $this->getNativeData();
121 }
122
123 /**
124 * Returns attempts to convert this content object to wikitext,
125 * and then returns the text string. The conversion may be lossy.
126 *
127 * @note: this allows any text-based content to be transcluded as if it was wikitext.
128 *
129 * @return string|false: the raw text, or null if the conversion failed
130 */
131 public function getWikitextForTransclusion() {
132 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
133
134 if ( $wikitext ) {
135 return $wikitext->getNativeData();
136 } else {
137 return false;
138 }
139 }
140
141 /**
142 * Returns a Content object with pre-save transformations applied.
143 * This implementation just trims trailing whitespace.
144 *
145 * @param $title Title
146 * @param $user User
147 * @param $popts ParserOptions
148 * @return Content
149 */
150 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
151 $text = $this->getNativeData();
152 $pst = rtrim( $text );
153
154 return ( $text === $pst ) ? $this : new static( $pst );
155 }
156
157 /**
158 * Diff this content object with another content object.
159 *
160 * @since 1.21
161 *
162 * @param $that Content: The other content object to compare this content
163 * object to.
164 * @param $lang Language: The language object to use for text segmentation.
165 * If not given, $wgContentLang is used.
166 *
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.
169 */
170 public function diff( Content $that, Language $lang = null ) {
171 global $wgContLang;
172
173 $this->checkModelID( $that->getModel() );
174
175 // @todo could implement this in DifferenceEngine and just delegate here?
176
177 if ( !$lang ) {
178 $lang = $wgContLang;
179 }
180
181 $otext = $this->getNativeData();
182 $ntext = $that->getNativeData();
183
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 ) );
187
188 $diff = new Diff( $ota, $nta );
189
190 return $diff;
191 }
192
193 /**
194 * Returns a generic ParserOutput object, wrapping the HTML returned by
195 * getHtml().
196 *
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
201 *
202 * @return ParserOutput representing the HTML form of the text
203 */
204 public function getParserOutput( Title $title,
205 $revId = null,
206 ParserOptions $options = null, $generateHtml = true
207 ) {
208 global $wgParser, $wgTextModelsToParse;
209
210 if ( !$options ) {
211 //NOTE: use canonical options per default to produce cacheable output
212 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
213 }
214
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 );
218 } else {
219 $po = new ParserOutput();
220 }
221
222 if ( $generateHtml ) {
223 $html = $this->getHtml();
224 } else {
225 $html = '';
226 }
227
228 $po->setText( $html );
229
230 return $po;
231 }
232
233 /**
234 * Generates an HTML version of the content, for display. Used by
235 * getParserOutput() to construct a ParserOutput object.
236 *
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
240 * appropriate HTML.
241 *
242 * @return string An HTML representation of the content
243 */
244 protected function getHtml() {
245 return $this->getHighlightHtml();
246 }
247
248 /**
249 * Generates a syntax-highlighted version of the content, as HTML.
250 * Used by the default implementation of getHtml().
251 *
252 * @return string an HTML representation of the content's markup
253 */
254 protected function getHighlightHtml() {
255 # TODO: make Highlighter interface, use highlighter here, if available
256 return htmlspecialchars( $this->getNativeData() );
257 }
258
259 /**
260 * @see Content::convert()
261 *
262 * This implementation provides lossless conversion between content models based
263 * on TextContent.
264 *
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.
268 *
269 * @return Content|bool A content object with the content model $toModel, or false if
270 * that conversion is not supported.
271 */
272 public function convert( $toModel, $lossy = '' ) {
273 $converted = parent::convert( $toModel, $lossy );
274
275 if ( $converted !== false ) {
276 return $converted;
277 }
278
279 $toHandler = ContentHandler::getForModelID( $toModel );
280
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 );
285 }
286
287 return $converted;
288 }
289 }