Code coverage for /20081101/modules/path/path.module

Line #Times calledCode
1
<?php
2
// $Id: path.module,v 1.149 2008/10/12 04:30:06 webchick Exp $
3
4
/**
5
 * @file
6
 * Enables users to rename URLs.
7
 */
8
9
/**
10
 * Implementation of hook_help().
11
 */
1268
function path_help($path, $arg) {
13
  switch ($path) {
1446
    case 'admin/help#path':
158
      $output = '<p>' . t('The path module allows you to specify aliases
for Drupal URLs. Such aliases improve readability of URLs for your users
and may help internet search engines to index your content more
effectively. More than one alias may be created for a given page.') .
'</p>';
168
      $output .= '<p>' . t('Some examples of URL aliases are:') . '</p>';
178
      $output .= '<ul><li>' . t('%alias for the path %path', array('%alias'
=> 'login', '%path' => 'user/login')) . '</li>';
188
      $output .= '<li>' . t('%alias for the path %path', array('%alias' =>
'store', '%path' => 'image/tid/16')) . '</li>';
198
      $output .= '<li>' . t('%alias for the path %path', array('%alias' =>
'store/products/whirlygigs', '%path' => 'taxonomy/term/7+19+20+21')) .
'</li>';
208
      $output .= '<li>' . t('%alias for the path %path', array('%alias' =>
'contact', '%path' => 'node/3')) . '</li></ul>';
218
      $output .= '<p>' . t('The path module enables appropriately
permissioned users to specify an optional alias in all node input and
editing forms, and provides an interface to view and edit all URL aliases.
The two permissions related to URL aliasing are <em>administer url
aliases</em> and <em>create url aliases</em>. ') . '</p>';
228
      $output .= '<p>' . t('This module also provides user-defined mass URL
aliasing capabilities, which is useful if you wish to uniformly use URLs
different from the default. For example, you may want to have your URLs
presented in a different language. Access to the Drupal source code on the
web server is required to set up mass URL aliasing. ') . '</p>';
238
      $output .= '<p>' . t('For more information, see the online handbook
entry for <a href="@path">Path module</a>.', array('@path' =>
'http://drupal.org/handbook/modules/path/')) . '</p>';
248
      return $output;
2546
    case 'admin/build/path':
263
      return '<p>' . t("Drupal provides complete control over URLs through
aliasing, which is often used to make URLs more readable or easy to
remember. For example, the alias 'about' may be mapped onto the post at the
system path 'node/1', creating a more meaningful URL. Each system path can
have multiple aliases.") . '</p>';
2743
    case 'admin/build/path/add':
283
      return '<p>' . t('Enter the path you wish to create the alias for,
followed by the name of the new alias.') . '</p>';
290
  }
3040
}
31
32
/**
33
 * Implementation of hook_menu().
34
 */
3568
function path_menu() {
362
  $items['admin/build/path'] = array(
372
    'title' => 'URL aliases',
382
    'description' => "Change your site's URL paths by aliasing them.",
392
    'page callback' => 'path_admin_overview',
402
    'access arguments' => array('administer url aliases'),
41
  );
422
  $items['admin/build/path/edit'] = array(
432
    'title' => 'Edit alias',
442
    'page callback' => 'path_admin_edit',
452
    'access arguments' => array('administer url aliases'),
462
    'type' => MENU_CALLBACK,
47
  );
482
  $items['admin/build/path/delete'] = array(
492
    'title' => 'Delete alias',
502
    'page callback' => 'drupal_get_form',
512
    'page arguments' => array('path_admin_delete_confirm'),
522
    'access arguments' => array('administer url aliases'),
532
    'type' => MENU_CALLBACK,
54
  );
552
  $items['admin/build/path/list'] = array(
562
    'title' => 'List',
572
    'type' => MENU_DEFAULT_LOCAL_TASK,
582
    'weight' => -10,
59
  );
602
  $items['admin/build/path/add'] = array(
612
    'title' => 'Add alias',
622
    'page callback' => 'path_admin_edit',
632
    'access arguments' => array('administer url aliases'),
642
    'type' => MENU_LOCAL_TASK,
65
  );
66
672
  return $items;
680
}
69
70
/**
71
 * Post-confirmation; delete an URL alias.
72
 */
7368
function path_admin_delete($pid = 0) {
741
  db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
751
  drupal_set_message(t('The alias has been deleted.'));
761
}
77
78
/**
79
 * Set an aliased path for a given Drupal path, preventing duplicates.
80
 */
8168
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language
= '') {
8211
  $path = urldecode($path);
8311
  $alias = urldecode($alias);
84
  // First we check if we deal with an existing alias and delete or modify
it based on pid.
8511
  if ($pid) {
86
    // An existing alias.
873
    if (!$path || !$alias) {
88
      // Delete the alias based on pid.
891
      db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
901
    }
91
    else {
92
      // Update the existing alias.
932
      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language =
'%s' WHERE pid = %d", $path, $alias, $language, $pid);
94
    }
953
  }
968
  elseif ($path && $alias) {
97
    // Check for existing aliases.
984
    if ($alias == drupal_get_path_alias($path, $language)) {
99
      // There is already such an alias, neutral or in this language.
100
      // Update the alias based on alias; setting the language if not yet
done.
1010
      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language =
'%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
1020
    }
103
    else {
104
      // A new alias. Add it to the database.
1054
      db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s',
'%s', '%s')", $path, $alias, $language);
106
    }
1074
  }
108
  else {
109
    // Delete the alias.
1104
    if ($alias) {
1110
      db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
1120
    }
113
    else {
1144
      db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
115
    }
116
  }
11711
  drupal_clear_path_cache();
11811
}
119
120
/**
121
 * Implementation of hook_nodeapi_validate().
122
 */
12368
function path_nodeapi_validate(&$node, $arg) {
12410
  if (user_access('create url aliases') || user_access('administer url
aliases')) {
12510
    if (isset($node->path)) {
12610
      $language = isset($node->language) ? $node->language : '';
12710
      $node->path = trim($node->path);
12810
      if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst
= '%s' AND src != '%s' AND language = '%s'", $node->path,
"node/$node->nid", $language))) {
1291
        form_set_error('path', t('The path is already in use.'));
1301
      }
13110
    }
13210
  }
13310
}
134
135
/**
136
 * Implementation of hook_nodeapi_load().
137
 */
