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