Package base :: Package includes :: Module path
[hide private]

Source Code for Module base.includes.path

  1  #!/usr/bin/env python 
  2  # $Id: path.inc,v 1.24 2008/06/24 22:12:15 dries Exp $ 
  3   
  4   
  5  """ 
  6    Functions to handle paths in Drupy, including path aliasing. 
  7   
  8    @package includes 
  9    @see <a href='http://drupy.net'>Drupy Homepage</a> 
 10    @see <a href='http://drupal.org'>Drupal Homepage</a> 
 11    @note Drupy is a port of the Drupal project. 
 12    @note This file was ported from Drupal's includes/path.inc 
 13    @author Brendon Crawford 
 14    @copyright 2008 Brendon Crawford 
 15    @contact message144 at users dot sourceforge dot net 
 16    @created 2008-01-10 
 17    @version 0.1 
 18    @note License: 
 19   
 20      This program is free software; you can redistribute it and/or 
 21      modify it under the terms of the GNU General Public License 
 22      as published by the Free Software Foundation; either version 2 
 23      of the License, or (at your option) any later version. 
 24   
 25      This program is distributed in the hope that it will be useful, 
 26      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 27      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 28      GNU General Public License for more details. 
 29   
 30      You should have received a copy of the GNU General Public License 
 31      along with this program; if not, write to: 
 32       
 33      The Free Software Foundation, Inc., 
 34      51 Franklin Street, Fifth Floor, 
 35      Boston, MA  02110-1301, 
 36      USA 
 37  """ 
 38   
 39  __version__ = "$Revision: 1 $" 
 40   
 41  # 
 42  # These functions are not loaded for cached pages, but plugins that need 
 43  #  to use them in hook_init() or hook exit() can make them available, by 
 44  #  executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);". 
 45  # 
 46   
 47  from lib.drupy import DrupyPHP as php 
 48  import bootstrap as lib_bootstrap 
 49  import plugin as lib_plugin 
 50   
 51   
