Made insertMessageBlob use (start|end)Atomic in case DBO_TRX is off
[mediawiki.git] / includes / content / WikitextContent.php
blobdbe09f91c9b20452bc2858f50d949f43938256eb
1 <?php
2 /**
3 * Content object for wiki text pages.
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.
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.
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
20 * @since 1.21
22 * @file
23 * @ingroup Content
25 * @author Daniel Kinzler
28 /**
29 * Content object for wiki text pages.
31 * @ingroup Content
33 class WikitextContent extends TextContent {
34 private $redirectTargetAndText = null;
36 public function __construct( $text ) {
37 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
40 /**
41 * @param string|number $sectionId
43 * @return Content|bool|null
45 * @see Content::getSection()
47 public function getSection( $sectionId ) {
48 global $wgParser;
50 $text = $this->getNativeData();
51 $sect = $wgParser->getSection( $text, $sectionId, false );
53 if ( $sect === false ) {
54 return false;
55 } else {
56 return new static( $sect );
60 /**
61 * @param string|number|null|bool $sectionId
62 * @param Content $with
63 * @param string $sectionTitle
65 * @throws MWException
66 * @return Content
68 * @see Content::replaceSection()
70 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
72 $myModelId = $this->getModel();
73 $sectionModelId = $with->getModel();
75 if ( $sectionModelId != $myModelId ) {
76 throw new MWException( "Incompatible content model for section: " .
77 "document uses $myModelId but " .
78 "section uses $sectionModelId." );
81 $oldtext = $this->getNativeData();
82 $text = $with->getNativeData();
84 if ( strval( $sectionId ) === '' ) {
86 return $with; # XXX: copy first?
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}";
98 } else {
99 # Replacing an existing section; roll out the big guns
100 global $wgParser;
102 $text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
105 $newContent = new static( $text );
107 return $newContent;
111 * Returns a new WikitextContent object with the given section heading
112 * prepended.
114 * @param string $header
116 * @return Content
118 public function addSectionHeader( $header ) {
119 $text = wfMessage( 'newsectionheaderdefaultlevel' )
120 ->rawParams( $header )->inContentLanguage()->text();
121 $text .= "\n\n";
122 $text .= $this->getNativeData();
124 return new static( $text );
128 * Returns a Content object with pre-save transformations applied using
129 * Parser::preSaveTransform().
131 * @param Title $title
132 * @param User $user
133 * @param ParserOptions $popts
135 * @return Content
137 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
138 global $wgParser;
140 $text = $this->getNativeData();
141 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
142 rtrim( $pst );
144 return ( $text === $pst ) ? $this : new static( $pst );
148 * Returns a Content object with preload transformations applied (or this
149 * object if no transformations apply).
151 * @param Title $title
152 * @param ParserOptions $popts
153 * @param array $params
155 * @return Content
157 public function preloadTransform( Title $title, ParserOptions $popts, $params = array() ) {
158 global $wgParser;
160 $text = $this->getNativeData();
161 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
163 return new static( $plt );
167 * Extract the redirect target and the remaining text on the page.
169 * @note migrated here from Title::newFromRedirectInternal()
171 * @since 1.23
173 * @return array List of two elements: Title|null and string.
175 protected function getRedirectTargetAndText() {
176 global $wgMaxRedirects;
178 if ( $this->redirectTargetAndText !== null ) {
179 return $this->redirectTargetAndText;
182 if ( $wgMaxRedirects < 1 ) {
183 // redirects are disabled, so quit early
184 $this->redirectTargetAndText = array( null, $this->getNativeData() );
185 return $this->redirectTargetAndText;
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], ':' ) );
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;
209 $this->redirectTargetAndText = array( $title, substr( $text, strlen( $m[0] ) ) );
210 return $this->redirectTargetAndText;
214 $this->redirectTargetAndText = array( null, $this->getNativeData() );
215 return $this->redirectTargetAndText;
219 * Implement redirect extraction for wikitext.
221 * @return Title|null
223 * @see Content::getRedirectTarget
225 public function getRedirectTarget() {
226 list( $title, ) = $this->getRedirectTargetAndText();
228 return $title;
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.
235 * @since 1.21
237 * @param Title $target
239 * @return Content
241 * @see Content::updateRedirect()
243 public function updateRedirect( Title $target ) {
244 if ( !$this->isRedirect() ) {
245 return $this;
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 );
255 return new static( $newText );
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.
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.
267 * @return bool
269 public function isCountable( $hasLinks = null, Title $title = null ) {
270 global $wgArticleCountMethod;
272 if ( $this->isRedirect() ) {
273 return false;
276 $text = $this->getNativeData();
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();
290 $po = $this->getParserOutput( $title, null, null, false );
291 $links = $po->getLinks();
292 $hasLinks = !empty( $links );
295 return $hasLinks;
298 return false;
302 * @param int $maxlength
303 * @return string
305 public function getTextForSummary( $maxlength = 250 ) {
306 $truncatedtext = parent::getTextForSummary( $maxlength );
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 );
313 return $truncatedtext;
317 * Returns a ParserOutput object resulting from parsing the content's text
318 * using $wgParser.
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.
327 protected function fillParserOutput( Title $title, $revId,
328 ParserOptions $options, $generateHtml, ParserOutput &$output
330 global $wgParser;
332 list( $redir, $text ) = $this->getRedirectTargetAndText();
333 $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
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()
345 $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
351 * @throws MWException
353 protected function getHtml() {
354 throw new MWException(
355 "getHtml() not implemented for wikitext. "
356 . "Use getParserOutput()->getText()."
361 * This implementation calls $word->match() on the this TextContent object's text.
363 * @param MagicWord $word
365 * @return bool
367 * @see Content::matchMagicWord()
369 public function matchMagicWord( MagicWord $word ) {
370 return $word->match( $this->getNativeData() );