View source
<?php
$pathauto_path = drupal_get_path('module', 'pathauto');
include_once "{$pathauto_path}/pathauto_menu.inc";
include_once "{$pathauto_path}/pathauto_node.inc";
include_once "{$pathauto_path}/pathauto_taxonomy.inc";
include_once "{$pathauto_path}/pathauto_user.inc";
include_once "{$pathauto_path}/contrib/pathauto_node_event.inc";
include_once "{$pathauto_path}/contrib/pathauto_node_i18n.inc";
unset($pathauto_path);
function pathauto_help($section) {
$output = '';
switch ($section) {
case 'admin/help#pathauto':
$output = t("<p>Provides a mechanism for modules to automatically generate aliases for the content they manage.</p>\n <h2>Settings</h2>\n <p>The <strong>Maximum Alias Length</strong> and <strong>Maximum component length</strong> values\n default to 100 and have a limit of 128 from pathauto. This length is limited by the length of the dst \n column of the url_alias database table. The default database schema for this column is 128. If you \n set a length that is equal to that of the one set in the dst column it will cause problems in situations \n where the system needs to append additional words to the aliased URL. For example... URLs generated\n for feeds will have '/feed' added to the end. You should enter a value that is the length of the dst\n column minus the length of any strings that might get added to the end of the URL. The length of \n strings that might get added to the end of your URLs depends on which modules you have enabled and \n on your Pathauto settings. The recommended and default value is 100.</p>");
break;
}
return $output;
}
function pathauto_perm() {
return array(
'administer pathauto',
);
}
function pathauto_admin_settings() {
if (!user_access('administer pathauto')) {
$form["error"] = array(
'#type' => 'item',
'#title' => t('You are not authorized to access the pathauto settings.'),
);
return system_settings_form($form);
}
$output = '';
_pathauto_update();
$ignore_words = array(
"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",
);
$group_weight = -20;
$form["general"] = array(
'#type' => 'fieldset',
'#weight' => $group_weight,
'#title' => t('General settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$group_weight++;
$form["general"]["pathauto_verbose"] = array(
'#type' => 'checkbox',
'#title' => t('Verbose'),
'#default_value' => variable_get('pathauto_verbose', FALSE),
'#description' => t('Display alias changes (except during bulk updates).'),
);
$form["general"]["pathauto_separator"] = array(
'#type' => 'textfield',
'#title' => t('Separator'),
'#size' => 1,
'#maxlength' => 1,
'#default_value' => variable_get('pathauto_separator', '-'),
'#description' => t('Character used to separate words in titles. This will replace any spaces and punctuation characters.'),
);
$form["general"]["pathauto_quotes"] = array(
'#type' => 'radios',
'#title' => t('Quotation marks'),
'#default_value' => variable_get('pathauto_quotes', 0),
'#options' => array(
t('Remove'),
t('Replace by separator'),
),
);
$form["general"]["pathauto_max_length"] = array(
'#type' => 'textfield',
'#title' => t('Maximum alias length'),
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('pathauto_max_length', 100),
'#description' => t('Maximum length of aliases to generate. 100 is recommended. See <a href="@pathauto-help">Pathauto help</a> for details.', array(
'@pathauto-help' => url('admin/help/pathauto'),
)),
);
$form["general"]["pathauto_max_component_length"] = array(
'#type' => 'textfield',
'#title' => t('Maximum component length'),
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('pathauto_max_component_length', 100),
'#description' => t('Maximum text length of any component in the alias (e.g., [title]). 100 is recommended. See <a href="@pathauto-help">Pathauto help</a> for details.', array(
'@pathauto-help' => url('admin/help/pathauto'),
)),
);
$form["general"]["pathauto_indexaliases"] = array(
'#type' => 'checkbox',
'#title' => t('Create index aliases'),
'#default_value' => variable_get('pathauto_indexaliases', FALSE),
'#description' => t('When a pattern generates a hierarchical alias (i.e., any alias containing a slash), generate aliases for each step of the hierarchy which can be used to list content within that hierarchy. For example, if a node alias "music/concert/beethoven" is created, also create an alias "music/concert" which will list all concert nodes, and an alias "music" which will list all music nodes.'),
);
$do_index_bulkupdate = variable_get('pathauto_indexaliases_bulkupdate', FALSE);
variable_set('pathauto_indexaliases_bulkupdate', FALSE);
$form["general"]["pathauto_indexaliases_bulkupdate"] = array(
'#type' => 'checkbox',
'#title' => t('Bulk generate index aliases'),
'#default_value' => FALSE,
'#description' => t('Generate index aliases based on all pre-existing aliases.'),
);
$form["general"]["pathauto_update_action"] = array(
'#type' => 'radios',
'#title' => t('Update action'),
'#default_value' => variable_get('pathauto_update_action', 2),
'#options' => array(
t('Do nothing, leaving the old alias intact'),
t('Create a new alias in addition to the old alias'),
t('Create a new alias, replacing the old one'),
),
'#description' => t('What should pathauto do when updating an existing content item which already has an alias?'),
);
$form["general"]["pathauto_ignore_words"] = array(
'#type' => 'textarea',
'#title' => t('Strings to Remove'),
'#default_value' => variable_get('pathauto_ignore_words', implode(",", $ignore_words)),
'#description' => t('Words to strip out of the URL alias, separated by commas'),
);
$all_settings = module_invoke_all('pathauto', 'settings');
$modulelist = '';
$indexcount = 0;
foreach ($all_settings as $settings) {
$items = '';
$module = $settings->module;
$modulelist[] = $module;
$patterndescr = $settings->patterndescr;
$patterndefault = $settings->patterndefault;
$groupheader = $settings->groupheader;
$supportsfeeds = isset($settings->supportsfeeds) ? $settings->supportsfeeds : NULL;
variable_set('pathauto_' . $module . '_supportsfeeds', $supportsfeeds);
$form[$module] = array(
'#type' => 'fieldset',
'#title' => $groupheader,
'#weight' => $group_weight,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$group_weight++;
$variable = 'pathauto_' . $module . '_pattern';
$form[$module][$variable] = array(
'#type' => 'textfield',
'#title' => $patterndescr,
'#default_value' => variable_get($variable, $patterndefault),
'#size' => 65,
'#maxlength' => variable_get('pathauto_max_length', 100),
);
if (isset($settings->patternitems)) {
foreach ($settings->patternitems as $itemname => $itemlabel) {
$variable = 'pathauto_' . $module . '_' . $itemname . '_pattern';
$form[$module][$variable] = array(
'#type' => 'textfield',
'#title' => $itemlabel,
'#default_value' => variable_get($variable, ''),
'#size' => 65,
'#maxlength' => variable_get('pathauto_max_length', 100),
);
}
}
$doc = "<dl>\n";
foreach ($settings->placeholders as $name => $description) {
$doc .= '<dt>' . $name . '</dt>';
$doc .= '<dd>' . $description . '</dd>';
}
$doc .= "</dl>\n";
$form[$module][$variable]["#description"] = $doc;
if ($settings->bulkname) {
$variable = 'pathauto_' . $module . '_bulkupdate';
if (variable_get($variable, FALSE)) {
variable_set($variable, FALSE);
$function = $module . '_pathauto_bulkupdate';
call_user_func($function);
}
$form[$module][$variable] = array(
'#type' => 'checkbox',
'#title' => $settings->bulkname,
'#default_value' => FALSE,
'#description' => $settings->bulkdescr,
);
}
if ($do_index_bulkupdate) {
$function = $module . '_pathauto_bulkupdate_indexes';
if (is_callable($function)) {
$indexcount += call_user_func($function);
}
}
if ($supportsfeeds) {
$variable = 'pathauto_' . $module . '_applytofeeds';
$form[$module][$variable] = array(
'#type' => 'checkbox',
'#title' => t('Create feed aliases'),
'#default_value' => variable_get($variable, FALSE),
'#description' => t('Also generate aliases for RSS feeds.'),
);
}
}
if ($do_index_bulkupdate) {
drupal_set_message(format_plural($indexcount, "Bulk update of index aliases completed, one alias generated.", "Bulk update of index aliases completed, @count aliases generated."));
}
variable_set('pathauto_modulelist', $modulelist);
return system_settings_form($form);
}
function _pathauto_alias_exists($alias, $src) {
return db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $alias, $src));
}
function pathauto_cleanstring($string) {
$ignore_words = array(
"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",
);
static $i18n_loaded = false;
static $translations = array();
if (!$i18n_loaded) {
$path = drupal_get_path('module', 'pathauto');
if (is_file($path . '/i18n-ascii.txt')) {
$translations = parse_ini_file($path . '/i18n-ascii.txt');
}
$i18n_loaded = true;
}
$output = strtr($output, $translations);
$separator = variable_get('pathauto_separator', '-');
$quotes = variable_get('pathauto_quotes', 0);
$output = str_replace("'", $quotes ? $separator : '', $string);
$output = strtr($output, $translations);
$ignore_re = "\\b" . preg_replace("/,/", "\\b|\\b", variable_get('pathauto_ignore_words', $ignore_words)) . "\\b";
$output = preg_replace("/{$ignore_re}/ie", "", $output);
$pattern = '/[^a-zA-Z0-9]+/ ';
$output = preg_replace($pattern, $separator, $output);
if ($separator) {
if (ctype_alnum($separator)) {
$seppattern = $separator;
}
else {
$seppattern = '\\' . $separator;
}
$output = preg_replace("/^{$seppattern}+|{$seppattern}+\$/", "", $output);
}
$maxlength = min(variable_get('pathauto_max_component_length', 100), 128);
$output = drupal_substr($output, 0, $maxlength);
return $output;
}
function pathauto_create_alias($module, $op, $placeholders, $src, $type = NULL) {
if ($op != 'bulkupdate' and variable_get('pathauto_verbose', FALSE)) {
$verbose = TRUE;
}
else {
$verbose = FALSE;
}
$pattern = '';
if ($type) {
$pattern = drupal_strtolower(variable_get('pathauto_' . $module . '_' . $type . '_pattern', ''));
}
if (!trim($pattern)) {
$pattern = drupal_strtolower(variable_get('pathauto_' . $module . '_pattern', ''));
}
if (!trim($pattern)) {
return '';
}
$pid = NULL;
if ($op == 'update' or $op == 'bulkupdate') {
$result = db_query("SELECT pid,dst FROM {url_alias} WHERE src='%s'", $src);
if ($data = db_fetch_object($result)) {
switch (variable_get('pathauto_update_action', 2)) {
case 0:
return '';
case 1:
$oldalias = $data->dst;
break;
case 2:
$pid = $data->pid;
$oldalias = $data->dst;
break;
default:
break;
}
}
}
$alias = str_replace(array_keys($placeholders), $placeholders, $pattern);
$alias = drupal_strtolower($alias);
$alias = preg_replace("/\\/+/", "/", $alias);
$alias = preg_replace("/^\\/|\\/+\$/", "", $alias);
$maxlength = min(variable_get('pathauto_max_length', 100), 128);
$alias = drupal_substr($alias, 0, $maxlength);
$separator = variable_get('pathauto_separator', '-');
if (_pathauto_alias_exists($alias, $src)) {
for ($i = 0; _pathauto_alias_exists($alias . $separator . $i, $src); $i++) {
}
$alias = drupal_substr($alias, 0, $maxlength - 1 - ($i / 10 + 1));
$alias = $alias . $separator . $i;
}
_pathauto_set_alias($src, $alias, $pid, $verbose, $oldalias);
if (variable_get('pathauto_' . $module . '_applytofeeds', FALSE)) {
$feedappend = variable_get('pathauto_' . $module . '_supportsfeeds', '');
_pathauto_set_alias("{$src}/{$feedappend}", "{$alias}/feed", NULL, $verbose);
}
if (variable_get('pathauto_indexaliases', FALSE)) {
pathauto_create_index_alias($alias, $module);
}
return $alias;
}
function _pathauto_path_is_callback($path) {
static $menu = NULL;
if (is_null($menu)) {
$menu = menu_get_menu();
}
return isset($menu["callbacks"][$path]);
}
function _pathauto_set_alias($src, $dst, $pid = NULL, $verbose = FALSE, $oldalias = NULL) {
if (_pathauto_path_is_callback($dst)) {
if ($verbose and user_access('create url aliases')) {
drupal_set_message(t('Ignoring alias ') . $dst . t(' due to existing path conflict'));
}
return;
}
if ($oldalias != $dst) {
path_set_alias($src, $dst, $pid, 1, 10);
if ($verbose and user_access('create url aliases')) {
if ($pid) {
drupal_set_message(t('Created new alias %dst for %src, replacing %oldalias', array(
'%dst' => $dst,
'%src' => $src,
'%oldalias' => $oldalias,
)));
}
else {
drupal_set_message(t('Created new alias %dst for %src', array(
'%dst' => $dst,
'%src' => $src,
)));
}
}
}
}
function pathauto_create_index_alias($alias, $module) {
$count = 0;
$components = explode('/', $alias);
array_pop($components);
$alias = '';
foreach ($components as $component) {
if ($alias) {
$alias .= '/' . $component;
}
else {
$alias .= $component;
}
$alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
if (!$alias_count) {
$src = "pathauto/{$module}/{$alias}";
_pathauto_set_alias($src, $alias);
$count++;
}
}
return $count;
}
function pathauto_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/pathauto',
'title' => t('Pathauto'),
'description' => t('Configure how pathauto generates clean URLs for your content.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'pathauto_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
}
$modulelist = variable_get('pathauto_modulelist', array());
if (is_array($modulelist)) {
foreach ($modulelist as $module) {
$indexfunc = $module . '_pathauto_page';
if (function_exists($indexfunc)) {
$items[] = array(
'path' => 'pathauto/' . $module,
'title' => t('Pathauto'),
'callback' => $indexfunc,
'access' => 1,
'type' => MENU_CALLBACK,
);
}
}
}
return $items;
}
function _pathauto_version() {
return array(
"text" => "2005-9-18",
"build" => 5,
);
}
function _pathauto_update() {
$installed_version = variable_get('pathauto_version', array(
'text' => 'Unknown',
'build' => 1,
));
$current_version = _pathauto_version();
if ($installed_version["build"] < $current_version["build"]) {
if ($installed_version["build"] <= 1) {
variable_del('pathauto_bulkupdate');
variable_del('pathauto_cat_bulkupdate');
$old_pattern = variable_get('pathauto_pattern', 0);
if ($old_pattern != 0) {
variable_set('pathauto_node_pattern', $old_pattern);
variable_del('pathauto_pattern');
}
$old_pattern = variable_get('pathauto_cat_pattern', 0);
if ($old_pattern != 0) {
variable_set('pathauto_taxonomy_pattern', $old_pattern);
variable_del('pathauto_cat_pattern');
}
$query = 'SELECT name,value FROM {variable} ' . "WHERE name LIKE '%_pathauto_pattern'";
$result = db_query($query);
$var = db_fetch_object($result);
while ($var) {
$type = drupal_substr($var->name, 0, drupal_strlen($var->name) - drupal_strlen('_pathauto_pattern'));
$old_pattern = variable_get($var->name, '');
$new_name = 'pathauto_node_' . $type . '_pattern';
variable_set($new_name, $old_pattern);
variable_del($var->name);
$var = db_fetch_object($result);
}
$query = 'SELECT name,value FROM {variable} ' . "WHERE name LIKE '%_pathauto_cat_pattern'";
$result = db_query($query);
$var = db_fetch_object($result);
while ($var) {
$type = drupal_substr($var->name, 0, drupal_strlen($var->name) - drupal_strlen('_pathauto_cat_pattern'));
$old_pattern = variable_get($var->name, '');
$new_name = 'pathauto_taxonomy_' . $type . '_pattern';
variable_set($new_name, $old_pattern);
variable_del($var->name);
$var = db_fetch_object($result);
}
}
if ($installed_version["build"] <= 2) {
$query = 'SELECT name,value FROM {variable} ' . "WHERE name LIKE 'pathauto_%_supportsfeeds'";
$result = db_query($query);
$var = db_fetch_object($result);
while ($var) {
$type = drupal_substr($var->name, 9, drupal_strlen($var->name) - 23);
$old_value = variable_get($var->name, FALSE);
if ($old_value) {
switch ($type) {
case 'blog':
case 'node':
variable_set($var->name, 'feed');
break;
case 'taxonomy':
variable_set($var->name, '0/feed');
break;
}
}
else {
variable_set($var->name, '');
}
$var = db_fetch_object($result);
}
$query = "SELECT * FROM {url_alias} WHERE src LIKE 'taxonomy/term/%/0'";
$result = db_query($query);
$aliasrow = db_fetch_object($result);
while ($aliasrow) {
$src = drupal_substr($aliasrow->src, 0, -2);
_pathauto_set_alias($src, $aliasrow->dst, $aliasrow->pid, FALSE);
$aliasrow = db_fetch_object($result);
}
}
if ($installed_version["build"] <= 3) {
$query = 'SELECT name,value FROM {variable} ' . "WHERE name LIKE 'pathauto_taxonomy_%_pattern'";
$result = db_query($query);
$var = db_fetch_object($result);
while ($var) {
$vocabname = drupal_substr($var->name, 18, drupal_strlen($var->name) - 26);
$query = "SELECT vid FROM {vocabulary} WHERE name='{$vocabname}'";
$vid = db_result(db_query($query));
variable_set("pathauto_taxonomy_{$vid_pattern}", $var->value);
variable_del($var->name);
$var = db_fetch_object($result);
}
}
if ($installed_version["build"] <= 4) {
$applytofeed = variable_get('pathauto_applytofeed', FALSE);
if ($applytofeed) {
$query = 'SELECT name,value FROM {variable} ' . "WHERE name LIKE 'pathauto_%_supportsfeeds'";
$result = db_query($query);
while ($var = db_fetch_object($result)) {
if (variable_get($var->name, FALSE)) {
$varname = str_replace('_supportsfeed', '_applytofeed', $var->name);
variable_set($varname, TRUE);
}
}
}
variable_del('pathauto_applytofeed');
}
variable_set('pathauto_version', $current_version);
drupal_set_message('Upgraded pathauto from ' . $installed_version["build"] . ' to ' . $current_version["build"]);
}
}