xmlsitemap_user.module in XML sitemap 6
Same filename and directory in other branches
Adds user profiles to the sitemap.
File
xmlsitemap_user/xmlsitemap_user.moduleView source
<?php
/**
* @file
* Adds user profiles to the sitemap.
*/
/**
* @addtogroup xmlsitemap
* @{
*/
/*****************************************************************************
* Drupal hooks.
****************************************************************************/
/**
* Implementation of hook_cron().
*/
function xmlsitemap_user_cron() {
if (($limit = variable_get('xmlsitemap_cron_limit', 100)) != -1) {
$sql = "SELECT u.* FROM {users} u\n LEFT JOIN {xmlsitemap_user} xu ON xu.uid = u.uid\n WHERE xu.uid IS NULL\n AND u.uid <> 0\n AND u.status <> 0";
$result = db_query_range($sql, 0, $limit);
$sitemap_changed = FALSE;
while ($account = db_fetch_object($result)) {
$row = new stdClass();
$row->uid = $account->uid;
$row->changed = !empty($account->access) ? $account->access : REQUEST_TIME;
$row->previously_changed = $account->created;
drupal_write_record('xmlsitemap_user', $row);
$sitemap_changed = TRUE;
}
if ($sitemap_changed) {
xmlsitemap_flag_sitemap();
}
}
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function xmlsitemap_user_form_user_admin_settings_alter(&$form, &$from_state) {
if (!isset($form['xmlsitemap'])) {
$form['xmlsitemap'] = array(
'#type' => 'fieldset',
'#title' => t('XML sitemap'),
'#collapsible' => TRUE,
);
}
$default = variable_get('xmlsitemap_user_default_priority', 0.5);
$form['xmlsitemap']['xmlsitemap_user_default_priority'] = array(
'#type' => 'select',
'#title' => t('Default user priority'),
'#description' => t('The default priority for users who have only the authenticated user role.'),
'#default_value' => $default,
'#options' => xmlsitemap_priority_options('exclude'),
);
$form['buttons']['#weight'] = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] + 1 : 1;
$form['#submit'][] = 'xmlsitemap_user_admin_settings_submit';
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function xmlsitemap_user_form_user_admin_role_alter(&$form, &$from_state) {
$options = xmlsitemap_priority_options('exclude');
$priority = db_result(db_query("SELECT priority\n FROM {xmlsitemap_user_role}\n WHERE rid = %d", $form['rid']['#value']));
if ($priority === FALSE) {
$priority = 0.5;
}
if (!isset($form['xmlsitemap'])) {
$form['xmlsitemap'] = array(
'#type' => 'fieldset',
'#title' => t('XML sitemap'),
'#collapsible' => TRUE,
);
}
$form['xmlsitemap']['xmlsitemap_user_role_priority'] = array(
'#type' => 'select',
'#title' => t('Default priority'),
'#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')],
)),
'#default_value' => $priority,
'#options' => $options,
);
$form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
$form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['delete']['#weight'] + 1 : 1;
$form['#submit'][] = 'xmlsitemap_user_admin_role_submit';
}
/**
* Implementation of hook_user().
*/
function xmlsitemap_user_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
if ($category != 'account') {
return array();
}
case 'register':
$priority = db_result(db_query("SELECT priority_override\n FROM {xmlsitemap_user}\n WHERE uid = %d", $account->uid));
$options = xmlsitemap_priority_options('both');
if (isset($account->roles)) {
$roles = array_keys($account->roles);
$default = db_fetch_object(db_query("SELECT MIN(priority) AS min, SUM(priority) AS sum\n FROM {xmlsitemap_user_role}\n WHERE rid IN (" . db_placeholders($roles, 'varchar') . ")", $roles));
}
if (isset($default->min) && isset($default->sum)) {
$default_priority = $default->min < 0 ? -1.0 : round(min($default->sum, 1), 1);
}
else {
$default_priority = variable_get('xmlsitemap_user_default_priority', 0.5);
}
$form = xmlsitemap_user_form_fieldset();
$form['xmlsitemap']['xmlsitemap_user_priority'] = array(
'#type' => 'select',
'#title' => t('User priority'),
'#description' => t('The default priority is %priority.', array(
'%priority' => $options[(string) $default_priority],
)),
'#default_value' => $priority !== FALSE ? $priority : -2.0,
'#options' => $options,
);
return $form;
case 'insert':
if ($account->status) {
$row = new stdClass();
$row->uid = $account->uid;
$row->changed = !empty($account->access) ? $account->access : REQUEST_TIME;
$row->previously_changed = $account->created;
$row->priority_override = isset($edit['xmlsitemap_user_priority']) ? $edit['xmlsitemap_user_priority'] : -2.0;
drupal_write_record('xmlsitemap_user', $row);
$edit['xmlsitemap_user_priority'] = NULL;
xmlsitemap_flag_sitemap();
}
break;
case 'update':
if ($account->status) {
if (($result = db_fetch_object(db_query("SELECT uid, changed, previously_changed, priority_override FROM {xmlsitemap_user} WHERE uid = %d", $account->uid))) === FALSE) {
$row = new stdClass();
$row->uid = $account->uid;
$row->previously_changed = $account->created;
$row->changed = !empty($account->access) ? $account->access : REQUEST_TIME;
}
else {
$row = $result;
if (!empty($account->access)) {
if ($row->changed < $account->access) {
$row->previously_changed = $row->changed;
$row->changed = $account->access;
}
}
else {
$row->previously_changed = $row->changed;
$row->changed = REQUEST_TIME;
}
}
if (isset($edit['xmlsitemap_user_priority'])) {
$row->priority_override = $edit['xmlsitemap_user_priority'];
}
drupal_write_record('xmlsitemap_user', $row, $result !== FALSE ? 'uid' : NULL);
xmlsitemap_flag_sitemap();
}
break;
case 'delete':
db_query("DELETE FROM {xmlsitemap_user} WHERE uid = %d", $account->uid);
db_query("DELETE FROM {xmlsitemap} WHERE type = 'user' AND id = %d", $account->uid);
xmlsitemap_flag_sitemap();
break;
}
}
/**
* Implementation of hook_xmlsitemap_description().
*/
function xmlsitemap_user_xmlsitemap_description() {
return '<dt>' . t('XML sitemap user') . '</dt>' . '<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>';
}
/**
* Implementation of hook_xmlsitemap_links().
*/
function xmlsitemap_user_xmlsitemap_links() {
$anon_account = drupal_anonymous_user();
$user_access = user_access('access user profiles', $anon_account);
$result = db_query("SELECT u.uid, u.access, u.status, xu.changed, xu.previously_changed, xu.priority_override\n FROM {xmlsitemap_user} xu\n INNER JOIN {users} u ON xu.uid = u.uid");
$row = new stdClass();
$row->module = 'xmlsitemap_user';
$row->type = 'user';
while ($user = db_fetch_object($result)) {
$row->loc = 'user/' . $user->uid;
$row->id = $user->uid;
$row->changed = $user->changed;
$row->changefreq = max(REQUEST_TIME - $user->changed, empty($user->previously_changed) ? 0 : $user->changed - $user->previously_changed);
$priority = xmlsitemap_user_get_priority($user);
$row->priority = $priority == -1 ? $priority : min(max(round($priority, 1), 0.0), 1.0);
$row->priority = $user_access ? $row->priority : -1;
$old_row = db_fetch_object(db_query("SELECT lid, type, priority FROM {xmlsitemap} WHERE loc = '%s'", $row->loc));
if ($old_row === FALSE) {
drupal_write_record('xmlsitemap', $row);
}
elseif ($old_row->type == 'user' && ($old_row->priority != $row->priority || $old_row->changed != $row->changed || $old_row->changefreq != $row->changefreq)) {
$row->lid = $old_row->lid;
drupal_write_record('xmlsitemap', $row, 'lid');
}
}
}
/*****************************************************************************
* Public functions.
****************************************************************************/
/**
* Get the user priority in the sitemap.
* @param $user
* The user object.
* @param $load
* TRUE, if priority_override must be loaded from the module table.
* @return
* The priority for the user.
* @see xmlsitemap_user_set_priority()
*/
function xmlsitemap_user_get_priority($user, $load = FALSE) {
if (!isset($user->priority_override) && $load && $user->uid) {
$priority_override = db_result(db_query("SELECT xu.priority_override FROM {xmlsitemap_user} xu WHERE xu.uid = %d", $user->uid));
if ($priority_override !== FALSE) {
$user->priority_override = $priority_override;
}
}
if (isset($user->priority_override) && $user->priority_override != -2.0) {
$priority = $user->priority_override;
}
else {
$default = db_fetch_object(db_query("SELECT MIN(xur.priority) AS min, SUM(xur.priority) AS sum\n FROM {users_roles} ur\n INNER JOIN {xmlsitemap_user_role} xur ON xur.rid = ur.rid\n WHERE ur.uid = %d", $user->uid));
if (isset($default->min) && isset($default->sum)) {
$priority = $default->min < 0 ? -1.0 : round(min($default->sum, 1.0), 1);
}
else {
$priority = variable_get('xmlsitemap_user_default_priority', 0.5);
}
}
if (!isset($priority) || !$user->status || !$user->access) {
$priority = -1.0;
}
return $priority;
}
/**
* Set the user priority in the sitemap. It doesn't change the priority of a
* blocked user.
* @param $user
* The user object, or the user ID.
* @param $priority
* The priority for the user.
* @return
* The user object, or FALSE.
*/
function xmlsitemap_user_set_priority($user, $priority) {
if (!is_numeric($user)) {
$user = (object) $user;
$uid = $user->uid;
}
else {
$uid = $user;
$user = user_load($uid);
}
if ($user && $user->status) {
$result = db_fetch_object(db_query("SELECT uid, changed, previously_changed, priority_override\n FROM {xmlsitemap_user}\n WHERE uid = %d", $uid));
if ($result === FALSE) {
$row = new stdClass();
$row->uid = $user->uid;
$row->changed = $user->access;
$row->previously_changed = $user->created;
}
else {
$row = $result;
if ($row->changed < $user->access) {
$row->previously_changed = $row->changed;
$row->changed = $user->access;
}
}
$row->priority_override = $priority;
drupal_write_record('xmlsitemap_user', $row, $result === FALSE ? NULL : 'uid');
return $user;
}
return FALSE;
}
/*****************************************************************************
* Menu callbacks / form builders, submit/validate functions.
****************************************************************************/
/**
* Form submit function.
*/
function xmlsitemap_user_admin_role_submit($form, &$form_state) {
$form_values = $form_state['values'];
$priority = db_result(db_query("SELECT priority FROM {xmlsitemap_user_role} WHERE rid = %d", $form_values['rid']));
$row = new stdClass();
$row->rid = $form_values['rid'];
$row->priority = $form_values['xmlsitemap_user_role_priority'];
drupal_write_record('xmlsitemap_user_role', $row, $priority !== FALSE ? 'rid' : NULL);
xmlsitemap_flag_sitemap();
}
/**
* Form submit function.
*/
function xmlsitemap_user_admin_settings_submit($form, &$form_state) {
xmlsitemap_flag_sitemap();
}
/**
* @} End of "addtogroup xmlsitemap".
*/
Functions
Name | Description |
---|---|
xmlsitemap_user_admin_role_submit | Form submit function. |
xmlsitemap_user_admin_settings_submit | Form submit function. |
xmlsitemap_user_cron | Implementation of hook_cron(). |
xmlsitemap_user_form_user_admin_role_alter | Implementation of hook_form_FORM_ID_alter(). |
xmlsitemap_user_form_user_admin_settings_alter | Implementation of hook_form_FORM_ID_alter(). |
xmlsitemap_user_get_priority | Get the user priority in the sitemap. |
xmlsitemap_user_set_priority | Set the user priority in the sitemap. It doesn't change the priority of a blocked user. |
xmlsitemap_user_user | Implementation of hook_user(). |
xmlsitemap_user_xmlsitemap_description | Implementation of hook_xmlsitemap_description(). |
xmlsitemap_user_xmlsitemap_links | Implementation of hook_xmlsitemap_links(). |