MediaWiki master
WikitextContent.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
9use InvalidArgumentException;
16
26
30 private $preSaveTransformFlags = [];
31
37 public function __construct( $text ) {
38 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
39 }
40
48 public function getSection( $sectionId ) {
49 $text = $this->getText();
50 $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
51 ->getSection( $text, $sectionId, false );
52
53 if ( $sect === false ) {
54 return false;
55 } else {
56 return new static( $sect );
57 }
58 }
59
69 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
70 // @phan-suppress-previous-line PhanParamSignatureMismatch False positive
71 $myModelId = $this->getModel();
72 $sectionModelId = $with->getModel();
73
74 if ( $sectionModelId != $myModelId ) {
75 throw new InvalidArgumentException( "Incompatible content model for section: " .
76 "document uses $myModelId but " .
77 "section uses $sectionModelId." );
78 }
80 '@phan-var self $with';
81
82 $oldtext = $this->getText();
83 $text = $with->getText();
84
85 if ( strval( $sectionId ) === '' ) {
86 return $with; # XXX: copy first?
87 }
88
89 if ( $sectionId === 'new' ) {
90 # Inserting a new section
91 $subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
92 ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
93 $hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) );
94 if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
95 $text = trim( $oldtext ) !== ''
96 ? "{$oldtext}\n\n{$subject}{$text}"
97 : "{$subject}{$text}";
98 }
99 } else {
100 # Replacing an existing section; roll out the big guns
101 $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
102 ->replaceSection( $oldtext, $sectionId, $text );
103 }
104
105 $newContent = new static( $text );
106
107 return $newContent;
108 }
109
118 public function addSectionHeader( $header ) {
119 $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
120 ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
121 $text .= $this->getText();
122
123 return new static( $text );
124 }
125
134 public function getRedirectTargetAndText() {
135 wfDeprecated( __METHOD__, '1.41' );
136
137 $handler = $this->getContentHandler();
138 [ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
139
140 return [ Title::castFromLinkTarget( $target ), $content->getText() ];
141 }
142
150 public function getRedirectTarget() {
151 // TODO: The redirect target should be injected on construction.
152 // But that only works if the object is created by WikitextContentHandler.
153
154 $handler = $this->getContentHandler();
155 [ $target, ] = $handler->extractRedirectTargetAndText( $this );
156
157 return Title::castFromLinkTarget( $target );
158 }
159
172 public function updateRedirect( Title $target ) {
173 if ( !$this->isRedirect() ) {
174 return $this;
175 }
176
177 # Fix the text
178 # Remember that redirect pages can have categories, templates, etc.,
179 # so the regex has to be fairly general
180 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
181 '[[' . $target->getFullText() . ']]',
182 $this->getText(), 1 );
183
184 return new static( $newText );
185 }
186
198 public function isCountable( $hasLinks = null, ?Title $title = null ) {
199 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
201
202 if ( $this->isRedirect() ) {
203 return false;
204 }
205
206 if ( $articleCountMethod === 'link' ) {
207 if ( $hasLinks === null ) { # not known, find out
208 // @TODO: require an injected title
209 if ( !$title ) {
210 $context = RequestContext::getMain();
211 $title = $context->getTitle();
212 }
213 $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
214 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
215 $po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
216 $hasLinks = $po->hasLinks();
217 }
218
219 return $hasLinks;
220 }
221
222 return true;
223 }
224
230 public function getTextForSummary( $maxlength = 250 ) {
231 $truncatedtext = parent::getTextForSummary( $maxlength );
232
233 # clean up unfinished links
234 # XXX: make this optional? wasn't there in autosummary, but required for
235 # deletion summary.
236 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
237
238 return $truncatedtext;
239 }
240
250 public function matchMagicWord( MagicWord $word ) {
251 return $word->match( $this->getText() );
252 }
253
261 public function setPreSaveTransformFlags( array $flags ) {
262 $this->preSaveTransformFlags = $flags;
263 }
264
271 public function getPreSaveTransformFlags() {
272 return $this->preSaveTransformFlags;
273 }
274
276 $handler = parent::getContentHandler();
277 '@phan-var WikitextContentHandler $handler';
278
279 return $handler;
280 }
281}
282
284class_alias( WikitextContent::class, 'WikitextContent' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
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:51
match( $text)
Check if given wikitext contains the magic word.
Represents a title within MediaWiki.
Definition Title.php:70
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition Title.php:1882
Content objects represent page content, e.g.
Definition Content.php:28
getModel()
Get the content model ID.
replaceSection( $sectionId, Content $with, $sectionTitle='')
Create a derived Content with the specified section added or replaced.