first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
name = XML sitemap taxonomy
description = Add taxonomy term links to the sitemap.
package = XML sitemap
core = 7.x
dependencies[] = xmlsitemap
dependencies[] = taxonomy
files[] = xmlsitemap_taxonomy.module
files[] = xmlsitemap_taxonomy.install
files[] = xmlsitemap_taxonomy.test
; Information added by drupal.org packaging script on 2012-12-08
version = "7.x-2.0-rc2+0-dev"
core = "7.x"
project = "xmlsitemap"
datestamp = "1354931808"

View File

@@ -0,0 +1,66 @@
<?php
/**
* @file
* Install and uninstall schema and functions for the xmlsitemap_taxonomy module.
*/
/**
* Implements hook_uninstall().
*/
function xmlsitemap_taxonomy_uninstall() {
drupal_load('module', 'taxonomy');
drupal_load('module', 'xmlsitemap');
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vid => $vocabulary) {
xmlsitemap_link_bundle_delete('taxonomy_term', $vocabulary->machine_name);
}
}
/**
* Implements hook_update_last_removed().
*/
function xmlsitemap_taxonomy_update_last_removed() {
return 6198;
}
/**
* Cleanup variables.
*/
function xmlsitemap_taxonomy_update_6200() {
drupal_load('module', 'taxonomy');
drupal_load('module', 'xmlsitemap');
$vids = array_keys(taxonomy_get_vocabularies());
foreach ($vids as $vid) {
$settings = array(
'status' => variable_get('xmlsitemap_taxonomy_status_' . $vid, XMLSITEMAP_STATUS_DEFAULT),
'priority' => variable_get('xmlsitemap_taxonomy_priority_' . $vid, XMLSITEMAP_PRIORITY_DEFAULT),
);
variable_set('xmlsitemap_settings_taxonomy_term_' . $vid, $settings);
variable_del('xmlsitemap_taxonomy_status_' . $vid);
variable_del('xmlsitemap_taxonomy_priority_' . $vid);
variable_del('xmlsitemap_taxonomy_calculate_priority_' . $vid);
variable_del('xmlsitemap_taxonomy_include_empty_terms_' . $vid);
}
variable_del('xmlsitemap_taxonomy_include_empty_terms');
variable_del('xmlsitemap_taxonomy_calculate_priority');
}
/**
* Empty update.
*/
function xmlsitemap_taxonomy_update_6201() {
}
/**
* Change bundles on taxonomy terms from vid to $vocabulary->machine_name.
*/
function xmlsitemap_taxonomy_update_7200() {
drupal_load('module', 'taxonomy');
drupal_load('module', 'xmlsitemap');
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vid => $vocabulary) {
xmlsitemap_link_bundle_rename('taxonomy_term', $vid, $vocabulary->machine_name);
}
}

View File

