/** * {@inheritdoc} */ public function cleanAlias($alias) { if (!isset($this->aliasMaxLength)) { $config = $this->configFactory->get('pathauto.settings'); $this->aliasMaxLength = min($config->get('max_length'), $this->aliasStorageHelper->getAliasSchemaMaxLength()); } $output = $alias; // Trim duplicate, leading, and trailing separators. Do this before cleaning // backslashes since a pattern like "[token1]/[token2]-[token3]/[token4]" // could end up like "value1/-/value2" and if backslashes were cleaned first // this would result in a duplicate blackslash. $output = $this->getCleanSeparators($output); // Trim duplicate, leading, and trailing backslashes. $output = $this->getCleanSeparators($output, '/'); // Shorten to a logical place based on word boundaries. $output = Unicode::truncate($output, $this->aliasMaxLength, TRUE); return $output; }
/** * {@inheritdoc} */ public function cleanString($string, array $options = array()) { if (empty($this->cleanStringCache)) { // Generate and cache variables used in this method. $config = $this->configFactory->get('pathauto.settings'); $this->cleanStringCache = array('separator' => $config->get('separator'), 'strings' => array(), 'transliterate' => $config->get('transliterate'), 'punctuation' => array(), 'reduce_ascii' => (bool) $config->get('reduce_ascii'), 'ignore_words_regex' => FALSE, 'lowercase' => (bool) $config->get('case'), 'maxlength' => min($config->get('max_component_length'), $this->aliasStorageHelper->getAliasSchemaMaxLength())); // Generate and cache the punctuation replacements for strtr(). $punctuation = $this->getPunctuationCharacters(); foreach ($punctuation as $name => $details) { $action = $config->get('punctuation.' . $name); switch ($action) { case PathautoManagerInterface::PUNCTUATION_REMOVE: $cache['punctuation'][$details['value']] = ''; $this->cleanStringCache; case PathautoManagerInterface::PUNCTUATION_REPLACE: $this->cleanStringCache['punctuation'][$details['value']] = $this->cleanStringCache['separator']; break; case PathautoManagerInterface::PUNCTUATION_DO_NOTHING: // Literally do nothing. break; } } // Generate and cache the ignored words regular expression. $ignore_words = $config->get('ignore_words'); $ignore_words_regex = preg_replace(array('/^[,\\s]+|[,\\s]+$/', '/[,\\s]+/'), array('', '\\b|\\b'), $ignore_words); if ($ignore_words_regex) { $this->cleanStringCache['ignore_words_regex'] = '\\b' . $ignore_words_regex . '\\b'; if (function_exists('mb_eregi_replace')) { mb_regex_encoding('UTF-8'); $this->cleanStringCache['ignore_words_callback'] = 'mb_eregi_replace'; } else { $this->cleanStringCache['ignore_words_callback'] = 'preg_replace'; $this->cleanStringCache['ignore_words_regex'] = '/' . $this->cleanStringCache['ignore_words_regex'] . '/i'; } } } // Empty strings do not need any processing. if ($string === '' || $string === NULL) { return ''; } $langcode = NULL; if (!empty($options['language'])) { $langcode = $options['language']->getId(); } elseif (!empty($options['langcode'])) { $langcode = $options['langcode']; } // Check if the string has already been processed, and if so return the // cached result. if (isset($this->cleanStringCache['strings'][$langcode][(string) $string])) { return $this->cleanStringCache['strings'][$langcode][(string) $string]; } // Remove all HTML tags from the string. $output = Html::decodeEntities($string); $output = PlainTextOutput::renderFromHtml($output); // Optionally transliterate. if ($this->cleanStringCache['transliterate']) { // If the reduce strings to letters and numbers is enabled, don't bother // replacing unknown characters with a question mark. Use an empty string // instead. $output = $this->transliteration->transliterate($output, $langcode, $this->cleanStringCache['reduce_ascii'] ? '' : '?'); } // Replace or drop punctuation based on user settings. $output = strtr($output, $this->cleanStringCache['punctuation']); // Reduce strings to letters and numbers. if ($this->cleanStringCache['reduce_ascii']) { $output = preg_replace('/[^a-zA-Z0-9\\/]+/', $this->cleanStringCache['separator'], $output); } // Get rid of words that are on the ignore list. if ($this->cleanStringCache['ignore_words_regex']) { $words_removed = $this->cleanStringCache['ignore_words_callback']($this->cleanStringCache['ignore_words_regex'], '', $output); if (Unicode::strlen(trim($words_removed)) > 0) { $output = $words_removed; } } // Always replace whitespace with the separator. $output = preg_replace('/\\s+/', $this->cleanStringCache['separator'], $output); // Trim duplicates and remove trailing and leading separators. $output = $this->getCleanSeparators($this->getCleanSeparators($output, $this->cleanStringCache['separator'])); // Optionally convert to lower case. if ($this->cleanStringCache['lowercase']) { $output = Unicode::strtolower($output); } // Shorten to a logical place based on word boundaries. $output = Unicode::truncate($output, $this->cleanStringCache['maxlength'], TRUE); // Cache this result in the static array. $this->cleanStringCache['strings'][$langcode][(string) $string] = $output; return $output; }