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