You are here

function _google_image_sitemap_build in Google Image Sitemap 6

Same name and namespace in other branches
  1. 7 google_image_sitemap.module \_google_image_sitemap_build()

Menu callback for sitemap generate.

1 string reference to '_google_image_sitemap_build'
google_image_sitemap_menu in ./google_image_sitemap.module
Implements hook_menu().

File

./google_image_sitemap.module, line 263
A module gives to Google information about images on your site.

Code

function _google_image_sitemap_build($sitemap_id) {
  if (!empty($sitemap_id) && ($sitemap_obj = _google_image_sitemap_valid_sitemap_id($sitemap_id))) {

    // Get records.
    $limit_start = $sitemap_obj->range_start - 1;
    $limit_end = $sitemap_obj->range_end;
    $result = db_query(db_rewrite_sql("SELECT n.nid, n.created, n.title, f.filepath FROM {node} n\n      INNER JOIN {upload} u ON n.nid = u.nid\n      INNER JOIN {files} f ON u.fid = f.fid\n      WHERE n.type = '%s' AND n.status = 1 AND f.filemime IN ('image/png', 'image/jpg', 'image/gif', 'image/jpeg')\n      ORDER BY n.created DESC, f.timestamp DESC LIMIT %d, %d", 'n', 'nid'), $sitemap_obj->node_type, $limit_start, $limit_end);
    while ($row = db_fetch_object($result)) {
      $nodes[] = $row;
    }

    // Create only if images are found.
    if (count($nodes) > 0) {
      $output = '<?xml version="1.0" encoding="UTF-8"?>';
      $output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
                  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
      foreach ($nodes as $node) {
        $output .= '<url><loc>' . url('node/' . $node->nid, array(
          'absolute' => TRUE,
        )) . '</loc>
                    <image:image>
                      <image:loc>' . file_create_url($node->filepath) . '</image:loc>
                      <image:title>' . check_plain($node->title) . '</image:title>
                      <image:caption>' . check_plain($node->title) . '</image:caption>
                      <image:license>' . check_url($sitemap_obj->license) . '</image:license>
                    </image:image></url>';
      }
      $output .= '</urlset>';

      // File build path.
      $path = file_create_path('google_image_sitemap');
      if (!is_dir($path)) {
        drupal_mkdir($path);
      }
      $filename = 'sitemap_' . $sitemap_obj->created . '.xml';
      if ($file = file_save_data($output, $path . '/' . $filename, FILE_EXISTS_REPLACE)) {
        db_query("UPDATE {google_image_sitemap} SET last_updated = %d WHERE sid = %d", time(), $sitemap_obj->sid);
        drupal_set_message(t("Sitemap created successfully!"));
      }
    }
    else {
      drupal_set_message(t("No Images found!"));
    }
  }
  drupal_goto(isset($_GET['destination']) ? $_GET['destination'] : GOOGLE_IMAGE_SITEMAP_ADMIN_PATH);
}