@@ -0,0 +1,265 @@
<?php
/**
* Implements hook_entity_info_alter().
*/
function xmlsitemap_taxonomy_entity_info_alter(&$entity_info) {
$entity_info['taxonomy_term']['bundle label'] = t('Vocabulary');
$entity_info['taxonomy_term']['xmlsitemap'] = array(
'process callback' => 'xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links',
);
}
/**
* Implements hook_xmlsitemap_link_info_alter().
*/
function xmlsitemap_taxonomy_xmlsitemap_link_info_alter(&$link_info) {
foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
// Adjust the edit path to the *real* edit path.
$link_info['taxonomy_term']['bundles'][$machine_name]['admin']['path'] .= '/edit';
$link_info['taxonomy_term']['bundles'][$machine_name]['admin']['real path'] .= '/edit';
}
}
/**
* Implements hook_cron().
*
* Process old taxonomy terms not found in the {xmlsitemap} table.
*/
function xmlsitemap_taxonomy_cron() {
xmlsitemap_taxonomy_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}
/**
* Implements hook_xmlsitemap_index_links().
*/
function xmlsitemap_taxonomy_xmlsitemap_index_links($limit) {
if ($bundles = xmlsitemap_get_link_type_enabled_bundles('taxonomy_term')) {
$tids = db_query_range("SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_vocabulary} tv USING (vid) LEFT JOIN {xmlsitemap} x ON x.type = 'taxonomy_term' AND t.tid = x.id WHERE x.id IS NULL AND tv.machine_name IN (:bundles) ORDER BY t.tid DESC", 0, $limit, array(':bundles' => $bundles))->fetchCol();
xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links($tids);
}
}
/**
* Process taxonomy term sitemap links.
*
* @param $tids
* An array of taxonomy term IDs.
*/
function xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links(array $tids) {
$terms = taxonomy_term_load_multiple($tids);
foreach ($terms as $term) {
$link = xmlsitemap_taxonomy_create_link($term);
xmlsitemap_link_save($link);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @see taxonomy_form_vocabulary()
* @see xmlsitemap_add_link_bundle_settings()
*/
function xmlsitemap_taxonomy_form_taxonomy_form_vocabulary_alter(&$form, $form_state) {
if (in_array('taxonomy_vocabulary_confirm_delete_submit', $form['#submit'])) {
// If this is the delete form, do not add our form elements.
return;
}
module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
xmlsitemap_add_link_bundle_settings($form, $form_state, 'taxonomy_term', $form['#vocabulary']->machine_name);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function xmlsitemap_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state) {
if ($form['name']['#type'] == 'value') {
// If this is the delete form, do not add our form elements.
return;
}
// Add the link options.
module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
xmlsitemap_add_form_link_options($form, 'taxonomy_term', $form['#term']['vocabulary_machine_name'], $form['tid']['#value']);
}
/**
* Implements hook_taxonomy_vocabulary_insert().
*/
function xmlsitemap_taxonomy_vocabulary_insert(stdClass $vocabulary) {
if (isset($vocabulary->xmlsitemap)) {
xmlsitemap_link_bundle_settings_save('taxonomy_term', $vocabulary->machine_name, $vocabulary->xmlsitemap);
}
}
/**
* Implements hook_taxonomy_vocabulary_update().
*/
function xmlsitemap_taxonomy_vocabulary_update(stdClass $vocabulary) {
if (isset($vocabulary->xmlsitemap)) {
xmlsitemap_link_bundle_settings_save('taxonomy_term', $vocabulary->machine_name, $vocabulary->xmlsitemap);
}
}
/**
* Implements hook_taxonomy_term_insert() {
*/
function xmlsitemap_taxonomy_term_insert(stdClass $term) {
$link = xmlsitemap_taxonomy_create_link($term);
xmlsitemap_link_save($link);
}
/**
* Implements hook_taxonomy_term_update() {
*/
function xmlsitemap_taxonomy_term_update(stdClass $term) {
$link = xmlsitemap_taxonomy_create_link($term);
xmlsitemap_link_save($link);
}
/**
* Implements hook_taxonomy_term_delete() {
*/
function xmlsitemap_taxonomy_term_delete(stdClass $term) {
xmlsitemap_link_delete('taxonomy_term', $term->tid);
}
/**
* Implements hook_field_extra_fields().
*/
function xmlsitemap_taxonomy_field_extra_fields() {
$extras = array();
foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
$extras['taxonomy_term'][$machine_name]['form']['xmlsitemap'] = array(
'label' => t('XML sitemap'),
'description' => t('XML sitemap module element'),
'weight' => 30,
);
}
return $extras;
}
/**
* Create a sitemap link from a taxonomy term.
*
* @param $term
* A taxonomy term object.
* @return
* An array representing a sitemap link.
*/
function xmlsitemap_taxonomy_create_link(stdClass &$term) {
if (!isset($term->xmlsitemap)) {
$term->xmlsitemap = array();
if ($term->tid && $link = xmlsitemap_link_load('taxonomy_term', $term->tid)) {
$term->xmlsitemap = $link;
}
}
$settings = xmlsitemap_link_bundle_load('taxonomy_term', $term->vocabulary_machine_name);
$uri = entity_uri('taxonomy_term', $term);
$term->xmlsitemap += array(
'id' => $term->tid,
'type' => 'taxonomy_term',
'subtype' => $term->vocabulary_machine_name,
'status' => $settings['status'],
'status_default' => $settings['status'],
'status_override' => 0,
'priority' => $settings['priority'],
'priority_default' => $settings['priority'],
'priority_override' => 0,
);
// The following values must always be checked because they are volatile.
// @todo How can/should we check taxonomy term access?
$term->xmlsitemap['loc'] = $uri['path'];
$term->xmlsitemap['access'] = 1;
$term->xmlsitemap['language'] = isset($term->language) ? $term->language : LANGUAGE_NONE;
return $term->xmlsitemap;
}
/**
* Calculate the priority of a taxonomy term based on depth and weight.
*/
//function xmlsitemap_taxonomy_calculate_term_priority(stdClass $term) {
// // Calculate priority.
// // Min weight = -128
// // Max weight = 127
// // Max depth = ?
//}
/**
* Find the tree depth of a taxonomy term.
*
* @param $term
* A taxonomy term object.
* @return
* The tree depth of the term.
*/
function xmlsitemap_taxonomy_get_term_depth(stdClass $term) {
static $depths = array();
if (!isset($depths[$term->tid])) {
if ($parent = db_query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = %d", $term->tid)->fetchField()) {
// If the term has a parent, the term's depth is the parent's depth + 1.
if (!isset($depths[$parent])) {
$depths[$parent] = xmlsitemap_taxonomy_get_term_depth($parent);
}
$depths[$term->tid] = $depths[$parent] + 1;
}
else {
// Term has no parents, so depth is 0.
$depths[$term->tid] = 0;
}
}
return $depths[$term->tid];
}
/**
* Find the number of nodes that are associated with a taxonomy term.
*
* @param $term
* A taxonomy term object.
* @return
* The number of nodes associated with the term.
*/
function xmlsitemap_taxonomy_get_node_count(stdClass $term) {
// @todo Use db_rewrite_sql() w/ switch user.
return db_query_range("SELECT COUNT(ti.nid) FROM {taxonomy_index} ti LEFT JOIN {node n} USING (nid) WHERE ti.tid = :tid AND n.status = 1", 0, 1, array(':tid' => $term->tid))->fetchField();
}
/**
* Implements hook_entity_query_alter().
*
* @todo Remove when http://drupal.org/node/1054162 is fixed.
*/
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']);
}
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* @file
* Unit tests for the xmlsitemap_taxonomy module.
*/
class XMLSitemapTaxonomyFunctionalTest extends XMLSitemapTestHelper {
protected $normal_user;
protected $terms = array();
public static function getInfo() {
return array(
'name' => 'XML sitemap taxonomy',
'description' => 'Functional tests for the XML sitemap taxonomy module.',
'group' => 'XML sitemap',
);
}
function setUp($modules = array()) {
$modules[] = 'xmlsitemap_taxonomy';
$modules[] = 'taxonomy';
parent::setUp($modules);
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer xmlsitemap'));
$this->normal_user = $this->drupalCreateUser(array('access content'));
}
function testTaxonomySettings() {
$this->drupalLogin($this->admin_user);
$edit = array(
'name' => $this->randomName(),
'machine_name' => drupal_strtolower($this->randomName()),
'xmlsitemap[status]' => '1',
'xmlsitemap[priority]' => '1.0',
);
$this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save');
$this->assertText("Created new vocabulary {$edit['name']}.");
$vocabulary = taxonomy_vocabulary_machine_name_load($edit['machine_name']);
$edit = array(
'name' => $this->randomName(),
'xmlsitemap[status]' => 'default',
'xmlsitemap[priority]' => 'default',
);
$this->drupalPost("admin/structure/taxonomy/{$vocabulary->machine_name}/add", $edit, 'Save');
}
}