const OT_MSG = 3;
const OT_PLAIN = 4; # like extractSections() - portions of the original are returned unchanged.
- /**
- * Prefix for temporary replacement strings generated by the preprocessor
- * ("strip markers"). Using \x7f at the front gives us a little extra
- * robustness since it shouldn't match when butted up against
- * identifier-like string constructs.
- *
- * Must not consist of all title characters, or else it will change
- * the behavior of <nowiki> in a link.
- */
- const MARKER_PREFIX = "\x7fUNIQ";
- /** Suffix for strip markers */
+ # Marker Suffix needs to be accessible staticly.
const MARKER_SUFFIX = "-QINU\x7f";
- /** Regex which matches the state ID part of strip markers */
- const MARKER_STATE_ID_REGEX = '[0-9a-f]{16}';
# Markers used for wrapping the table of contents
const TOC_START = '<mw:toc>';
$this->mLangLinkLanguages = array();
$this->currentRevisionCache = null;
- $stripId = self::getRandomString();
- $this->mUniqPrefix = self::MARKER_PREFIX . $stripId;
- $this->mStripState = new StripState( $stripId );
+ /**
+ * Prefix for temporary replacement strings for the multipass parser.
+ * \x07 should never appear in input as it's disallowed in XML.
+ * Using it at the front also gives us a little extra robustness
+ * since it shouldn't match when butted up against identifier-like
+ * string constructs.
+ *
+ * Must not consist of all title characters, or else it will change
+ * the behavior of <nowiki> in a link.
+ */
+ $this->mUniqPrefix = "\x7fUNIQ" . self::getRandomString();
+ $this->mStripState = new StripState( $this->mUniqPrefix );
# Clear these on every parse, bug 4549
$this->mTplRedirCache = $this->mTplDomCache = array();
* @ingroup Parser
*/
class StripState {
- protected $id;
protected $prefix;
protected $data;
protected $regex;
const UNSTRIP_RECURSION_LIMIT = 20;
/**
- * @param string $id
+ * @param string $prefix
*/
- public function __construct( $id ) {
- $this->id = $id;
- $this->prefix = Parser::MARKER_PREFIX . $id;
+ public function __construct( $prefix ) {
+ $this->prefix = $prefix;
$this->data = array(
'nowiki' => array(),
'general' => array()
);
- $this->regex = "/" . Parser::MARKER_PREFIX .
- '(' . Parser::MARKER_STATE_ID_REGEX . ")([^\x7f]+)" . Parser::MARKER_SUFFIX . '/';
+ $this->regex = "/{$this->prefix}([^\x7f]+)" . Parser::MARKER_SUFFIX . '/';
$this->circularRefGuard = array();
}
* @param string $value
*/
protected function addItem( $type, $marker, $value ) {
- if ( !preg_match( $this->regex, $marker, $m ) || $m[1] !== $this->id ) {
+ if ( !preg_match( $this->regex, $marker, $m ) ) {
throw new MWException( "Invalid marker: $marker" );
}
- $this->data[$type][$m[2]] = $value;
+ $this->data[$type][$m[1]] = $value;
}
/**
* @return array
*/
protected function unstripCallback( $m ) {
- $marker = $m[2];
- if ( $m[1] === $this->id && isset( $this->data[$this->tempType][$marker] ) ) {
+ $marker = $m[1];
+ if ( isset( $this->data[$this->tempType][$marker] ) ) {
if ( isset( $this->circularRefGuard[$marker] ) ) {
return '<span class="error">'
. wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text()
* @return StripState
*/
public function getSubState( $text ) {
- $subState = new StripState( $this->id );
+ $subState = new StripState( $this->prefix );
$pos = 0;
while ( true ) {
$startPos = strpos( $text, $this->prefix, $pos );
$endPos += strlen( Parser::MARKER_SUFFIX );
$marker = substr( $text, $startPos, $endPos - $startPos );
- if ( !preg_match( $this->regex, $marker, $m ) || $m[1] !== $this->id ) {
+ if ( !preg_match( $this->regex, $marker, $m ) ) {
continue;
}
- $key = $m[2];
+ $key = $m[1];
if ( isset( $this->data['nowiki'][$key] ) ) {
$subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
} elseif ( isset( $this->data['general'][$key] ) ) {
* @return string
*/
protected function mergeCallback( $m ) {
- if ( $m[1] === $this->id ) {
- $key = $m[2];
- return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;
- } else {
- return $m[0];
- }
+ $key = $m[1];
+ return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;
}
/**
* @return string
*/
public function killMarkers( $text ) {
- $id = $this->id; // PHP 5.3 hack
- return preg_replace_callback( $this->regex,
- function ( $m ) use ( $id ) {
- if ( $m[1] === $id ) {
- return '';
- } else {
- return $m[0];
- }
- },
- $text );
+ return preg_replace( $this->regex, '', $text );
}
}