Commit 8e46baeb authored by Dave Reid's avatar Dave Reid

by Dave Reid: Use EntityFieldQuery for general querying entities rather than...

by Dave Reid: Use EntityFieldQuery for general querying entities rather than building them manually.
parent 64ffaa6c
......@@ -495,9 +495,9 @@ function xmlsitemap_add_form_entity_summary(&$form, $entity, array $entity_info)
$totals['visible'] += $status['visible'];
$row = array();
$edit_link = xmlsitemap_get_operation_link("admin/config/search/xmlsitemap/settings/$entity/$bundle", array('modal' => TRUE));
if (!empty($edit_link['router info']['access'])) {
$row[] = l($bundle_info['label'], $edit_link['href'], $edit_link);
if (drupal_valid_path("admin/config/search/xmlsitemap/settings/$entity/$bundle")) {
$edit_link = xmlsitemap_get_operation_link("admin/config/search/xmlsitemap/settings/$entity/$bundle", array('title' => $bundle_info['label'], 'modal' => TRUE));
$row[] = l($edit_link['title'], $edit_link['href'], $edit_link);
}
else {
$row[] = $bundle_info['label'];
......
......@@ -427,16 +427,18 @@ function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
}
$info = $context['sandbox']['info'];
$query = db_select($info['base table'], 'base');
$query->addField('base', $info['entity keys']['id']);
$query->condition($info['entity keys']['id'], $context['sandbox']['last_id'], '>');
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity);
$query->entityCondition('entity_id', $context['sandbox']['last_id'], '>');
$query->addTag('xmlsitemap_link_bundle_access');
$query->addTag('xmlsitemap_rebuild');
$query->addMetaData('entity', $entity);
$query->addMetaData('entity_info', $info);
if (!isset($context['sandbox']['max'])) {
$context['sandbox']['max'] = $query->countQuery()->execute()->fetchField();
$count_query = clone $query;
$count_query->count();
$context['sandbox']['max'] = $count_query->execute();
if (!$context['sandbox']['max']) {
// If there are no items to process, skip everything else.
return;
......@@ -444,12 +446,14 @@ function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
}
// PostgreSQL cannot have the ORDERED BY in the count query.
$query->orderBy($info['entity keys']['id']);
$query->entityOrderBy('entity_id');
$limit = 20; //variable_get('xmlsitemap_batch_limit', 100)
$query->range(0, $limit);
$ids = $query->execute()->fetchCol();
$result = $query->execute();
$ids = array_keys($result[$entity]);
$info['xmlsitemap']['process callback']($ids);
$context['sandbox']['last_id'] = end($ids);
$context['sandbox']['progress'] += count($ids);
......
......@@ -2,7 +2,7 @@
// $Id$
/**
* @defgroup xmlsitemap XML sitemap: create sitemaps.org sitemaps.
* @defgroup xmlsitemap XML sitemap
*/
/**
......@@ -79,6 +79,7 @@ function xmlsitemap_hook_info() {
foreach ($hooks as $hook => $info) {
$hooks[$hook] = array('group' => 'xmlsitemap');
}
return $hooks;
}
......@@ -149,8 +150,10 @@ function xmlsitemap_menu() {
'type' => MENU_LOCAL_ACTION,
'file' => 'xmlsitemap.admin.inc',
'modal' => TRUE,
'options' => array('modal' => TRUE),
);
$items['admin/config/search/xmlsitemap/edit/%xmlsitemap_sitemap'] = array(
'title' => 'Edit XML sitemap',
'page callback' => 'drupal_get_form',
'page arguments' => array('xmlsitemap_sitemap_edit_form', 5),
'access arguments' => array('administer xmlsitemap'),
......@@ -158,6 +161,7 @@ function xmlsitemap_menu() {
'modal' => TRUE,
);
$items['admin/config/search/xmlsitemap/delete/%xmlsitemap_sitemap'] = array(
'title' => 'Delete XML sitemap',
'page callback' => 'drupal_get_form',
'page arguments' => array('xmlsitemap_sitemap_delete_form', 5),
'access arguments' => array('administer xmlsitemap'),
......@@ -975,21 +979,35 @@ function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
$status['indexed'] = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle", array(':entity' => $entity_type, ':bundle' => $bundle))->fetchField();
$status['visible'] = db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = :entity AND subtype = :bundle AND status = 1 AND access = 1", array(':entity' => $entity_type, ':bundle' => $bundle))->fetchField();
// @todo Convert this query to a countQuery().
$total = db_select($info['base table'], 'base');
$total->addExpression('COUNT(:id)', 'count_expression', array(':id' => $info['entity keys']['id']));
//$total->addField('base', $info['entity keys']['id']);
$total->condition($info['entity keys']['id'], 0, '>');
$total->addTag('xmlsitemap_link_bundle_access');
$total = new EntityFieldQuery();
$total->entityCondition('entity_type', $entity_type);
$total->entityCondition('bundle', $bundle);
$total->entityCondition('entity_id', 0, '>');
//$total->addTag('xmlsitemap_link_bundle_access');
$total->addTag('xmlsitemap_link_indexed_status');
$total->addMetaData('entity', $entity_type);
$total->addMetaData('bundle', $bundle);
$total->addMetaData('entity_info', $info);
$status['total'] = $total->/*countQuery()->*/execute()->fetchField();
$total->count();
$status['total'] = $total->execute();
return $status;
}
/**
* Implements hook_entity_query_alter().
*
* @todo Remove when http://drupal.org/node/1054168 is fixed.
*/
function xmlsitemap_entity_query_alter($query) {
$conditions = &$query->entityConditions;
// Alter user entity queries only.
if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'user' && isset($conditions['bundle'])) {
unset($conditions['bundle']);
}
}
function xmlsitemap_link_bundle_settings_save($entity, $bundle, array $settings, $update_links = TRUE) {
if ($update_links) {
$old_settings = xmlsitemap_link_bundle_load($entity, $bundle);
......@@ -1488,9 +1506,11 @@ function xmlsitemap_get_operation_link($url, $options = array()) {
$link = array('href' => $url) + $options;
// Fetch the item's menu router link info and title.
$item = menu_get_item($url);
$link += array('title' => $item['title'], 'router info' => $item, 'query' => $destination);
if (!isset($link['title'])) {
$item = menu_get_item($url);
$link['title'] = $item['title'];
}
drupal_alter('xmlsitemap_operation_link', $link);
$link += array('query' => $destination);
return $link;
}
......@@ -294,17 +294,12 @@ function xmlsitemap_sitemap_multiple_update(array $smids) {
* Implements hook_query_TAG_alter().
*/
function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
if ($entity = $query->getMetaData('entity')) {
if ($query instanceof EntityFieldQuery && $entity = $query->getMetaData('entity')) {
$info = $query->getMetaData('entity_info');
$bundle = $query->getMetaData('bundle');
if (empty($bundle)) {
$bundle = xmlsitemap_get_link_type_enabled_bundles($entity);
}
if (!empty($info['bundle keys']['bundle'])) {
$query->condition($info['bundle keys']['bundle'], $bundle);
}
elseif (!empty($info['entity keys']['bundle'])) {
$query->condition($info['entity keys']['bundle'], $bundle);
}
$query->entityCondition('bundle', $bundle, is_array($bundle) ? 'IN' : '=');
}
}
......@@ -237,11 +237,34 @@ function xmlsitemap_taxonomy_get_node_count(stdClass $term) {
}
/**
* Implements hook_query_TAG_alter().
* Implements hook_entity_query_alter().
*
* @todo Remove when http://drupal.org/node/1054162 is fixed.
*/
function xmlsitemap_taxonomy_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
if ($query->getMetaData('entity') == 'taxonomy_term') {
$query->innerJoin('taxonomy_vocabulary', 'v', 'base.vid = v.vid');
$query->addField('v', 'machine_name', 'vocabulary_machine_name');
function xmlsitemap_taxonomy_entity_query_alter($query) {
$conditions = &$query->entityConditions;
// Alter taxonomy term queries only.
if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'taxonomy_term' && isset($conditions['bundle'])) {
// We can only support the operators that are explicit in values.
if (in_array($conditions['bundle']['operator'], array(NULL, '=', '!=', 'IN', 'NOT IN'))) {
$vids = array();
// Convert vocabulary machine names to vocabulary IDs.
if (is_array($conditions['bundle']['value'])) {
foreach ($conditions['bundle']['value'] as $vocabulary_machine_name) {
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
$vids[] = $vocabulary->vid;
}
}
else {
$vocabulary = taxonomy_vocabulary_machine_name_load($conditions['bundle']['value']);
$vids = $vocabulary->vid;
}
$query->propertyCondition('vid', $vids, $conditions['bundle']['operator']);
unset($conditions['bundle']);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment