function transformByGroup($text, $group, $charset = false, $useCache = true) { if ($text === '') { return $text; } $charsetName = $charset === false ? eZTextCodec::internalCharset() : eZCharsetInfo::realCharsetCode($charset); if ($useCache) { // CRC32 is used for speed, MD5 would be more unique but is slower $keyText = 'Group:' . $group; $key = eZSys::ezcrc32($keyText . '-' . $charset); $filepath = $this->cacheFilePath('g-' . $group . '-', '-' . $charsetName, $key); // Try to execute code in the cache file, if it succeeds // \a $text will/ transformated $retText = $this->executeCacheFile($text, $filepath); if ($retText !== false) { return $retText; } } $commands = $this->groupCommands($group); if ($commands === false) { return false; } $mapper = new eZCodeMapper(); $mapper->loadTransformationFiles($charsetName, $group); $rules = array(); foreach ($commands as $command) { $rules = array_merge($rules, $mapper->decodeCommand($command['command'], $command['parameters'])); } // First generate a unicode based mapping table from the rules $unicodeTable = $mapper->generateMappingCode($rules); unset($unicodeTable[0]); // Then transform that to a table that works with the current charset // Any character not available in the current charset will be removed $charsetTable = $mapper->generateCharsetMappingTable($unicodeTable, $charset); $transformationData = array('table' => $charsetTable); unset($unicodeTable); if ($useCache) { $extraCode = ''; foreach ($commands as $command) { $code = $mapper->generateCommandCode($command, $charsetName); if ($code !== false) { $extraCode .= $code . "\n"; } } $this->storeCacheFile($filepath, $transformationData, $extraCode, 'Group:' . $group, $charsetName); } // Execute transformations $text = strtr($text, $transformationData['table']); // Execute custom code foreach ($commands as $command) { $mapper->executeCommandCode($text, $command, $charsetName); } return $text; }
function generateSimpleMappingTable($table, $allowedRanges) { if (!is_array($table)) { return false; } $unicodeMap = array(); foreach ($table as $tableItem) { $type = $tableItem[0]; $item = $tableItem[1]; if (isset($tableItem[2])) { $identifier = $tableItem[2]; // print( "identifier: $identifier\n" ); } if ($type == self::TYPE_DIRECT) { foreach ($item as $fromCode => $toCode) { // print( "from: $fromCode, to: $toCode\n" ); // if ( $fromCode == 1026 ) // { // print( "<pre>oldcode<br/>" ); var_dump( $toCode ); print( "</pre>" ); // } $toCode = eZCodeMapper::ordinalValues($unicodeMap, $toCode); // if ( $fromCode == 1026 ) // { // print( "<pre>newcode<br/>" ); var_dump( $toCode ); print( "</pre>" ); // } if (count($allowedRanges) == 0) { if (count($toCode) == 1) { $toCode = $toCode[0]; } // If the mapping already exists we skip it if (isset($unicodeMap[$fromCode])) { continue; } $unicodeMap[$fromCode] = $toCode; $unicodeMap = eZCodeMapper::mapExistingCodes($unicodeMap, $fromCode, $toCode); } else { $allowed = false; foreach ($allowedRanges as $allowedRange) { if ($fromCode >= $allowedRange[0] and $fromCode <= $allowedRange[1]) { $allowed = true; break; } } if (!$allowed) { continue; } $toCodeList = $toCode; $newToCodeList = array(); foreach ($toCodeList as $toCode) { if (is_bool($toCode)) { $newToCodeList[] = $toCode; continue; } foreach ($allowedRanges as $allowedRange) { if ($toCode >= $allowedRange[0] and $toCode <= $allowedRange[1]) { break; } } if ($allowed) { $newToCodeList[] = $toCode; } } $toCode = $newToCodeList; if (count($toCode) > 0) { if (count($toCode) == 1) { $toCode = $toCode[0]; } // If the mapping already exists we skip it if (isset($unicodeMap[$fromCode])) { continue; } $unicodeMap = eZCodeMapper::mapExistingCodes($unicodeMap, $fromCode, $toCode); $unicodeMap[$fromCode] = $toCode; } } } } else { if ($type == self::TYPE_RANGE) { foreach ($item as $rangeItem) { $start = $rangeItem[0]; $stop = $rangeItem[1]; if ($start > $stop) { $tmp = $stop; $stop = $start; $start = $tmp; } $add = $rangeItem[2]; $modulo = $rangeItem[3]; // Sanity-check, to avoid infinite loops if ($modulo == 0) { $modulo = 1; } for ($i = $start; $i <= $stop; $i += $modulo) { if (count($allowedRanges) == 0) { $allowed = true; } else { $allowed = false; foreach ($allowedRanges as $allowedRange) { if ($i >= $allowedRange[0] and $i <= $allowedRange[1]) { $allowed = true; break; } } if (!$allowed) { continue; } } $replace = $i + $add; $replace = eZCodeMapper::ordinalValues($unicodeMap, $replace); if (count($allowedRanges) == 0) { if (count($replace) == 0) { $replace = false; } else { if (count($replace) == 1) { $replace = $replace[0]; } } $unicodeMap = eZCodeMapper::mapExistingCodes($unicodeMap, $i, $replace); // If the mapping already exists we skip it if (isset($unicodeMap[$i])) { continue; } $unicodeMap[$i] = $replace; } else { $newReplace = array(); foreach ($allowedRanges as $allowedRange) { foreach ($replace as $replaceOrdinal) { if ($replaceOrdinal >= $allowedRange[0] and $replaceOrdinal <= $allowedRange[1]) { $newReplace[] = $replaceOrdinal; } } } if (count($newReplace) == 0) { $replace = false; } else { if (count($newReplace) == 1) { $replace = $newReplace[0]; } else { $replace = $newReplace; } } // If the mapping already exists we skip it if (isset($unicodeMap[$i])) { continue; } $unicodeMap = eZCodeMapper::mapExistingCodes($unicodeMap, $i, $replace); $unicodeMap[$i] = $replace; } } } } else { if ($type == self::TYPE_REPLACE) { foreach ($item as $rangeItem) { $start = $rangeItem[0]; $stop = $rangeItem[1]; if ($start > $stop) { $tmp = $stop; $stop = $start; $start = $tmp; } $replace = $rangeItem[2]; $replace = eZCodeMapper::ordinalValues($unicodeMap, $replace); if (count($allowedRanges) == 0) { if (count($replace) == 0) { $replace = false; } else { if (count($replace) == 1) { $replace = $replace[0]; } } for ($i = $start; $i <= $stop; ++$i) { // If the mapping already exists we skip it if (isset($unicodeMap[$i])) { continue; } $unicodeMap = eZCodeMapper::mapExistingCodes($unicodeMap, $i, $replace); $unicodeMap[$i] = $replace; } } else { $newReplace = array(); foreach ($allowedRanges as $allowedRange) { foreach ($replace as $replaceOrdinal) { if ($replaceOrdinal >= $allowedRange[0] and $replaceOrdinal <= $allowedRange[1]) { $newReplace[] = $replaceOrdinal; } } } if (count($newReplace) == 0) { $replace = false; } else { if (count($newReplace) == 1) { $replace = $newReplace[0]; } else { $replace = $newReplace; } } for ($i = $start; $i <= $stop; ++$i) { $allowed = false; foreach ($allowedRanges as $allowedRange) { if ($i >= $allowedRange[0] and $i <= $allowedRange[1]) { $allowed = true; break; } } if ($allowed) { // If the mapping already exists we skip it if (isset($unicodeMap[$i])) { continue; } $unicodeMap = eZCodeMapper::mapExistingCodes($unicodeMap, $i, $replace); $unicodeMap[$i] = $replace; } } } } } } } } return $unicodeMap; }