Commit f54ee273 authored by Dave Reid's avatar Dave Reid

by Dave Reid: Synced API changes from Drupal 6.x-2.x branch.

parent a46d41b3
......@@ -409,28 +409,40 @@ function _xmlsitemap_check_changed_link(array $link, $original_link = NULL, $fla
}
/**
* Load a specific sitemap link.
* Load sitemap links from the database.
*
* @param $conditions
* An array of values to match keyed by field.
* An array of conditions on the {xmlsitemap} table in the form
* 'field' => $value.
* @return
* An array representing the first sitemap link matching the conditions found.
*
* @todo Convert to use $type and $id as parameters.
* An array of sitemap link arrays.
*/
function xmlsitemap_load_link(array $conditions) {
function xmlsitemap_link_load_multiple(array $conditions = array()) {
$query = db_select('xmlsitemap', 'x');
$query->fields('x');
foreach ($conditions as $field => $value) {
$query->condition($field, $value);
}
$query->range(0, 1);
$link = $query->execute()->fetchAssoc();
// Allow other modules to respond after loading the link.
//module_invoke_all('xmlsitemap_load_link', $link, $conditions, $args);
$links = $query->execute()->fetchAllAssoc();
return $link;
return $links;
}
/**
* Load a specific sitemap link from the database.
*
* @param $entity_type
* A string with the entity type.
* @param $entity_id
* An integer with the entity ID.
* @return
* A sitemap link (array) or FALSE if the conditions were not found.
*/
function xmlsitemap_link_load($entity_type, $entity_id) {
$link = xmlsitemap_link_load_multiple(array('type' => $entity_type, 'id' => $entity_id));
return $link ? reset($link) : FALSE;
}
/**
......@@ -516,17 +528,36 @@ function xmlsitemap_update_links($updates = array(), $conditions = array()) {
}
/**
* Delete one or more sitemap links.
* Delete a specific sitemap link from the database.
*
* If a visible sitemap link was deleted, this will automatically set the
* regenerate needed flag.
*
* @param $entity_type
* A string with the entity type.
* @param $entity_id
* An integer with the entity ID.
* @return
* The number of links that were deleted.
*/
function xmlsitemap_link_delete($entity_type, $entity_id) {
$conditions = array('type' => $entity_type, 'id' => $entity_id);
return xmlsitemap_link_delete_multiple($conditions);
}
/**
* Delete multiple sitemap links from the database.
*
* If visible sitemap links were deleted, this will automatically set the
* regenerate needed flag.
*
* @param $conditions
* An array of values to match keyed by field.
* An array of conditions on the {xmlsitemap} table in the form
* 'field' => $value.
* @return
* The number of links that were deleted.
*/
function xmlsitemap_delete_link(array $conditions) {
function xmlsitemap_link_delete_multiple(array $conditions) {
if (!variable_get('xmlsitemap_regenerate_needed', TRUE)) {
_xmlsitemap_check_changed_links($conditions, array(), TRUE);
}
......@@ -535,7 +566,6 @@ function xmlsitemap_delete_link(array $conditions) {
foreach ($conditions as $field => $value) {
$query->condition($field, $value);
}
return $query->execute();
}
......@@ -861,3 +891,27 @@ function xmlsitemap_language_load($language = LANGUAGE_NONE) {
return $languages[$language];
}
function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
$info = xmlsitemap_get_link_info($entity_type);
$status = array();
$indexed = db_select('xmlsitemap');
$indexed->condition('type', $entity_type);
$indexed->condition('subtype', $bundle);
$visible = $indexed;
$visible->condition('status', 1);
$visible->condition('access', 1);
$status['indexed'] = $indexed->countQuery()->execute();
$status['visible'] = $visible->countQuery()->execute();
$total = db_select($info['base table']);
$total->addField($info['object keys']['id']);
if (!empty($info['object keys']['bundle'])) {
$total->condition($info['object keys']['bundle'], $bundle);
}
$status['total'] = $total->countQuery()->execute();
return $status;
}
......@@ -13,6 +13,7 @@
*/
class XMLSitemapTestHelper extends DrupalWebTestCase {
protected $admin_user;
protected $seen_ids = array();
function setUp() {
// Call parent::setUp() allowing test cases to pass further modules.
......@@ -30,7 +31,7 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
// Capture any (remaining) watchdog errors.
$this->assertNoWatchdogErrors();
// Reset the watchdog seen IDs for the next test run.
$this->getWatchdogMessages(array(), TRUE);
$this->seen_ids = array();
parent::tearDown();
}
......@@ -178,12 +179,10 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
*
* @todo Add unit tests for this function.
*/
protected function getWatchdogMessages(array $conditions = array(), $reset = FALSE) {
static $seen_ids = array();
protected function getWatchdogMessages(array $conditions = array(), $exclude_seen = TRUE) {
static $levels;
if (!module_exists('dblog') || $reset) {
$seen_ids = array();
if (!module_exists('dblog')) {
return;
}
if (!isset($levels)) {
......@@ -198,8 +197,8 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
}
$query->condition($field, $value);
}
if ($seen_ids) {
$query->condition('wid', $seen_ids, 'NOT IN');
if ($exclude_seen && $this->seen_ids) {
$query->condition('wid', $this->seen_ids, 'NOT IN');
}
$query->orderBy('timestamp', 'ASC');
$messages = $query->execute()->fetchAllAssoc('wid');
......@@ -209,10 +208,10 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
if (!is_array($message->variables)) {
$message->variables = array();
}
$message->text = $message->timestamp . ' - ' . $levels[$message->severity] . ' - ' . t($message->message, $message->variables);
$message->text = $message->timestamp . ' - ' . $levels[$message->severity] . ' - ' . $message->type . ' - ' . t($message->message, $message->variables);
}
$seen_ids = array_merge($seen_ids, array_keys($messages));
$this->seen_ids = array_merge($this->seen_ids, array_keys($messages));
return $messages;
}
......@@ -228,11 +227,11 @@ class XMLSitemapTestHelper extends DrupalWebTestCase {
* Check that there were no watchdog errors or worse.
*/
protected function assertNoWatchdogErrors() {
$messages = $this->getWatchdogMessages();
$messages = $this->getWatchdogMessages(array(), FALSE);
$verbose = array();
foreach ($messages as $message) {
if (in_array($message->severity, array(WATCHDOG_EMERG, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING))) {
if (in_array($message->severity, array(WATCHDOG_EMERG, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING)) && !in_array($message->wid, $this->seen_ids)) {
$this->fail($message->text);
}
$verbose[] = $message->text;
......
......@@ -191,7 +191,7 @@ function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) {
function xmlsitemap_custom_delete_link_form_submit($form, &$form_state) {
$link = $form_state['values']['link'];
xmlsitemap_delete_link(array('type' => 'custom', 'id' => $link['id']));
xmlsitemap_link_delete('custom', $link['id']);
drupal_set_message(t('The custom link for %loc has been deleted.', array('%loc' => $link['loc'])));
watchdog('xmlsitemap', 'The custom link for %loc has been deleted.', array('%loc' => $link['loc']), WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
......
......@@ -11,5 +11,5 @@
*/
function xmlsitemap_custom_uninstall() {
drupal_load('module', 'xmlsitemap');
xmlsitemap_delete_link(array('type' => 'custom'));
xmlsitemap_link_delete_multiple(array('type' => 'custom'));
}
......@@ -45,10 +45,10 @@ function xmlsitemap_custom_menu() {
* @param $id
* The sitemap link ID of the custom link to load.
*
* @see xmlsitemap_load_link()
* @see xmlsitemap_link_load()
*/
function xmlsitemap_custom_load($id) {
return xmlsitemap_load_link(array('type' => 'custom', 'id' => $id));
return xmlsitemap_link_load('custom', $id);
}
/**
......@@ -57,7 +57,7 @@ function xmlsitemap_custom_load($id) {
function xmlsitemap_custom_xmlsitemap_link_info() {
return array(
'custom' => array(
'purge' => FALSE,
'label' => t('Custom links'),
),
);
}
......@@ -54,7 +54,9 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$edit['loc'] = ' public-files ';
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText('The custom link for system/files was saved');
$link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => 'system/files'));
$links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => 'system/files'));
$this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
$link = reset($links);
$this->assertSitemapLinkValues($link, array('priority' => 0.5, 'changefreq' => 0, 'access' => 1, 'status' => 1));
$this->clickLink('Edit');
......@@ -64,7 +66,7 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText('The custom link for system/files was saved');
$link = $this->assertSitemapLink(array('type' => 'custom', 'id' => $link['id']));
$link = $this->assertSitemapLink('custom', $link['id']);
$this->assertSitemapLinkValues($link, array('priority' => 0.1, 'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS, 'access' => 1, 'status' => 1));
$this->clickLink('Delete');
......@@ -93,12 +95,14 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$edit['loc'] = 'misc/drupal.js';
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
$link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
$links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
// Test a valid folder.
$edit['loc'] = 'misc';
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this->assertText('The custom link for ' . $edit['loc'] . ' was saved');
$link = $this->assertSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
$links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
}
}
......@@ -24,6 +24,7 @@ class XMLSitemapI18nWebTestCase extends XMLSitemapTestHelper {
// Add predefined language and reset the locale cache.
require_once DRUPAL_ROOT . '/includes/locale.inc';
locale_add_language('fr', NULL, NULL, LANGUAGE_LTR, '', 'fr');
drupal_language_initialize();
variable_set('xmlsitemap_languages', array('en', 'fr'));
variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
......@@ -119,11 +120,11 @@ class XMLSitemapI18nNodeTest extends XMLSitemapI18nWebTestCase {
$node = $this->drupalCreateNode(array());
$this->drupalPost('node/' . $node->nid . '/edit', array('language' => 'en'), t('Save'));
$node = node_load($node->nid, NULL, TRUE);
$this->assertIdentical($node->xmlsitemap['language'], 'en');
$link = $this->assertSitemapLink('node', $node->nid);
$this->assertIdentical($link['language'], 'en');
$this->drupalPost('node/' . $node->nid . '/edit', array('language' => 'fr'), t('Save'));
$node = node_load($node->nid, NULL, TRUE);
$this->assertIdentical($node->xmlsitemap['language'], 'fr');
$link = $this->assertSitemapLink('node', $node->nid);
$this->assertIdentical($link['language'], 'fr');
}
}
......@@ -88,7 +88,7 @@ function xmlsitemap_menu_xmlsitemap_link_info() {
*/
function xmlsitemap_menu_menu_link_load($mlid) {
$menu_item = menu_link_load($mlid);
if ($data = xmlsitemap_load_link(array('type' => 'menu', 'id' => $mlid))) {
if ($data = xmlsitemap_link_load('menu', $mlid)) {
$menu_item['xmlsitemap'] = $data;
}
return $menu_item;
......@@ -186,7 +186,7 @@ function xmlsitemap_menu_form_menu_delete_menu_confirm_alter(&$form, $form_state
*/
function xmlsitemap_menu_form_menu_delete_menu_confirm_submit($form, $form_state) {
$menu = $form['#menu']['menu_name'];
xmlsitemap_delete_link(array('type' => 'menu', 'subtype' => $menu));
xmlsitemap_link_delete_multiple(array('type' => 'menu', 'subtype' => $menu));
variable_del('xmlsitemap_menu_status_' . $menu);
variable_del('xmlsitemap_menu_priority_ ' . $menu);
}
......@@ -245,7 +245,7 @@ function xmlsitemap_menu_item_update(array $menu_item) {
}
function xmlsitemap_menu_item_delete(array $menu_item) {
xmlsitemap_delete_link(array('type' => 'menu', 'id' => $menu_item['mlid']));
xmlsitemap_link_delete('menu', $menu_item['mlid']);
}
/**
......
......@@ -43,7 +43,7 @@ function xmlsitemap_node_node_update($node) {
* Implements hook_node_delete().
*/
function xmlsitemap_node_node_delete($node) {
xmlsitemap_delete_link(array('type' => 'node', 'id' => $node->nid));
xmlsitemap_link_delete('node', $node->nid);
}
/**
......@@ -82,7 +82,7 @@ function xmlsitemap_node_comment_delete(stdClass $comment) {
function xmlsitemap_node_node_type_delete($info) {
variable_del('xmlsitemap_node_status_' . $info->type);
variable_del('xmlsitemap_node_priority_' . $info->type);
//xmlsitemap_delete_link(array('type' => 'node', 'subtype' => $info->type));
//xmlsitemap_link_delete_multiple(array('type' => 'node', 'subtype' => $info->type));
}
/**
......@@ -188,7 +188,7 @@ function xmlsitemap_node_xmlsitemap_links($offset = 0, $limit = 0) {
if ($types = xmlsitemap_node_get_types()) {
$sql = "SELECT n.nid FROM {node} n WHERE n.nid > :offset AND n.type IN (:types) ORDER BY n.nid";
$args = array(':offset' => $offset, ':types' => $types);
$nids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args));
$nids = ($limit ? db_query_range($sql, 0, $limit, $args)->fetchCol() : db_query($sql, $args)->fetchCol());
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
......@@ -281,7 +281,7 @@ function xmlsitemap_node_get_timestamps($node) {
function xmlsitemap_node_create_link(&$node) {
if (!isset($node->xmlsitemap) || !is_array($node->xmlsitemap)) {
$node->xmlsitemap = array();
if ($node->nid && $link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid))) {
if ($node->nid && $link = xmlsitemap_link_load('node', $node->nid)) {
$node->xmlsitemap = $link;
}
}
......@@ -331,22 +331,3 @@ function xmlsitemap_node_variables() {
}
return $defaults;
}
///**
// * Internal implementation of variable_get().
// */
//function xmlsitemap_node_var($name, $default = NULL) {
// static $defaults;
// if (!isset($defaults)) {
// $defaults = xmlsitemap_node_variables();
// }
//
// $name = 'xmlsitemap_node_'. $name;
//
// // @todo Remove when stable.
// if (!isset($defaults[$name])) {
// trigger_error(t('Default variable for %variable not found.', array('%variable' => $name)));
// }
//
// return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
//}
......@@ -25,17 +25,17 @@ class XMLSitemapNodeTestHelper extends XMLSitemapTestHelper {
//}
protected function assertNodeSitemapLinkVisible(stdClass $node) {
$link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid));
$link = xmlsitemap_link_load('node', $node->nid);
return $this->assertSitemapLinkVisible($link);
}
protected function assertNodeSitemapLinkNotVisible(stdClass $node) {
$link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid));
$link = xmlsitemap_link_load('node', $node->nid);
return $this->assertSitemapLinkNotVisible($link);
}
protected function assertNodeSitemapLinkValues(stdClass $node, array $values) {
$link = xmlsitemap_load_link(array('type' => 'node', 'id' => $node->nid));
$link = xmlsitemap_link_load('node', $node->nid);
if (!$link) {
$this->fail(t('Could not load sitemap link for node @nid.', array('@nid' => $node->nid)));
}
......
......@@ -15,9 +15,9 @@ function xmlsitemap_taxonomy_cron() {
*/
function xmlsitemap_taxonomy_xmlsitemap_index_links($limit) {
if ($vids = xmlsitemap_taxonomy_get_vids()) {
$tids = db_query_range("SELECT t.tid FROM {term_data} t LEFT JOIN {xmlsitemap} x ON x.type = 'taxonomy_term' AND t.tid = x.id WHERE x.id IS NULL AND t.vid IN (:vids) ORDER BY t.tid DESC", 0, $limit, array(':vids' => $vids));
foreach ($tids as $tid) {
$term = xmlsitemap_taxonomy_taxonomy_term_load($tid);
$tids = db_query_range("SELECT t.tid FROM {term_data} t LEFT JOIN {xmlsitemap} x ON x.type = 'taxonomy_term' AND t.tid = x.id WHERE x.id IS NULL AND t.vid IN (:vids) ORDER BY t.tid DESC", 0, $limit, array(':vids' => $vids))->fetchCol();
$terms = taxonomy_term_load_multiple($tids);
foreach ($terms as $term) {
$link = xmlsitemap_taxonomy_create_link($term);
xmlsitemap_save_link($link);
}
......@@ -33,10 +33,10 @@ function xmlsitemap_taxonomy_xmlsitemap_links($offset = 0, $limit = 0) {
if ($vids = xmlsitemap_taxonomy_get_vids()) {
$sql = "SELECT t.tid FROM {term_data} t WHERE t.tid > :tid AND t.vid IN (:vids) ORDER BY t.tid";
$args = array(':tid' => $offset, ':vids' => $vids);
$tids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args));
$tids = ($limit ? db_query_range($sql, 0, $limit, $args)->fetchCol() : db_query($sql, $args)->fetchCol());
$terms = taxonomy_term_load_multiple($tids);
foreach ($tids as $tid) {
$term = xmlsitemap_taxonomy_taxonomy_term_load($tid);
foreach ($terms as $term) {
$links[] = xmlsitemap_taxonomy_create_link($term);
}
}
......@@ -69,21 +69,6 @@ function xmlsitemap_taxonomy_xmlsitemap_link_info() {
);
}
/**
* Load a taxonomy term and its associated sitemap link data.
*
* Use this instead of taxonomy_get_term().
*
* @todo Convert this to hook_taxonomy_term_load() in Drupal 7.
*/
function xmlsitemap_taxonomy_taxonomy_term_load($tid) {
$term = taxonomy_get_term($tid);
if ($data = xmlsitemap_load_link(array('type' => 'taxonomy_term', 'id' => $tid))) {
$term->xmlsitemap = $data;
}
return $term;
}
/**
* Implements hook_form_FORM_ID_alter().
*
......@@ -176,7 +161,7 @@ function xmlsitemap_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state)
// in Drupal 7.
if (isset($form['identification'])) {
if ($form['#term']['tid']) {
$term = xmlsitemap_taxonomy_taxonomy_term_load($form['#term']['tid']);
$term = taxonomy_term_load($form['#term']['tid']);
}
else {
$term = (object) $form['#term'];
......@@ -194,9 +179,9 @@ function xmlsitemap_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state)
}
// The submit and delete buttons need to be weighted down.
$form['submit'] += array('#weight' => 50);
$form['submit'] += array('#weight' => 100);
if (isset($form['delete'])) {
$form['delete'] += array('#weight' => 51);
$form['delete'] += array('#weight' => 110);
}
}
}
......@@ -209,7 +194,7 @@ function xmlsitemap_taxonomy_taxonomy($op, $type, $array = NULL) {
$vid = $array['vid'];
// Insert and update actions handled by xmlsitemap_taxonomy_taxonomy_form_vocabulary_submit().
if ($op == 'delete') {
xmlsitemap_delete_link(array('type' => 'taxonomy_term', 'subtype' => $vid));
xmlsitemap_link_delete_multiple(array('type' => 'taxonomy_term', 'subtype' => $vid));
variable_del('xmlsitemap_taxonomy_status_' . $vid);
variable_del('xmlsitemap_taxonomy_priority_' . $vid);
}
......@@ -221,7 +206,7 @@ function xmlsitemap_taxonomy_taxonomy($op, $type, $array = NULL) {
xmlsitemap_save_link($link);
}
elseif ($op == 'delete') {
xmlsitemap_delete_link(array('type' => 'taxonomy_term', 'id' => $tid));
xmlsitemap_link_delete('taxonomy_term', $tid);
}
}
}
......@@ -237,6 +222,9 @@ function xmlsitemap_taxonomy_taxonomy($op, $type, $array = NULL) {
function xmlsitemap_taxonomy_create_link(stdClass $term) {
if (!isset($term->xmlsitemap)) {
$term->xmlsitemap = array();
if ($term->tid && $link = xmlsitemap_load_link(array('type' => 'taxonomy_term', 'id' => $term->tid))) {
$term->xmlsitemap = $link;
}
}
$term->xmlsitemap += array(
......
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