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