MediaWiki REL1_41
WikitextContent.php
Go to the documentation of this file.
1<?php
33
41
45 private $preSaveTransformFlags = [];
46
52 public function __construct( $text ) {
53 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
54 }
55
63 public function getSection( $sectionId ) {
64 $text = $this->getText();
65 $sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
66 ->getSection( $text, $sectionId, false );
67
68 if ( $sect === false ) {
69 return false;
70 } else {
71 return new static( $sect );
72 }
73 }
74
85 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
86 // @phan-suppress-previous-line PhanParamSignatureMismatch False positive
87 $myModelId = $this->getModel();
88 $sectionModelId = $with->getModel();
89
90 if ( $sectionModelId != $myModelId ) {
91 throw new MWException( "Incompatible content model for section: " .
92 "document uses $myModelId but " .
93 "section uses $sectionModelId." );
94 }
96 '@phan-var self $with';
97
98 $oldtext = $this->getText();
99 $text = $with->getText();
100
101 if ( strval( $sectionId ) === '' ) {
102 return $with; # XXX: copy first?
103 }
104
105 if ( $sectionId === 'new' ) {
106 # Inserting a new section
107 $subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
108 ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
109 $hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) );
110 if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
111 $text = strlen( trim( $oldtext ) ) > 0
112 ? "{$oldtext}\n\n{$subject}{$text}"
113 : "{$subject}{$text}";
114 }
115 } else {
116 # Replacing an existing section; roll out the big guns
117 $text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
118 ->replaceSection( $oldtext, $sectionId, $text );
119 }
120
121 $newContent = new static( $text );
122
123 return $newContent;
124 }
125
134 public function addSectionHeader( $header ) {
135 $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
136 ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
137 $text .= $this->getText();
138
139 return new static( $text );
140 }
141
150 public function getRedirectTargetAndText() {
151 wfDeprecated( __METHOD__, '1.41' );
152
153 $handler = $this->getContentHandler();
154 [ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
155
156 return [ Title::castFromLinkTarget( $target ), $content->getText() ];
157 }
158
166 public function getRedirectTarget() {
167 // TODO: The redirect target should be injected on construction.
168 // But that only works if the object is created by WikitextContentHandler.
169
170 $handler = $this->getContentHandler();
171 [ $target, ] = $handler->extractRedirectTargetAndText( $this );
172
173 return Title::castFromLinkTarget( $target );
174 }
175
188 public function updateRedirect( Title $target ) {
189 if ( !$this->isRedirect() ) {
190 return $this;
191 }
192
193 # Fix the text
194 # Remember that redirect pages can have categories, templates, etc.,
195 # so the regex has to be fairly general
196 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
197 '[[' . $target->getFullText() . ']]',
198 $this->getText(), 1 );
199
200 return new static( $newText );
201 }
202
214 public function isCountable( $hasLinks = null, Title $title = null ) {
215 $articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
216 ->get( MainConfigNames::ArticleCountMethod );
217
218 if ( $this->isRedirect() ) {
219 return false;
220 }
221
222 if ( $articleCountMethod === 'link' ) {
223 if ( $hasLinks === null ) { # not known, find out
224 // @TODO: require an injected title
225 if ( !$title ) {
226 $context = RequestContext::getMain();
227 $title = $context->getTitle();
228 }
229 $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
230 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
231 $po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
232 $links = $po->getLinks();
233 $hasLinks = $links !== [];
234 }
235
236 return $hasLinks;
237 }
238
239 return true;
240 }
241
246 public function getTextForSummary( $maxlength = 250 ) {
247 $truncatedtext = parent::getTextForSummary( $maxlength );
248
249 # clean up unfinished links
250 # XXX: make this optional? wasn't there in autosummary, but required for
251 # deletion summary.
252 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
253
254 return $truncatedtext;
255 }
256
266 public function matchMagicWord( MagicWord $word ) {
267 return $word->match( $this->getText() );
268 }
269
275 public function setPreSaveTransformFlags( array $flags ) {
276 $this->preSaveTransformFlags = $flags;
277 }
278
284 public function getPreSaveTransformFlags() {
285 return $this->preSaveTransformFlags;
286 }
287
289 $handler = parent::getContentHandler();
290 '@phan-var WikitextContentHandler $handler';
291 return $handler;
292 }
293}
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:209
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.
MediaWiki exception.
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.
Service locator for MediaWiki core services.
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition MagicWord.php:65
match( $text)
Returns true if the text contains the word.
Represents a title within MediaWiki.
Definition Title.php:76
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition Title.php:1909
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.
updateRedirect(Title $target)
This implementation replaces the first link on the page with the given new target if this Content obj...
setPreSaveTransformFlags(array $flags)
Records flags set by preSaveTransform.
getRedirectTarget()
Implement redirect extraction for wikitext.
getTextForSummary( $maxlength=250)
getPreSaveTransformFlags()
Records flags set by preSaveTransform.
getRedirectTargetAndText()
Extract the redirect target and the remaining text on the page.
addSectionHeader( $header)
Returns a new WikitextContent object with the given section heading prepended.
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...
getSection( $sectionId)
matchMagicWord(MagicWord $word)
This implementation calls $word->match() on the this TextContent object's text.
replaceSection( $sectionId, Content $with, $sectionTitle='')
Base interface for representing page content.
Definition Content.php:39
getModel()
Returns the ID of the content model used by this Content object.
$content
Definition router.php:76
$header