I have used the xmlsitemap module to generate the organic search xmlsitemap and i extended the module using hook_xmlsitemap_element_alter for adding the image and video tags. I am having the node id info and i need to load the node for getting the attached image and video references. To get the details of the image and videos, i need to load the reference ids again. My site is having more than 10k nodes and xmlsitemap is throwing the time out error. I found the problem is due to the heavy node load operations.

But my question if we are running this operation in a batch, it should not fail. I have seen that the following lines of code in a while loop in xmlsitemap.generate.inc

Code at line number 232.

drupal_alter('xmlsitemap_element', $element, $link, $sitemap);
  $writer->writeSitemapElement('url', $element);  

Should above lines of code go in a batch?

My code

function mymodue_xmlsitemap_xmlsitemap_element_alter(array &$element, array $link, $sitemap) {
  if ($link['subtype'] == 'article') {
    mymodule_xmlsitemap_get_tags($element, $link);
  }
}

 */
function mymodule_xmlsitemap_get_tags(&$tag_info, $result) {
  $node = node_load($result['id']);
  $image_ref = $node->field_image['und'][0]['target_id'];
  $video_ref = node->field_video['und'][0]['target_id'];
  $image_node = node_load($image_ref); 
 /*
code goes here
*/

}

Please give me suggestions.

Comments

vishnu358’s picture

Issue summary: View changes
vishnu358’s picture

Issue summary: View changes
osopolar’s picture

Status: Active » Needs review
StatusFileSize
new1.49 KB

I also got that error message while running drush xmlsitemap-rebuild on command-line where max execution time normally is not limited (set to 0). I changed the max execution time code to respect the cases where max_execution_time is 0 and also the case where max_execution_time > 240 (to not decrease time limit).

This is not the solution expected by vishnu358 but it might help for these cases, if a command line is available. a different approach would be to use a variable for time limit and make it configurable in xmlsitemaps admin settings.

I also run into a memory exhaustion after rebuilding about 45000 items, i didn't expect that neither.

vishnu358’s picture

Hi @osopolar,

Thanks very much for your time.I found one solution. Instead of loading the nodes in hook_xmsitemap_element_alter, we can alter the xmlsitemap query by hook_xmlsitemap_query_xmlsitemap_generate_alter(). I have queried all the required fields through JOINS. This method is very fast compared to more than thousands of node loads.

vishnu358’s picture

Status: Needs review » Closed (fixed)
osopolar’s picture

I am glad it worked for you, but was this actually fixed?

In my case, the module sets a max execution time on command line, where it is not necessary as by default there is no limit for execution time on command line.

Please reopen the issue, so it could be fixed properly.

dave reid’s picture

@osopolar: According to drupal_set_time_limit() it already has that protection, so this wouldn't be necessary. Are you using an older version of Drupal 7?

dave reid’s picture

osopolar’s picture

Luckily I'm not using an old version, I just didn't test if the issue exists. I just came across this issue while checking if the patch is still required after updating xmlsitemap. I thought it was not solved as the patch was not committed, but as I can see now, it was, just somewhere else. Thanks Dave for pointing that out.