13868
function path_nodeapi_load(&$node, $arg) {
13930
  $language = isset($node->language) ? $node->language : '';
14030
  $path = 'node/' . $node->nid;
14130
  $alias = drupal_get_path_alias($path, $language);
14230
  if ($path != $alias) {
14318
    $node->path = $alias;
14418
  }
14530
}
146
147
/**
148
 * Implementation of hook_nodeapi_insert().
149
 */
15068
function path_nodeapi_insert(&$node, $arg) {
1516
  if (user_access('create url aliases') || user_access('administer url
aliases')) {
1526
    $language = isset($node->language) ? $node->language : '';
153
    // Don't try to insert if path is NULL. We may have already set
154
    // the alias ahead of time.
1556
    if (isset($node->path)) {
1566
      path_set_alias('node/' . $node->nid, $node->path, NULL, $language);
1576
    }
1586
  }
1596
}
160
161
/**
162
 * Implementation of hook_nodeapi_update().
163
 */
16468
function path_nodeapi_update(&$node, $arg) {
1653
  if (user_access('create url aliases') || user_access('administer url
aliases')) {
1663
    $language = isset($node->language) ? $node->language : '';
1673
    path_set_alias('node/' . $node->nid, isset($node->path) ? $node->path :
NULL, isset($node->pid) ? $node->pid : NULL, $language);
1683
  }
1693
}
170
171
/**
172
 * Implementation of hook_nodeapi_delete().
173
 */
17468
function path_nodeapi_delete(&$node, $arg) {
1750
  if (user_access('create url aliases') || user_access('administer url
aliases')) {
1760
    $language = isset($node->language) ? $node->language : '';
1770
    $path = 'node/' . $node->nid;
1780
    if (drupal_get_path_alias($path) != $path) {
1790
      path_set_alias($path);
1800
    }
1810
  }
1820
}
183
184
/**
185
 * Implementation of hook_form_alter().
186
 */
18768
function path_form_alter(&$form, $form_state, $form_id) {
18833
  if (!empty($form['#node_edit_form'])) {
18910
    $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
19010
    $form['path'] = array(
19110
      '#type' => 'fieldset',
19210
      '#title' => t('URL path settings'),
19310
      '#collapsible' => TRUE,
19410
      '#collapsed' => empty($path),
19510
      '#access' => user_access('create url aliases'),
19610
      '#weight' => 30,
197
    );
19810
    $form['path']['path'] = array(
19910
      '#type' => 'textfield',
20010
      '#default_value' => $path,
20110
      '#maxlength' => 250,
20210
      '#collapsible' => TRUE,
20310
      '#collapsed' => TRUE,
20410
      '#description' => t('Optionally specify an alternative URL by which
this node can be accessed. For example, type "about" when writing an about
page. Use a relative path and don\'t add a trailing slash or the URL alias
won\'t work.'),
205
    );
20610
    if ($path) {
2072
      $form['path']['pid'] = array(
2082
        '#type' => 'value',
2092
        '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE
dst = '%s' AND language = '%s'", $path, $form['#node']->language))
2102
      );
2112
    }
21210
  }
21333
}
214
215
/**
216
 * Implementation of hook_perm().
217
 */
21868
function path_perm() {
219
  return array(
220
    'administer url aliases' => array(
2212
      'title' => t('Administer URL aliases'),
2222
      'description' => t('Manage URL aliases across the entire website.'),
2232
    ),
224
    'create url aliases' => array(
2252
      'title' => t('Create URL aliases'),
2262
      'description' => t('Manage URL aliases on content.'),
2272
    ),
2282
  );
2290
}
230
231
/**
232
 * Fetch a specific URL alias from the database.
233
 */
23468
function path_load($pid) {
2354
  return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid =
%d', $pid));
2360
}
23768