Skip to content

Instantly share code, notes, and snippets.

@Renrhaf
Created February 29, 2016 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Renrhaf/333a55285c3105098d3f to your computer and use it in GitHub Desktop.
Save Renrhaf/333a55285c3105098d3f to your computer and use it in GitHub Desktop.
AFOUND-628
diff --git a/modules/features/scald_sitemap/README.md b/modules/features/scald_sitemap/README.md
index 8ba2b5d..5b98a94 100644
--- a/modules/features/scald_sitemap/README.md
+++ b/modules/features/scald_sitemap/README.md
@@ -1,12 +1,25 @@
-The Video Sitemaps are located at (Google and ARTE formats):
+# Scald Sitemap
- http://example.com/LANGCODE/sitemaps/video.xml
- http://example.com/LANGCODE/sitemaps/video-arte.xml
-Each sitemapindex contains URLs to sitemaps. Each sitemaps contains (upto 1000) URLs of videos.
+## Description
+
+This module allow to generate XML sitemaps for scald video atoms.
+The sitemaps are located at (Google and ARTE formats):
+ - http://example.com/LANGCODE/sitemaps/video.xml
+ - http://example.com/LANGCODE/sitemaps/video-arte.xml
+
+Each sitemapindex contains URLs to sitemaps, and up to 1000 URLs of videos.
+This can be altered via a variable setting.
Sitemaps are cached in sites/default/files/sitemaps directory and are generated with drush:
- drush scald-sitemap-generate
-A cron job could be set up to run this command daily or more frequently.
+## Installation instructions
+
+1. Install and enable the module.
+
+2. Throw `drush scald-sitemap-generate`
+
+3. Check some sitemap pages : LANG/sitemaps/video-arte.xml
+
+4. A cron job could be set up to run this command daily or more frequently.
\ No newline at end of file
diff --git a/modules/features/scald_sitemap/scald_sitemap.drush.inc b/modules/features/scald_sitemap/scald_sitemap.drush.inc
old mode 100755
new mode 100644
index 216719c..30402c5
--- a/modules/features/scald_sitemap/scald_sitemap.drush.inc
+++ b/modules/features/scald_sitemap/scald_sitemap.drush.inc
@@ -19,27 +19,29 @@ function scald_sitemap_drush_command() {
/**
* Generate Scald sitemap files.
+ * Prepares and execute a batch.
*/
function drush_scald_sitemap_generate() {
- // On Acquia Cloud the wait_timeout is set to 600s. This command requires
- // about 15 minutes for ASF (1300 videos). Set a bigger value to avoid
- // "MySQL server has gone away".
- db_query("SET SESSION wait_timeout = 28800");
+ // Increase the timeout to avoid "MySQL server has gone away" error.
+ $timeout = (int) variable_get('scald_sitemap_timeout', 28800);
+ db_query("SET SESSION wait_timeout = $timeout");
// Number of URLs per file.
- $limit = (int)variable_get('scald_sitemap_limit', 1000);
+ $limit = (int) variable_get('scald_sitemap_limit', 1000);
+ // Prepare batch operations.
$videos = array_keys(_scald_sitemap_get_videos());
$count = ceil(count($videos)/$limit);
- $batch = [
- 'operations' => [],
+ $batch = array(
+ 'operations' => array(),
'file' => drupal_get_path('module', 'scald_sitemap') . '/scald_sitemap.drush.inc',
- ];
+ );
for ($i = 1; $i <= $count; $i++) {
- $batch['operations'][] = ['_scald_sitemap_do_write_files', [$i, array_slice($videos, ($i - 1) * $limit, $limit)]];
+ $batch['operations'][] = array('_scald_sitemap_do_write_files', array($i, array_slice($videos, ($i - 1) * $limit, $limit)));
}
- $batch['operations'][] = ['_scald_sitemap_do_write_indexes', [$count]];
+ $batch['operations'][] = array('_scald_sitemap_do_write_indexes', array($count));
+ // Set and execute batch.
batch_set($batch);
$batch =& batch_get();
$batch['progressive'] = FALSE;
@@ -47,9 +49,18 @@ function drush_scald_sitemap_generate() {
drush_backend_batch_process();
}
+/**
+ * Batch operation to build sitemap data.
+ *
+ * @param $page
+ * @param $videos
+ * @param $context
+ *
+ * @throws Exception
+ */
function _scald_sitemap_do_write_files($page, $videos, &$context) {
- // Number of node per iteration. 50 is a quite safe value. It's 20 by default
- // in xmlsitemap.
+ // Number of node per iteration. 50 is a quite safe value.
+ // It's 20 by default in xmlsitemap.
$limit = 50;
if (empty($context['sandbox']['progress'])) {
@@ -58,6 +69,7 @@ function _scald_sitemap_do_write_files($page, $videos, &$context) {
$context['sandbox']['content'] = array();
}
+ // Load the range of video atoms.
$videos = array_slice($videos, $context['sandbox']['progress'], $limit);
$atoms = scald_atom_load_multiple($videos);
@@ -65,10 +77,7 @@ function _scald_sitemap_do_write_files($page, $videos, &$context) {
$content = '';
foreach ($atoms as $atom) {
// Check atom language translation is available.
- if (
- isset($atom->translations) &&
- empty($atom->translations->data[$langcode]['status'])
- ) {
+ if (isset($atom->translations) && empty($atom->translations->data[$langcode]['status'])) {
continue;
}
@@ -79,26 +88,37 @@ function _scald_sitemap_do_write_files($page, $videos, &$context) {
continue;
}
- $tmp_nid = _scald_sitemap_get_lowest_nid_containing_scald_atom($atom->sid);
- if (!$tmp_nid) {
+ // Only consider videos linked to a node.
+ $nid = _scald_sitemap_get_lowest_nid_containing_scald_atom($atom->sid);
+ if (!$nid) {
continue;
}
- $node = node_load($tmp_nid);
- drupal_alter('scald_sitemap_node', $node);
-
+ // Load node and check if it is published.
+ $node = node_load($nid);
if (!$node || !$node->status) {
continue;
}
- // Check node language translation is available.
- if (
- isset($node->translations) &&
- empty($node->translations->data[$langcode]['status'])
- ) {
- continue;
+ // CHECK IF TRANSLATION EXISTS.
+ if (module_exists('entity_translation') && entity_translation_enabled('node', $node)) {
+ // Entity Translation integration.
+ $handler = entity_translation_get_handler('node', $node);
+ if ($translations = $handler->getTranslations()->data) {
+ if (!isset($translations[$langcode]) || !entity_translation_access('node', $translations[$langcode])) {
+ continue;
+ }
+ }
+
+ } elseif (module_exists('translation') && translation_supported_type($node->type)) {
+ // Content Translation integration.
+ $translations = translation_node_get_translations($node->tnid);
+ if (!isset($translations[$langcode])) {
+ continue;
+ }
}
+ // Finally, write sitemap data.
$content .= _scald_sitemap_build_data($atom, $node, $language);
}
diff --git a/modules/features/scald_sitemap/scald_sitemap.info b/modules/features/scald_sitemap/scald_sitemap.info
old mode 100755
new mode 100644
diff --git a/modules/features/scald_sitemap/scald_sitemap.install b/modules/features/scald_sitemap/scald_sitemap.install
old mode 100755
new mode 100644
index 9b8020e..28962b9
--- a/modules/features/scald_sitemap/scald_sitemap.install
+++ b/modules/features/scald_sitemap/scald_sitemap.install
@@ -5,8 +5,17 @@
*/
/**
+ * Implements hook_install().
+ */
+function scald_sitemap_install() {
+ variable_set('scald_sitemap_limit', 1000);
+ variable_set('scald_sitemap_timeout', 28800);
+}
+
+/**
* Implements hook_uninstall().
*/
function scald_sitemap_uninstall() {
variable_del('scald_sitemap_limit');
+ variable_del('scald_sitemap_timeout');
}
diff --git a/modules/features/scald_sitemap/scald_sitemap.module b/modules/features/scald_sitemap/scald_sitemap.module
index 75ba463..f86d81c 100644
--- a/modules/features/scald_sitemap/scald_sitemap.module
+++ b/modules/features/scald_sitemap/scald_sitemap.module
@@ -1,58 +1,68 @@
<?php
+
/**
* @file
- * Code for the Scald Sitemap feature.
+ * Code for the Scald Video Sitemap functionality.
*/
include_once 'scald_sitemap.features.inc';
/**
- * @file
- * Code for the scald video sitemap functionality.
- * Based on ArteCreative Scald Sitemap module: https://github.com/ArteGEIE/ARTE-Creative/tree/develop/sites/default/modules/custom/scald_sitemap
- */
-
-/**
* Implementation of hook_menu()
*/
function scald_sitemap_menu() {
$items = array();
+
$items['sitemaps/video.xml'] = array(
'title' => 'Video XML sitemap (SE version)',
'page callback' => 'scald_sitemap_output',
'page arguments' => array('google'),
- 'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
+
$items['sitemaps/video-arte.xml'] = array(
'title' => 'Video XML sitemap (ARTE version)',
'page callback' => 'scald_sitemap_output',
'page arguments' => array('arte'),
- 'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
+
return $items;
}
+/**
+ * Menu callback to generate sitemap.
+ *
+ * @param string $format
+ * Can be 'google' or 'arte'
+ *
+ * @throws Exception
+ */
function scald_sitemap_output($format = 'google') {
global $language;
- module_load_include('pages.inc', 'xmlsitemap');
+
$page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
+ module_load_include('pages.inc', 'xmlsitemap');
xmlsitemap_output_file(_scald_sitemap_get_file_location($language->language, $format, $page));
}
/**
- * Function that retrieves the file location of a subsitemap.
+ * Function that retrieves the file location of a sub-sitemap.
*
* @param string $langcode
+ * The language code.
* @param string $format
- * 'google' or 'arte'
+ * Can be 'google' or 'arte'.
* @param int $page
* 0 for index file.
+ *
* @throws Exception
+ * If an error occurs.
+ *
* @return string $path
+ * Path to the sitemap file.
*/
function _scald_sitemap_get_file_location($langcode, $format, $page = 0) {
$path = 'public://sitemaps';
@@ -68,8 +78,11 @@ function _scald_sitemap_get_file_location($langcode, $format, $page = 0) {
*/
function scald_sitemap_robotstxt() {
$robotstxt = array();
+
+ // Adding sitemaps to the robot file.
foreach (language_list() as $langcode => $language) {
$robotstxt[] = 'Sitemap: ' . url('sitemaps/video.xml', ['language' => $language]);
}
+
return $robotstxt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment