Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / WikitextContent.php
blobda98cb23cd86d26e2b6cdd67fd9c108aaef82204
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 namespace MediaWiki\Content;
30 use InvalidArgumentException;
31 use MediaWiki\Context\RequestContext;
32 use MediaWiki\HookContainer\HookRunner;
33 use MediaWiki\MainConfigNames;
34 use MediaWiki\MediaWikiServices;
35 use MediaWiki\Parser\MagicWord;
36 use MediaWiki\Title\Title;
38 /**
39 * Content object for wiki text pages.
41 * @newable
42 * @ingroup Content
44 class WikitextContent extends TextContent {
46 /**
47 * @var string[] flags set by PST
49 private $preSaveTransformFlags = [];
51 /**
52 * @stable to call
54 * @param string $text
56 public function __construct( $text ) {
57 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
60 /**
61 * @param string|int $sectionId
63 * @return Content|false|null
65 * @see Content::getSection()
67 public function getSection( $sectionId ) {
68 $text = $this->getText();
69 $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
70 ->getSection( $text, $sectionId, false );
72 if ( $sect === false ) {
73 return false;
74 } else {
75 return new static( $sect );
79 /**
80 * @param string|int|null|false $sectionId
81 * @param Content $with New section content, must have the same content model as $this.
82 * @param string $sectionTitle
84 * @return Content
86 * @see Content::replaceSection()
88 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
89 // @phan-suppress-previous-line PhanParamSignatureMismatch False positive
90 $myModelId = $this->getModel();
91 $sectionModelId = $with->getModel();
93 if ( $sectionModelId != $myModelId ) {
94 throw new InvalidArgumentException( "Incompatible content model for section: " .
95 "document uses $myModelId but " .
96 "section uses $sectionModelId." );
98 /** @var self $with $oldtext */
99 '@phan-var self $with';
101 $oldtext = $this->getText();
102 $text = $with->getText();
104 if ( strval( $sectionId ) === '' ) {
105 return $with; # XXX: copy first?
108 if ( $sectionId === 'new' ) {
109 # Inserting a new section
110 $subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
111 ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
112 $hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) );
113 if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
114 $text = strlen( trim( $oldtext ) ) > 0
115 ? "{$oldtext}\n\n{$subject}{$text}"
116 : "{$subject}{$text}";
118 } else {
119 # Replacing an existing section; roll out the big guns
120 $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
121 ->replaceSection( $oldtext, $sectionId, $text );
124 $newContent = new static( $text );
126 return $newContent;
130 * Returns a new WikitextContent object with the given section heading
131 * prepended.
133 * @param string $header
135 * @return Content
137 public function addSectionHeader( $header ) {
138 $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
139 ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
140 $text .= $this->getText();
142 return new static( $text );
146 * Extract the redirect target and the remaining text on the page.
148 * @since 1.23
149 * @deprecated since 1.41, use WikitextContentHandler::getRedirectTargetAndText
151 * @return array List of two elements: Title|null and string.
153 public function getRedirectTargetAndText() {
154 wfDeprecated( __METHOD__, '1.41' );
156 $handler = $this->getContentHandler();
157 [ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
159 return [ Title::castFromLinkTarget( $target ), $content->getText() ];
163 * Implement redirect extraction for wikitext.
165 * @return Title|null
167 * @see Content::getRedirectTarget
169 public function getRedirectTarget() {
170 // TODO: The redirect target should be injected on construction.
171 // But that only works if the object is created by WikitextContentHandler.
173 $handler = $this->getContentHandler();
174 [ $target, ] = $handler->extractRedirectTargetAndText( $this );
176 return Title::castFromLinkTarget( $target );
180 * This implementation replaces the first link on the page with the given new target
181 * if this Content object is a redirect. Otherwise, this method returns $this.
183 * @since 1.21
185 * @param Title $target
187 * @return Content
189 * @see Content::updateRedirect()
191 public function updateRedirect( Title $target ) {
192 if ( !$this->isRedirect() ) {
193 return $this;
196 # Fix the text
197 # Remember that redirect pages can have categories, templates, etc.,
198 # so the regex has to be fairly general
199 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
200 '[[' . $target->getFullText() . ']]',
201 $this->getText(), 1 );
203 return new static( $newText );
207 * Returns true if this content is not a redirect, and this content's text
208 * is countable according to the criteria defined by $wgArticleCountMethod.
210 * @param bool|null $hasLinks If it is known whether this content contains
211 * links, provide this information here, to avoid redundant parsing to
212 * find out (default: null).
213 * @param Title|null $title Optional title, defaults to the title from the current main request.
215 * @return bool
217 public function isCountable( $hasLinks = null, ?Title $title = null ) {
218 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
219 ->get( MainConfigNames::ArticleCountMethod );
221 if ( $this->isRedirect() ) {
222 return false;
225 if ( $articleCountMethod === 'link' ) {
226 if ( $hasLinks === null ) { # not known, find out
227 // @TODO: require an injected title
228 if ( !$title ) {
229 $context = RequestContext::getMain();
230 $title = $context->getTitle();
232 $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
233 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
234 $po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
235 $hasLinks = $po->hasLinks();
238 return $hasLinks;
241 return true;
245 * @param int $maxlength
247 * @return string
249 public function getTextForSummary( $maxlength = 250 ) {
250 $truncatedtext = parent::getTextForSummary( $maxlength );
252 # clean up unfinished links
253 # XXX: make this optional? wasn't there in autosummary, but required for
254 # deletion summary.
255 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
257 return $truncatedtext;
261 * This implementation calls $word->match() on the this TextContent object's text.
263 * @param MagicWord $word
265 * @return bool
267 * @see Content::matchMagicWord()
269 public function matchMagicWord( MagicWord $word ) {
270 return $word->match( $this->getText() );
274 * Records flags set by preSaveTransform
276 * @internal for use by WikitextContentHandler
278 * @param string[] $flags
280 public function setPreSaveTransformFlags( array $flags ) {
281 $this->preSaveTransformFlags = $flags;
285 * Records flags set by preSaveTransform
287 * @internal for use by WikitextContentHandler
288 * @return string[]
290 public function getPreSaveTransformFlags() {
291 return $this->preSaveTransformFlags;
294 public function getContentHandler(): WikitextContentHandler {
295 $handler = parent::getContentHandler();
296 '@phan-var WikitextContentHandler $handler';
298 return $handler;
302 /** @deprecated class alias since 1.43 */
303 class_alias( WikitextContent::class, 'WikitextContent' );