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

Line #Times calledCode
1
<?php
2
// $Id: path.module,v 1.144 2008/06/21 18:22:38 dries Exp $
3
4
/**
5
 * @file
6
 * Enables users to rename URLs.
7
 */
8
9
/**
10
 * Implementation of hook_help().
11
 */
1251
function path_help($path, $arg) {
13
  switch ($path) {
1438
    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;
2538
    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>';
2735
    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
  }
3032
}
31
32
/**
33
 * Implementation of hook_menu().
34
 */
3551
function path_menu() {
361
  $items['admin/build/path'] = array(
371
    'title' => 'URL aliases',
381
    'description' => "Change your site's URL paths by aliasing them.",
391
    'page callback' => 'path_admin_overview',
401
    'access arguments' => array('administer url aliases'),
41
  );
421
  $items['admin/build/path/edit'] = array(
431
    'title' => 'Edit alias',
441
    'page callback' => 'path_admin_edit',
451
    'access arguments' => array('administer url aliases'),
461
    'type' => MENU_CALLBACK,
47
  );
481
  $items['admin/build/path/delete'] = array(
491
    'title' => 'Delete alias',
501
    'page callback' => 'drupal_get_form',
511
    'page arguments' => array('path_admin_delete_confirm'),
521
    'access arguments' => array('administer url aliases'),
531
    'type' => MENU_CALLBACK,
54
  );
551
  $items['admin/build/path/list'] = array(
561
    'title' => 'List',
571
    'type' => MENU_DEFAULT_LOCAL_TASK,
581
    'weight' => -10,
59
  );
601
  $items['admin/build/path/add'] = array(
611
    'title' => 'Add alias',
621
    'page callback' => 'path_admin_edit',
631
    'access arguments' => array('administer url aliases'),
641
    'type' => MENU_LOCAL_TASK,
65
  );
66
671
  return $items;
680
}
69
70
/**
71
 * Post-confirmation; delete an URL alias.
72
 */
7351
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
 */
8151
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language
= '') {
829
  $path = urldecode($path);
839
  $alias = urldecode($alias);
84
  // First we check if we deal with an existing alias and delete or modify
it based on pid.
859
  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
  }
966
  else if ($path && $alias) {
97
    // Check for existing aliases.
982
    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.
1052
      db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s',
'%s', '%s')", $path, $alias, $language);
106
    }
1072
  }
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
  }
1179
  drupal_clear_path_cache();
1189
}
119
120
121
/**
122
 * Implementation of hook_nodeapi().
123
 *
124
 * Allows URL aliases for nodes to be specified at node edit time rather
125
 * than through the administrative interface.
126
 */
12751
function path_nodeapi(&$node, $op, $arg) {
128
  // Permissions are required for everything except node loading.
12931
  if (user_access('create url aliases') || user_access('administer url
aliases') || ($op == 'load')) {
13031
    $language = isset($node->language) ? $node->language : '';
131
    switch ($op) {
13231
      case 'validate':
1338
        if (isset($node->path)) {
1348
          $node->path = trim($node->path);
1358
          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))) {
1361
            form_set_error('path', t('The path is already in use.'));
1371
          }
1388
        }
1398
        break;
140
14131
      case 'load':
14223
        $path = 'node/' . $node->nid;
14323
        $alias = drupal_get_path_alias($path, $language);
14423
        if ($path != $alias) {
14511
          $node->path = $alias;
14611
        }
14723
        break;
148
14926
      case 'insert':
150
        // Don't try to insert if path is NULL. We may have already set
151
        // the alias ahead of time.
1524
        if (isset($node->path)) {
1534
          path_set_alias('node/' . $node->nid, $node->path, NULL,
$language);
1544
        }
1554
        break;
156
15726
      case 'update':
1583
        path_set_alias('node/' . $node->nid, isset($node->path) ?
$node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
1593
        break;
160
16126
      case 'delete':
1620
        $path = 'node/' . $node->nid;
1630
        if (drupal_get_path_alias($path) != $path) {
1640
          path_set_alias($path);
1650
        }
1660
        break;
1670
    }
16831
  }
16931
}
170
171
/**
172
 * Implementation of hook_form_alter().
173
 */
17451
function path_form_alter(&$form, $form_state, $form_id) {
17525
  if (isset($form['type']) && isset($form['#node']) &&
$form['type']['#value'] . '_node_form' == $form_id) {
1768
    $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
1778
    $form['path'] = array(
1788
      '#type' => 'fieldset',
1798
      '#title' => t('URL path settings'),
1808
      '#collapsible' => TRUE,
1818
      '#collapsed' => empty($path),
1828
      '#access' => user_access('create url aliases'),
1838
      '#weight' => 30,
184
    );
1858
    $form['path']['path'] = array(
1868
      '#type' => 'textfield',
1878
      '#default_value' => $path,
1888
      '#maxlength' => 250,
1898
      '#collapsible' => TRUE,
1908
      '#collapsed' => TRUE,
1918
      '#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.'),
192
    );
1938
    if ($path) {
1942
      $form['path']['pid'] = array(
1952
        '#type' => 'value',
1962
        '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE
dst = '%s' AND language = '%s'", $path, $form['#node']->language))
1972
      );
1982
    }
1998
  }
20025
}
201
202
/**
203
 * Implementation of hook_perm().
204
 */
20551
function path_perm() {
206
  return array(
2071
    'create url aliases' => t('Manage URL aliases on content.'),
2081
    'administer url aliases' => t('Manage URL aliases across the entire
website.'),
2091
  );
2100
}
211
212
/**
213
 * Fetch a specific URL alias from the database.
214
 */
21551
function path_load($pid) {
2164
  return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid =
%d', $pid));
2170
}
21851