a17bf319a7dd53399f28c2a7e7ae99c3bcb16dbe
[lhc/web/wiklou.git] / includes / content / WikitextContent.php
1 <?php
2 /**
3 * @since 1.21
4 */
5 class WikitextContent extends TextContent {
6
7 public function __construct( $text ) {
8 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
9 }
10
11 /**
12 * @see Content::getSection()
13 */
14 public function getSection( $section ) {
15 global $wgParser;
16
17 $text = $this->getNativeData();
18 $sect = $wgParser->getSection( $text, $section, false );
19
20 if ( $sect === false ) {
21 return false;
22 } else {
23 return new WikitextContent( $sect );
24 }
25 }
26
27 /**
28 * @see Content::replaceSection()
29 */
30 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
31 wfProfileIn( __METHOD__ );
32
33 $myModelId = $this->getModel();
34 $sectionModelId = $with->getModel();
35
36 if ( $sectionModelId != $myModelId ) {
37 throw new MWException( "Incompatible content model for section: " .
38 "document uses $myModelId but " .
39 "section uses $sectionModelId." );
40 }
41
42 $oldtext = $this->getNativeData();
43 $text = $with->getNativeData();
44
45 if ( $section === '' ) {
46 return $with; # XXX: copy first?
47 } if ( $section == 'new' ) {
48 # Inserting a new section
49 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
50 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
51 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
52 $text = strlen( trim( $oldtext ) ) > 0
53 ? "{$oldtext}\n\n{$subject}{$text}"
54 : "{$subject}{$text}";
55 }
56 } else {
57 # Replacing an existing section; roll out the big guns
58 global $wgParser;
59
60 $text = $wgParser->replaceSection( $oldtext, $section, $text );
61 }
62
63 $newContent = new WikitextContent( $text );
64
65 wfProfileOut( __METHOD__ );
66 return $newContent;
67 }
68
69 /**
70 * Returns a new WikitextContent object with the given section heading
71 * prepended.
72 *
73 * @param $header string
74 * @return Content
75 */
76 public function addSectionHeader( $header ) {
77 $text = wfMessage( 'newsectionheaderdefaultlevel' )
78 ->rawParams( $header )->inContentLanguage()->text();
79 $text .= "\n\n";
80 $text .= $this->getNativeData();
81
82 return new WikitextContent( $text );
83 }
84
85 /**
86 * Returns a Content object with pre-save transformations applied using
87 * Parser::preSaveTransform().
88 *
89 * @param $title Title
90 * @param $user User
91 * @param $popts ParserOptions
92 * @return Content
93 */
94 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
95 global $wgParser;
96
97 $text = $this->getNativeData();
98 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
99
100 return new WikitextContent( $pst );
101 }
102
103 /**
104 * Returns a Content object with preload transformations applied (or this
105 * object if no transformations apply).
106 *
107 * @param $title Title
108 * @param $popts ParserOptions
109 * @return Content
110 */
111 public function preloadTransform( Title $title, ParserOptions $popts ) {
112 global $wgParser;
113
114 $text = $this->getNativeData();
115 $plt = $wgParser->getPreloadText( $text, $title, $popts );
116
117 return new WikitextContent( $plt );
118 }
119
120 /**
121 * Implement redirect extraction for wikitext.
122 *
123 * @return null|Title
124 *
125 * @note: migrated here from Title::newFromRedirectInternal()
126 *
127 * @see Content::getRedirectTarget
128 * @see AbstractContent::getRedirectTarget
129 */
130 public function getRedirectTarget() {
131 global $wgMaxRedirects;
132 if ( $wgMaxRedirects < 1 ) {
133 // redirects are disabled, so quit early
134 return null;
135 }
136 $redir = MagicWord::get( 'redirect' );
137 $text = trim( $this->getNativeData() );
138 if ( $redir->matchStartAndRemove( $text ) ) {
139 // Extract the first link and see if it's usable
140 // Ensure that it really does come directly after #REDIRECT
141 // Some older redirects included a colon, so don't freak about that!
142 $m = array();
143 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
144 // Strip preceding colon used to "escape" categories, etc.
145 // and URL-decode links
146 if ( strpos( $m[1], '%' ) !== false ) {
147 // Match behavior of inline link parsing here;
148 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
149 }
150 $title = Title::newFromText( $m[1] );
151 // If the title is a redirect to bad special pages or is invalid, return null
152 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
153 return null;
154 }
155 return $title;
156 }
157 }
158 return null;
159 }
160
161 /**
162 * @see Content::updateRedirect()
163 *
164 * This implementation replaces the first link on the page with the given new target
165 * if this Content object is a redirect. Otherwise, this method returns $this.
166 *
167 * @since 1.21
168 *
169 * @param Title $target
170 *
171 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
172 */
173 public function updateRedirect( Title $target ) {
174 if ( !$this->isRedirect() ) {
175 return $this;
176 }
177
178 # Fix the text
179 # Remember that redirect pages can have categories, templates, etc.,
180 # so the regex has to be fairly general
181 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
182 '[[' . $target->getFullText() . ']]',
183 $this->getNativeData(), 1 );
184
185 return new WikitextContent( $newText );
186 }
187
188 /**
189 * Returns true if this content is not a redirect, and this content's text
190 * is countable according to the criteria defined by $wgArticleCountMethod.
191 *
192 * @param $hasLinks Bool if it is known whether this content contains
193 * links, provide this information here, to avoid redundant parsing to
194 * find out.
195 * @param $title null|\Title
196 *
197 * @internal param \IContextSource $context context for parsing if necessary
198 *
199 * @return bool True if the content is countable
200 */
201 public function isCountable( $hasLinks = null, Title $title = null ) {
202 global $wgArticleCountMethod;
203
204 if ( $this->isRedirect( ) ) {
205 return false;
206 }
207
208 $text = $this->getNativeData();
209
210 switch ( $wgArticleCountMethod ) {
211 case 'any':
212 return true;
213 case 'comma':
214 return strpos( $text, ',' ) !== false;
215 case 'link':
216 if ( $hasLinks === null ) { # not known, find out
217 if ( !$title ) {
218 $context = RequestContext::getMain();
219 $title = $context->getTitle();
220 }
221
222 $po = $this->getParserOutput( $title, null, null, false );
223 $links = $po->getLinks();
224 $hasLinks = !empty( $links );
225 }
226
227 return $hasLinks;
228 }
229
230 return false;
231 }
232
233 public function getTextForSummary( $maxlength = 250 ) {
234 $truncatedtext = parent::getTextForSummary( $maxlength );
235
236 # clean up unfinished links
237 # XXX: make this optional? wasn't there in autosummary, but required for
238 # deletion summary.
239 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
240
241 return $truncatedtext;
242 }
243
244 /**
245 * Returns a ParserOutput object resulting from parsing the content's text
246 * using $wgParser.
247 *
248 * @since 1.21
249 *
250 * @param $content Content the content to render
251 * @param $title \Title
252 * @param $revId null
253 * @param $options null|ParserOptions
254 * @param $generateHtml bool
255 *
256 * @internal param \IContextSource|null $context
257 * @return ParserOutput representing the HTML form of the text
258 */
259 public function getParserOutput( Title $title,
260 $revId = null,
261 ParserOptions $options = null, $generateHtml = true
262 ) {
263 global $wgParser;
264
265 if ( !$options ) {
266 //NOTE: use canonical options per default to produce cacheable output
267 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
268 }
269
270 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
271 return $po;
272 }
273
274 protected function getHtml() {
275 throw new MWException(
276 "getHtml() not implemented for wikitext. "
277 . "Use getParserOutput()->getText()."
278 );
279 }
280
281 /**
282 * @see Content::matchMagicWord()
283 *
284 * This implementation calls $word->match() on the this TextContent object's text.
285 *
286 * @param MagicWord $word
287 *
288 * @return bool whether this Content object matches the given magic word.
289 */
290 public function matchMagicWord( MagicWord $word ) {
291 return $word->match( $this->getNativeData() );
292 }
293 }