Add release note for Ieec65c90
[mediawiki.git] / includes / content / WikitextContent.php
blobc11018bca316b89b93c8fecac589f8529661ce87
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @since 1.21
20 * @file
21 * @ingroup Content
23 * @author Daniel Kinzler
25 class WikitextContent extends TextContent {
27 public function __construct( $text ) {
28 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
31 /**
32 * @see Content::getSection()
34 public function getSection( $section ) {
35 global $wgParser;
37 $text = $this->getNativeData();
38 $sect = $wgParser->getSection( $text, $section, false );
40 if ( $sect === false ) {
41 return false;
42 } else {
43 return new WikitextContent( $sect );
47 /**
48 * @see Content::replaceSection()
50 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
51 wfProfileIn( __METHOD__ );
53 $myModelId = $this->getModel();
54 $sectionModelId = $with->getModel();
56 if ( $sectionModelId != $myModelId ) {
57 throw new MWException( "Incompatible content model for section: " .
58 "document uses $myModelId but " .
59 "section uses $sectionModelId." );
62 $oldtext = $this->getNativeData();
63 $text = $with->getNativeData();
65 if ( $section === '' ) {
66 return $with; # XXX: copy first?
67 } if ( $section == 'new' ) {
68 # Inserting a new section
69 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
70 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
71 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
72 $text = strlen( trim( $oldtext ) ) > 0
73 ? "{$oldtext}\n\n{$subject}{$text}"
74 : "{$subject}{$text}";
76 } else {
77 # Replacing an existing section; roll out the big guns
78 global $wgParser;
80 $text = $wgParser->replaceSection( $oldtext, $section, $text );
83 $newContent = new WikitextContent( $text );
85 wfProfileOut( __METHOD__ );
86 return $newContent;
89 /**
90 * Returns a new WikitextContent object with the given section heading
91 * prepended.
93 * @param $header string
94 * @return Content
96 public function addSectionHeader( $header ) {
97 $text = wfMessage( 'newsectionheaderdefaultlevel' )
98 ->rawParams( $header )->inContentLanguage()->text();
99 $text .= "\n\n";
100 $text .= $this->getNativeData();
102 return new WikitextContent( $text );
106 * Returns a Content object with pre-save transformations applied using
107 * Parser::preSaveTransform().
109 * @param $title Title
110 * @param $user User
111 * @param $popts ParserOptions
112 * @return Content
114 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
115 global $wgParser;
117 $text = $this->getNativeData();
118 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
119 rtrim( $pst );
121 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
125 * Returns a Content object with preload transformations applied (or this
126 * object if no transformations apply).
128 * @param $title Title
129 * @param $popts ParserOptions
130 * @return Content
132 public function preloadTransform( Title $title, ParserOptions $popts ) {
133 global $wgParser;
135 $text = $this->getNativeData();
136 $plt = $wgParser->getPreloadText( $text, $title, $popts );
138 return new WikitextContent( $plt );
142 * Implement redirect extraction for wikitext.
144 * @return null|Title
146 * @note: migrated here from Title::newFromRedirectInternal()
148 * @see Content::getRedirectTarget
149 * @see AbstractContent::getRedirectTarget
151 public function getRedirectTarget() {
152 global $wgMaxRedirects;
153 if ( $wgMaxRedirects < 1 ) {
154 // redirects are disabled, so quit early
155 return null;
157 $redir = MagicWord::get( 'redirect' );
158 $text = trim( $this->getNativeData() );
159 if ( $redir->matchStartAndRemove( $text ) ) {
160 // Extract the first link and see if it's usable
161 // Ensure that it really does come directly after #REDIRECT
162 // Some older redirects included a colon, so don't freak about that!
163 $m = array();
164 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
165 // Strip preceding colon used to "escape" categories, etc.
166 // and URL-decode links
167 if ( strpos( $m[1], '%' ) !== false ) {
168 // Match behavior of inline link parsing here;
169 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
171 $title = Title::newFromText( $m[1] );
172 // If the title is a redirect to bad special pages or is invalid, return null
173 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
174 return null;
176 return $title;
179 return null;
183 * @see Content::updateRedirect()
185 * This implementation replaces the first link on the page with the given new target
186 * if this Content object is a redirect. Otherwise, this method returns $this.
188 * @since 1.21
190 * @param Title $target
192 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
194 public function updateRedirect( Title $target ) {
195 if ( !$this->isRedirect() ) {
196 return $this;
199 # Fix the text
200 # Remember that redirect pages can have categories, templates, etc.,
201 # so the regex has to be fairly general
202 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
203 '[[' . $target->getFullText() . ']]',
204 $this->getNativeData(), 1 );
206 return new WikitextContent( $newText );
210 * Returns true if this content is not a redirect, and this content's text
211 * is countable according to the criteria defined by $wgArticleCountMethod.
213 * @param $hasLinks Bool if it is known whether this content contains
214 * links, provide this information here, to avoid redundant parsing to
215 * find out (default: null).
216 * @param $title Title: (default: null)
218 * @internal param \IContextSource $context context for parsing if necessary
220 * @return bool True if the content is countable
222 public function isCountable( $hasLinks = null, Title $title = null ) {
223 global $wgArticleCountMethod;
225 if ( $this->isRedirect( ) ) {
226 return false;
229 $text = $this->getNativeData();
231 switch ( $wgArticleCountMethod ) {
232 case 'any':
233 return true;
234 case 'comma':
235 return strpos( $text, ',' ) !== false;
236 case 'link':
237 if ( $hasLinks === null ) { # not known, find out
238 if ( !$title ) {
239 $context = RequestContext::getMain();
240 $title = $context->getTitle();
243 $po = $this->getParserOutput( $title, null, null, false );
244 $links = $po->getLinks();
245 $hasLinks = !empty( $links );
248 return $hasLinks;
251 return false;
254 public function getTextForSummary( $maxlength = 250 ) {
255 $truncatedtext = parent::getTextForSummary( $maxlength );
257 # clean up unfinished links
258 # XXX: make this optional? wasn't there in autosummary, but required for
259 # deletion summary.
260 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
262 return $truncatedtext;
266 * Returns a ParserOutput object resulting from parsing the content's text
267 * using $wgParser.
269 * @since 1.21
271 * @param $title Title
272 * @param $revId int Revision to pass to the parser (default: null)
273 * @param $options ParserOptions (default: null)
274 * @param $generateHtml bool (default: false)
276 * @internal param \IContextSource|null $context
277 * @return ParserOutput representing the HTML form of the text
279 public function getParserOutput( Title $title,
280 $revId = null,
281 ParserOptions $options = null, $generateHtml = true
283 global $wgParser;
285 if ( !$options ) {
286 //NOTE: use canonical options per default to produce cacheable output
287 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
290 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
291 return $po;
294 protected function getHtml() {
295 throw new MWException(
296 "getHtml() not implemented for wikitext. "
297 . "Use getParserOutput()->getText()."
302 * @see Content::matchMagicWord()
304 * This implementation calls $word->match() on the this TextContent object's text.
306 * @param MagicWord $word
308 * @return bool whether this Content object matches the given magic word.
310 public function matchMagicWord( MagicWord $word ) {
311 return $word->match( $this->getNativeData() );