fix some spacing
[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 return strlen( $text );
78 }
79
80 /**
81 * Returns true if this content is not a redirect, and $wgArticleCountMethod
82 * is "any".
83 *
84 * @param $hasLinks Bool: 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 return $text;
111 }
112
113 /**
114 * Returns the text represented by this Content object, as a string.
115 *
116 * @return string: the raw text
117 */
118 public function getTextForSearchIndex( ) {
119 return $this->getNativeData();
120 }
121
122 /**
123 * Returns attempts to convert this content object to wikitext,
124 * and then returns the text string. The conversion may be lossy.
125 *
126 * @note: this allows any text-based content to be transcluded as if it was wikitext.
127 *
128 * @return string|false: the raw text, or null if the conversion failed
129 */
130 public function getWikitextForTransclusion( ) {
131 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
132
133 if ( $wikitext ) {
134 return $wikitext->getNativeData();
135 } else {
136 return false;
137 }
138 }
139
140 /**
141 * Returns a Content object with pre-save transformations applied.
142 * This implementation just trims trailing whitespace.
143 *
144 * @param $title Title
145 * @param $user User
146 * @param $popts ParserOptions
147 * @return Content
148 */
149 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
150 $text = $this->getNativeData();
151 $pst = rtrim( $text );
152
153 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
154 }
155
156 /**
157 * Diff this content object with another content object.
158 *
159 * @since 1.21diff
160 *
161 * @param $that Content: The other content object to compare this content
162 * object to.
163 * @param $lang Language: The language object to use for text segmentation.
164 * If not given, $wgContentLang is used.
165 *
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.
168 */
169 public function diff( Content $that, Language $lang = null ) {
170 global $wgContLang;
171
172 $this->checkModelID( $that->getModel() );
173
174 # @todo: could implement this in DifferenceEngine and just delegate here?
175
176 if ( !$lang ) $lang = $wgContLang;
177
178 $otext = $this->getNativeData();
179 $ntext = $this->getNativeData();
180
181 # Note: Use native PHP diff, external engines don't give us abstract output
182 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
183 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
184
185 $diff = new Diff( $ota, $nta );
186 return $diff;
187 }
188
189 /**
190 * Returns a generic ParserOutput object, wrapping the HTML returned by
191 * getHtml().
192 *
193 * @param $title Title Context title for parsing
194 * @param $revId int|null Revision ID (for {{REVISIONID}})
195 * @param $options ParserOptions|null Parser options
196 * @param $generateHtml bool Whether or not to generate HTML
197 *
198 * @return ParserOutput representing the HTML form of the text
199 */
200 public function getParserOutput( Title $title,
201 $revId = null,
202 ParserOptions $options = null, $generateHtml = true
203 ) {
204 global $wgParser, $wgTextModelsToParse;
205
206 if ( !$options ) {
207 //NOTE: use canonical options per default to produce cacheable output
208 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
209 }
210
211 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
212 // parse just to get links etc into the database
213 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
214 } else {
215 $po = new ParserOutput();
216 }
217
218 if ( $generateHtml ) {
219 $html = $this->getHtml();
220 } else {
221 $html = '';
222 }
223
224 $po->setText( $html );
225 return $po;
226 }
227
228 /**
229 * Generates an HTML version of the content, for display. Used by
230 * getParserOutput() to construct a ParserOutput object.
231 *
232 * This default implementation just calls getHighlightHtml(). Content
233 * models that have another mapping to HTML (as is the case for markup
234 * languages like wikitext) should override this method to generate the
235 * appropriate HTML.
236 *
237 * @return string An HTML representation of the content
238 */
239 protected function getHtml() {
240 return $this->getHighlightHtml();
241 }
242
243 /**
244 * Generates a syntax-highlighted version of the content, as HTML.
245 * Used by the default implementation of getHtml().
246 *
247 * @return string an HTML representation of the content's markup
248 */
249 protected function getHighlightHtml( ) {
250 # TODO: make Highlighter interface, use highlighter here, if available
251 return htmlspecialchars( $this->getNativeData() );
252 }
253
254 /**
255 * @see Content::convert()
256 *
257 * This implementation provides lossless conversion between content models based
258 * on TextContent.
259 *
260 * @param String $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
261 * @param String $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
262 * not allowed, full round-trip conversion is expected to work without losing information.
263 *
264 * @return Content|bool A content object with the content model $toModel, or false if
265 * that conversion is not supported.
266 */
267 public function convert( $toModel, $lossy = '' ) {
268 $converted = parent::convert( $toModel, $lossy );
269
270 if ( $converted !== false ) {
271 return $converted;
272 }
273
274 $toHandler = ContentHandler::getForModelID( $toModel );
275
276 if ( $toHandler instanceof TextContentHandler ) {
277 //NOTE: ignore content serialization format - it's just text anyway.
278 $text = $this->getNativeData();
279 $converted = $toHandler->unserializeContent( $text );
280 }
281
282 return $converted;
283 }
284 }