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