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|int $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|int|null|bool $sectionId
62 * @param Content $with
63 * @param string $sectionTitle
68 * @see Content::replaceSection()
70 public function replaceSection( $sectionId, Content
$with, $sectionTitle = '' ) {
71 $myModelId = $this->getModel();
72 $sectionModelId = $with->getModel();
74 if ( $sectionModelId != $myModelId ) {
75 throw new MWException( "Incompatible content model for section: " .
76 "document uses $myModelId but " .
77 "section uses $sectionModelId." );
80 $oldtext = $this->getNativeData();
81 $text = $with->getNativeData();
83 if ( strval( $sectionId ) === '' ) {
84 return $with; # XXX: copy first?
87 if ( $sectionId === 'new' ) {
88 # Inserting a new section
89 $subject = $sectionTitle ?
wfMessage( 'newsectionheaderdefaultlevel' )
90 ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
91 if ( Hooks
::run( 'PlaceNewSection', [ $this, $oldtext, $subject, &$text ] ) ) {
92 $text = strlen( trim( $oldtext ) ) > 0
93 ?
"{$oldtext}\n\n{$subject}{$text}"
94 : "{$subject}{$text}";
97 # Replacing an existing section; roll out the big guns
100 $text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
103 $newContent = new static( $text );
109 * Returns a new WikitextContent object with the given section heading
112 * @param string $header
116 public function addSectionHeader( $header ) {
117 $text = wfMessage( 'newsectionheaderdefaultlevel' )
118 ->rawParams( $header )->inContentLanguage()->text();
120 $text .= $this->getNativeData();
122 return new static( $text );
126 * Returns a Content object with pre-save transformations applied using
127 * Parser::preSaveTransform().
129 * @param Title $title
131 * @param ParserOptions $popts
135 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
138 $text = $this->getNativeData();
139 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
141 return ( $text === $pst ) ?
$this : new static( $pst );
145 * Returns a Content object with preload transformations applied (or this
146 * object if no transformations apply).
148 * @param Title $title
149 * @param ParserOptions $popts
150 * @param array $params
154 public function preloadTransform( Title
$title, ParserOptions
$popts, $params = [] ) {
157 $text = $this->getNativeData();
158 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
160 return new static( $plt );
164 * Extract the redirect target and the remaining text on the page.
166 * @note migrated here from Title::newFromRedirectInternal()
170 * @return array List of two elements: Title|null and string.
172 protected function getRedirectTargetAndText() {
173 global $wgMaxRedirects;
175 if ( $this->redirectTargetAndText
!== null ) {
176 return $this->redirectTargetAndText
;
179 if ( $wgMaxRedirects < 1 ) {
180 // redirects are disabled, so quit early
181 $this->redirectTargetAndText
= [ null, $this->getNativeData() ];
182 return $this->redirectTargetAndText
;
185 $redir = MagicWord
::get( 'redirect' );
186 $text = ltrim( $this->getNativeData() );
187 if ( $redir->matchStartAndRemove( $text ) ) {
188 // Extract the first link and see if it's usable
189 // Ensure that it really does come directly after #REDIRECT
190 // Some older redirects included a colon, so don't freak about that!
192 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
193 // Strip preceding colon used to "escape" categories, etc.
194 // and URL-decode links
195 if ( strpos( $m[1], '%' ) !== false ) {
196 // Match behavior of inline link parsing here;
197 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
199 $title = Title
::newFromText( $m[1] );
200 // If the title is a redirect to bad special pages or is invalid, return null
201 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
202 $this->redirectTargetAndText
= [ null, $this->getNativeData() ];
203 return $this->redirectTargetAndText
;
206 $this->redirectTargetAndText
= [ $title, substr( $text, strlen( $m[0] ) ) ];
207 return $this->redirectTargetAndText
;
211 $this->redirectTargetAndText
= [ null, $this->getNativeData() ];
212 return $this->redirectTargetAndText
;
216 * Implement redirect extraction for wikitext.
220 * @see Content::getRedirectTarget
222 public function getRedirectTarget() {
223 list( $title, ) = $this->getRedirectTargetAndText();
229 * This implementation replaces the first link on the page with the given new target
230 * if this Content object is a redirect. Otherwise, this method returns $this.
234 * @param Title $target
238 * @see Content::updateRedirect()
240 public function updateRedirect( Title
$target ) {
241 if ( !$this->isRedirect() ) {
246 # Remember that redirect pages can have categories, templates, etc.,
247 # so the regex has to be fairly general
248 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
249 '[[' . $target->getFullText() . ']]',
250 $this->getNativeData(), 1 );
252 return new static( $newText );
256 * Returns true if this content is not a redirect, and this content's text
257 * is countable according to the criteria defined by $wgArticleCountMethod.
259 * @param bool|null $hasLinks If it is known whether this content contains
260 * links, provide this information here, to avoid redundant parsing to
261 * find out (default: null).
262 * @param Title|null $title Optional title, defaults to the title from the current main request.
266 public function isCountable( $hasLinks = null, Title
$title = null ) {
267 global $wgArticleCountMethod;
269 if ( $this->isRedirect() ) {
273 switch ( $wgArticleCountMethod ) {
277 $text = $this->getNativeData();
278 return strpos( $text, ',' ) !== false;
280 if ( $hasLinks === null ) { # not known, find out
282 $context = RequestContext
::getMain();
283 $title = $context->getTitle();
286 $po = $this->getParserOutput( $title, null, null, false );
287 $links = $po->getLinks();
288 $hasLinks = !empty( $links );
298 * @param int $maxlength
301 public function getTextForSummary( $maxlength = 250 ) {
302 $truncatedtext = parent
::getTextForSummary( $maxlength );
304 # clean up unfinished links
305 # XXX: make this optional? wasn't there in autosummary, but required for
307 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
309 return $truncatedtext;
313 * Returns a ParserOutput object resulting from parsing the content's text
316 * @param Title $title
317 * @param int $revId Revision to pass to the parser (default: null)
318 * @param ParserOptions $options (default: null)
319 * @param bool $generateHtml (default: true)
320 * @param ParserOutput &$output ParserOutput representing the HTML form of the text,
321 * may be manipulated or replaced.
323 protected function fillParserOutput( Title
$title, $revId,
324 ParserOptions
$options, $generateHtml, ParserOutput
&$output
328 list( $redir, $text ) = $this->getRedirectTargetAndText();
329 $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
331 // Add redirect indicator at the top
333 // Make sure to include the redirect link in pagelinks
334 $output->addLink( $redir );
335 if ( $generateHtml ) {
336 $chain = $this->getRedirectChain();
338 Article
::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
339 $output->getRawText()
341 $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
347 * @throws MWException
349 protected function getHtml() {
350 throw new MWException(
351 "getHtml() not implemented for wikitext. "
352 . "Use getParserOutput()->getText()."
357 * This implementation calls $word->match() on the this TextContent object's text.
359 * @param MagicWord $word
363 * @see Content::matchMagicWord()
365 public function matchMagicWord( MagicWord
$word ) {
366 return $word->match( $this->getNativeData() );