|
1 <?php |
|
2 // $Id: xmlsitemap_user.module,v 1.12.2.106 2009/07/16 12:56:04 earnie Exp $ |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * Adds user profiles to the sitemap. |
|
7 */ |
|
8 |
|
9 /** |
|
10 * @addtogroup xmlsitemap |
|
11 * @{ |
|
12 */ |
|
13 |
|
14 /***************************************************************************** |
|
15 * Drupal hooks. |
|
16 ****************************************************************************/ |
|
17 |
|
18 /** |
|
19 * Implementation of hook_cron(). |
|
20 */ |
|
21 function xmlsitemap_user_cron() { |
|
22 if (($limit = variable_get('xmlsitemap_cron_limit', 100)) != -1) { |
|
23 $sql = "SELECT u.* FROM {users} u |
|
24 LEFT JOIN {xmlsitemap_user} xu ON xu.uid = u.uid |
|
25 WHERE xu.uid IS NULL |
|
26 AND u.uid <> 0 |
|
27 AND u.status <> 0"; |
|
28 $result = db_query_range($sql, 0, $limit); |
|
29 while ($account = db_fetch_object($result)) { |
|
30 $row = new stdClass(); |
|
31 $row->uid = $account->uid; |
|
32 $row->changed = !empty($account->access) ? $account->access : REQUEST_TIME; |
|
33 $row->previously_changed = $account->created; |
|
34 drupal_write_record('xmlsitemap_user', $row); |
|
35 } |
|
36 if ($result) { |
|
37 xmlsitemap_flag_sitemap(); |
|
38 } |
|
39 } |
|
40 } |
|
41 |
|
42 /** |
|
43 * Implementation of hook_form_FORM_ID_alter(). |
|
44 */ |
|
45 function xmlsitemap_user_form_user_admin_settings_alter(&$form, &$from_state) { |
|
46 if (!isset($form['xmlsitemap'])) { |
|
47 $form['xmlsitemap'] = array( |
|
48 '#type' => 'fieldset', |
|
49 '#title' => t('XML sitemap'), |
|
50 '#collapsible' => TRUE, |
|
51 ); |
|
52 } |
|
53 $default = variable_get('xmlsitemap_user_default_priority', 0.5); |
|
54 $form['xmlsitemap']['xmlsitemap_user_default_priority'] = array( |
|
55 '#type' => 'select', |
|
56 '#title' => t('Default user priority'), |
|
57 '#description' => t('The default priority for users who have only the authenticated user role.'), |
|
58 '#default_value' => $default, |
|
59 '#options' => xmlsitemap_priority_options('exclude'), |
|
60 ); |
|
61 $form['buttons']['#weight'] = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] + 1 : 1; |
|
62 $form['#submit'][] = 'xmlsitemap_user_admin_settings_submit'; |
|
63 } |
|
64 |
|
65 /** |
|
66 * Implementation of hook_form_FORM_ID_alter(). |
|
67 */ |
|
68 function xmlsitemap_user_form_user_admin_role_alter(&$form, &$from_state) { |
|
69 $options = xmlsitemap_priority_options('exclude'); |
|
70 $priority = db_result(db_query("SELECT priority |
|
71 FROM {xmlsitemap_user_role} |
|
72 WHERE rid = %d", $form['rid']['#value']) |
|
73 ); |
|
74 if ($priority === FALSE) { |
|
75 $priority = 0.5; |
|
76 } |
|
77 if (!isset($form['xmlsitemap'])) { |
|
78 $form['xmlsitemap'] = array( |
|
79 '#type' => 'fieldset', |
|
80 '#title' => t('XML sitemap'), |
|
81 '#collapsible' => TRUE, |
|
82 ); |
|
83 } |
|
84 $form['xmlsitemap']['xmlsitemap_user_role_priority'] = array( |
|
85 '#type' => 'select', |
|
86 '#title' => t('Default priority'), |
|
87 '#description' => t("The priority of all user roles will be added together to set the user's default priority. If all role priorities are left at default, the user's default priority will be %priority.", array('%priority' => $options[variable_get('xmlsitemap_user_default_priority', '0.5')])), |
|
88 '#default_value' => $priority, |
|
89 '#options' => $options, |
|
90 ); |
|
91 $form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1; |
|
92 $form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['delete']['#weight'] + 1 : 1; |
|
93 $form['#submit'][] = 'xmlsitemap_user_admin_role_submit'; |
|
94 } |
|
95 |
|
96 /** |
|
97 * Implementation of hook_user(). |
|
98 */ |
|
99 function xmlsitemap_user_user($op, &$edit, &$account, $category = NULL) { |
|
100 switch ($op) { |
|
101 case 'form': |
|
102 if ($category != 'account') { |
|
103 return array(); |
|
104 } |
|
105 case 'register': |
|
106 $priority = db_result(db_query("SELECT priority_override |
|
107 FROM {xmlsitemap_user} |
|
108 WHERE uid = %d", $account->uid) |
|
109 ); |
|
110 $options = xmlsitemap_priority_options('both'); |
|
111 if (isset($account->roles)) { |
|
112 $roles = array_keys($account->roles); |
|
113 $default = db_fetch_object( |
|
114 db_query("SELECT MIN(priority) AS min, SUM(priority) AS sum |
|
115 FROM {xmlsitemap_user_role} |
|
116 WHERE rid IN (". db_placeholders($roles, 'varchar') .")", |
|
117 $roles |
|
118 ) |
|
119 ); |
|
120 } |
|
121 if (isset($default->min) && isset($default->sum)) { |
|
122 $default_priority = $default->min < 0 ? -1.0 : round(min($default->sum, 1), 1); |
|
123 } |
|
124 else { |
|
125 $default_priority = variable_get('xmlsitemap_user_default_priority', 0.5); |
|
126 } |
|
127 $form = xmlsitemap_user_form_fieldset(); |
|
128 $form['xmlsitemap']['xmlsitemap_user_priority'] = array( |
|
129 '#type' => 'select', |
|
130 '#title' => t('User priority'), |
|
131 '#description' => t('The default priority is %priority.', array('%priority' => $options[(string)$default_priority])), |
|
132 '#default_value' => $priority !== FALSE ? $priority : -2.0, |
|
133 '#options' => $options, |
|
134 ); |
|
135 return $form; |
|
136 case 'insert': |
|
137 if ($account->status) { |
|
138 $row = new stdClass(); |
|
139 $row->uid = $account->uid; |
|
140 $row->changed = !empty($account->access) ? $account->access : REQUEST_TIME; |
|
141 $row->previously_changed = $account->created; |
|
142 $row->priority_override = isset($edit['xmlsitemap_user_priority']) ? $edit['xmlsitemap_user_priority'] : -2.0; |
|
143 drupal_write_record('xmlsitemap_user', $row); |
|
144 $edit['xmlsitemap_user_priority'] = NULL; |
|
145 xmlsitemap_flag_sitemap(); |
|
146 } |
|
147 break; |
|
148 case 'update': |
|
149 if ($account->status) { |
|
150 if (($result = db_fetch_object(db_query("SELECT uid, changed, previously_changed, priority_override FROM {xmlsitemap_user} WHERE uid = %d", $account->uid))) === FALSE) { |
|
151 $row = new stdClass(); |
|
152 $row->uid = $account->uid; |
|
153 $row->previously_changed = $account->created; |
|
154 $row->changed = !empty($account->access) ? $account->access : REQUEST_TIME; |
|
155 } |
|
156 else { |
|
157 $row = $result; |
|
158 if (!empty($account->access)) { |
|
159 if ($row->changed < $account->access) { |
|
160 $row->previously_changed = $row->changed; |
|
161 $row->changed = $account->access; |
|
162 } |
|
163 } |
|
164 else { |
|
165 $row->previously_changed = $row->changed; |
|
166 $row->changed = REQUEST_TIME; |
|
167 } |
|
168 } |
|
169 if (isset($edit['xmlsitemap_user_priority'])) { |
|
170 $row->priority_override = $edit['xmlsitemap_user_priority']; |
|
171 } |
|
172 drupal_write_record('xmlsitemap_user', $row, $result !== FALSE ? 'uid' : NULL); |
|
173 xmlsitemap_flag_sitemap(); |
|
174 } |
|
175 break; |
|
176 case 'delete': |
|
177 db_query("DELETE FROM {xmlsitemap_user} WHERE uid = %d", $account->uid); |
|
178 db_query("DELETE FROM {xmlsitemap} WHERE type = 'user' AND id = %d", $account->uid); |
|
179 xmlsitemap_flag_sitemap(); |
|
180 break; |
|
181 } |
|
182 } |
|
183 |
|
184 /** |
|
185 * Implementation of hook_xmlsitemap_description(). |
|
186 */ |
|
187 function xmlsitemap_user_xmlsitemap_description() { |
|
188 return '<dt>'. t('XML sitemap user') .'</dt>'. |
|
189 '<dd>'. t('The module adds user profiles to the sitemap. The <em>anonymous user</em> role must have permission to access user profiles on the <a href="@access">access control page</a>. You can change the default user priority on the <a href="@user_settings">user settings</a> page. The <a href="@user">user role priority</a> will override the default user priority. You can override both the default priority and the role priority if you add or edit a user.', array('@access' => url('admin/user/access'), '@user_settings' => url('admin/user/settings'), '@user_role' => url('admin/user/roles'))) .'</dd>'; |
|
190 } |
|
191 |
|
192 /** |
|
193 * Implementation of hook_xmlsitemap_links(). |
|
194 */ |
|
195 function xmlsitemap_user_xmlsitemap_links() { |
|
196 $anon_account = drupal_anonymous_user(); |
|
197 $user_access = user_access('access user profiles', $anon_account); |
|
198 |
|
199 $result = db_query("SELECT u.uid, u.access, u.status, xu.changed, xu.previously_changed, xu.priority_override |
|
200 FROM {xmlsitemap_user} xu |
|
201 INNER JOIN {users} u ON xu.uid = u.uid" |
|
202 ); |
|
203 $row = new stdClass(); |
|
204 $row->module = 'xmlsitemap_user'; |
|
205 $row->type = 'user'; |
|
206 while ($user = db_fetch_object($result)) { |
|
207 $row->loc = 'user/'. $user->uid; |
|
208 $row->id = $user->uid; |
|
209 $row->changed = $user->changed; |
|
210 $row->changefreq = max(REQUEST_TIME - $user->changed, empty($user->previously_changed) ? 0 : $user->changed - $user->previously_changed); |
|
211 $priority = xmlsitemap_user_get_priority($user); |
|
212 $row->priority = $priority == -1 ? $priority : min(max(round($priority, 1), 0.0), 1.0); |
|
213 $row->priority = $user_access ? $row->priority : -1; |
|
214 $old_row = db_fetch_object(db_query("SELECT lid, type, priority FROM {xmlsitemap} WHERE loc = '%s'", $row->loc)); |
|
215 if ($old_row === FALSE) { |
|
216 drupal_write_record('xmlsitemap', $row); |
|
217 } |
|
218 elseif ($old_row->type == 'user' && $old_row->priority != $row->priority) { |
|
219 $row->lid = $old_row->lid; |
|
220 drupal_write_record('xmlsitemap', $row, 'lid'); |
|
221 } |
|
222 } |
|
223 } |
|
224 |
|
225 /***************************************************************************** |
|
226 * Public functions. |
|
227 ****************************************************************************/ |
|
228 |
|
229 /** |
|
230 * Get the user priority in the sitemap. |
|
231 * @param $user |
|
232 * The user object. |
|
233 * @param $load |
|
234 * TRUE, if priority_override must be loaded from the module table. |
|
235 * @return |
|
236 * The priority for the user. |
|
237 * @see xmlsitemap_user_set_priority() |
|
238 */ |
|
239 function xmlsitemap_user_get_priority($user, $load = FALSE) { |
|
240 if (!isset($user->priority_override) && $load && $user->uid) { |
|
241 $priority_override = db_result(db_query("SELECT xu.priority_override FROM {xmlsitemap_user} xu WHERE xu.uid = %d", $user->uid)); |
|
242 if ($priority_override !== FALSE) { |
|
243 $user->priority_override = $priority_override; |
|
244 } |
|
245 } |
|
246 if (isset($user->priority_override) && $user->priority_override != -2.0) { |
|
247 $priority = $user->priority_override; |
|
248 } |
|
249 else { |
|
250 $default = db_fetch_object( |
|
251 db_query("SELECT MIN(xur.priority) AS min, SUM(xur.priority) AS sum |
|
252 FROM {users_roles} ur |
|
253 INNER JOIN {xmlsitemap_user_role} xur ON xur.rid = ur.rid |
|
254 WHERE ur.uid = %d", $user->uid |
|
255 ) |
|
256 ); |
|
257 if (isset($default->min) && isset($default->sum)) { |
|
258 $priority = $default->min < 0 ? -1.0 : round(min($default->sum, 1.0), 1); |
|
259 } |
|
260 else { |
|
261 $priority = variable_get('xmlsitemap_user_default_priority', 0.5); |
|
262 } |
|
263 } |
|
264 if (!isset($priority) || !$user->status || !$user->access) { |
|
265 $priority = -1.0; |
|
266 } |
|
267 return $priority; |
|
268 } |
|
269 |
|
270 /** |
|
271 * Set the user priority in the sitemap. It doesn't change the priority of a |
|
272 * blocked user. |
|
273 * @param $user |
|
274 * The user object, or the user ID. |
|
275 * @param $priority |
|
276 * The priority for the user. |
|
277 * @return |
|
278 * The user object, or FALSE. |
|
279 */ |
|
280 function xmlsitemap_user_set_priority($user, $priority) { |
|
281 if (!is_numeric($user)) { |
|
282 $user = (object) $user; |
|
283 $uid = $user->uid; |
|
284 } |
|
285 else { |
|
286 $uid = $user; |
|
287 $user = user_load($uid); |
|
288 } |
|
289 if ($user && $user->status) { |
|
290 $result = db_fetch_object(db_query("SELECT uid, changed, previously_changed, priority_override |
|
291 FROM {xmlsitemap_user} |
|
292 WHERE uid = %d", $uid) |
|
293 ); |
|
294 if ($result === FALSE) { |
|
295 $row = new stdClass(); |
|
296 $row->uid = $user->uid; |
|
297 $row->changed = $user->access; |
|
298 $row->previously_changed = $user->created; |
|
299 } |
|
300 else { |
|
301 $row = $result; |
|
302 if ($row->changed < $user->access) { |
|
303 $row->previously_changed = $row->changed; |
|
304 $row->changed = $user->access; |
|
305 } |
|
306 } |
|
307 $row->priority_override = $priority; |
|
308 drupal_write_record('xmlsitemap_user', $row, $result === FALSE ? NULL : 'uid'); |
|
309 return $user; |
|
310 } |
|
311 return FALSE; |
|
312 } |
|
313 |
|
314 /***************************************************************************** |
|
315 * Menu callbacks / form builders, submit/validate functions. |
|
316 ****************************************************************************/ |
|
317 |
|
318 /** |
|
319 * Form submit function. |
|
320 */ |
|
321 function xmlsitemap_user_admin_role_submit($form, &$form_state) { |
|
322 $form_values = $form_state['values']; |
|
323 $priority = db_result(db_query("SELECT priority FROM {xmlsitemap_user_role} WHERE rid = %d", $form_values['rid'])); |
|
324 $row = new stdClass(); |
|
325 $row->rid = $form_values['rid']; |
|
326 $row->priority = $form_values['xmlsitemap_user_role_priority']; |
|
327 drupal_write_record('xmlsitemap_user_role', $row, $priority !== FALSE ? 'rid' : NULL); |
|
328 xmlsitemap_flag_sitemap(); |
|
329 } |
|
330 |
|
331 /** |
|
332 * Form submit function. |
|
333 */ |
|
334 function xmlsitemap_user_admin_settings_submit($form, &$form_state) { |
|
335 xmlsitemap_flag_sitemap(); |
|
336 } |
|
337 |
|
338 /** |
|
339 * @} End of "addtogroup xmlsitemap". |
|
340 */ |