Update formatting
[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
37 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) {
38 parent::__construct( $model_id );
39
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." );
43
44 $text = '';
45 }
46
47 if ( !is_string( $text ) ) {
48 throw new MWException( "TextContent expects a string in the constructor." );
49 }
50
51 $this->mText = $text;
52 }
53
54 public function copy() {
55 return $this; # NOTE: this is ok since TextContent are immutable.
56 }
57
58 public function getTextForSummary( $maxlength = 250 ) {
59 global $wgContLang;
60
61 $text = $this->getNativeData();
62
63 $truncatedtext = $wgContLang->truncate(
64 preg_replace( "/[\n\r]/", ' ', $text ),
65 max( 0, $maxlength ) );
66
67 return $truncatedtext;
68 }
69
70 /**
71 * returns the text's size in bytes.
72 *
73 * @return int The size
74 */
75 public function getSize() {
76 $text = $this->getNativeData();
77
78 return strlen( $text );
79 }
80
81 /**
82 * Returns true if this content is not a redirect, and $wgArticleCountMethod
83 * is "any".
84 *
85 * @param bool $hasLinks if it is known whether this content contains links,
86 * provide this information here, to avoid redundant parsing to find out.
87 *
88 * @return bool True if the content is countable
89 */
90 public function isCountable( $hasLinks = null ) {
91 global $wgArticleCountMethod;
92
93 if ( $this->isRedirect() ) {
94 return false;
95 }
96
97 if ( $wgArticleCountMethod === 'any' ) {
98 return true;
99 }
100
101 return false;
102 }
103
104 /**
105 * Returns the text represented by this Content object, as a string.
106 *
107 * @return string: the raw text
108 */
109 public function getNativeData() {
110 $text = $this->mText;
111
112 return $text;
113 }
114
115 /**
116 * Returns the text represented by this Content object, as a string.
117 *
118 * @return string: the raw text
119 */
120 public function getTextForSearchIndex() {
121 return $this->getNativeData();
122 }
123
124 /**
125 * Returns attempts to convert this content object to wikitext,
126 * and then returns the text string. The conversion may be lossy.
127 *
128 * @note: this allows any text-based content to be transcluded as if it was wikitext.
129 *
130 * @return string|false: the raw text, or null if the conversion failed
131 */
132 public function getWikitextForTransclusion() {
133 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
134
135 if ( $wikitext ) {
136 return $wikitext->getNativeData();
137 } else {
138 return false;
139 }
140 }
141
142 /**
143 * Returns a Content object with pre-save transformations applied.
144 * This implementation just trims trailing whitespace.
145 *
146 * @param $title Title
147 * @param $user User
148 * @param $popts ParserOptions
149 * @return Content
150 */
151 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
152 $text = $this->getNativeData();
153 $pst = rtrim( $text );
154
155 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
156 }
157
158 /**
159 * Diff this content object with another content object.
160 *
161 * @since 1.21
162 *
163 * @param $that Content: The other content object to compare this content
164 * object to.
165 * @param $lang Language: The language object to use for text segmentation.
166 * If not given, $wgContentLang is used.
167 *
168 * @return Diff A diff representing the changes that would have to be
169 * made to this content object to make it equal to $that.
170 */
171 public function diff( Content $that, Language $lang = null ) {
172 global $wgContLang;
173
174 $this->checkModelID( $that->getModel() );
175
176 // @todo could implement this in DifferenceEngine and just delegate here?
177
178 if ( !$lang ) {
179 $lang = $wgContLang;
180 }
181
182 $otext = $this->getNativeData();
183 $ntext = $that->getNativeData();
184
185 # Note: Use native PHP diff, external engines don't give us abstract output
186 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
187 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
188
189 $diff = new Diff( $ota, $nta );
190
191 return $diff;
192 }
193
194 /**
195 * Returns a generic ParserOutput object, wrapping the HTML returned by
196 * getHtml().
197 *
198 * @param $title Title Context title for parsing
199 * @param int|null $revId Revision ID (for {{REVISIONID}})
200 * @param $options ParserOptions|null Parser options
201 * @param bool $generateHtml Whether or not to generate HTML
202 *
203 * @return ParserOutput representing the HTML form of the text
204 */
205 public function getParserOutput( Title $title,
206 $revId = null,
207 ParserOptions $options = null, $generateHtml = true
208 ) {
209 global $wgParser, $wgTextModelsToParse;
210
211 if ( !$options ) {
212 //NOTE: use canonical options per default to produce cacheable output
213 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
214 }
215
216 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
217 // parse just to get links etc into the database
218 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
219 } else {
220 $po = new ParserOutput();
221 }
222
223 if ( $generateHtml ) {
224 $html = $this->getHtml();
225 } else {
226 $html = '';
227 }
228
229 $po->setText( $html );
230
231 return $po;
232 }
233
234 /**
235 * Generates an HTML version of the content, for display. Used by
236 * getParserOutput() to construct a ParserOutput object.
237 *
238 * This default implementation just calls getHighlightHtml(). Content
239 * models that have another mapping to HTML (as is the case for markup
240 * languages like wikitext) should override this method to generate the
241 * appropriate HTML.
242 *
243 * @return string An HTML representation of the content
244 */
245 protected function getHtml() {
246 return $this->getHighlightHtml();
247 }
248
249 /**
250 * Generates a syntax-highlighted version of the content, as HTML.
251 * Used by the default implementation of getHtml().
252 *
253 * @return string an HTML representation of the content's markup
254 */
255 protected function getHighlightHtml() {
256 # TODO: make Highlighter interface, use highlighter here, if available
257 return htmlspecialchars( $this->getNativeData() );
258 }
259
260 /**
261 * @see Content::convert()
262 *
263 * This implementation provides lossless conversion between content models based
264 * on TextContent.
265 *
266 * @param string $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
267 * @param string $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
268 * not allowed, full round-trip conversion is expected to work without losing information.
269 *
270 * @return Content|bool A content object with the content model $toModel, or false if
271 * that conversion is not supported.
272 */
273 public function convert( $toModel, $lossy = '' ) {
274 $converted = parent::convert( $toModel, $lossy );
275
276 if ( $converted !== false ) {
277 return $converted;
278 }
279
280 $toHandler = ContentHandler::getForModelID( $toModel );
281
282 if ( $toHandler instanceof TextContentHandler ) {
283 //NOTE: ignore content serialization format - it's just text anyway.
284 $text = $this->getNativeData();
285 $converted = $toHandler->unserializeContent( $text );
286 }
287
288 return $converted;
289 }
290 }