Make sure that SQLite uses no prefix
[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 the text represented by this Content object, as a string.
119 *
120 * @return string: the raw text
121 */
122 public function getWikitextForTransclusion( ) {
123 return $this->getNativeData();
124 }
125
126 /**
127 * Returns a Content object with pre-save transformations applied.
128 * This implementation just trims trailing whitespace.
129 *
130 * @param $title Title
131 * @param $user User
132 * @param $popts ParserOptions
133 * @return Content
134 */
135 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
136 $text = $this->getNativeData();
137 $pst = rtrim( $text );
138
139 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
140 }
141
142 /**
143 * Diff this content object with another content object.
144 *
145 * @since 1.21diff
146 *
147 * @param $that Content: The other content object to compare this content
148 * object to.
149 * @param $lang Language: The language object to use for text segmentation.
150 * If not given, $wgContentLang is used.
151 *
152 * @return DiffResult: A diff representing the changes that would have to be
153 * made to this content object to make it equal to $that.
154 */
155 public function diff( Content $that, Language $lang = null ) {
156 global $wgContLang;
157
158 $this->checkModelID( $that->getModel() );
159
160 # @todo: could implement this in DifferenceEngine and just delegate here?
161
162 if ( !$lang ) $lang = $wgContLang;
163
164 $otext = $this->getNativeData();
165 $ntext = $this->getNativeData();
166
167 # Note: Use native PHP diff, external engines don't give us abstract output
168 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
169 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
170
171 $diff = new Diff( $ota, $nta );
172 return $diff;
173 }
174
175
176 /**
177 * Returns a generic ParserOutput object, wrapping the HTML returned by
178 * getHtml().
179 *
180 * @param $title Title Context title for parsing
181 * @param $revId int|null Revision ID (for {{REVISIONID}})
182 * @param $options ParserOptions|null Parser options
183 * @param $generateHtml bool Whether or not to generate HTML
184 *
185 * @return ParserOutput representing the HTML form of the text
186 */
187 public function getParserOutput( Title $title,
188 $revId = null,
189 ParserOptions $options = null, $generateHtml = true
190 ) {
191 global $wgParser, $wgTextModelsToParse;
192
193 if ( !$options ) {
194 //NOTE: use canonical options per default to produce cacheable output
195 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
196 }
197
198 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
199 // parse just to get links etc into the database
200 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
201 } else {
202 $po = new ParserOutput();
203 }
204
205 if ( $generateHtml ) {
206 $html = $this->getHtml();
207 } else {
208 $html = '';
209 }
210
211 $po->setText( $html );
212 return $po;
213 }
214
215 /**
216 * Generates an HTML version of the content, for display. Used by
217 * getParserOutput() to construct a ParserOutput object.
218 *
219 * This default implementation just calls getHighlightHtml(). Content
220 * models that have another mapping to HTML (as is the case for markup
221 * languages like wikitext) should override this method to generate the
222 * appropriate HTML.
223 *
224 * @return string An HTML representation of the content
225 */
226 protected function getHtml() {
227 return $this->getHighlightHtml();
228 }
229
230 /**
231 * Generates a syntax-highlighted version of the content, as HTML.
232 * Used by the default implementation of getHtml().
233 *
234 * @return string an HTML representation of the content's markup
235 */
236 protected function getHighlightHtml( ) {
237 # TODO: make Highlighter interface, use highlighter here, if available
238 return htmlspecialchars( $this->getNativeData() );
239 }
240 }