Merge "Avoid undefined index when query old block logs with api"
[lhc/web/wiklou.git] / includes / content / WikitextContent.php
1 <?php
2 /**
3 * Content object for wiki text pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
28 /**
29 * Content object for wiki text pages.
30 *
31 * @ingroup Content
32 */
33 class WikitextContent extends TextContent {
34 private $redirectTargetAndText = null;
35
36 public function __construct( $text ) {
37 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
38 }
39
40 /**
41 * @param string|number $sectionId
42 *
43 * @return Content|bool|null
44 *
45 * @see Content::getSection()
46 */
47 public function getSection( $sectionId ) {
48 global $wgParser;
49
50 $text = $this->getNativeData();
51 $sect = $wgParser->getSection( $text, $sectionId, false );
52
53 if ( $sect === false ) {
54 return false;
55 } else {
56 return new static( $sect );
57 }
58 }
59
60 /**
61 * @param string|number|null|bool $sectionId
62 * @param Content $with
63 * @param string $sectionTitle
64 *
65 * @throws MWException
66 * @return Content
67 *
68 * @see Content::replaceSection()
69 */
70 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
71
72 $myModelId = $this->getModel();
73 $sectionModelId = $with->getModel();
74
75 if ( $sectionModelId != $myModelId ) {
76 throw new MWException( "Incompatible content model for section: " .
77 "document uses $myModelId but " .
78 "section uses $sectionModelId." );
79 }
80
81 $oldtext = $this->getNativeData();
82 $text = $with->getNativeData();
83
84 if ( strval( $sectionId ) === '' ) {
85
86 return $with; # XXX: copy first?
87 }
88
89 if ( $sectionId === 'new' ) {
90 # Inserting a new section
91 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
92 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
93 if ( Hooks::run( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
94 $text = strlen( trim( $oldtext ) ) > 0
95 ? "{$oldtext}\n\n{$subject}{$text}"
96 : "{$subject}{$text}";
97 }
98 } else {
99 # Replacing an existing section; roll out the big guns
100 global $wgParser;
101
102 $text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
103 }
104
105 $newContent = new static( $text );
106
107 return $newContent;
108 }
109
110 /**
111 * Returns a new WikitextContent object with the given section heading
112 * prepended.
113 *
114 * @param string $header
115 *
116 * @return Content
117 */
118 public function addSectionHeader( $header ) {
119 $text = wfMessage( 'newsectionheaderdefaultlevel' )
120 ->rawParams( $header )->inContentLanguage()->text();
121 $text .= "\n\n";
122 $text .= $this->getNativeData();
123
124 return new static( $text );
125 }
126
127 /**
128 * Returns a Content object with pre-save transformations applied using
129 * Parser::preSaveTransform().
130 *
131 * @param Title $title
132 * @param User $user
133 * @param ParserOptions $popts
134 *
135 * @return Content
136 */
137 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
138 global $wgParser;
139
140 $text = $this->getNativeData();
141 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
142 rtrim( $pst );
143
144 return ( $text === $pst ) ? $this : new static( $pst );
145 }
146
147 /**
148 * Returns a Content object with preload transformations applied (or this
149 * object if no transformations apply).
150 *
151 * @param Title $title
152 * @param ParserOptions $popts
153 * @param array $params
154 *
155 * @return Content
156 */
157 public function preloadTransform( Title $title, ParserOptions $popts, $params = array() ) {
158 global $wgParser;
159
160 $text = $this->getNativeData();
161 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
162
163 return new static( $plt );
164 }
165
166 /**
167 * Extract the redirect target and the remaining text on the page.
168 *
169 * @note migrated here from Title::newFromRedirectInternal()
170 *
171 * @since 1.23
172 *
173 * @return array List of two elements: Title|null and string.
174 */
175 protected function getRedirectTargetAndText() {
176 global $wgMaxRedirects;
177
178 if ( $this->redirectTargetAndText !== null ) {
179 return $this->redirectTargetAndText;
180 }
181
182 if ( $wgMaxRedirects < 1 ) {
183 // redirects are disabled, so quit early
184 $this->redirectTargetAndText = array( null, $this->getNativeData() );
185 return $this->redirectTargetAndText;
186 }
187
188 $redir = MagicWord::get( 'redirect' );
189 $text = ltrim( $this->getNativeData() );
190 if ( $redir->matchStartAndRemove( $text ) ) {
191 // Extract the first link and see if it's usable
192 // Ensure that it really does come directly after #REDIRECT
193 // Some older redirects included a colon, so don't freak about that!
194 $m = array();
195 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
196 // Strip preceding colon used to "escape" categories, etc.
197 // and URL-decode links
198 if ( strpos( $m[1], '%' ) !== false ) {
199 // Match behavior of inline link parsing here;
200 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
201 }
202 $title = Title::newFromText( $m[1] );
203 // If the title is a redirect to bad special pages or is invalid, return null
204 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
205 $this->redirectTargetAndText = array( null, $this->getNativeData() );
206 return $this->redirectTargetAndText;
207 }
208
209 $this->redirectTargetAndText = array( $title, substr( $text, strlen( $m[0] ) ) );
210 return $this->redirectTargetAndText;
211 }
212 }
213
214 $this->redirectTargetAndText = array( null, $this->getNativeData() );
215 return $this->redirectTargetAndText;
216 }
217
218 /**
219 * Implement redirect extraction for wikitext.
220 *
221 * @return Title|null
222 *
223 * @see Content::getRedirectTarget
224 */
225 public function getRedirectTarget() {
226 list( $title, ) = $this->getRedirectTargetAndText();
227
228 return $title;
229 }
230
231 /**
232 * This implementation replaces the first link on the page with the given new target
233 * if this Content object is a redirect. Otherwise, this method returns $this.
234 *
235 * @since 1.21
236 *
237 * @param Title $target
238 *
239 * @return Content
240 *
241 * @see Content::updateRedirect()
242 */
243 public function updateRedirect( Title $target ) {
244 if ( !$this->isRedirect() ) {
245 return $this;
246 }
247
248 # Fix the text
249 # Remember that redirect pages can have categories, templates, etc.,
250 # so the regex has to be fairly general
251 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
252 '[[' . $target->getFullText() . ']]',
253 $this->getNativeData(), 1 );
254
255 return new static( $newText );
256 }
257
258 /**
259 * Returns true if this content is not a redirect, and this content's text
260 * is countable according to the criteria defined by $wgArticleCountMethod.
261 *
262 * @param bool $hasLinks If it is known whether this content contains
263 * links, provide this information here, to avoid redundant parsing to
264 * find out (default: null).
265 * @param Title $title Optional title, defaults to the title from the current main request.
266 *
267 * @return bool
268 */
269 public function isCountable( $hasLinks = null, Title $title = null ) {
270 global $wgArticleCountMethod;
271
272 if ( $this->isRedirect() ) {
273 return false;
274 }
275
276 $text = $this->getNativeData();
277
278 switch ( $wgArticleCountMethod ) {
279 case 'any':
280 return true;
281 case 'comma':
282 return strpos( $text, ',' ) !== false;
283 case 'link':
284 if ( $hasLinks === null ) { # not known, find out
285 if ( !$title ) {
286 $context = RequestContext::getMain();
287 $title = $context->getTitle();
288 }
289
290 $po = $this->getParserOutput( $title, null, null, false );
291 $links = $po->getLinks();
292 $hasLinks = !empty( $links );
293 }
294
295 return $hasLinks;
296 }
297
298 return false;
299 }
300
301 /**
302 * @param int $maxlength
303 * @return string
304 */
305 public function getTextForSummary( $maxlength = 250 ) {
306 $truncatedtext = parent::getTextForSummary( $maxlength );
307
308 # clean up unfinished links
309 # XXX: make this optional? wasn't there in autosummary, but required for
310 # deletion summary.
311 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
312
313 return $truncatedtext;
314 }
315
316 /**
317 * Returns a ParserOutput object resulting from parsing the content's text
318 * using $wgParser.
319 *
320 * @param Title $title
321 * @param int $revId Revision to pass to the parser (default: null)
322 * @param ParserOptions $options (default: null)
323 * @param bool $generateHtml (default: true)
324 * @param ParserOutput &$output ParserOutput representing the HTML form of the text,
325 * may be manipulated or replaced.
326 */
327 protected function fillParserOutput( Title $title, $revId,
328 ParserOptions $options, $generateHtml, ParserOutput &$output
329 ) {
330 global $wgParser;
331
332 list( $redir, $text ) = $this->getRedirectTargetAndText();
333 $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
334
335 // Add redirect indicator at the top
336 if ( $redir ) {
337 // Make sure to include the redirect link in pagelinks
338 $output->addLink( $redir );
339 if ( $generateHtml ) {
340 $chain = $this->getRedirectChain();
341 $output->setText(
342 Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
343 $output->getText()
344 );
345 $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
346 }
347 }
348 }
349
350 /**
351 * @throws MWException
352 */
353 protected function getHtml() {
354 throw new MWException(
355 "getHtml() not implemented for wikitext. "
356 . "Use getParserOutput()->getText()."
357 );
358 }
359
360 /**
361 * This implementation calls $word->match() on the this TextContent object's text.
362 *
363 * @param MagicWord $word
364 *
365 * @return bool
366 *
367 * @see Content::matchMagicWord()
368 */
369 public function matchMagicWord( MagicWord $word ) {
370 return $word->match( $this->getNativeData() );
371 }
372
373 }