CologneBlue rewrite: rewrite bottomLinks()
[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 $text = '';
37 }
38
39 if ( !is_string( $text ) ) {
40 throw new MWException( "TextContent expects a string in the constructor." );
41 }
42
43 $this->mText = $text;
44 }
45
46 public function copy() {
47 return $this; # NOTE: this is ok since TextContent are immutable.
48 }
49
50 public function getTextForSummary( $maxlength = 250 ) {
51 global $wgContLang;
52
53 $text = $this->getNativeData();
54
55 $truncatedtext = $wgContLang->truncate(
56 preg_replace( "/[\n\r]/", ' ', $text ),
57 max( 0, $maxlength ) );
58
59 return $truncatedtext;
60 }
61
62 /**
63 * returns the text's size in bytes.
64 *
65 * @return int The size
66 */
67 public function getSize( ) {
68 $text = $this->getNativeData( );
69 return strlen( $text );
70 }
71
72 /**
73 * Returns true if this content is not a redirect, and $wgArticleCountMethod
74 * is "any".
75 *
76 * @param $hasLinks Bool: if it is known whether this content contains links,
77 * provide this information here, to avoid redundant parsing to find out.
78 *
79 * @return bool True if the content is countable
80 */
81 public function isCountable( $hasLinks = null ) {
82 global $wgArticleCountMethod;
83
84 if ( $this->isRedirect( ) ) {
85 return false;
86 }
87
88 if ( $wgArticleCountMethod === 'any' ) {
89 return true;
90 }
91
92 return false;
93 }
94
95 /**
96 * Returns the text represented by this Content object, as a string.
97 *
98 * @return string: the raw text
99 */
100 public function getNativeData( ) {
101 $text = $this->mText;
102 return $text;
103 }
104
105 /**
106 * Returns the text represented by this Content object, as a string.
107 *
108 * @return string: the raw text
109 */
110 public function getTextForSearchIndex( ) {
111 return $this->getNativeData();
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 getWikitextForTransclusion( ) {
120 return $this->getNativeData();
121 }
122
123 /**
124 * Returns a Content object with pre-save transformations applied.
125 * This implementation just trims trailing whitespace.
126 *
127 * @param $title Title
128 * @param $user User
129 * @param $popts ParserOptions
130 * @return Content
131 */
132 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
133 $text = $this->getNativeData();
134 $pst = rtrim( $text );
135
136 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
137 }
138
139 /**
140 * Diff this content object with another content object.
141 *
142 * @since 1.21diff
143 *
144 * @param $that Content: The other content object to compare this content
145 * object to.
146 * @param $lang Language: The language object to use for text segmentation.
147 * If not given, $wgContentLang is used.
148 *
149 * @return DiffResult: A diff representing the changes that would have to be
150 * made to this content object to make it equal to $that.
151 */
152 public function diff( Content $that, Language $lang = null ) {
153 global $wgContLang;
154
155 $this->checkModelID( $that->getModel() );
156
157 # @todo: could implement this in DifferenceEngine and just delegate here?
158
159 if ( !$lang ) $lang = $wgContLang;
160
161 $otext = $this->getNativeData();
162 $ntext = $this->getNativeData();
163
164 # Note: Use native PHP diff, external engines don't give us abstract output
165 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
166 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
167
168 $diff = new Diff( $ota, $nta );
169 return $diff;
170 }
171
172
173 /**
174 * Returns a generic ParserOutput object, wrapping the HTML returned by
175 * getHtml().
176 *
177 * @param $title Title Context title for parsing
178 * @param $revId int|null Revision ID (for {{REVISIONID}})
179 * @param $options ParserOptions|null Parser options
180 * @param $generateHtml bool Whether or not to generate HTML
181 *
182 * @return ParserOutput representing the HTML form of the text
183 */
184 public function getParserOutput( Title $title,
185 $revId = null,
186 ParserOptions $options = null, $generateHtml = true
187 ) {
188 global $wgParser, $wgTextModelsToParse;
189
190 if ( !$options ) {
191 //NOTE: use canonical options per default to produce cacheable output
192 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
193 }
194
195 if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
196 // parse just to get links etc into the database
197 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
198 } else {
199 $po = new ParserOutput();
200 }
201
202 if ( $generateHtml ) {
203 $html = $this->getHtml();
204 } else {
205 $html = '';
206 }
207
208 $po->setText( $html );
209 return $po;
210 }
211
212 /**
213 * Generates an HTML version of the content, for display. Used by
214 * getParserOutput() to construct a ParserOutput object.
215 *
216 * This default implementation just calls getHighlightHtml(). Content
217 * models that have another mapping to HTML (as is the case for markup
218 * languages like wikitext) should override this method to generate the
219 * appropriate HTML.
220 *
221 * @return string An HTML representation of the content
222 */
223 protected function getHtml() {
224 return $this->getHighlightHtml();
225 }
226
227 /**
228 * Generates a syntax-highlighted version of the content, as HTML.
229 * Used by the default implementation of getHtml().
230 *
231 * @return string an HTML representation of the content's markup
232 */
233 protected function getHighlightHtml( ) {
234 # TODO: make Highlighter interface, use highlighter here, if available
235 return htmlspecialchars( $this->getNativeData() );
236 }
237 }