pathauto.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. /**
  3. * @file
  4. * Miscellaneous functions for Pathauto.
  5. *
  6. * This also contains some constants giving human readable names to some numeric
  7. * settings; they're included here as they're only rarely used outside this file
  8. * anyway. Use module_load_include('inc', 'pathauto') if the constants need to
  9. * be available.
  10. *
  11. * @ingroup pathauto
  12. */
  13. /**
  14. * Case should be left as is in the generated path.
  15. */
  16. define('PATHAUTO_CASE_LEAVE_ASIS', 0);
  17. /**
  18. * Case should be lowercased in the generated path.
  19. */
  20. define('PATHAUTO_CASE_LOWER', 1);
  21. /**
  22. * "Do nothing. Leave the old alias intact."
  23. */
  24. define('PATHAUTO_UPDATE_ACTION_NO_NEW', 0);
  25. /**
  26. * "Create a new alias. Leave the existing alias functioning."
  27. */
  28. define('PATHAUTO_UPDATE_ACTION_LEAVE', 1);
  29. /**
  30. * "Create a new alias. Delete the old alias."
  31. */
  32. define('PATHAUTO_UPDATE_ACTION_DELETE', 2);
  33. /**
  34. * Remove the punctuation from the alias.
  35. */
  36. define('PATHAUTO_PUNCTUATION_REMOVE', 0);
  37. /**
  38. * Replace the punctuation with the separator in the alias.
  39. */
  40. define('PATHAUTO_PUNCTUATION_REPLACE', 1);
  41. /**
  42. * Leave the punctuation as it is in the alias.
  43. */
  44. define('PATHAUTO_PUNCTUATION_DO_NOTHING', 2);
  45. /**
  46. * Check to see if there is already an alias pointing to a different item.
  47. *
  48. * @param $alias
  49. * A string alias.
  50. * @param $source
  51. * A string that is the internal path.
  52. * @param $langcode
  53. * A string indicating the path's language.
  54. *
  55. * @return bool
  56. * TRUE if an alias exists, FALSE if not.
  57. *
  58. * @deprecated Use path_pathauto_is_alias_reserved() instead.
  59. */
  60. function _pathauto_alias_exists($alias, $source, $langcode = LANGUAGE_NONE) {
  61. return path_pathauto_is_alias_reserved($alias, $source, $langcode);
  62. }
  63. /**
  64. * Fetches an existing URL alias given a path and optional language.
  65. *
  66. * @param string $source
  67. * An internal Drupal path.
  68. * @param string $language
  69. * An optional language code to look up the path in.
  70. *
  71. * @return bool|array
  72. * FALSE if no alias was found or an associative array containing the
  73. * following keys:
  74. * - pid: Unique path alias identifier.
  75. * - alias: The URL alias.
  76. */
  77. function _pathauto_existing_alias_data($source, $language = LANGUAGE_NONE) {
  78. $pid = db_query_range("SELECT pid FROM {url_alias} WHERE source = :source AND language IN (:language, :language_none) ORDER BY language DESC, pid DESC", 0, 1, array(':source' => $source, ':language' => $language, ':language_none' => LANGUAGE_NONE))->fetchField();
  79. return path_load(array('pid' => $pid));
  80. }
  81. /**
  82. * Clean up a string segment to be used in an URL alias.
  83. *
  84. * Performs the following possible alterations:
  85. * - Remove all HTML tags.
  86. * - Process the string through the transliteration module.
  87. * - Replace or remove punctuation with the separator character.
  88. * - Remove back-slashes.
  89. * - Replace non-ascii and non-numeric characters with the separator.
  90. * - Remove common words.
  91. * - Replace whitespace with the separator character.
  92. * - Trim duplicate, leading, and trailing separators.
  93. * - Convert to lower-case.
  94. * - Shorten to a desired length and logical position based on word boundaries.
  95. *
  96. * This function should *not* be called on URL alias or path strings because it
  97. * is assumed that they are already clean.
  98. *
  99. * @param string $string
  100. * A string to clean.
  101. * @param array $options
  102. * (optional) A keyed array of settings and flags to control the Pathauto
  103. * clean string replacement process. Supported options are:
  104. * - langcode: A language code to be used when translating strings.
  105. *
  106. * @return string
  107. * The cleaned string.
  108. */
  109. function pathauto_cleanstring($string, array $options = array()) {
  110. // Use the advanced drupal_static() pattern, since this is called very often.
  111. static $drupal_static_fast;
  112. if (!isset($drupal_static_fast)) {
  113. $drupal_static_fast['cache'] = &drupal_static(__FUNCTION__);
  114. }
  115. $cache = &$drupal_static_fast['cache'];
  116. // Generate and cache variables used in this function so that on the second
  117. // call to pathauto_cleanstring() we focus on processing.
  118. if (!isset($cache)) {
  119. $cache = array(
  120. 'separator' => variable_get('pathauto_separator', '-'),
  121. 'strings' => array(),
  122. 'transliterate' => variable_get('pathauto_transliterate', FALSE) && module_exists('transliteration'),
  123. 'punctuation' => array(),
  124. 'reduce_ascii' => (bool) variable_get('pathauto_reduce_ascii', FALSE),
  125. 'ignore_words_regex' => FALSE,
  126. 'lowercase' => (bool) variable_get('pathauto_case', PATHAUTO_CASE_LOWER),
  127. 'maxlength' => min(variable_get('pathauto_max_component_length', 100), _pathauto_get_schema_alias_maxlength()),
  128. );
  129. // Generate and cache the punctuation replacements for strtr().
  130. $punctuation = pathauto_punctuation_chars();
  131. foreach ($punctuation as $name => $details) {
  132. $action = variable_get('pathauto_punctuation_' . $name, PATHAUTO_PUNCTUATION_REMOVE);
  133. switch ($action) {
  134. case PATHAUTO_PUNCTUATION_REMOVE:
  135. $cache['punctuation'][$details['value']] = '';
  136. break;
  137. case PATHAUTO_PUNCTUATION_REPLACE:
  138. $cache['punctuation'][$details['value']] = $cache['separator'];
  139. break;
  140. case PATHAUTO_PUNCTUATION_DO_NOTHING:
  141. // Literally do nothing.
  142. break;
  143. }
  144. }
  145. // Generate and cache the ignored words regular expression.
  146. $ignore_words = variable_get('pathauto_ignore_words', PATHAUTO_IGNORE_WORDS);
  147. $ignore_words_regex = preg_replace(array('/^[,\s]+|[,\s]+$/', '/[,\s]+/'), array('', '\b|\b'), $ignore_words);
  148. if ($ignore_words_regex) {
  149. $cache['ignore_words_regex'] = '\b' . $ignore_words_regex . '\b';
  150. if (function_exists('mb_eregi_replace')) {
  151. mb_regex_encoding('UTF-8');
  152. $cache['ignore_words_callback'] = 'mb_eregi_replace';
  153. }
  154. else {
  155. $cache['ignore_words_callback'] = 'preg_replace';
  156. $cache['ignore_words_regex'] = '/' . $cache['ignore_words_regex'] . '/i';
  157. }
  158. }
  159. }
  160. // Empty strings do not need any processing.
  161. if ($string === '' || $string === NULL) {
  162. return '';
  163. }
  164. $langcode = NULL;
  165. if (!empty($options['language']->language)) {
  166. $langcode = $options['language']->language;
  167. }
  168. elseif (!empty($options['langcode'])) {
  169. $langcode = $options['langcode'];
  170. }
  171. // Check if the string has already been processed, and if so return the
  172. // cached result.
  173. if (isset($cache['strings'][$langcode][$string])) {
  174. return $cache['strings'][$langcode][$string];
  175. }
  176. // Remove all HTML tags from the string.
  177. $output = strip_tags(decode_entities($string));
  178. // Optionally transliterate (by running through the Transliteration module)
  179. if ($cache['transliterate']) {
  180. // If the reduce strings to letters and numbers is enabled, don't bother
  181. // replacing unknown characters with a question mark. Use an empty string
  182. // instead.
  183. $output = transliteration_get($output, $cache['reduce_ascii'] ? '' : '?', $langcode);
  184. }
  185. // Replace or drop punctuation based on user settings
  186. $output = strtr($output, $cache['punctuation']);
  187. // Reduce strings to letters and numbers
  188. if ($cache['reduce_ascii']) {
  189. $output = preg_replace('/[^a-zA-Z0-9\/]+/', $cache['separator'], $output);
  190. }
  191. // Get rid of words that are on the ignore list
  192. if ($cache['ignore_words_regex']) {
  193. $words_removed = $cache['ignore_words_callback']($cache['ignore_words_regex'], '', $output);
  194. if (drupal_strlen(trim($words_removed)) > 0) {
  195. $output = $words_removed;
  196. }
  197. }
  198. // Always replace whitespace with the separator.
  199. $output = preg_replace('/\s+/', $cache['separator'], $output);
  200. // Trim duplicates and remove trailing and leading separators.
  201. $output = _pathauto_clean_separators($output, $cache['separator']);
  202. // Optionally convert to lower case.
  203. if ($cache['lowercase']) {
  204. $output = drupal_strtolower($output);
  205. }
  206. // Shorten to a logical place based on word boundaries.
  207. $output = truncate_utf8($output, $cache['maxlength'], TRUE);
  208. // Cache this result in the static array.
  209. $cache['strings'][$langcode][$string] = $output;
  210. return $output;
  211. }
  212. /**
  213. * Trims duplicate, leading, and trailing separators from a string.
  214. *
  215. * @param string $string
  216. * The string to clean path separators from.
  217. * @param string $separator
  218. * The path separator to use when cleaning.
  219. *
  220. * @return string
  221. * The cleaned version of the string.
  222. *
  223. * @see pathauto_cleanstring()
  224. * @see pathauto_clean_alias()
  225. */
  226. function _pathauto_clean_separators($string, $separator = NULL) {
  227. static $default_separator;
  228. if (!isset($separator)) {
  229. if (!isset($default_separator)) {
  230. $default_separator = variable_get('pathauto_separator', '-');
  231. }
  232. $separator = $default_separator;
  233. }
  234. $output = $string;
  235. if (strlen($separator)) {
  236. // Trim any leading or trailing separators.
  237. $output = trim($output, $separator);
  238. // Escape the separator for use in regular expressions.
  239. $seppattern = preg_quote($separator, '/');
  240. // Replace multiple separators with a single one.
  241. $output = preg_replace("/$seppattern+/", $separator, $output);
  242. // Replace trailing separators around slashes.
  243. if ($separator !== '/') {
  244. $output = preg_replace("/\/+$seppattern\/+|$seppattern\/+|\/+$seppattern/", "/", $output);
  245. }
  246. }
  247. return $output;
  248. }
  249. /**
  250. * Clean up an URL alias.
  251. *
  252. * Performs the following alterations:
  253. * - Trim duplicate, leading, and trailing back-slashes.
  254. * - Trim duplicate, leading, and trailing separators.
  255. * - Shorten to a desired length and logical position based on word boundaries.
  256. *
  257. * @param string $alias
  258. * A string with the URL alias to clean up.
  259. *
  260. * @return string
  261. * The cleaned URL alias.
  262. */
  263. function pathauto_clean_alias($alias) {
  264. $cache = &drupal_static(__FUNCTION__);
  265. if (!isset($cache)) {
  266. $cache = array(
  267. 'maxlength' => min(variable_get('pathauto_max_length', 100), _pathauto_get_schema_alias_maxlength()),
  268. );
  269. }
  270. $output = $alias;
  271. // Trim duplicate, leading, and trailing separators. Do this before cleaning
  272. // backslashes since a pattern like "[token1]/[token2]-[token3]/[token4]"
  273. // could end up like "value1/-/value2" and if backslashes were cleaned first
  274. // this would result in a duplicate blackslash.
  275. $output = _pathauto_clean_separators($output);
  276. // Trim duplicate, leading, and trailing backslashes.
  277. $output = _pathauto_clean_separators($output, '/');
  278. // Shorten to a logical place based on word boundaries.
  279. $output = truncate_utf8($output, $cache['maxlength'], TRUE);
  280. return $output;
  281. }
  282. /**
  283. * Apply patterns to create an alias.
  284. *
  285. * @param $module
  286. * The name of your module (e.g., 'node').
  287. * @param $op
  288. * Operation being performed on the content being aliased
  289. * ('insert', 'update', 'return', or 'bulkupdate').
  290. * @param $source
  291. * An internal Drupal path to be aliased.
  292. * @param $data
  293. * An array of keyed objects to pass to token_replace(). For simple
  294. * replacement scenarios 'node', 'user', and others are common keys, with an
  295. * accompanying node or user object being the value. Some token types, like
  296. * 'site', do not require any explicit information from $data and can be
  297. * replaced even if it is empty.
  298. * @param $type
  299. * For modules which provided pattern items in hook_pathauto(),
  300. * the relevant identifier for the specific item to be aliased
  301. * (e.g., $node->type).
  302. * @param $language
  303. * A string specify the path's language.
  304. *
  305. * @return array|null|false
  306. * The alias array that was created, NULL if an empty alias was generated, or
  307. * FALSE if the alias generation was not possible.
  308. *
  309. * @see _pathauto_set_alias()
  310. * @see token_replace()
  311. */
  312. function pathauto_create_alias($module, $op, $source, $data, $type = NULL, $language = LANGUAGE_NONE) {
  313. // Retrieve and apply the pattern for this content type.
  314. $pattern = pathauto_pattern_load_by_entity($module, $type, $language);
  315. // Allow other modules to alter the pattern.
  316. $context = array(
  317. 'module' => $module,
  318. 'op' => $op,
  319. 'source' => $source,
  320. 'data' => $data,
  321. 'type' => $type,
  322. 'language' => &$language,
  323. );
  324. drupal_alter('pathauto_pattern', $pattern, $context);
  325. if (empty($pattern)) {
  326. // No pattern? Do nothing (otherwise we may blow away existing aliases...)
  327. return FALSE;
  328. }
  329. // Special handling when updating an item which is already aliased.
  330. $existing_alias = NULL;
  331. if ($op != 'insert') {
  332. if ($existing_alias = _pathauto_existing_alias_data($source, $language)) {
  333. switch (variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE)) {
  334. case PATHAUTO_UPDATE_ACTION_NO_NEW:
  335. // If an alias already exists, and the update action is set to do nothing,
  336. // then gosh-darn it, do nothing.
  337. return FALSE;
  338. }
  339. }
  340. }
  341. // Replace any tokens in the pattern. Uses callback option to clean replacements. No sanitization.
  342. $alias = token_replace($pattern, $data, array(
  343. 'sanitize' => FALSE,
  344. 'clear' => TRUE,
  345. 'callback' => 'pathauto_clean_token_values',
  346. 'language' => (object) array('language' => $language),
  347. 'pathauto' => TRUE,
  348. ));
  349. // Check if the token replacement has not actually replaced any values. If
  350. // that is the case, then stop because we should not generate an alias.
  351. // @see token_scan()
  352. $pattern_tokens_removed = preg_replace('/\[[^\s\]:]*:[^\s\]]*\]/', '', $pattern);
  353. if ($alias === $pattern_tokens_removed) {
  354. return;
  355. }
  356. $alias = pathauto_clean_alias($alias);
  357. // Allow other modules to alter the alias.
  358. $context['source'] = &$source;
  359. $context['pattern'] = $pattern;
  360. drupal_alter('pathauto_alias', $alias, $context);
  361. // If we have arrived at an empty string, discontinue.
  362. if (!drupal_strlen($alias)) {
  363. return;
  364. }
  365. // If the alias already exists, generate a new, hopefully unique, variant.
  366. $original_alias = $alias;
  367. pathauto_alias_uniquify($alias, $source, $language);
  368. if ($original_alias != $alias) {
  369. // Alert the user why this happened.
  370. _pathauto_verbose(t('The automatically generated alias %original_alias conflicted with an existing alias. Alias changed to %alias.', array(
  371. '%original_alias' => $original_alias,
  372. '%alias' => $alias,
  373. )), $op);
  374. }
  375. // Return the generated alias if requested.
  376. if ($op == 'return') {
  377. return $alias;
  378. }
  379. // Build the new path alias array and send it off to be created.
  380. $path = array(
  381. 'source' => $source,
  382. 'alias' => $alias,
  383. 'language' => $language,
  384. );
  385. $path = _pathauto_set_alias($path, $existing_alias, $op);
  386. return $path;
  387. }
  388. /**
  389. * Check to ensure a path alias is unique and add suffix variants if necessary.
  390. *
  391. * Given an alias 'content/test' if a path alias with the exact alias already
  392. * exists, the function will change the alias to 'content/test-0' and will
  393. * increase the number suffix until it finds a unique alias.
  394. *
  395. * @param $alias
  396. * A string with the alias. Can be altered by reference.
  397. * @param $source
  398. * A string with the path source.
  399. * @param $langcode
  400. * A string with a language code.
  401. */
  402. function pathauto_alias_uniquify(&$alias, $source, $langcode) {
  403. if (!pathauto_is_alias_reserved($alias, $source, $langcode)) {
  404. return;
  405. }
  406. // If the alias already exists, generate a new, hopefully unique, variant
  407. $maxlength = min(variable_get('pathauto_max_length', 100), _pathauto_get_schema_alias_maxlength());
  408. $separator = variable_get('pathauto_separator', '-');
  409. $original_alias = $alias;
  410. $i = 0;
  411. do {
  412. // Append an incrementing numeric suffix until we find a unique alias.
  413. $unique_suffix = $separator . $i;
  414. $alias = truncate_utf8($original_alias, $maxlength - drupal_strlen($unique_suffix), TRUE) . $unique_suffix;
  415. $i++;
  416. } while (pathauto_is_alias_reserved($alias, $source, $langcode));
  417. }
  418. /**
  419. * Verify if the given path is a valid menu callback.
  420. *
  421. * Taken from menu_execute_active_handler().
  422. *
  423. * @param $path
  424. * A string containing a relative path.
  425. * @return
  426. * TRUE if the path already exists.
  427. */
  428. function _pathauto_path_is_callback($path) {
  429. // We need to use a try/catch here because of a core bug which will throw an
  430. // exception if $path is something like 'node/foo/bar'.
  431. // @todo Remove when http://drupal.org/node/1003788 is fixed in core.
  432. try {
  433. $menu = menu_get_item($path);
  434. }
  435. catch (Exception $e) {
  436. return FALSE;
  437. }
  438. if (isset($menu['path']) && $menu['path'] == $path) {
  439. return TRUE;
  440. }
  441. elseif (is_file(DRUPAL_ROOT . '/' . $path) || is_dir(DRUPAL_ROOT . '/' . $path)) {
  442. // Do not allow existing files or directories to get assigned an automatic
  443. // alias. Note that we do not need to use is_link() to check for symbolic
  444. // links since this returns TRUE for either is_file() or is_dir() already.
  445. return TRUE;
  446. }
  447. return FALSE;
  448. }
  449. /**
  450. * Private function for Pathauto to create an alias.
  451. *
  452. * @param $path
  453. * An associative array containing the following keys:
  454. * - source: The internal system path.
  455. * - alias: The URL alias.
  456. * - pid: (optional) Unique path alias identifier.
  457. * - language: (optional) The language of the alias.
  458. * @param $existing_alias
  459. * (optional) An associative array of the existing path alias.
  460. * @param $op
  461. * An optional string with the operation being performed.
  462. *
  463. * @return
  464. * The saved path from path_save() or FALSE if the path was not saved.
  465. *
  466. * @see path_save()
  467. */
  468. function _pathauto_set_alias(array $path, $existing_alias = NULL, $op = NULL) {
  469. $verbose = _pathauto_verbose(NULL, $op);
  470. // Alert users if they are trying to create an alias that is the same as the internal path
  471. if ($path['source'] == $path['alias']) {
  472. if ($verbose) {
  473. _pathauto_verbose(t('Ignoring alias %alias because it is the same as the internal path.', array('%alias' => $path['alias'])));
  474. }
  475. return FALSE;
  476. }
  477. // Skip replacing the current alias with an identical alias
  478. if (empty($existing_alias) || $existing_alias['alias'] != $path['alias']) {
  479. $path += array('pathauto' => TRUE, 'original' => $existing_alias);
  480. // If there is already an alias, respect some update actions.
  481. if (!empty($existing_alias)) {
  482. switch (variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE)) {
  483. case PATHAUTO_UPDATE_ACTION_NO_NEW:
  484. // Do not create the alias.
  485. return FALSE;
  486. case PATHAUTO_UPDATE_ACTION_LEAVE:
  487. // Create a new alias instead of overwriting the existing by leaving
  488. // $path['pid'] empty.
  489. break;
  490. case PATHAUTO_UPDATE_ACTION_DELETE:
  491. // The delete actions should overwrite the existing alias.
  492. $path['pid'] = $existing_alias['pid'];
  493. break;
  494. }
  495. }
  496. // Save the path array.
  497. path_save($path);
  498. if ($verbose) {
  499. if (!empty($existing_alias['pid'])) {
  500. _pathauto_verbose(t('Created new alias %alias for %source, replacing %old_alias.', array('%alias' => $path['alias'], '%source' => $path['source'], '%old_alias' => $existing_alias['alias'])));
  501. }
  502. else {
  503. _pathauto_verbose(t('Created new alias %alias for %source.', array('%alias' => $path['alias'], '%source' => $path['source'])));
  504. }
  505. }
  506. return $path;
  507. }
  508. }
  509. /**
  510. * Output a helpful message if verbose output is enabled.
  511. *
  512. * Verbose output is only enabled when:
  513. * - The 'pathauto_verbose' setting is enabled.
  514. * - The current user has the 'notify of path changes' permission.
  515. * - The $op parameter is anything but 'bulkupdate' or 'return'.
  516. *
  517. * @param $message
  518. * An optional string of the verbose message to display. This string should
  519. * already be run through t().
  520. * @param $op
  521. * An optional string with the operation being performed.
  522. * @return
  523. * TRUE if verbose output is enabled, or FALSE otherwise.
  524. */
  525. function _pathauto_verbose($message = NULL, $op = NULL) {
  526. static $verbose;
  527. if (!isset($verbose)) {
  528. $verbose = variable_get('pathauto_verbose', FALSE) && user_access('notify of path changes');
  529. }
  530. if (!$verbose || (isset($op) && in_array($op, array('bulkupdate', 'return')))) {
  531. return FALSE;
  532. }
  533. if ($message) {
  534. drupal_set_message($message);
  535. }
  536. return $verbose;
  537. }
  538. /**
  539. * Clean tokens so they are URL friendly.
  540. *
  541. * @param $replacements
  542. * An array of token replacements that need to be "cleaned" for use in the URL.
  543. * @param $data
  544. * An array of objects used to generate the replacements.
  545. * @param $options
  546. * An array of options used to generate the replacements.
  547. */
  548. function pathauto_clean_token_values(&$replacements, $data = array(), $options = array()) {
  549. foreach ($replacements as $token => $value) {
  550. // Only clean non-path tokens.
  551. if (!preg_match('/(path|alias|url|url-brief)\]$/', $token)) {
  552. $replacements[$token] = pathauto_cleanstring($value, $options);
  553. }
  554. }
  555. }
  556. /**
  557. * Return an array of arrays for punctuation values.
  558. *
  559. * Returns an array of arrays for punctuation values keyed by a name, including
  560. * the value and a textual description.
  561. * Can and should be expanded to include "all" non text punctuation values.
  562. *
  563. * @return
  564. * An array of arrays for punctuation values keyed by a name, including the
  565. * value and a textual description.
  566. */
  567. function pathauto_punctuation_chars() {
  568. $punctuation = &drupal_static(__FUNCTION__);
  569. if (!isset($punctuation)) {
  570. $cid = 'pathauto:punctuation:' . $GLOBALS['language']->language;
  571. if ($cache = cache_get($cid)) {
  572. $punctuation = $cache->data;
  573. }
  574. else {
  575. $punctuation = array();
  576. $punctuation['double_quotes'] = array('value' => '"', 'name' => t('Double quotation marks'));
  577. $punctuation['quotes'] = array('value' => '\'', 'name' => t("Single quotation marks (apostrophe)"));
  578. $punctuation['backtick'] = array('value' => '`', 'name' => t('Back tick'));
  579. $punctuation['comma'] = array('value' => ',', 'name' => t('Comma'));
  580. $punctuation['period'] = array('value' => '.', 'name' => t('Period'));
  581. $punctuation['hyphen'] = array('value' => '-', 'name' => t('Hyphen'));
  582. $punctuation['underscore'] = array('value' => '_', 'name' => t('Underscore'));
  583. $punctuation['colon'] = array('value' => ':', 'name' => t('Colon'));
  584. $punctuation['semicolon'] = array('value' => ';', 'name' => t('Semicolon'));
  585. $punctuation['pipe'] = array('value' => '|', 'name' => t('Vertical bar (pipe)'));
  586. $punctuation['left_curly'] = array('value' => '{', 'name' => t('Left curly bracket'));
  587. $punctuation['left_square'] = array('value' => '[', 'name' => t('Left square bracket'));
  588. $punctuation['right_curly'] = array('value' => '}', 'name' => t('Right curly bracket'));
  589. $punctuation['right_square'] = array('value' => ']', 'name' => t('Right square bracket'));
  590. $punctuation['plus'] = array('value' => '+', 'name' => t('Plus sign'));
  591. $punctuation['equal'] = array('value' => '=', 'name' => t('Equal sign'));
  592. $punctuation['asterisk'] = array('value' => '*', 'name' => t('Asterisk'));
  593. $punctuation['ampersand'] = array('value' => '&', 'name' => t('Ampersand'));
  594. $punctuation['percent'] = array('value' => '%', 'name' => t('Percent sign'));
  595. $punctuation['caret'] = array('value' => '^', 'name' => t('Caret'));
  596. $punctuation['dollar'] = array('value' => '$', 'name' => t('Dollar sign'));
  597. $punctuation['hash'] = array('value' => '#', 'name' => t('Number sign (pound sign, hash)'));
  598. $punctuation['at'] = array('value' => '@', 'name' => t('At sign'));
  599. $punctuation['exclamation'] = array('value' => '!', 'name' => t('Exclamation mark'));
  600. $punctuation['tilde'] = array('value' => '~', 'name' => t('Tilde'));
  601. $punctuation['left_parenthesis'] = array('value' => '(', 'name' => t('Left parenthesis'));
  602. $punctuation['right_parenthesis'] = array('value' => ')', 'name' => t('Right parenthesis'));
  603. $punctuation['question_mark'] = array('value' => '?', 'name' => t('Question mark'));
  604. $punctuation['less_than'] = array('value' => '<', 'name' => t('Less-than sign'));
  605. $punctuation['greater_than'] = array('value' => '>', 'name' => t('Greater-than sign'));
  606. $punctuation['slash'] = array('value' => '/', 'name' => t('Slash'));
  607. $punctuation['back_slash'] = array('value' => '\\', 'name' => t('Backslash'));
  608. // Allow modules to alter the punctuation list and cache the result.
  609. drupal_alter('pathauto_punctuation_chars', $punctuation);
  610. cache_set($cid, $punctuation);
  611. }
  612. }
  613. return $punctuation;
  614. }
  615. /**
  616. * Fetch the maximum length of the {url_alias}.alias field from the schema.
  617. *
  618. * @return
  619. * An integer of the maximum URL alias length allowed by the database.
  620. */
  621. function _pathauto_get_schema_alias_maxlength() {
  622. $maxlength = &drupal_static(__FUNCTION__);
  623. if (!isset($maxlength)) {
  624. $schema = drupal_get_schema('url_alias');
  625. $maxlength = $schema['fields']['alias']['length'];
  626. }
  627. return $maxlength;
  628. }