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
25 * @author Daniel Kinzler
29 * Content object for wiki text pages.
33 class WikitextContent
extends TextContent
{
34 private $redirectTargetAndText = null;
36 public function __construct( $text ) {
37 parent
::__construct( $text, CONTENT_MODEL_WIKITEXT
);
41 * @param string|number $sectionId
43 * @return Content|bool|null
45 * @see Content::getSection()
47 public function getSection( $sectionId ) {
50 $text = $this->getNativeData();
51 $sect = $wgParser->getSection( $text, $sectionId, false );
53 if ( $sect === false ) {
56 return new static( $sect );
61 * @param string|number|null|bool $sectionId
62 * @param Content $with
63 * @param string $sectionTitle
68 * @see Content::replaceSection()
70 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
71 wfProfileIn( __METHOD__
);
73 $myModelId = $this->getModel();
74 $sectionModelId = $with->getModel();
76 if ( $sectionModelId != $myModelId ) {
77 wfProfileOut( __METHOD__
);
78 throw new MWException( "Incompatible content model for section: " .
79 "document uses $myModelId but " .
80 "section uses $sectionModelId." );
83 $oldtext = $this->getNativeData();
84 $text = $with->getNativeData();
86 if ( strval( $sectionId ) === '' ) {
87 wfProfileOut( __METHOD__
);
89 return $with; # XXX: copy first?
92 if ( $sectionId === 'new' ) {
93 # Inserting a new section
94 $subject = $sectionTitle ?
wfMessage( 'newsectionheaderdefaultlevel' )
95 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
96 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
97 $text = strlen( trim( $oldtext ) ) > 0
98 ?
"{$oldtext}\n\n{$subject}{$text}"
99 : "{$subject}{$text}";
102 # Replacing an existing section; roll out the big guns
105 $text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
108 $newContent = new static( $text );
110 wfProfileOut( __METHOD__
);
116 * Returns a new WikitextContent object with the given section heading
119 * @param string $header
123 public function addSectionHeader( $header ) {
124 $text = wfMessage( 'newsectionheaderdefaultlevel' )
125 ->rawParams( $header )->inContentLanguage()->text();
127 $text .= $this->getNativeData();
129 return new static( $text );
133 * Returns a Content object with pre-save transformations applied using
134 * Parser::preSaveTransform().
136 * @param Title $title
138 * @param ParserOptions $popts
142 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
145 $text = $this->getNativeData();
146 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
149 return ( $text === $pst ) ?
$this : new static( $pst );
153 * Returns a Content object with preload transformations applied (or this
154 * object if no transformations apply).
156 * @param Title $title
157 * @param ParserOptions $popts
158 * @param array $params
162 public function preloadTransform( Title
$title, ParserOptions
$popts, $params = array() ) {
165 $text = $this->getNativeData();
166 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
168 return new static( $plt );
172 * Extract the redirect target and the remaining text on the page.
174 * @note migrated here from Title::newFromRedirectInternal()
178 * @return array List of two elements: Title|null and string.
180 protected function getRedirectTargetAndText() {
181 global $wgMaxRedirects;
183 if ( $this->redirectTargetAndText
!== null ) {
184 return $this->redirectTargetAndText
;
187 if ( $wgMaxRedirects < 1 ) {
188 // redirects are disabled, so quit early
189 $this->redirectTargetAndText
= array( null, $this->getNativeData() );
190 return $this->redirectTargetAndText
;
193 $redir = MagicWord
::get( 'redirect' );
194 $text = ltrim( $this->getNativeData() );
195 if ( $redir->matchStartAndRemove( $text ) ) {
196 // Extract the first link and see if it's usable
197 // Ensure that it really does come directly after #REDIRECT
198 // Some older redirects included a colon, so don't freak about that!
200 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
201 // Strip preceding colon used to "escape" categories, etc.
202 // and URL-decode links
203 if ( strpos( $m[1], '%' ) !== false ) {
204 // Match behavior of inline link parsing here;
205 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
207 $title = Title
::newFromText( $m[1] );
208 // If the title is a redirect to bad special pages or is invalid, return null
209 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
210 $this->redirectTargetAndText
= array( null, $this->getNativeData() );
211 return $this->redirectTargetAndText
;
214 $this->redirectTargetAndText
= array( $title, substr( $text, strlen( $m[0] ) ) );
215 return $this->redirectTargetAndText
;
219 $this->redirectTargetAndText
= array( null, $this->getNativeData() );
220 return $this->redirectTargetAndText
;
224 * Implement redirect extraction for wikitext.
228 * @see Content::getRedirectTarget
230 public function getRedirectTarget() {
231 list( $title, ) = $this->getRedirectTargetAndText();
237 * This implementation replaces the first link on the page with the given new target
238 * if this Content object is a redirect. Otherwise, this method returns $this.
242 * @param Title $target
246 * @see Content::updateRedirect()
248 public function updateRedirect( Title
$target ) {
249 if ( !$this->isRedirect() ) {
254 # Remember that redirect pages can have categories, templates, etc.,
255 # so the regex has to be fairly general
256 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
257 '[[' . $target->getFullText() . ']]',
258 $this->getNativeData(), 1 );
260 return new static( $newText );
264 * Returns true if this content is not a redirect, and this content's text
265 * is countable according to the criteria defined by $wgArticleCountMethod.
267 * @param bool $hasLinks If it is known whether this content contains
268 * links, provide this information here, to avoid redundant parsing to
269 * find out (default: null).
270 * @param Title $title Optional title, defaults to the title from the current main request.
274 public function isCountable( $hasLinks = null, Title
$title = null ) {
275 global $wgArticleCountMethod;
277 if ( $this->isRedirect() ) {
281 $text = $this->getNativeData();
283 switch ( $wgArticleCountMethod ) {
287 return strpos( $text, ',' ) !== false;
289 if ( $hasLinks === null ) { # not known, find out
291 $context = RequestContext
::getMain();
292 $title = $context->getTitle();
295 $po = $this->getParserOutput( $title, null, null, false );
296 $links = $po->getLinks();
297 $hasLinks = !empty( $links );
307 * @param int $maxlength
310 public function getTextForSummary( $maxlength = 250 ) {
311 $truncatedtext = parent
::getTextForSummary( $maxlength );
313 # clean up unfinished links
314 # XXX: make this optional? wasn't there in autosummary, but required for
316 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
318 return $truncatedtext;
322 * Returns a ParserOutput object resulting from parsing the content's text
325 * @param Title $title
326 * @param int $revId Revision to pass to the parser (default: null)
327 * @param ParserOptions $options (default: null)
328 * @param bool $generateHtml (default: true)
329 * @param ParserOutput &$output ParserOutput representing the HTML form of the text,
330 * may be manipulated or replaced.
332 protected function fillParserOutput( Title
$title, $revId,
333 ParserOptions
$options, $generateHtml, ParserOutput
&$output
337 list( $redir, $text ) = $this->getRedirectTargetAndText();
338 $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
340 // Add redirect indicator at the top
342 // Make sure to include the redirect link in pagelinks
343 $output->addLink( $redir );
344 if ( $generateHtml ) {
345 $chain = $this->getRedirectChain();
347 Article
::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
350 $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
356 * @throws MWException
358 protected function getHtml() {
359 throw new MWException(
360 "getHtml() not implemented for wikitext. "
361 . "Use getParserOutput()->getText()."
366 * This implementation calls $word->match() on the this TextContent object's text.
368 * @param MagicWord $word
372 * @see Content::matchMagicWord()
374 public function matchMagicWord( MagicWord
$word ) {
375 return $word->match( $this->getNativeData() );