1
2
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
43
44
45
46
47 from lib.drupy import DrupyPHP as php
48 import bootstrap as lib_bootstrap
49 import plugin as lib_plugin
50
51
61
62
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
90
91 path_language = (path_language if (path_language != '') else \
92 lib_appglobals.language.language)
93
94
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
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
113
114 elif (action == 'source' and not \
115 php.isset(drupal_lookup_path.no_src[path_language], path_)):
116
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
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
129
130
131 drupal_lookup_path.no_src[path_language][path_] = True
132 return src
133 return False
134
135
136
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
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
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
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
224
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
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
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
257
258 return (php.GET['q'] == \
259 drupal_get_normal_path(variable_get('site_frontpage', 'node')));
260
261
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