MediaWiki master
WikitextContent.php
Go to the documentation of this file.
1<?php
28namespace MediaWiki\Content;
29
30use InvalidArgumentException;
37
45
49 private $preSaveTransformFlags = [];
50
56 public function __construct( $text ) {
57 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
58 }
59
67 public function getSection( $sectionId ) {
68 $text = $this->getText();
69 $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
70 ->getSection( $text, $sectionId, false );
71
72 if ( $sect === false ) {
73 return false;
74 } else {
75 return new static( $sect );
76 }
77 }
78
88 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
89 // @phan-suppress-previous-line PhanParamSignatureMismatch False positive
90 $myModelId = $this->getModel();
91 $sectionModelId = $with->getModel();
92
93 if ( $sectionModelId != $myModelId ) {
94 throw new InvalidArgumentException( "Incompatible content model for section: " .
95 "document uses $myModelId but " .
96 "section uses $sectionModelId." );
97 }
99 '@phan-var self $with';
100
101 $oldtext = $this->getText();
102 $text = $with->getText();
103
104 if ( strval( $sectionId ) === '' ) {
105 return $with; # XXX: copy first?
106 }
107
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}";
117 }
118 } else {
119 # Replacing an existing section; roll out the big guns
120 $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
121 ->replaceSection( $oldtext, $sectionId, $text );
122 }
123
124 $newContent = new static( $text );
125
126 return $newContent;
127 }
128
137 public function addSectionHeader( $header ) {
138 $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
139 ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
140 $text .= $this->getText();
141
142 return new static( $text );
143 }
144
153 public function getRedirectTargetAndText() {
154 wfDeprecated( __METHOD__, '1.41' );
155
156 $handler = $this->getContentHandler();
157 [ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
158
159 return [ Title::castFromLinkTarget( $target ), $content->getText() ];
160 }
161
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.
172
173 $handler = $this->getContentHandler();
174 [ $target, ] = $handler->extractRedirectTargetAndText( $this );
175
176 return Title::castFromLinkTarget( $target );
177 }
178
191 public function updateRedirect( Title $target ) {
192 if ( !$this->isRedirect() ) {
193 return $this;
194 }
195
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 );
202
203 return new static( $newText );
204 }
205
217 public function isCountable( $hasLinks = null, ?Title $title = null ) {
218 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
220
221 if ( $this->isRedirect() ) {
222 return false;
223 }
224
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();
231 }
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 $links = $po->getLinks();
236 $hasLinks = $links !== [];
237 }
238
239 return $hasLinks;
240 }
241
242 return true;
243 }
244
250 public function getTextForSummary( $maxlength = 250 ) {
251 $truncatedtext = parent::getTextForSummary( $maxlength );
252
253 # clean up unfinished links
254 # XXX: make this optional? wasn't there in autosummary, but required for
255 # deletion summary.
256 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
257
258 return $truncatedtext;
259 }
260
270 public function matchMagicWord( MagicWord $word ) {
271 return $word->match( $this->getText() );
272 }
273
281 public function setPreSaveTransformFlags( array $flags ) {
282 $this->preSaveTransformFlags = $flags;
283 }
284
291 public function getPreSaveTransformFlags() {
292 return $this->preSaveTransformFlags;
293 }
294
296 $handler = parent::getContentHandler();
297 '@phan-var WikitextContentHandler $handler';
298
299 return $handler;
300 }
301}
302
304class_alias( WikitextContent::class, 'WikitextContent' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:222
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.
Content handler for wiki text pages.
Content object for wiki text pages.
setPreSaveTransformFlags(array $flags)
Records flags set by preSaveTransform.
updateRedirect(Title $target)
This implementation replaces the first link on the page with the given new target if this Content obj...
addSectionHeader( $header)
Returns a new WikitextContent object with the given section heading prepended.
replaceSection( $sectionId, Content $with, $sectionTitle='')
isCountable( $hasLinks=null, ?Title $title=null)
Returns true if this content is not a redirect, and this content's text is countable according to the...
matchMagicWord(MagicWord $word)
This implementation calls $word->match() on the this TextContent object's text.
getPreSaveTransformFlags()
Records flags set by preSaveTransform.
getRedirectTarget()
Implement redirect extraction for wikitext.
getRedirectTargetAndText()
Extract the redirect target and the remaining text on the page.
Group all the pieces relevant to the context of a request into one instance.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
const ArticleCountMethod
Name constant for the ArticleCountMethod setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition MagicWord.php:65
match( $text)
Check if given wikitext contains the magic word.
Represents a title within MediaWiki.
Definition Title.php:78
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition Title.php:1882
Base interface for representing page content.
Definition Content.php:39
getModel()
Returns the ID of the content model used by this Content object.
replaceSection( $sectionId, Content $with, $sectionTitle='')
Replaces a section of the content and returns a Content object with the section replaced.
$header