/** * convert text to different variants of a language. the automatic * conversion is done in autoConvert(). here we parse the text * marked with -{}-, which specifies special conversions of the * text that can not be accomplished in autoConvert() * * syntax of the markup: * -{code1:text1;code2:text2;...}- or * -{flags|code1:text1;code2:text2;...}- or * -{text}- in which case no conversion should take place for text * * @param string $text text to be converted * @param bool $isTitle whether this conversion is for the article title * @return string converted text * @public */ function convert($text, $isTitle = false) { $mw =& MagicWord::get('notitleconvert'); if ($mw->matchAndRemove($text)) { $this->mDoTitleConvert = false; } $mw =& MagicWord::get('nocontentconvert'); if ($mw->matchAndRemove($text)) { $this->mDoContentConvert = false; } // no conversion if redirecting $mw =& MagicWord::get('redirect'); if ($mw->matchStart($text)) { return $text; } $plang = $this->getPreferredVariant(); // for title convertion if ($isTitle) { return $this->convertTitle($text, $plang); } $tarray = StringUtils::explode($this->mMarkup['end'], $text); $text = ''; $marks = array(); foreach ($tarray as $txt) { $marked = explode($this->mMarkup['begin'], $txt, 2); if (array_key_exists(1, $marked)) { $crule = new ConverterRule($marked[1], $this); $crule->parse($plang); $marked[1] = $crule->getDisplay(); $this->prepareManualConv($crule); } else { $marked[0] .= $this->mMarkup['end']; } array_push($marks, $marked); } $this->applyManualConv(); foreach ($marks as $marked) { if ($this->mDoContentConvert) { $text .= $this->autoConvert($marked[0], $plang); } else { $text .= $marked[0]; } if (array_key_exists(1, $marked)) { $text .= $marked[1]; } } // Remove the last delimiter (wasn't real) $text = substr($text, 0, -strlen($this->mMarkup['end'])); return $text; }
/** * Recursively convert text on the inside. * * @param string $text Text to be converted * @param string $variant The target variant code * @param int $startPos * @param int $depth Depth of recursion * * @throws MWException * @return string Converted text */ protected function recursiveConvertRule($text, $variant, &$startPos, $depth = 0) { // Quick sanity check (no function calls) if ($text[$startPos] !== '-' || $text[$startPos + 1] !== '{') { throw new MWException(__METHOD__ . ': invalid input string'); } $startPos += 2; $inner = ''; $warningDone = false; $length = strlen($text); while ($startPos < $length) { $m = false; preg_match('/-\\{|\\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos); if (!$m) { // Unclosed rule break; } $token = $m[0][0]; $pos = $m[0][1]; // Markup found // Append initial segment $inner .= substr($text, $startPos, $pos - $startPos); // Advance position $startPos = $pos; switch ($token) { case '-{': // Check max depth if ($depth >= $this->mMaxDepth) { $inner .= '-{'; if (!$warningDone) { $inner .= '<span class="error">' . wfMessage('language-converter-depth-warning')->numParams($this->mMaxDepth)->inContentLanguage()->text() . '</span>'; $warningDone = true; } $startPos += 2; continue; } // Recursively parse another rule $inner .= $this->recursiveConvertRule($text, $variant, $startPos, $depth + 1); break; case '}-': // Apply the rule $startPos += 2; $rule = new ConverterRule($inner, $this); $rule->parse($variant); $this->applyManualConv($rule); return $rule->getDisplay(); default: throw new MWException(__METHOD__ . ': invalid regex match'); } } // Unclosed rule if ($startPos < $length) { $inner .= substr($text, $startPos); } $startPos = $length; return '-{' . $this->autoConvert($inner, $variant); }
/** * convert text to different variants of a language. the automatic * conversion is done in autoConvert(). here we parse the text * marked with -{}-, which specifies special conversions of the * text that can not be accomplished in autoConvert() * * syntax of the markup: * -{code1:text1;code2:text2;...}- or * -{flags|code1:text1;code2:text2;...}- or * -{text}- in which case no conversion should take place for text * * @param string $text text to be converted * @param bool $isTitle whether this conversion is for the article title * @return string converted text * @public */ function convert($text, $isTitle = false) { $mw =& MagicWord::get('notitleconvert'); if ($mw->matchAndRemove($text)) { $this->mDoTitleConvert = false; } $mw =& MagicWord::get('nocontentconvert'); if ($mw->matchAndRemove($text)) { $this->mDoContentConvert = false; } // no conversion if redirecting $mw =& MagicWord::get('redirect'); if ($mw->matchStart($text)) { return $text; } // for title convertion if ($isTitle) { return $this->convertTitle($text); } $plang = $this->getPreferredVariant(); $tarray = StringUtils::explode($this->mMarkup['end'], $text); $text = ''; $lastDelim = false; foreach ($tarray as $txt) { $marked = explode($this->mMarkup['begin'], $txt, 2); if ($this->mDoContentConvert) { $text .= $this->autoConvert($marked[0], $plang); } else { $text .= $marked[0]; } if (array_key_exists(1, $marked)) { // strip the flags from syntax like -{T| ... }- $crule = new ConverterRule($marked[1], $this); $crule->parse($plang); $text .= $crule->getDisplay(); $this->applyManualConv($crule); $lastDelim = false; } else { // Reinsert the }- which wasn't part of anything $text .= $this->mMarkup['end']; $lastDelim = true; } } if ($lastDelim) { // Remove the last delimiter (wasn't real) $text = substr($text, 0, -strlen($this->mMarkup['end'])); } return $text; }