2006-01-24 08:18:26 +00:00
< ? php
2006-01-23 07:54:08 +00:00
/**
* @ file
* Functions to handle paths in Drupal , including path aliasing .
*
* These functions are not loaded for cached pages , but modules that need
2009-10-24 05:13:44 +00:00
* to use them in hook_boot () or hook exit () can make them available , by
* executing " drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); " .
2006-01-23 07:54:08 +00:00
*/
/**
2012-04-29 15:16:27 +00:00
* Initializes the current path to the proper normal path .
2006-01-23 07:54:08 +00:00
*/
2009-07-14 10:22:17 +00:00
function drupal_path_initialize () {
2012-04-29 15:16:27 +00:00
// At this point, the current path is either the request path (due to
// drupal_environment_initialize()) or some modified version of it due to
// other bootstrap code (e.g., language negotiation), but it has not yet been
// normalized by drupal_get_normal_path().
$path = _current_path ();
// If on the front page, resolve to the front page path, including for calls
// to current_path() while drupal_get_normal_path() is in progress.
if ( empty ( $path )) {
$path = variable_get ( 'site_frontpage' , 'user' );
_current_path ( $path );
2006-01-23 07:54:08 +00:00
}
2012-04-29 15:16:27 +00:00
// Normalize the path.
_current_path ( drupal_get_normal_path ( $path ));
2006-01-23 07:54:08 +00:00
}
/**
* Given an alias , return its Drupal system URL if one exists . Given a Drupal
2006-11-26 01:55:37 +00:00
* system URL return one of its aliases if such a one exists . Otherwise ,
* return FALSE .
2006-01-23 07:54:08 +00:00
*
* @ param $action
* One of the following values :
* - wipe : delete the alias cache .
* - alias : return an alias for a given Drupal system path ( if one exists ) .
* - source : return the Drupal system URL for a path alias ( if one exists ) .
* @ param $path
* The path to investigate for corresponding aliases or system URLs .
2012-01-23 02:14:12 +00:00
* @ param $langcode
2007-03-26 01:32:22 +00:00
* Optional language code to search the path with . Defaults to the page language .
* If there ' s no path defined for that language it will search paths without
* language .
2006-01-23 07:54:08 +00:00
*
* @ return
* Either a Drupal system path , an aliased path , or FALSE if no path was
* found .
*/
2012-01-23 02:14:12 +00:00
function drupal_lookup_path ( $action , $path = '' , $langcode = NULL ) {
2009-11-20 06:12:45 +00:00
// Use the advanced drupal_static() pattern, since this is called very often.
2010-01-07 04:54:18 +00:00
static $drupal_static_fast ;
if ( ! isset ( $drupal_static_fast )) {
$drupal_static_fast [ 'cache' ] = & drupal_static ( __FUNCTION__ );
}
$cache = & $drupal_static_fast [ 'cache' ];
2009-11-20 06:12:45 +00:00
if ( ! isset ( $cache )) {
$cache = array (
'map' => array (),
'no_source' => array (),
'whitelist' => NULL ,
'system_paths' => array (),
'no_aliases' => array (),
'first_call' => TRUE ,
);
}
2007-03-26 01:32:22 +00:00
2009-06-01 11:23:27 +00:00
// Retrieve the path alias whitelist.
2009-08-02 06:48:24 +00:00
if ( ! isset ( $cache [ 'whitelist' ])) {
$cache [ 'whitelist' ] = variable_get ( 'path_alias_whitelist' , NULL );
if ( ! isset ( $cache [ 'whitelist' ])) {
$cache [ 'whitelist' ] = drupal_path_alias_whitelist_rebuild ();
2009-06-01 11:23:27 +00:00
}
2008-06-24 22:12:15 +00:00
}
2010-07-30 01:52:54 +00:00
// If no language is explicitly specified we default to the current URL
// language. If we used a language different from the one conveyed by the
// requested URL, we might end up being unable to check if there is a path
// alias matching the URL path.
2012-04-18 18:30:50 +00:00
$langcode = $langcode ? $langcode : drupal_container () -> get ( LANGUAGE_TYPE_URL ) -> langcode ;
2009-06-01 11:23:27 +00:00
2006-01-23 07:54:08 +00:00
if ( $action == 'wipe' ) {
2009-08-02 06:48:24 +00:00
$cache = array ();
$cache [ 'whitelist' ] = drupal_path_alias_whitelist_rebuild ();
2006-01-23 07:54:08 +00:00
}
2009-08-02 06:48:24 +00:00
elseif ( $cache [ 'whitelist' ] && $path != '' ) {
2006-01-23 07:54:08 +00:00
if ( $action == 'alias' ) {
2009-05-16 19:07:02 +00:00
// During the first call to drupal_lookup_path() per language, load the
// expected system paths for the page from cache.
2009-08-02 06:48:24 +00:00
if ( ! empty ( $cache [ 'first_call' ])) {
$cache [ 'first_call' ] = FALSE ;
2009-06-01 11:23:27 +00:00
2012-01-23 02:14:12 +00:00
$cache [ 'map' ][ $langcode ] = array ();
2009-05-16 19:07:02 +00:00
// Load system paths from cache.
$cid = current_path ();
2011-09-11 16:14:18 +00:00
if ( $cached = cache ( 'path' ) -> get ( $cid )) {
2009-08-02 06:48:24 +00:00
$cache [ 'system_paths' ] = $cached -> data ;
2009-05-16 19:07:02 +00:00
// Now fetch the aliases corresponding to these system paths.
2010-11-30 01:05:24 +00:00
$args = array (
2009-08-02 06:48:24 +00:00
':system' => $cache [ 'system_paths' ],
2012-01-23 02:14:12 +00:00
':langcode' => $langcode ,
2012-03-08 15:10:59 +00:00
':langcode_undetermined' => LANGUAGE_NOT_SPECIFIED ,
2010-11-30 01:05:24 +00:00
);
// Always get the language-specific alias before the language-neutral
// one. For example 'de' is less than 'und' so the order needs to be
// ASC, while 'xx-lolspeak' is more than 'und' so the order needs to
// be DESC. We also order by pid ASC so that fetchAllKeyed() returns
// the most recently created alias for each source. Subsequent queries
// using fetchField() must use pid DESC to have the same effect.
// For performance reasons, the query builder is not used here.
2012-03-08 15:10:59 +00:00
if ( $langcode == LANGUAGE_NOT_SPECIFIED ) {
2010-11-30 01:05:24 +00:00
// Prevent PDO from complaining about a token the query doesn't use.
2012-01-23 02:14:12 +00:00
unset ( $args [ ':langcode' ]);
2012-03-08 15:10:59 +00:00
$result = db_query ( 'SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND langcode = :langcode_undetermined ORDER BY pid ASC' , $args );
2010-11-30 01:05:24 +00:00
}
2012-03-08 15:10:59 +00:00
elseif ( $langcode < LANGUAGE_NOT_SPECIFIED ) {
$result = db_query ( 'SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode ASC, pid ASC' , $args );
2010-11-30 01:05:24 +00:00
}
else {
2012-03-08 15:10:59 +00:00
$result = db_query ( 'SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode DESC, pid ASC' , $args );
2010-11-30 01:05:24 +00:00
}
2012-01-23 02:14:12 +00:00
$cache [ 'map' ][ $langcode ] = $result -> fetchAllKeyed ();
2009-05-16 19:07:02 +00:00
// Keep a record of paths with no alias to avoid querying twice.
2012-01-23 02:14:12 +00:00
$cache [ 'no_aliases' ][ $langcode ] = array_flip ( array_diff_key ( $cache [ 'system_paths' ], array_keys ( $cache [ 'map' ][ $langcode ])));
2009-05-16 19:07:02 +00:00
}
}
// If the alias has already been loaded, return it.
2012-01-23 02:14:12 +00:00
if ( isset ( $cache [ 'map' ][ $langcode ][ $path ])) {
return $cache [ 'map' ][ $langcode ][ $path ];
2006-01-23 07:54:08 +00:00
}
2009-06-01 11:23:27 +00:00
// Check the path whitelist, if the top_level part before the first /
// is not in the list, then there is no need to do anything further,
// it is not in the database.
2009-08-02 06:48:24 +00:00
elseif ( ! isset ( $cache [ 'whitelist' ][ strtok ( $path , '/' )])) {
2009-06-01 11:23:27 +00:00
return FALSE ;
}
2009-05-16 19:07:02 +00:00
// For system paths which were not cached, query aliases individually.
2012-01-23 02:14:12 +00:00
elseif ( ! isset ( $cache [ 'no_aliases' ][ $langcode ][ $path ])) {
2010-11-30 01:05:24 +00:00
$args = array (
2009-10-15 17:53:34 +00:00
':source' => $path ,
2012-01-23 02:14:12 +00:00
':langcode' => $langcode ,
2012-03-08 15:10:59 +00:00
':langcode_undetermined' => LANGUAGE_NOT_SPECIFIED ,
2010-11-30 01:05:24 +00:00
);
// See the queries above.
2012-03-08 15:10:59 +00:00
if ( $langcode == LANGUAGE_NOT_SPECIFIED ) {
2012-01-23 02:14:12 +00:00
unset ( $args [ ':langcode' ]);
2012-03-08 15:10:59 +00:00
$alias = db_query ( " SELECT alias FROM { url_alias} WHERE source = :source AND langcode = :langcode_undetermined ORDER BY pid DESC " , $args ) -> fetchField ();
2010-11-30 01:05:24 +00:00
}
2012-03-08 15:10:59 +00:00
elseif ( $langcode > LANGUAGE_NOT_SPECIFIED ) {
$alias = db_query ( " SELECT alias FROM { url_alias} WHERE source = :source AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode DESC, pid DESC " , $args ) -> fetchField ();
2010-11-30 01:05:24 +00:00
}
else {
2012-03-08 15:10:59 +00:00
$alias = db_query ( " SELECT alias FROM { url_alias} WHERE source = :source AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode ASC, pid DESC " , $args ) -> fetchField ();
2010-11-30 01:05:24 +00:00
}
2012-01-23 02:14:12 +00:00
$cache [ 'map' ][ $langcode ][ $path ] = $alias ;
2009-05-16 19:07:02 +00:00
return $alias ;
}
2006-01-23 07:54:08 +00:00
}
2009-10-15 17:53:34 +00:00
// Check $no_source for this $path in case we've already determined that there
2006-11-26 01:55:37 +00:00
// isn't a path that has this alias
2012-01-23 02:14:12 +00:00
elseif ( $action == 'source' && ! isset ( $cache [ 'no_source' ][ $langcode ][ $path ])) {
2006-11-26 01:55:37 +00:00
// Look for the value $path within the cached $map
2010-07-31 04:10:30 +00:00
$source = FALSE ;
2012-01-23 02:14:12 +00:00
if ( ! isset ( $cache [ 'map' ][ $langcode ]) || ! ( $source = array_search ( $path , $cache [ 'map' ][ $langcode ]))) {
2010-11-30 01:05:24 +00:00
$args = array (
':alias' => $path ,
2012-01-23 02:14:12 +00:00
':langcode' => $langcode ,
2012-03-08 15:10:59 +00:00
':langcode_undetermined' => LANGUAGE_NOT_SPECIFIED ,
2010-11-30 01:05:24 +00:00
);
// See the queries above.
2012-03-08 15:10:59 +00:00
if ( $langcode == LANGUAGE_NOT_SPECIFIED ) {
2012-01-23 02:14:12 +00:00
unset ( $args [ ':langcode' ]);
2012-03-08 15:10:59 +00:00
$result = db_query ( " SELECT source FROM { url_alias} WHERE alias = :alias AND langcode = :langcode_undetermined ORDER BY pid DESC " , $args );
2010-11-30 01:05:24 +00:00
}
2012-03-08 15:10:59 +00:00
elseif ( $langcode > LANGUAGE_NOT_SPECIFIED ) {
$result = db_query ( " SELECT source FROM { url_alias} WHERE alias = :alias AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode DESC, pid DESC " , $args );
2010-11-30 01:05:24 +00:00
}
else {
2012-03-08 15:10:59 +00:00
$result = db_query ( " SELECT source FROM { url_alias} WHERE alias = :alias AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode ASC, pid DESC " , $args );
2010-11-30 01:05:24 +00:00
}
if ( $source = $result -> fetchField ()) {
2012-01-23 02:14:12 +00:00
$cache [ 'map' ][ $langcode ][ $source ] = $path ;
2006-11-26 01:55:37 +00:00
}
else {
// We can't record anything into $map because we do not have a valid
// index and there is no need because we have not learned anything
2009-10-15 17:53:34 +00:00
// about any Drupal path. Thus cache to $no_source.
2012-01-23 02:14:12 +00:00
$cache [ 'no_source' ][ $langcode ][ $path ] = TRUE ;
2006-01-23 07:54:08 +00:00
}
}
2010-07-31 04:10:30 +00:00
return $source ;
2006-01-23 07:54:08 +00:00
}
}
return FALSE ;
}
2009-05-16 19:07:02 +00:00
/**
* Cache system paths for a page .
*
* Cache an array of the system paths available on each page . We assume
2010-01-25 10:38:35 +00:00
* that aliases will be needed for the majority of these paths during
2009-05-16 19:07:02 +00:00
* subsequent requests , and load them in a single query during
* drupal_lookup_path () .
*/
function drupal_cache_system_paths () {
// Check if the system paths for this page were loaded from cache in this
// request to avoid writing to cache on every request.
2009-08-02 06:48:24 +00:00
$cache = & drupal_static ( 'drupal_lookup_path' , array ());
2009-12-07 06:07:19 +00:00
if ( empty ( $cache [ 'system_paths' ]) && ! empty ( $cache [ 'map' ])) {
2009-05-16 19:07:02 +00:00
// Generate a cache ID (cid) specifically for this page.
$cid = current_path ();
2009-08-02 06:48:24 +00:00
// The static $map array used by drupal_lookup_path() includes all
// system paths for the page request.
if ( $paths = current ( $cache [ 'map' ])) {
2009-05-16 19:07:02 +00:00
$data = array_keys ( $paths );
$expire = REQUEST_TIME + ( 60 * 60 * 24 );
2011-09-11 16:14:18 +00:00
cache ( 'path' ) -> set ( $cid , $data , $expire );
2009-05-16 19:07:02 +00:00
}
}
}
2006-01-23 07:54:08 +00:00
/**
* Given an internal Drupal path , return the alias set by the administrator .
*
2009-05-07 15:49:57 +00:00
* If no path is provided , the function will return the alias of the current
* page .
*
2006-01-23 07:54:08 +00:00
* @ param $path
* An internal Drupal path .
2012-01-23 02:14:12 +00:00
* @ param $langcode
2007-03-26 01:32:22 +00:00
* An optional language code to look up the path in .
2006-01-23 07:54:08 +00:00
*
* @ return
* An aliased path if one was found , or the original path if no alias was
* found .
*/
2012-01-23 02:14:12 +00:00
function drupal_get_path_alias ( $path = NULL , $langcode = NULL ) {
2009-05-07 15:49:57 +00:00
// If no path is specified, use the current page's path.
if ( $path == NULL ) {
2012-04-29 15:16:27 +00:00
$path = current_path ();
2009-05-07 15:49:57 +00:00
}
2006-01-23 07:54:08 +00:00
$result = $path ;
2012-01-23 02:14:12 +00:00
if ( $alias = drupal_lookup_path ( 'alias' , $path , $langcode )) {
2006-01-23 07:54:08 +00:00
$result = $alias ;
}
return $result ;
}
/**
* Given a path alias , return the internal path it represents .
*
* @ param $path
* A Drupal path alias .
2012-01-23 02:14:12 +00:00
* @ param $langcode
2007-03-26 01:32:22 +00:00
* An optional language code to look up the path in .
2006-01-23 07:54:08 +00:00
*
* @ return
* The internal path represented by the alias , or the original alias if no
* internal path was found .
*/
2012-01-23 02:14:12 +00:00
function drupal_get_normal_path ( $path , $langcode = NULL ) {
2009-10-24 05:13:44 +00:00
$original_path = $path ;
// Lookup the path alias first.
2012-01-23 02:14:12 +00:00
if ( $source = drupal_lookup_path ( 'source' , $path , $langcode )) {
2009-10-24 05:13:44 +00:00
$path = $source ;
2006-01-23 07:54:08 +00:00
}
2009-10-24 05:13:44 +00:00
// Allow other modules to alter the inbound URL. We cannot use drupal_alter()
// here because we need to run hook_url_inbound_alter() in the reverse order
// of hook_url_outbound_alter().
foreach ( array_reverse ( module_implements ( 'url_inbound_alter' )) as $module ) {
$function = $module . '_url_inbound_alter' ;
2012-01-23 02:14:12 +00:00
$function ( $path , $original_path , $langcode );
2006-01-23 07:54:08 +00:00
}
2009-10-24 05:13:44 +00:00
return $path ;
2006-01-23 07:54:08 +00:00
}
2006-04-24 19:25:37 +00:00
/**
* Check if the current page is the front page .
*
* @ return
* Boolean value : TRUE if the current page is the front page ; FALSE if otherwise .
*/
function drupal_is_front_page () {
2009-11-20 06:12:45 +00:00
// Use the advanced drupal_static() pattern, since this is called very often.
2010-01-07 04:54:18 +00:00
static $drupal_static_fast ;
if ( ! isset ( $drupal_static_fast )) {
$drupal_static_fast [ 'is_front_page' ] = & drupal_static ( __FUNCTION__ );
}
$is_front_page = & $drupal_static_fast [ 'is_front_page' ];
2008-12-09 07:16:10 +00:00
if ( ! isset ( $is_front_page )) {
2012-04-29 15:16:27 +00:00
$is_front_page = ( current_path () == variable_get ( 'site_frontpage' , 'user' ));
2008-12-09 07:16:10 +00:00
}
return $is_front_page ;
2006-04-24 19:25:37 +00:00
}
2007-10-16 14:10:33 +00:00
/**
* Check if a path matches any pattern in a set of patterns .
*
* @ param $path
* The path to match .
* @ param $patterns
* String containing a set of patterns separated by \n , \r or \r\n .
*
* @ return
* Boolean value : TRUE if the path matches a pattern , FALSE otherwise .
*/
function drupal_match_path ( $path , $patterns ) {
2009-04-03 17:41:32 +00:00
$regexps = & drupal_static ( __FUNCTION__ );
2007-10-17 12:34:16 +00:00
2007-10-16 14:10:33 +00:00
if ( ! isset ( $regexps [ $patterns ])) {
2010-04-24 15:10:34 +00:00
// Convert path settings to a regular expression.
// Therefore replace newlines with a logical or, /* with asterisks and the <front> with the frontpage.
$to_replace = array (
'/(\r\n?|\n)/' , // newlines
'/\\\\\*/' , // asterisks
'/(^|\|)\\\\<front\\\\>($|\|)/' // <front>
);
$replacements = array (
'|' ,
'.*' ,
2012-01-08 07:14:15 +00:00
'\1' . preg_quote ( variable_get ( 'site_frontpage' , 'user' ), '/' ) . '\2'
2010-04-24 15:10:34 +00:00
);
$patterns_quoted = preg_quote ( $patterns , '/' );
$regexps [ $patterns ] = '/^(' . preg_replace ( $to_replace , $replacements , $patterns_quoted ) . ')$/' ;
2007-10-16 14:10:33 +00:00
}
2008-06-18 03:36:24 +00:00
return ( bool ) preg_match ( $regexps [ $patterns ], $path );
2007-10-16 14:10:33 +00:00
}
2009-05-06 15:51:36 +00:00
/**
* Return the current URL path of the page being viewed .
*
* Examples :
* - http :// example . com / node / 306 returns " node/306 " .
* - http :// example . com / drupalfolder / node / 306 returns " node/306 " while
* base_path () returns " /drupalfolder/ " .
* - http :// example . com / path / alias ( which is a path alias for node / 306 ) returns
* " node/306 " as opposed to the path alias .
*
2012-04-29 15:16:27 +00:00
* This function is not available in hook_boot () so use request_path () instead .
2009-05-06 15:51:36 +00:00
* However , be careful when doing that because in the case of Example #3
2012-04-29 15:16:27 +00:00
* request_path () will contain " path/alias " . If " node/306 " is needed , calling
2009-10-24 05:13:44 +00:00
* drupal_bootstrap ( DRUPAL_BOOTSTRAP_FULL ) makes this function available .
2009-05-06 15:51:36 +00:00
*
* @ return
* The current Drupal URL path .
2010-01-29 22:40:41 +00:00
*
* @ see request_path ()
2009-05-06 15:51:36 +00:00
*/
function current_path () {
2012-04-29 15:16:27 +00:00
return _current_path ();
2009-05-06 15:51:36 +00:00
}
2009-06-01 11:23:27 +00:00
/**
* Rebuild the path alias white list .
*
2010-03-07 06:58:14 +00:00
* @ param $source
* An optional system path for which an alias is being inserted .
*
2009-06-01 11:23:27 +00:00
* @ return
* An array containing a white list of path aliases .
*/
2010-03-07 06:58:14 +00:00
function drupal_path_alias_whitelist_rebuild ( $source = NULL ) {
// When paths are inserted, only rebuild the whitelist if the system path
// has a top level component which is not already in the whitelist.
if ( ! empty ( $source )) {
$whitelist = variable_get ( 'path_alias_whitelist' , NULL );
if ( isset ( $whitelist [ strtok ( $source , '/' )])) {
return $whitelist ;
}
}
2009-06-01 11:23:27 +00:00
// For each alias in the database, get the top level component of the system
2010-03-07 06:58:14 +00:00
// path it corresponds to. This is the portion of the path before the first
// '/', if present, otherwise the whole path itself.
2009-06-01 11:23:27 +00:00
$whitelist = array ();
2010-07-19 22:12:14 +00:00
$result = db_query ( " SELECT DISTINCT SUBSTRING_INDEX(source, '/', 1) AS path FROM { url_alias} " );
2009-06-01 11:23:27 +00:00
foreach ( $result as $row ) {
$whitelist [ $row -> path ] = TRUE ;
}
variable_set ( 'path_alias_whitelist' , $whitelist );
return $whitelist ;
}
2009-10-22 01:27:18 +00:00
/**
* Fetch a specific URL alias from the database .
*
* @ param $conditions
* A string representing the source , a number representing the pid , or an
* array of query conditions .
*
* @ return
* FALSE if no alias was found or an associative array containing the
* following keys :
* - source : The internal system path .
* - alias : The URL alias .
* - pid : Unique path alias identifier .
2012-01-23 02:14:12 +00:00
* - langcode : The language code of the alias .
2009-10-22 01:27:18 +00:00
*/
function path_load ( $conditions ) {
if ( is_numeric ( $conditions )) {
$conditions = array ( 'pid' => $conditions );
}
elseif ( is_string ( $conditions )) {
$conditions = array ( 'source' => $conditions );
}
elseif ( ! is_array ( $conditions )) {
return FALSE ;
}
$select = db_select ( 'url_alias' );
foreach ( $conditions as $field => $value ) {
$select -> condition ( $field , $value );
}
return $select
-> fields ( 'url_alias' )
-> execute ()
-> fetchAssoc ();
}
/**
* Save a path alias to the database .
*
* @ param $path
* An associative array containing the following keys :
* - source : The internal system path .
* - alias : The URL alias .
* - pid : ( optional ) Unique path alias identifier .
2012-01-23 02:14:12 +00:00
* - langcode : ( optional ) The language code of the alias .
2009-10-22 01:27:18 +00:00
*/
function path_save ( & $path ) {
2012-03-08 15:10:59 +00:00
$path += array ( 'langcode' => LANGUAGE_NOT_SPECIFIED );
2009-10-22 01:27:18 +00:00
2012-02-08 11:03:32 +00:00
// Load the stored alias, if any.
if ( ! empty ( $path [ 'pid' ]) && ! isset ( $path [ 'original' ])) {
$path [ 'original' ] = path_load ( $path [ 'pid' ]);
}
2009-10-22 01:27:18 +00:00
2012-02-08 11:03:32 +00:00
if ( empty ( $path [ 'pid' ])) {
drupal_write_record ( 'url_alias' , $path );
module_invoke_all ( 'path_insert' , $path );
}
else {
drupal_write_record ( 'url_alias' , $path , array ( 'pid' ));
module_invoke_all ( 'path_update' , $path );
2009-10-22 01:27:18 +00:00
}
2012-02-08 11:03:32 +00:00
// Clear internal properties.
unset ( $path [ 'original' ]);
// Clear the static alias cache.
drupal_clear_path_cache ( $path [ 'source' ]);
2009-10-22 01:27:18 +00:00
}
/**
* Delete a URL alias .
*
* @ param $criteria
* A number representing the pid or an array of criteria .
*/
function path_delete ( $criteria ) {
if ( ! is_array ( $criteria )) {
$criteria = array ( 'pid' => $criteria );
}
$path = path_load ( $criteria );
$query = db_delete ( 'url_alias' );
foreach ( $criteria as $field => $value ) {
$query -> condition ( $field , $value );
}
$query -> execute ();
module_invoke_all ( 'path_delete' , $path );
2010-03-07 06:58:14 +00:00
drupal_clear_path_cache ( $path [ 'source' ]);
2009-10-22 01:27:18 +00:00
}
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
/**
* Determine whether a path is in the administrative section of the site .
*
* By default , paths are considered to be non - administrative . If a path does not
* match any of the patterns in path_get_admin_paths (), or if it matches both
* administrative and non - administrative patterns , it is considered
* non - administrative .
*
* @ param $path
* A Drupal path .
2010-07-16 02:51:44 +00:00
*
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
* @ return
* TRUE if the path is administrative , FALSE otherwise .
*
* @ see path_get_admin_paths ()
* @ see hook_admin_paths ()
* @ see hook_admin_paths_alter ()
*/
function path_is_admin ( $path ) {
$path_map = & drupal_static ( __FUNCTION__ );
if ( ! isset ( $path_map [ 'admin' ][ $path ])) {
$patterns = path_get_admin_paths ();
$path_map [ 'admin' ][ $path ] = drupal_match_path ( $path , $patterns [ 'admin' ]);
$path_map [ 'non_admin' ][ $path ] = drupal_match_path ( $path , $patterns [ 'non_admin' ]);
}
return $path_map [ 'admin' ][ $path ] && ! $path_map [ 'non_admin' ][ $path ];
}
/**
* Get a list of administrative and non - administrative paths .
*
* @ return array
* An associative array containing the following keys :
* 'admin' : An array of administrative paths and regular expressions
* in a format suitable for drupal_match_path () .
* 'non_admin' : An array of non - administrative paths and regular expressions .
*
* @ see hook_admin_paths ()
* @ see hook_admin_paths_alter ()
*/
function path_get_admin_paths () {
$patterns = & drupal_static ( __FUNCTION__ );
if ( ! isset ( $patterns )) {
$paths = module_invoke_all ( 'admin_paths' );
drupal_alter ( 'admin_paths' , $paths );
// Combine all admin paths into one array, and likewise for non-admin paths,
// for easier handling.
$patterns = array ();
$patterns [ 'admin' ] = array ();
$patterns [ 'non_admin' ] = array ();
foreach ( $paths as $path => $enabled ) {
if ( $enabled ) {
$patterns [ 'admin' ][] = $path ;
}
else {
$patterns [ 'non_admin' ][] = $path ;
}
}
$patterns [ 'admin' ] = implode ( " \n " , $patterns [ 'admin' ]);
$patterns [ 'non_admin' ] = implode ( " \n " , $patterns [ 'non_admin' ]);
}
return $patterns ;
}
2009-12-17 13:10:19 +00:00
/**
* Checks a path exists and the current user has access to it .
*
* @ param $path
* The path to check .
* @ param $dynamic_allowed
* Whether paths with menu wildcards ( like user /% ) should be allowed .
*
* @ return
* TRUE if it is a valid path AND the current user has access permission ,
* FALSE otherwise .
*/
function drupal_valid_path ( $path , $dynamic_allowed = FALSE ) {
global $menu_admin ;
// We indicate that a menu administrator is running the menu access check.
$menu_admin = TRUE ;
if ( $path == '<front>' || url_is_external ( $path )) {
$item = array ( 'access' => TRUE );
}
elseif ( $dynamic_allowed && preg_match ( '/\/\%/' , $path )) {
// Path is dynamic (ie 'user/%'), so check directly against menu_router table.
if ( $item = db_query ( " SELECT * FROM { menu_router} where path = :path " , array ( ':path' => $path )) -> fetchAssoc ()) {
$item [ 'link_path' ] = $form_item [ 'link_path' ];
$item [ 'link_title' ] = $form_item [ 'link_title' ];
$item [ 'external' ] = FALSE ;
$item [ 'options' ] = '' ;
_menu_link_translate ( $item );
}
}
else {
$item = menu_get_item ( $path );
}
$menu_admin = FALSE ;
return $item && $item [ 'access' ];
}
2010-03-07 06:58:14 +00:00
/**
* Clear the path cache .
*
* @ param $source
* An optional system path for which an alias is being changed .
*/
function drupal_clear_path_cache ( $source = NULL ) {
// Clear the drupal_lookup_path() static cache.
drupal_static_reset ( 'drupal_lookup_path' );
drupal_path_alias_whitelist_rebuild ( $source );
}