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