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