MediaWiki REL1_32
WikitextContent.php
Go to the documentation of this file.
1<?php
29
36 private $redirectTargetAndText = null;
37
42 private $hadSignature = false;
43
44 public function __construct( $text ) {
45 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
46 }
47
55 public function getSection( $sectionId ) {
56 global $wgParser;
57
58 $text = $this->getNativeData();
59 $sect = $wgParser->getSection( $text, $sectionId, false );
60
61 if ( $sect === false ) {
62 return false;
63 } else {
64 return new static( $sect );
65 }
66 }
67
78 public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
79 $myModelId = $this->getModel();
80 $sectionModelId = $with->getModel();
81
82 if ( $sectionModelId != $myModelId ) {
83 throw new MWException( "Incompatible content model for section: " .
84 "document uses $myModelId but " .
85 "section uses $sectionModelId." );
86 }
87
88 $oldtext = $this->getNativeData();
89 $text = $with->getNativeData();
90
91 if ( strval( $sectionId ) === '' ) {
92 return $with; # XXX: copy first?
93 }
94
95 if ( $sectionId === 'new' ) {
96 # Inserting a new section
97 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
98 ->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
99 if ( Hooks::run( 'PlaceNewSection', [ $this, $oldtext, $subject, &$text ] ) ) {
100 $text = strlen( trim( $oldtext ) ) > 0
101 ? "{$oldtext}\n\n{$subject}{$text}"
102 : "{$subject}{$text}";
103 }
104 } else {
105 # Replacing an existing section; roll out the big guns
106 global $wgParser;
107
108 $text = $wgParser->replaceSection( $oldtext, $sectionId, $text );
109 }
110
111 $newContent = new static( $text );
112
113 return $newContent;
114 }
115
124 public function addSectionHeader( $header ) {
125 $text = wfMessage( 'newsectionheaderdefaultlevel' )
126 ->rawParams( $header )->inContentLanguage()->text();
127 $text .= "\n\n";
128 $text .= $this->getNativeData();
129
130 return new static( $text );
131 }
132
143 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
144 global $wgParser;
145
146 $text = $this->getNativeData();
147 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
148
149 if ( $text === $pst ) {
150 return $this;
151 }
152
153 $ret = new static( $pst );
154
155 if ( $wgParser->getOutput()->getFlag( 'user-signature' ) ) {
156 $ret->hadSignature = true;
157 }
158
159 return $ret;
160 }
161
172 public function preloadTransform( Title $title, ParserOptions $popts, $params = [] ) {
173 global $wgParser;
174
175 $text = $this->getNativeData();
176 $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
177
178 return new static( $plt );
179 }
180
190 protected function getRedirectTargetAndText() {
191 global $wgMaxRedirects;
192
193 if ( $this->redirectTargetAndText !== null ) {
194 return $this->redirectTargetAndText;
195 }
196
197 if ( $wgMaxRedirects < 1 ) {
198 // redirects are disabled, so quit early
199 $this->redirectTargetAndText = [ null, $this->getNativeData() ];
200 return $this->redirectTargetAndText;
201 }
202
203 $redir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
204 $text = ltrim( $this->getNativeData() );
205 if ( $redir->matchStartAndRemove( $text ) ) {
206 // Extract the first link and see if it's usable
207 // Ensure that it really does come directly after #REDIRECT
208 // Some older redirects included a colon, so don't freak about that!
209 $m = [];
210 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
211 // Strip preceding colon used to "escape" categories, etc.
212 // and URL-decode links
213 if ( strpos( $m[1], '%' ) !== false ) {
214 // Match behavior of inline link parsing here;
215 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
216 }
217 $title = Title::newFromText( $m[1] );
218 // If the title is a redirect to bad special pages or is invalid, return null
219 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
220 $this->redirectTargetAndText = [ null, $this->getNativeData() ];
221 return $this->redirectTargetAndText;
222 }
223
224 $this->redirectTargetAndText = [ $title, substr( $text, strlen( $m[0] ) ) ];
225 return $this->redirectTargetAndText;
226 }
227 }
228
229 $this->redirectTargetAndText = [ null, $this->getNativeData() ];
230 return $this->redirectTargetAndText;
231 }
232
240 public function getRedirectTarget() {
241 list( $title, ) = $this->getRedirectTargetAndText();
242
243 return $title;
244 }
245
258 public function updateRedirect( Title $target ) {
259 if ( !$this->isRedirect() ) {
260 return $this;
261 }
262
263 # Fix the text
264 # Remember that redirect pages can have categories, templates, etc.,
265 # so the regex has to be fairly general
266 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
267 '[[' . $target->getFullText() . ']]',
268 $this->getNativeData(), 1 );
269
270 return new static( $newText );
271 }
272
284 public function isCountable( $hasLinks = null, Title $title = null ) {
286
287 if ( $this->isRedirect() ) {
288 return false;
289 }
290
291 if ( $wgArticleCountMethod === 'link' ) {
292 if ( $hasLinks === null ) { # not known, find out
293 if ( !$title ) {
294 $context = RequestContext::getMain();
296 }
297
298 $po = $this->getParserOutput( $title, null, null, false );
299 $links = $po->getLinks();
300 $hasLinks = !empty( $links );
301 }
302
303 return $hasLinks;
304 }
305
306 return true;
307 }
308
313 public function getTextForSummary( $maxlength = 250 ) {
314 $truncatedtext = parent::getTextForSummary( $maxlength );
315
316 # clean up unfinished links
317 # XXX: make this optional? wasn't there in autosummary, but required for
318 # deletion summary.
319 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
320
321 return $truncatedtext;
322 }
323
335 protected function fillParserOutput( Title $title, $revId,
337 ) {
338 global $wgParser;
339
340 list( $redir, $text ) = $this->getRedirectTargetAndText();
341 $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
342
343 // Add redirect indicator at the top
344 if ( $redir ) {
345 // Make sure to include the redirect link in pagelinks
346 $output->addLink( $redir );
347 if ( $generateHtml ) {
348 $chain = $this->getRedirectChain();
349 $output->setText(
350 Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
351 $output->getRawText()
352 );
353 $output->addModuleStyles( 'mediawiki.action.view.redirectPage' );
354 }
355 }
356
357 // Pass along user-signature flag
358 if ( $this->hadSignature ) {
359 $output->setFlag( 'user-signature' );
360 }
361 }
362
366 protected function getHtml() {
367 throw new MWException(
368 "getHtml() not implemented for wikitext. "
369 . "Use getParserOutput()->getText()."
370 );
371 }
372
382 public function matchMagicWord( MagicWord $word ) {
383 return $word->match( $this->getNativeData() );
384 }
385
386}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgMaxRedirects
Max number of redirects to follow when resolving redirects.
$wgArticleCountMethod
Method used to determine if a page in a content namespace should be counted as a valid article.
$wgParser
Definition Setup.php:921
getParserOutput(Title $title, $revId=null, ParserOptions $options=null, $generateHtml=true)
Returns a ParserOutput object containing information derived from this content.
static getRedirectHeaderHtml(Language $lang, $target, $forceKnown=false)
Return the HTML for the top of a redirect page.
Definition Article.php:1683
MediaWiki exception.
This class encapsulates "magic words" such as "#redirect", NOTOC, etc.
Definition MagicWord.php:57
match( $text)
Returns true if the text contains the word.
MediaWikiServices is the service locator for the application scope of MediaWiki.
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
Set options of the Parser.
Content object implementation for representing flat text.
getNativeData()
Returns the text represented by this Content object, as a string.
Represents a title within MediaWiki.
Definition Title.php:39
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition Title.php:1722
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
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...
getRedirectTarget()
Implement redirect extraction for wikitext.
getTextForSummary( $maxlength=250)
preloadTransform(Title $title, ParserOptions $popts, $params=[])
Returns a Content object with preload transformations applied (or this object if no transformations a...
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
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.
bool $hadSignature
Tracks if the parser set the user-signature flag when creating this content, which would make it expi...
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Returns a Content object with pre-save transformations applied using Parser::preSaveTransform().
replaceSection( $sectionId, Content $with, $sectionTitle='')
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2885
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2054
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2317
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition LICENSE.txt:11
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Base interface for content objects.
Definition Content.php:34
getNativeData()
Returns native representation of the data.
getModel()
Returns the ID of the content model used by this Content object.
$params
$header