MediaWiki  1.30.0
StripState.php
Go to the documentation of this file.
1 <?php
28 class StripState {
29  protected $prefix;
30  protected $data;
31  protected $regex;
32 
34  protected $circularRefGuard;
35  protected $recursionLevel = 0;
36 
38 
44  public function __construct( $prefix = null ) {
45  if ( $prefix !== null ) {
46  wfDeprecated( __METHOD__ . ' with called with $prefix argument' .
47  ' (call with no arguments instead)', '1.26' );
48  }
49  $this->data = [
50  'nowiki' => [],
51  'general' => []
52  ];
53  $this->regex = '/' . Parser::MARKER_PREFIX . "([^\x7f<>&'\"]+)" . Parser::MARKER_SUFFIX . '/';
54  $this->circularRefGuard = [];
55  }
56 
62  public function addNoWiki( $marker, $value ) {
63  $this->addItem( 'nowiki', $marker, $value );
64  }
65 
70  public function addGeneral( $marker, $value ) {
71  $this->addItem( 'general', $marker, $value );
72  }
73 
80  protected function addItem( $type, $marker, $value ) {
81  if ( !preg_match( $this->regex, $marker, $m ) ) {
82  throw new MWException( "Invalid marker: $marker" );
83  }
84 
85  $this->data[$type][$m[1]] = $value;
86  }
87 
92  public function unstripGeneral( $text ) {
93  return $this->unstripType( 'general', $text );
94  }
95 
100  public function unstripNoWiki( $text ) {
101  return $this->unstripType( 'nowiki', $text );
102  }
103 
108  public function unstripBoth( $text ) {
109  $text = $this->unstripType( 'general', $text );
110  $text = $this->unstripType( 'nowiki', $text );
111  return $text;
112  }
113 
119  protected function unstripType( $type, $text ) {
120  // Shortcut
121  if ( !count( $this->data[$type] ) ) {
122  return $text;
123  }
124 
125  $oldType = $this->tempType;
126  $this->tempType = $type;
127  $text = preg_replace_callback( $this->regex, [ $this, 'unstripCallback' ], $text );
128  $this->tempType = $oldType;
129  return $text;
130  }
131 
136  protected function unstripCallback( $m ) {
137  $marker = $m[1];
138  if ( isset( $this->data[$this->tempType][$marker] ) ) {
139  if ( isset( $this->circularRefGuard[$marker] ) ) {
140  return '<span class="error">'
141  . wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text()
142  . '</span>';
143  }
144  if ( $this->recursionLevel >= self::UNSTRIP_RECURSION_LIMIT ) {
145  return '<span class="error">' .
146  wfMessage( 'parser-unstrip-recursion-limit' )
147  ->numParams( self::UNSTRIP_RECURSION_LIMIT )->inContentLanguage()->text() .
148  '</span>';
149  }
150  $this->circularRefGuard[$marker] = true;
151  $this->recursionLevel++;
152  $value = $this->data[$this->tempType][$marker];
153  if ( $value instanceof Closure ) {
154  $value = $value();
155  }
156  $ret = $this->unstripType( $this->tempType, $value );
157  $this->recursionLevel--;
158  unset( $this->circularRefGuard[$marker] );
159  return $ret;
160  } else {
161  return $m[0];
162  }
163  }
164 
173  public function getSubState( $text ) {
174  $subState = new StripState();
175  $pos = 0;
176  while ( true ) {
177  $startPos = strpos( $text, Parser::MARKER_PREFIX, $pos );
178  $endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
179  if ( $startPos === false || $endPos === false ) {
180  break;
181  }
182 
183  $endPos += strlen( Parser::MARKER_SUFFIX );
184  $marker = substr( $text, $startPos, $endPos - $startPos );
185  if ( !preg_match( $this->regex, $marker, $m ) ) {
186  continue;
187  }
188 
189  $key = $m[1];
190  if ( isset( $this->data['nowiki'][$key] ) ) {
191  $subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
192  } elseif ( isset( $this->data['general'][$key] ) ) {
193  $subState->data['general'][$key] = $this->data['general'][$key];
194  }
195  $pos = $endPos;
196  }
197  return $subState;
198  }
199 
209  public function merge( $otherState, $texts ) {
210  $mergePrefix = wfRandomString( 16 );
211 
212  foreach ( $otherState->data as $type => $items ) {
213  foreach ( $items as $key => $value ) {
214  $this->data[$type]["$mergePrefix-$key"] = $value;
215  }
216  }
217 
218  $this->tempMergePrefix = $mergePrefix;
219  $texts = preg_replace_callback( $otherState->regex, [ $this, 'mergeCallback' ], $texts );
220  $this->tempMergePrefix = null;
221  return $texts;
222  }
223 
228  protected function mergeCallback( $m ) {
229  $key = $m[1];
230  return Parser::MARKER_PREFIX . $this->tempMergePrefix . '-' . $key . Parser::MARKER_SUFFIX;
231  }
232 
239  public function killMarkers( $text ) {
240  return preg_replace( $this->regex, '', $text );
241  }
242 }
StripState\merge
merge( $otherState, $texts)
Merge another StripState object into this one.
Definition: StripState.php:209
StripState\getSubState
getSubState( $text)
Get a StripState object which is sufficient to unstrip the given text.
Definition: StripState.php:173
StripState\unstripNoWiki
unstripNoWiki( $text)
Definition: StripState.php:100
captcha-old.count
count
Definition: captcha-old.py:249
StripState\UNSTRIP_RECURSION_LIMIT
const UNSTRIP_RECURSION_LIMIT
Definition: StripState.php:37
StripState\unstripGeneral
unstripGeneral( $text)
Definition: StripState.php:92
StripState\$data
$data
Definition: StripState.php:30
StripState\$tempType
$tempType
Definition: StripState.php:33
StripState\addNoWiki
addNoWiki( $marker, $value)
Add a nowiki strip item.
Definition: StripState.php:62
StripState
Definition: StripState.php:28
php
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:35
StripState\$recursionLevel
$recursionLevel
Definition: StripState.php:35
MWException
MediaWiki exception.
Definition: MWException.php:26
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1176
StripState\$circularRefGuard
$circularRefGuard
Definition: StripState.php:34
StripState\addGeneral
addGeneral( $marker, $value)
Definition: StripState.php:70
StripState\$prefix
$prefix
Definition: StripState.php:29
$value
$value
Definition: styleTest.css.php:45
StripState\unstripCallback
unstripCallback( $m)
Definition: StripState.php:136
StripState\$tempMergePrefix
$tempMergePrefix
Definition: StripState.php:33
$ret
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:1965
StripState\addItem
addItem( $type, $marker, $value)
Definition: StripState.php:80
StripState\__construct
__construct( $prefix=null)
Definition: StripState.php:44
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
StripState\unstripType
unstripType( $type, $text)
Definition: StripState.php:119
StripState\$regex
$regex
Definition: StripState.php:31
StripState\unstripBoth
unstripBoth( $text)
Definition: StripState.php:108
wfMessage
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 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
StripState\mergeCallback
mergeCallback( $m)
Definition: StripState.php:228
data
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
StripState\killMarkers
killMarkers( $text)
Remove any strip markers found in the given text.
Definition: StripState.php:239
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition: GlobalFunctions.php:370
$type
$type
Definition: testCompression.php:48