function _google_image_sitemap_build in Google Image Sitemap 7
Same name and namespace in other branches
- 6 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 275 - 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.
$query = db_select('node', 'n');
$query
->fields('n', array(
'nid',
'created',
'title',
));
$query
->innerJoin('file_usage', 'fu', "n.nid = fu.id");
$query
->innerJoin('file_managed', 'f', "fu.fid = f.fid");
$query
->fields('f', array(
'uri',
));
// Use a 'node_access' tag with a query to find out how many this user has
// access to. This will be the standard way to make lists while respecting
// node access restrictions.
$query
->addTag('node_access');
$query
->condition('n.type', $sitemap_obj->node_type);
$query
->condition('n.status', NODE_PUBLISHED);
$query
->condition('f.filemime', array(
'image/png',
'image/jpg',
'image/gif',
'image/jpeg',
), 'IN');
$query
->orderBy('n.created', 'DESC');
$query
->orderBy('f.timestamp', 'DESC');
$query
->range($sitemap_obj->range_start - 1, $sitemap_obj->range_end);
$nodes = $query
->execute()
->fetchAll();
// 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->uri) . '</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_build_uri('google_image_sitemap');
$path = variable_get('file_public_path', 'sites/default/files') . '/google_image_sitemap';
if (!is_dir($path)) {
drupal_mkdir($path);
}
$filename = 'sitemap_' . $sitemap_obj->created . '.xml';
if ($file = file_unmanaged_save_data($output, $path . '/' . $filename, FILE_EXISTS_REPLACE)) {
db_update('google_image_sitemap')
->fields(array(
'last_updated' => REQUEST_TIME,
))
->condition('sid', $sitemap_obj->sid, '=')
->execute();
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);
}