View source
<?php
define('PATHAUTO_IGNORE_WORDS', 'a, an, as, at, before, but, by, for, from, is, in, into, like, of, off, on, onto, per, since, than, the, this, that, to, up, via, with');
function pathauto_help($path, $arg) {
switch ($path) {
case 'admin/help#pathauto':
module_load_include('inc', 'pathauto');
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '</p>';
$output .= '<h3>' . t('Settings') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Maximum alias and component length') . '</dt>';
$output .= '<dd>' . t('The <strong>maximum alias length</strong> and <strong>maximum component length</strong> values default to 100 and have a limit of @max from Pathauto. This length is limited by the length of the "alias" column of the url_alias database table. The default database schema for this column is @max. If you set a length that is equal to that of the one set in the "alias" column it will cause problems in situations where the system needs to append additional words to the aliased URL. You should enter a value that is the length of the "alias" column minus the length of any strings that might get added to the end of the URL. The length of strings that might get added to the end of your URLs depends on which modules you have enabled and on your Pathauto settings. The recommended and default value is 100.', array(
'@max' => _pathauto_get_schema_alias_maxlength(),
)) . '</dd>';
$output .= '<dt>' . t('Raw tokens') . '</dt>';
$output .= '<dd>' . t('In Pathauto it is appropriate to use the -raw form of tokens. Paths are sent through a filtering system which ensures that raw user content is filtered. Failure to use -raw tokens can cause problems with the Pathauto punctuation filtering system.') . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/build/path/update_bulk':
$output = '<p>' . t('Bulk generation will only generate URL aliases for items that currently have no aliases. This is typically used when installing Pathauto on a site that has existing un-aliased content that needs to be aliased in bulk.') . '</p>';
return $output;
}
}
function pathauto_perm() {
return array(
'administer pathauto',
'notify of path changes',
);
}
function pathauto_menu() {
$items['admin/build/path/patterns'] = array(
'title' => 'Patterns',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pathauto_patterns_form',
),
'access arguments' => array(
'administer pathauto',
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'file' => 'pathauto.admin.inc',
);
$items['admin/build/path/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pathauto_settings_form',
),
'access arguments' => array(
'administer pathauto',
),
'type' => MENU_LOCAL_TASK,
'weight' => 20,
'file' => 'pathauto.admin.inc',
);
$items['admin/build/path/update_bulk'] = array(
'title' => 'Bulk generate',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pathauto_bulk_update_form',
),
'access arguments' => array(
'administer url aliases',
),
'type' => MENU_LOCAL_TASK,
'weight' => 30,
'file' => 'pathauto.admin.inc',
);
$items['admin/build/path/delete_bulk'] = array(
'title' => 'Delete aliases',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pathauto_admin_delete',
),
'access arguments' => array(
'administer url aliases',
),
'type' => MENU_LOCAL_TASK,
'weight' => 40,
'file' => 'pathauto.admin.inc',
);
return $items;
}
function _pathauto_include() {
module_load_include('inc', 'pathauto');
$backtrace = debug_backtrace();
watchdog('pathauto', "The !deprecated function has been deprecated and !replacement should be used instead. It was called from %file on line %line, function %function().", array(
'!deprecated' => __FUNCTION__,
'!replacement' => "module_load_include('inc', 'pathauto')",
'%file' => basename($backtrace[0]['file']),
'%line' => $backtrace[0]['line'],
'%function' => $backtrace[1]['function'],
), WATCHDOG_NOTICE);
}
function pathauto_token_list($type = 'all') {
module_load_include('inc', 'pathauto', 'pathauto.tokens');
return _pathauto_token_list($type);
}
function pathauto_token_values($type, $object = NULL, $options = array()) {
module_load_include('inc', 'pathauto', 'pathauto.tokens');
return _pathauto_token_values($type, $object, $options);
}
function pathauto_path_alias_types() {
$objects['user/'] = t('Users');
$objects['node/'] = t('Content');
if (module_exists('blog')) {
$objects['blog/'] = t('User blogs');
}
if (module_exists('taxonomy')) {
$objects['taxonomy/term/'] = t('Taxonomy terms');
}
if (module_exists('forum')) {
$objects['forum/'] = t('Forums');
}
return $objects;
}
function pathauto_pattern_load_by_entity($entity, $bundle = '', $language = '') {
$patterns =& pathauto_static(__FUNCTION__, array());
$pattern_id = "{$entity}:{$bundle}:{$language}";
if (!isset($patterns[$pattern_id])) {
$variables = array();
if ($language) {
$variables[] = "pathauto_{$entity}_{$bundle}_{$language}_pattern";
}
if ($bundle) {
$variables[] = "pathauto_{$entity}_{$bundle}_pattern";
}
$variables[] = "pathauto_{$entity}_pattern";
foreach ($variables as $variable) {
if ($pattern = trim(variable_get($variable, ''))) {
break;
}
}
$patterns[$pattern_id] = $pattern;
}
return $patterns[$pattern_id];
}
function pathauto_path_delete_all($source) {
$source = urldecode($source);
db_query("DELETE FROM {url_alias} WHERE src = '%s'", $source);
db_query("DELETE FROM {url_alias} WHERE src LIKE '%s'", $source . '/%%');
drupal_clear_path_cache();
}
function _pathauto_sql_concat() {
$args = func_get_args();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
return 'CONCAT(' . implode(', ', $args) . ')';
default:
return '(' . implode(' || ', $args) . ')';
}
}
function _pathauto_sql_count($sql) {
$sql = preg_replace('/SELECT .* FROM/', 'SELECT COUNT(*) FROM', $sql);
$sql = preg_replace('/ORDER BY .*/', '', $sql);
return $sql;
}
function pathauto_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
$result = db_query("SELECT name FROM {variable} WHERE name LIKE '%s%%'", "pathauto_{$entity_type}_{$bundle_old}_");
while ($variable = db_result($result)) {
$value = variable_get($variable, '');
variable_del($variable);
$variable = strtr($variable, array(
"{$entity_type}_{$bundle_old}" => "{$entity_type}_{$bundle_new}",
));
variable_set($variable, $value);
}
}
function pathauto_field_attach_delete_bundle($entity_type, $bundle) {
$result = db_query("SELECT name FROM {variable} WHERE name LIKE '%s%%'", "pathauto_{$entity_type}_{$bundle}_");
while ($variable = db_result($result)) {
variable_del($variable);
}
}
function pathauto_action_info() {
$info['pathauto_node_update_action'] = array(
'type' => 'node',
'description' => t('Update node alias'),
'configurable' => FALSE,
'hooks' => array(),
);
$info['pathauto_taxonomy_term_update_action'] = array(
'type' => 'term',
'description' => t('Update taxonomy term alias'),
'configurable' => FALSE,
'hooks' => array(),
);
$info['pathauto_user_update_action'] = array(
'type' => 'user',
'description' => t('Update user alias'),
'configurable' => FALSE,
'hooks' => array(),
);
return $info;
}
function node_pathauto($op) {
module_load_include('inc', 'pathauto', 'pathauto.pathauto');
return _node_pathauto($op);
}
function pathauto_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'presave':
if (!empty($node->pathauto_perform_alias) && isset($node->old_alias) && $node->path == '' && $node->old_alias != '') {
$node->path = $node->old_alias;
}
break;
case 'insert':
case 'update':
pathauto_node_update_alias($node, $op);
break;
case 'delete':
pathauto_path_delete_all("node/{$node->nid}");
break;
}
}
function pathauto_node_type($op, $info) {
switch ($op) {
case 'update':
if (!empty($info->old_type) && $info->old_type != $info->type) {
pathauto_field_attach_rename_bundle('node', $info->old_type, $info->type);
}
break;
case 'delete':
pathauto_field_attach_delete_bundle('node', $info->type);
break;
}
}
function pathauto_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
$node = $form['#node'];
$language = isset($node->language) ? $node->language : '';
$pattern = pathauto_pattern_load_by_entity('node', $node->type, $language);
if ($pattern) {
if (!isset($node->pathauto_perform_alias)) {
if (!empty($node->nid)) {
module_load_include('inc', 'pathauto');
$pathauto_alias = pathauto_create_alias('node', 'return', "node/{$node->nid}", array(
'node' => $node,
), $node->nid, $node->type, $node->language);
$node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias;
}
else {
$node->pathauto_perform_alias = TRUE;
}
}
drupal_add_js(drupal_get_path('module', 'pathauto') . '/pathauto.js');
$form['path']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'pathauto') . '/pathauto.js';
$form['path']['pathauto_perform_alias'] = array(
'#type' => 'checkbox',
'#title' => t('Generate automatic URL alias'),
'#default_value' => $node->pathauto_perform_alias,
'#description' => t('Uncheck this to create a custom alias below.'),
'#weight' => -1,
);
if (menu_valid_path(array(
'link_path' => 'admin/build/path/patterns',
))) {
$form['path']['pathauto_perform_alias']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/build/path/patterns');
}
if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($node->path)) {
$form['path']['path']['#default_value'] = $node->old_alias;
$node->path = $node->old_alias;
}
if (isset($node->path)) {
$form['path']['old_alias'] = array(
'#type' => 'value',
'#value' => $node->path,
);
}
}
}
}
function pathauto_node_operations() {
$operations['pathauto_update_alias'] = array(
'label' => t('Update URL alias'),
'callback' => 'pathauto_node_update_alias_multiple',
'callback arguments' => array(
'bulkupdate',
array(
'message' => TRUE,
),
),
);
return $operations;
}
function pathauto_node_update_alias($node, $op, $options = array()) {
if (isset($node->pathauto_perform_alias) && empty($node->pathauto_perform_alias) && empty($options['force'])) {
return;
}
$options += array(
'language' => isset($node->language) ? $node->language : '',
);
if (!pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) {
return;
}
module_load_include('inc', 'pathauto');
if ($alias = pathauto_create_alias('node', $op, "node/{$node->nid}", array(
'node' => $node,
), $node->nid, $node->type, $options['language'])) {
$node->path = $alias;
}
}
function pathauto_node_update_alias_multiple($nids, $op, $options = array()) {
$options += array(
'message' => FALSE,
);
foreach ($nids as $nid) {
if ($node = node_load($nid, NULL, TRUE)) {
pathauto_node_update_alias($node, $op, $options);
}
}
if (!empty($options['message'])) {
drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
}
}
function pathauto_node_update_action($node, $context = array()) {
pathauto_node_update_alias($node, 'bulkupdate', array(
'message' => TRUE,
));
}
function taxonomy_pathauto($op) {
module_load_include('inc', 'pathauto', 'pathauto.pathauto');
return _taxonomy_pathauto($op);
}
function forum_pathauto($op) {
module_load_include('inc', 'pathauto', 'pathauto.pathauto');
return _forum_pathauto($op);
}
function pathauto_taxonomy($op, $type, $object = NULL) {
switch ($type) {
case 'term':
switch ($op) {
case 'insert':
case 'update':
$term = (object) $object;
if ($op == 'update') {
taxonomy_get_term($term->tid, TRUE);
}
pathauto_taxonomy_term_update_alias($term, $op);
break;
case 'delete':
$term = (object) $object;
$term_path = taxonomy_term_path($term);
pathauto_path_delete_all($term_path);
if ($term_path != "taxonomy/term/{$term->tid}") {
pathauto_path_delete_all("taxonomy/term/{$term->tid}");
}
break;
}
break;
case 'vocabulary':
$vocabulary = (object) $object;
switch ($op) {
case 'delete':
pathauto_field_attach_delete_bundle('taxonomy', $vocabulary->vid);
break;
}
break;
}
}
function pathauto_taxonomy_term_update_alias($term, $op, $options = array()) {
if (isset($term->pathauto_perform_alias) && empty($term->pathauto_perform_alias) && empty($options['force'])) {
return;
}
$options += array(
'alias children' => FALSE,
'language' => isset($term->language) ? $term->language : '',
);
$module = 'taxonomy';
if (module_exists('forum') && $term->vid == variable_get('forum_nav_vocabulary', '')) {
$module = 'forum';
}
if (!pathauto_pattern_load_by_entity($module, $term->vid, $options['language'])) {
return;
}
module_load_include('inc', 'pathauto');
$source = taxonomy_term_path($term);
pathauto_create_alias($module, $op, $source, array(
'taxonomy' => $term,
), $term->tid, $term->vid, $options['language']);
if (!empty($options['alias children'])) {
$options['alias children'] = FALSE;
unset($options['language']);
foreach (taxonomy_get_tree($term->vid, $term->tid) as $subterm) {
pathauto_taxonomy_term_update_alias($subterm, $op, $options);
}
}
}
function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) {
$options += array(
'message' => FALSE,
);
foreach ($tids as $tid) {
if ($term = taxonomy_get_term($tid, TRUE)) {
pathauto_taxonomy_term_update_alias($term, $op, $options);
}
}
if (!empty($options['message'])) {
drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
}
}
function pathauto_taxonomy_term_update_action($term, $context = array()) {
pathauto_taxonomy_term_update_alias($term, 'bulkupdate', array(
'message' => TRUE,
));
}
function user_pathauto($op) {
module_load_include('inc', 'pathauto', 'pathauto.pathauto');
return _user_pathauto($op);
}
function blog_pathauto($op) {
module_load_include('inc', 'pathauto', 'pathauto.pathauto');
return _blog_pathauto($op);
}
function pathauto_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
$merged_account = drupal_clone($account);
$merged_account->data = array();
$user_fields = user_fields();
foreach ($edit as $key => $value) {
if (substr($key, 0, 4) !== 'auth' && $key != 'roles' && !in_array($key, $user_fields) && $value !== NULL) {
$merged_account->data[$key] = $value;
}
}
if (isset($edit['roles']) && is_array($edit['roles'])) {
$roles = user_roles();
foreach (array_keys($edit['roles']) as $rid) {
if (!isset($merged_account->roles[$rid])) {
$merged_account->roles[$rid] = $roles[$rid];
}
}
}
pathauto_user_update_alias($merged_account, 'insert');
break;
case 'after_update':
pathauto_user_update_alias($account, 'update');
break;
case 'delete':
pathauto_path_delete_all("user/{$account->uid}");
pathauto_path_delete_all("blog/{$account->uid}");
break;
}
}
function pathauto_user_operations() {
$operations['pathauto_update_alias'] = array(
'label' => t('Update URL alias'),
'callback' => 'pathauto_user_update_alias_multiple',
'callback arguments' => array(
'bulkupdate',
array(
'message' => TRUE,
),
),
);
return $operations;
}
function pathauto_user_update_alias($account, $op, $options = array()) {
if (isset($account->pathauto_perform_alias) && empty($account->pathauto_perform_alias) && empty($options['force'])) {
return;
}
$options += array(
'alias blog' => module_exists('blog'),
'language' => '',
);
if (!pathauto_pattern_load_by_entity('user', '', $options['language'])) {
return;
}
module_load_include('inc', 'pathauto');
pathauto_create_alias('user', $op, "user/{$account->uid}", array(
'user' => $account,
), $account->uid, NULL, $options['language']);
if (!empty($options['alias blog'])) {
pathauto_blog_update_alias($account, $op);
}
}
function pathauto_user_update_alias_multiple($uids, $op, $options = array()) {
$options += array(
'message' => FALSE,
);
foreach ($uids as $uid) {
if ($account = user_load($uid)) {
pathauto_user_update_alias($account, $op, $options);
}
}
if (!empty($options['message'])) {
drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
}
}
function pathauto_user_update_action($account, $context = array()) {
pathauto_user_update_alias($account, 'bulkupdate', array(
'message' => TRUE,
));
}
function pathauto_blog_update_alias($account, $op, $options = array()) {
if (!pathauto_pattern_load_by_entity('blog')) {
return;
}
$options += array(
'language' => '',
);
module_load_include('inc', 'pathauto');
if (node_access('create', 'blog', $account)) {
pathauto_create_alias('blog', $op, "blog/{$account->uid}", array(
'user' => $account,
), $account->uid, NULL, $options['language']);
}
else {
pathauto_path_delete_all("blog/{$account->uid}");
}
}
function &pathauto_static($name, $default_value = NULL, $reset = FALSE) {
static $data = array(), $default = array();
if (isset($data[$name]) || array_key_exists($name, $data)) {
if ($reset) {
$data[$name] = $default[$name];
}
return $data[$name];
}
if (isset($name)) {
if ($reset) {
return $data;
}
$default[$name] = $data[$name] = $default_value;
return $data[$name];
}
foreach ($default as $name => $value) {
$data[$name] = $value;
}
return $data;
}
function pathauto_static_reset($name = NULL) {
pathauto_static($name, NULL, TRUE);
}