wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / content / WikitextContent.php
blob8beae393788464b01b4a055258614168e0394b6b
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 ) === '' ) {
85 return $with; # XXX: copy first?
88 if ( $sectionId === 'new' ) {
89 # Inserting a new section
90 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
91 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
92 if ( Hooks::run( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
93 $text = strlen( trim( $oldtext ) ) > 0
94 ? "{$oldtext}\n\n{$subject}{$text}"
95 : "{$subject}{$text}";
97 } else {
98 # Replacing an existing section; roll out the big guns
99 global $wgParser;
101 $text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
104 $newContent = new static( $text );
106 return $newContent;
110 * Returns a new WikitextContent object with the given section heading
111 * prepended.
113 * @param string $header
115 * @return Content
117 public function addSectionHeader( $header ) {
118 $text = wfMessage( 'newsectionheaderdefaultlevel' )
119 ->rawParams( $header )->inContentLanguage()->text();
120 $text .= "\n\n";
121 $text .= $this->getNativeData();
123 return new static( $text );
127 * Returns a Content object with pre-save transformations applied using
128 * Parser::preSaveTransform().
130 * @param Title $title
131 * @param User $user
132 * @param ParserOptions $popts
134 * @return Content
136 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
137 global $wgParser;
139 $text = $this->getNativeData();
140 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
141 rtrim( $pst );
143 return ( $text === $pst ) ? $this : new static( $pst );
147 * Returns a Content object with preload transformations applied (or this
148 * object if no transformations apply).
150 * @param Title $title
151 * @param ParserOptions $popts
152 * @param array $params
154 * @return Content
156 public function preloadTransform( Title $title, ParserOptions $popts, $params = array() ) {
157 global $wgParser;
159 $text = $this->getNativeData();
160 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
162 return new static( $plt );
166 * Extract the redirect target and the remaining text on the page.
168 * @note migrated here from Title::newFromRedirectInternal()
170 * @since 1.23
172 * @return array List of two elements: Title|null and string.
174 protected function getRedirectTargetAndText() {
175 global $wgMaxRedirects;
177 if ( $this->redirectTargetAndText !== null ) {
178 return $this->redirectTargetAndText;
181 if ( $wgMaxRedirects < 1 ) {
182 // redirects are disabled, so quit early
183 $this->redirectTargetAndText = array( null, $this->getNativeData() );
184 return $this->redirectTargetAndText;
187 $redir = MagicWord::get( 'redirect' );
188 $text = ltrim( $this->getNativeData() );
189 if ( $redir->matchStartAndRemove( $text ) ) {
190 // Extract the first link and see if it's usable
191 // Ensure that it really does come directly after #REDIRECT
192 // Some older redirects included a colon, so don't freak about that!
193 $m = array();
194 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
195 // Strip preceding colon used to "escape" categories, etc.
196 // and URL-decode links
197 if ( strpos( $m[1], '%' ) !== false ) {
198 // Match behavior of inline link parsing here;
199 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
201 $title = Title::newFromText( $m[1] );
202 // If the title is a redirect to bad special pages or is invalid, return null
203 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
204 $this->redirectTargetAndText = array( null, $this->getNativeData() );
205 return $this->redirectTargetAndText;
208 $this->redirectTargetAndText = array( $title, substr( $text, strlen( $m[0] ) ) );
209 return $this->redirectTargetAndText;
213 $this->redirectTargetAndText = array( null, $this->getNativeData() );
214 return $this->redirectTargetAndText;
218 * Implement redirect extraction for wikitext.
220 * @return Title|null
222 * @see Content::getRedirectTarget
224 public function getRedirectTarget() {
225 list( $title, ) = $this->getRedirectTargetAndText();
227 return $title;
231 * This implementation replaces the first link on the page with the given new target
232 * if this Content object is a redirect. Otherwise, this method returns $this.
234 * @since 1.21
236 * @param Title $target
238 * @return Content
240 * @see Content::updateRedirect()
242 public function updateRedirect( Title $target ) {
243 if ( !$this->isRedirect() ) {
244 return $this;
247 # Fix the text
248 # Remember that redirect pages can have categories, templates, etc.,
249 # so the regex has to be fairly general
250 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
251 '[[' . $target->getFullText() . ']]',
252 $this->getNativeData(), 1 );
254 return new static( $newText );
258 * Returns true if this content is not a redirect, and this content's text
259 * is countable according to the criteria defined by $wgArticleCountMethod.
261 * @param bool $hasLinks If it is known whether this content contains
262 * links, provide this information here, to avoid redundant parsing to
263 * find out (default: null).
264 * @param Title $title Optional title, defaults to the title from the current main request.
266 * @return bool
268 public function isCountable( $hasLinks = null, Title $title = null ) {
269 global $wgArticleCountMethod;
271 if ( $this->isRedirect() ) {
272 return false;
275 switch ( $wgArticleCountMethod ) {
276 case 'any':
277 return true;
278 case 'comma':
279 $text = $this->getNativeData();
280 return strpos( $text, ',' ) !== false;
281 case 'link':
282 if ( $hasLinks === null ) { # not known, find out
283 if ( !$title ) {
284 $context = RequestContext::getMain();
285 $title = $context->getTitle();
288 $po = $this->getParserOutput( $title, null, null, false );
289 $links = $po->getLinks();
290 $hasLinks = !empty( $links );
293 return $hasLinks;
296 return false;
300 * @param int $maxlength
301 * @return string
303 public function getTextForSummary( $maxlength = 250 ) {
304 $truncatedtext = parent::getTextForSummary( $maxlength );
306 # clean up unfinished links
307 # XXX: make this optional? wasn't there in autosummary, but required for
308 # deletion summary.
309 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
311 return $truncatedtext;
315 * Returns a ParserOutput object resulting from parsing the content's text
316 * using $wgParser.
318 * @param Title $title
319 * @param int $revId Revision to pass to the parser (default: null)
320 * @param ParserOptions $options (default: null)
321 * @param bool $generateHtml (default: true)
322 * @param ParserOutput &$output ParserOutput representing the HTML form of the text,
323 * may be manipulated or replaced.
325 protected function fillParserOutput( Title $title, $revId,
326 ParserOptions $options, $generateHtml, ParserOutput &$output
328 global $wgParser;
330 list( $redir, $text ) = $this->getRedirectTargetAndText();
331 $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
333 // Add redirect indicator at the top
334 if ( $redir ) {
335 // Make sure to include the redirect link in pagelinks
336 $output->addLink( $redir );
337 if ( $generateHtml ) {
338 $chain = $this->getRedirectChain();
339 $output->setText(
340 Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
341 $output->getText()
343 $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
349 * @throws MWException
351 protected function getHtml() {
352 throw new MWException(
353 "getHtml() not implemented for wikitext. "
354 . "Use getParserOutput()->getText()."
359 * This implementation calls $word->match() on the this TextContent object's text.
361 * @param MagicWord $word
363 * @return bool
365 * @see Content::matchMagicWord()
367 public function matchMagicWord( MagicWord $word ) {
368 return $word->match( $this->getNativeData() );