52 -def drupal_init_path():
53 """ 54 Initialize the php.GET['q'] variable to the proper normal path. 55 """ 56 if (php.isset(php.GET, 'q') and not php.empty(php.GET['q'])): 57 php.GET['q'] = drupal_get_normal_path(php.trim(php.GET['q'], '/')) 58 else: 59 php.GET['q'] = drupal_get_normal_path( \ 60 lib_bootstrap.variable_get('site_frontpage', 'node'))
61 62
63 -def drupal_lookup_path(action, path_ = '', path_language = ''):
64 """ 65 Given an alias, return its Drupal system URL if one exists. Given a Drupal 66 system URL return one of its aliases if such a one exists. Otherwise, 67 return FALSE. 68 69 @param action 70 One of the following values: 71 - wipe: delete the alias cache. 72 - alias: return an alias for a given Drupal system path (if one exists). 73 - source: return the Drupal system URL for a path alias (if one exists). 74 @param path 75 The path to investigate for corresponding aliases or system URLs. 76 @param path_language 77 Optional language code to search the path with. 78 Defaults to the page language. 79 If there's no path defined for that language it will search paths without 80 language. 81 82 @return 83 Either a Drupal system path, an aliased path, or FALSE if no path was 84 found. 85 """ 86 php.static(drupal_lookup_path, 'map_', {}) 87 php.static(drupal_lookup_path, 'no_src', {}) 88 php.static(drupal_lookup_path, 'count', None) 89 # map is an array with language keys, holding arrays of Drupal 90 # paths to alias relations 91 path_language = (path_language if (path_language != '') else \ 92 lib_appglobals.language.language) 93 # Use $count to avoid looking up paths in subsequent 94 # calls if there simply are no aliases 95 if (drupal_lookup_path.count is None): 96 drupal_lookup_path.count = lib_database.result( \ 97 lib_database.query('SELECT COUNT(pid) FROM {url_alias}')); 98 if (action == 'wipe' ): 99 drupal_lookup_path._map = {} 100 drupal_lookup_path.no_src = {} 101 elif (drupal_lookup_path.count > 0 and path_ != ''): 102 if (action == 'alias'): 103 if (php.isset(drupal_lookup_path.map_[path_language], path_)): 104 return drupal_lookup_path.map_[path_language][path_] 105 # Get the most fitting result falling back with alias without language 106 alias = db_result(db_query(\ 107 "SELECT dst FROM {url_alias} " + \ 108 "WHERE src = '%s' AND language IN('%s', '') " + \ 109 "ORDER BY language DESC", path_, path_language)); 110 drupal_lookup_path.map_[path_language][path_] = alias 111 return alias 112 # Check no_src for this path in case we've already determined that there 113 # isn't a path that has this alias 114 elif (action == 'source' and not \ 115 php.isset(drupal_lookup_path.no_src[path_language], path_)): 116 # Look for the value path within the cached map 117 src = '' 118 src = array_search(path, drupal_lookup_path.map_[path_language]) 119 if (not php.isset(drupal_lookup_path.map_, path_language) or not src): 120 # Get the most fitting result falling back with alias without language 121 src = db_result(db_query(\ 122 "SELECT src FROM {url_alias} " + \ 123 "WHERE dst = '%s' AND language IN('%s', '') " + \ 124 "ORDER BY language DESC", path_, path_language)) 125 if (src): 126 drupal_lookup_path.map_[path_language][src] = path_ 127 else: 128 # We can't record anything into map because we do not have a valid 129 # index and there is no need because we have not learned anything 130 # about any Drupal path. Thus cache to no_src. 131 drupal_lookup_path.no_src[path_language][path_] = True 132 return src 133 return False
134 135 136
137 -def drupal_get_path_alias(path, path_language = ''):
138 """ 139 Given an internal Drupal path, return the alias set by the administrator. 140 141 @param path 142 An internal Drupal path. 143 @param path_language 144 An optional language code to look up the path in. 145 146 @return 147 An aliased path if one was found, or the original path if no alias was 148 found. 149 """ 150 result = path 151 alias = drupal_lookup_path('alias', path, path_language) 152 if (alias): 153 result = alias 154 return result
155 156 157
158 -def drupal_get_normal_path(path, path_language = ''):
159 """ 160 Given a path alias, return the internal path it represents. 161 162 @param path 163 A Drupal path alias. 164 @param path_language 165 An optional language code to look up the path in. 166 167 @return 168 The internal path represented by the alias, or the original alias if no 169 internal path was found. 170 """ 171 result = path; 172 src = drupal_lookup_path('source', path, path_language); 173 if (src): 174 result = src; 175 if (php.function_exists('custom_url_rewrite_inbound')): 176 # Modules may alter the inbound request path by reference. 177 custom_url_rewrite_inbound(result, path, path_language); 178 return result;
179 180 181
182 -def arg(index = None, path = None):
183 """ 184 Return a component of the current Drupal path. 185 186 When viewing a page at the path "admin/build/types", for example, arg(0) 187 would return "admin", arg(1) would return "content", and arg(2) would return 188 "types". 189 190 Avoid use of this function where possible, as resulting code is hard to read. 191 Instead, attempt to use named arguments in menu callback functions. See the 192 explanation in menu.inc for how to construct callbacks that take arguments. 193 194 @param index 195 The index of the component, where each component is separated by a '/' 196 (forward-slash), and where the first component has an index of 0 (zero). 197 198 @return 199 The component specified by index, or NULL if the specified component was 200 not found. 201 """ 202 php.static(arg, 'arguments', {}) 203 if (path is None): 204 path = php.GET['q']; 205 if (not php.isset(arg.arguments, path)): 206 arg.arguments[path] = php.explode('/', path); 207 if (index is None): 208 return arg.arguments[path]; 209 if (php.isset(arg.arguments[path], index)): 210 return arg.arguments[path][index];
211 212 213
214 -def drupal_get_title():
215 """ 216 Get the title of the current page, for display on 217 the page and in the title bar. 218 219 @return 220 The current page's title. 221 """ 222 title = drupal_set_title(); 223 # during a bootstrap, menu.inc is not included 224 # and thus we cannot provide a title 225 if (title == None and php.function_exists('menu_get_active_title')): 226 title = check_plain(menu_get_active_title()); 227 return title;
228 229
230 -def drupal_set_title(title = None):
231 """ 232 Set the title of the current page, for display on the 233 page and in the title bar. 234 235 @param title 236 Optional string value to assign to the page title; or if set to NULL 237 (default), leaves the current title unchanged. 238 239 @return 240 The updated title of the current page. 241 """ 242 php.static(drupal_set_title, 'stored_title') 243 if (title == None): 244 drupal_set_title.stored_title = title; 245 return drupal_set_title.stored_title;
246 247
248 -def drupal_is_front_page():
249 """ 250 Check if the current page is the front page. 251 252 @return 253 Boolean value: TRUE if the current page is the front page; 254 FALSE if otherwise. 255 """ 256 # As drupal_init_path updates php.GET['q'] with the 'site_frontpage' path, 257 # we can check it against the 'site_frontpage' variable. 258 return (php.GET['q'] == \ 259 drupal_get_normal_path(variable_get('site_frontpage', 'node')));
260 261
262 -def drupal_match_path(path_, patterns):
263 """ 264 Check if a path matches any pattern in a set of patterns. 265 266 @note DRUPY: 267 This function was substantially modified 268 @param path 269 The path to match. 270 @param patterns 271 String containing a set of patterns separated by \n, \r or \r\n. 272 @return 273 Boolean value: TRUE if the path matches a pattern, FALSE otherwise. 274 """ 275 php.static(drupal_match_path, 'regexps') 276 if (not php.isset(drupal_match_path.regexps, patterns)): 277 frnt = variable_get('site_frontpage', 'node'); 278 frnt_q = php.preg_quote(frnt, '/'); 279 frnt_p = '\1' + frnt_q + '\2'; 280 pra2 = ['|', '.*', frnt_p]; 281 pra1 = ['/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/']; 282 pat_q = php.preg_quote(patterns, '/'); 283 pat_prep = php.preg_replace(pra1, pra2, pat_q); 284 pat_final = '/^(' + pat_prep + ')$/'; 285 drupal_match_path.regexps[patterns] = pat_final; 286 return (php.preg_match(drupal_match_path.regexps[patterns], path_) > 0) 287 else: 288 return False
289