Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / content / WikitextContent.php
blob26337db947dea7fd64fbcab39e7b0433fc940e01
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 {
35 public function __construct( $text ) {
36 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
39 /**
40 * @see Content::getSection()
42 public function getSection( $section ) {
43 global $wgParser;
45 $text = $this->getNativeData();
46 $sect = $wgParser->getSection( $text, $section, false );
48 if ( $sect === false ) {
49 return false;
50 } else {
51 return new WikitextContent( $sect );
55 /**
56 * @see Content::replaceSection()
58 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
59 wfProfileIn( __METHOD__ );
61 $myModelId = $this->getModel();
62 $sectionModelId = $with->getModel();
64 if ( $sectionModelId != $myModelId ) {
65 wfProfileOut( __METHOD__ );
66 throw new MWException( "Incompatible content model for section: " .
67 "document uses $myModelId but " .
68 "section uses $sectionModelId." );
71 $oldtext = $this->getNativeData();
72 $text = $with->getNativeData();
74 if ( $section === '' ) {
75 wfProfileOut( __METHOD__ );
76 return $with; # XXX: copy first?
77 } if ( $section == 'new' ) {
78 # Inserting a new section
79 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
80 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
81 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
82 $text = strlen( trim( $oldtext ) ) > 0
83 ? "{$oldtext}\n\n{$subject}{$text}"
84 : "{$subject}{$text}";
86 } else {
87 # Replacing an existing section; roll out the big guns
88 global $wgParser;
90 $text = $wgParser->replaceSection( $oldtext, $section, $text );
93 $newContent = new WikitextContent( $text );
95 wfProfileOut( __METHOD__ );
96 return $newContent;
99 /**
100 * Returns a new WikitextContent object with the given section heading
101 * prepended.
103 * @param $header string
104 * @return Content
106 public function addSectionHeader( $header ) {
107 $text = wfMessage( 'newsectionheaderdefaultlevel' )
108 ->rawParams( $header )->inContentLanguage()->text();
109 $text .= "\n\n";
110 $text .= $this->getNativeData();
112 return new WikitextContent( $text );
116 * Returns a Content object with pre-save transformations applied using
117 * Parser::preSaveTransform().
119 * @param $title Title
120 * @param $user User
121 * @param $popts ParserOptions
122 * @return Content
124 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
125 global $wgParser;
127 $text = $this->getNativeData();
128 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
129 rtrim( $pst );
131 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
135 * Returns a Content object with preload transformations applied (or this
136 * object if no transformations apply).
138 * @param $title Title
139 * @param $popts ParserOptions
140 * @return Content
142 public function preloadTransform( Title $title, ParserOptions $popts ) {
143 global $wgParser;
145 $text = $this->getNativeData();
146 $plt = $wgParser->getPreloadText( $text, $title, $popts );
148 return new WikitextContent( $plt );
152 * Implement redirect extraction for wikitext.
154 * @return null|Title
156 * @note: migrated here from Title::newFromRedirectInternal()
158 * @see Content::getRedirectTarget
159 * @see AbstractContent::getRedirectTarget
161 public function getRedirectTarget() {
162 global $wgMaxRedirects;
163 if ( $wgMaxRedirects < 1 ) {
164 // redirects are disabled, so quit early
165 return null;
167 $redir = MagicWord::get( 'redirect' );
168 $text = trim( $this->getNativeData() );
169 if ( $redir->matchStartAndRemove( $text ) ) {
170 // Extract the first link and see if it's usable
171 // Ensure that it really does come directly after #REDIRECT
172 // Some older redirects included a colon, so don't freak about that!
173 $m = array();
174 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
175 // Strip preceding colon used to "escape" categories, etc.
176 // and URL-decode links
177 if ( strpos( $m[1], '%' ) !== false ) {
178 // Match behavior of inline link parsing here;
179 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
181 $title = Title::newFromText( $m[1] );
182 // If the title is a redirect to bad special pages or is invalid, return null
183 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
184 return null;
186 return $title;
189 return null;
193 * @see Content::updateRedirect()
195 * This implementation replaces the first link on the page with the given new target
196 * if this Content object is a redirect. Otherwise, this method returns $this.
198 * @since 1.21
200 * @param Title $target
202 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
204 public function updateRedirect( Title $target ) {
205 if ( !$this->isRedirect() ) {
206 return $this;
209 # Fix the text
210 # Remember that redirect pages can have categories, templates, etc.,
211 # so the regex has to be fairly general
212 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
213 '[[' . $target->getFullText() . ']]',
214 $this->getNativeData(), 1 );
216 return new WikitextContent( $newText );
220 * Returns true if this content is not a redirect, and this content's text
221 * is countable according to the criteria defined by $wgArticleCountMethod.
223 * @param bool $hasLinks if it is known whether this content contains
224 * links, provide this information here, to avoid redundant parsing to
225 * find out (default: null).
226 * @param $title Title: (default: null)
228 * @internal param \IContextSource $context context for parsing if necessary
230 * @return bool True if the content is countable
232 public function isCountable( $hasLinks = null, Title $title = null ) {
233 global $wgArticleCountMethod;
235 if ( $this->isRedirect() ) {
236 return false;
239 $text = $this->getNativeData();
241 switch ( $wgArticleCountMethod ) {
242 case 'any':
243 return true;
244 case 'comma':
245 return strpos( $text, ',' ) !== false;
246 case 'link':
247 if ( $hasLinks === null ) { # not known, find out
248 if ( !$title ) {
249 $context = RequestContext::getMain();
250 $title = $context->getTitle();
253 $po = $this->getParserOutput( $title, null, null, false );
254 $links = $po->getLinks();
255 $hasLinks = !empty( $links );
258 return $hasLinks;
261 return false;
264 public function getTextForSummary( $maxlength = 250 ) {
265 $truncatedtext = parent::getTextForSummary( $maxlength );
267 # clean up unfinished links
268 # XXX: make this optional? wasn't there in autosummary, but required for
269 # deletion summary.
270 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
272 return $truncatedtext;
276 * Returns a ParserOutput object resulting from parsing the content's text
277 * using $wgParser.
279 * @since 1.21
281 * @param $title Title
282 * @param int $revId Revision to pass to the parser (default: null)
283 * @param $options ParserOptions (default: null)
284 * @param bool $generateHtml (default: false)
286 * @internal param \IContextSource|null $context
287 * @return ParserOutput representing the HTML form of the text
289 public function getParserOutput( Title $title,
290 $revId = null,
291 ParserOptions $options = null, $generateHtml = true
293 global $wgParser;
295 if ( !$options ) {
296 //NOTE: use canonical options per default to produce cacheable output
297 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
300 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
301 return $po;
304 protected function getHtml() {
305 throw new MWException(
306 "getHtml() not implemented for wikitext. "
307 . "Use getParserOutput()->getText()."
312 * @see Content::matchMagicWord()
314 * This implementation calls $word->match() on the this TextContent object's text.
316 * @param MagicWord $word
318 * @return bool whether this Content object matches the given magic word.
320 public function matchMagicWord( MagicWord $word ) {
321 return $word->match( $this->getNativeData() );