Merge "Remove several ancient skins"
[mediawiki.git] / includes / content / WikitextContent.php
blob8be4ebabaffb75a568d808560e6c8a34986a7e95
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 throw new MWException( "Incompatible content model for section: " .
66 "document uses $myModelId but " .
67 "section uses $sectionModelId." );
70 $oldtext = $this->getNativeData();
71 $text = $with->getNativeData();
73 if ( $section === '' ) {
74 wfProfileOut( __METHOD__ );
75 return $with; # XXX: copy first?
76 } if ( $section == 'new' ) {
77 # Inserting a new section
78 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
79 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
80 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
81 $text = strlen( trim( $oldtext ) ) > 0
82 ? "{$oldtext}\n\n{$subject}{$text}"
83 : "{$subject}{$text}";
85 } else {
86 # Replacing an existing section; roll out the big guns
87 global $wgParser;
89 $text = $wgParser->replaceSection( $oldtext, $section, $text );
92 $newContent = new WikitextContent( $text );
94 wfProfileOut( __METHOD__ );
95 return $newContent;
98 /**
99 * Returns a new WikitextContent object with the given section heading
100 * prepended.
102 * @param $header string
103 * @return Content
105 public function addSectionHeader( $header ) {
106 $text = wfMessage( 'newsectionheaderdefaultlevel' )
107 ->rawParams( $header )->inContentLanguage()->text();
108 $text .= "\n\n";
109 $text .= $this->getNativeData();
111 return new WikitextContent( $text );
115 * Returns a Content object with pre-save transformations applied using
116 * Parser::preSaveTransform().
118 * @param $title Title
119 * @param $user User
120 * @param $popts ParserOptions
121 * @return Content
123 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
124 global $wgParser;
126 $text = $this->getNativeData();
127 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
128 rtrim( $pst );
130 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
134 * Returns a Content object with preload transformations applied (or this
135 * object if no transformations apply).
137 * @param $title Title
138 * @param $popts ParserOptions
139 * @return Content
141 public function preloadTransform( Title $title, ParserOptions $popts ) {
142 global $wgParser;
144 $text = $this->getNativeData();
145 $plt = $wgParser->getPreloadText( $text, $title, $popts );
147 return new WikitextContent( $plt );
151 * Implement redirect extraction for wikitext.
153 * @return null|Title
155 * @note: migrated here from Title::newFromRedirectInternal()
157 * @see Content::getRedirectTarget
158 * @see AbstractContent::getRedirectTarget
160 public function getRedirectTarget() {
161 global $wgMaxRedirects;
162 if ( $wgMaxRedirects < 1 ) {
163 // redirects are disabled, so quit early
164 return null;
166 $redir = MagicWord::get( 'redirect' );
167 $text = trim( $this->getNativeData() );
168 if ( $redir->matchStartAndRemove( $text ) ) {
169 // Extract the first link and see if it's usable
170 // Ensure that it really does come directly after #REDIRECT
171 // Some older redirects included a colon, so don't freak about that!
172 $m = array();
173 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
174 // Strip preceding colon used to "escape" categories, etc.
175 // and URL-decode links
176 if ( strpos( $m[1], '%' ) !== false ) {
177 // Match behavior of inline link parsing here;
178 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
180 $title = Title::newFromText( $m[1] );
181 // If the title is a redirect to bad special pages or is invalid, return null
182 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
183 return null;
185 return $title;
188 return null;
192 * @see Content::updateRedirect()
194 * This implementation replaces the first link on the page with the given new target
195 * if this Content object is a redirect. Otherwise, this method returns $this.
197 * @since 1.21
199 * @param Title $target
201 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
203 public function updateRedirect( Title $target ) {
204 if ( !$this->isRedirect() ) {
205 return $this;
208 # Fix the text
209 # Remember that redirect pages can have categories, templates, etc.,
210 # so the regex has to be fairly general
211 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
212 '[[' . $target->getFullText() . ']]',
213 $this->getNativeData(), 1 );
215 return new WikitextContent( $newText );
219 * Returns true if this content is not a redirect, and this content's text
220 * is countable according to the criteria defined by $wgArticleCountMethod.
222 * @param bool $hasLinks if it is known whether this content contains
223 * links, provide this information here, to avoid redundant parsing to
224 * find out (default: null).
225 * @param $title Title: (default: null)
227 * @internal param \IContextSource $context context for parsing if necessary
229 * @return bool True if the content is countable
231 public function isCountable( $hasLinks = null, Title $title = null ) {
232 global $wgArticleCountMethod;
234 if ( $this->isRedirect() ) {
235 return false;
238 $text = $this->getNativeData();
240 switch ( $wgArticleCountMethod ) {
241 case 'any':
242 return true;
243 case 'comma':
244 return strpos( $text, ',' ) !== false;
245 case 'link':
246 if ( $hasLinks === null ) { # not known, find out
247 if ( !$title ) {
248 $context = RequestContext::getMain();
249 $title = $context->getTitle();
252 $po = $this->getParserOutput( $title, null, null, false );
253 $links = $po->getLinks();
254 $hasLinks = !empty( $links );
257 return $hasLinks;
260 return false;
263 public function getTextForSummary( $maxlength = 250 ) {
264 $truncatedtext = parent::getTextForSummary( $maxlength );
266 # clean up unfinished links
267 # XXX: make this optional? wasn't there in autosummary, but required for
268 # deletion summary.
269 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
271 return $truncatedtext;
275 * Returns a ParserOutput object resulting from parsing the content's text
276 * using $wgParser.
278 * @since 1.21
280 * @param $title Title
281 * @param int $revId Revision to pass to the parser (default: null)
282 * @param $options ParserOptions (default: null)
283 * @param bool $generateHtml (default: false)
285 * @internal param \IContextSource|null $context
286 * @return ParserOutput representing the HTML form of the text
288 public function getParserOutput( Title $title,
289 $revId = null,
290 ParserOptions $options = null, $generateHtml = true
292 global $wgParser;
294 if ( !$options ) {
295 //NOTE: use canonical options per default to produce cacheable output
296 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
299 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
300 return $po;
303 protected function getHtml() {
304 throw new MWException(
305 "getHtml() not implemented for wikitext. "
306 . "Use getParserOutput()->getText()."
311 * @see Content::matchMagicWord()
313 * This implementation calls $word->match() on the this TextContent object's text.
315 * @param MagicWord $word
317 * @return bool whether this Content object matches the given magic word.
319 public function matchMagicWord( MagicWord $word ) {
320 return $word->match( $this->getNativeData() );