MediaWiki master
WikitextContent.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
23use InvalidArgumentException;
30
40
44 private $preSaveTransformFlags = [];
45
51 public function __construct( $text ) {
52 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
53 }
54
62 public function getSection( $sectionId ) {
63 $text = $this->getText();
64 $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
65 ->getSection( $text, $sectionId, false );
66
67 if ( $sect === false ) {
68 return false;
69 } else {
70 return new static( $sect );
71 }
72 }
73
83 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
84 // @phan-suppress-previous-line PhanParamSignatureMismatch False positive
85 $myModelId = $this->getModel();
86 $sectionModelId = $with->getModel();
87
88 if ( $sectionModelId != $myModelId ) {
89 throw new InvalidArgumentException( "Incompatible content model for section: " .
90 "document uses $myModelId but " .
91 "section uses $sectionModelId." );
92 }
94 '@phan-var self $with';
95
96 $oldtext = $this->getText();
97 $text = $with->getText();
98
99 if ( strval( $sectionId ) === '' ) {
100 return $with; # XXX: copy first?
101 }
102
103 if ( $sectionId === 'new' ) {
104 # Inserting a new section
105 $subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
106 ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
107 $hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) );
108 if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
109 $text = trim( $oldtext ) !== ''
110 ? "{$oldtext}\n\n{$subject}{$text}"
111 : "{$subject}{$text}";
112 }
113 } else {
114 # Replacing an existing section; roll out the big guns
115 $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
116 ->replaceSection( $oldtext, $sectionId, $text );
117 }
118
119 $newContent = new static( $text );
120
121 return $newContent;
122 }
123
132 public function addSectionHeader( $header ) {
133 $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
134 ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
135 $text .= $this->getText();
136
137 return new static( $text );
138 }
139
148 public function getRedirectTargetAndText() {
149 wfDeprecated( __METHOD__, '1.41' );
150
151 $handler = $this->getContentHandler();
152 [ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
153
154 return [ Title::castFromLinkTarget( $target ), $content->getText() ];
155 }
156
164 public function getRedirectTarget() {
165 // TODO: The redirect target should be injected on construction.
166 // But that only works if the object is created by WikitextContentHandler.
167
168 $handler = $this->getContentHandler();
169 [ $target, ] = $handler->extractRedirectTargetAndText( $this );
170
171 return Title::castFromLinkTarget( $target );
172 }
173
186 public function updateRedirect( Title $target ) {
187 if ( !$this->isRedirect() ) {
188 return $this;
189 }
190
191 # Fix the text
192 # Remember that redirect pages can have categories, templates, etc.,
193 # so the regex has to be fairly general
194 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
195 '[[' . $target->getFullText() . ']]',
196 $this->getText(), 1 );
197
198 return new static( $newText );
199 }
200
212 public function isCountable( $hasLinks = null, ?Title $title = null ) {
213 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
215
216 if ( $this->isRedirect() ) {
217 return false;
218 }
219
220 if ( $articleCountMethod === 'link' ) {
221 if ( $hasLinks === null ) { # not known, find out
222 // @TODO: require an injected title
223 if ( !$title ) {
224 $context = RequestContext::getMain();
225 $title = $context->getTitle();
226 }
227 $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
228 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
229 $po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
230 $hasLinks = $po->hasLinks();
231 }
232
233 return $hasLinks;
234 }
235
236 return true;
237 }
238
244 public function getTextForSummary( $maxlength = 250 ) {
245 $truncatedtext = parent::getTextForSummary( $maxlength );
246
247 # clean up unfinished links
248 # XXX: make this optional? wasn't there in autosummary, but required for
249 # deletion summary.
250 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
251
252 return $truncatedtext;
253 }
254
264 public function matchMagicWord( MagicWord $word ) {
265 return $word->match( $this->getText() );
266 }
267
275 public function setPreSaveTransformFlags( array $flags ) {
276 $this->preSaveTransformFlags = $flags;
277 }
278
285 public function getPreSaveTransformFlags() {
286 return $this->preSaveTransformFlags;
287 }
288
290 $handler = parent::getContentHandler();
291 '@phan-var WikitextContentHandler $handler';
292
293 return $handler;
294 }
295}
296
298class_alias( WikitextContent::class, 'WikitextContent' );
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:249
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:1879
Content objects represent page content, e.g.
Definition Content.php:42
getModel()
Get the content model ID.
replaceSection( $sectionId, Content $with, $sectionTitle='')
Create a derived Content with the specified section added or replaced.
$header