1)); // restore error reporting to its normal setting error_reporting(E_ALL); //main run function sitemap_run($argv); /** * Main function responsible for building the sitemap * @param $argv * @return unknown_type */ function sitemap_run($argv) { // sitemap_init_drupal($argv); //initialize stuff that drupal needs, otherwise this will not run $chunk_size = isset($argv[3]) ? $argv[3] : 5000; //default to 5000 if nothing is passed in $sitemap_directory = $argv[2].'/sitemaps'; $urls = array(); $urls = array_merge(sitemap_links(), $urls); $urls = array_merge(sitemap_get_nodes(), $urls); $urls = array_merge(sitemap_get_profiles(), $urls); if (count($urls) > 0 && count($urls) > $chunk_size) { //keep a count of the chunks so i can write out the smaller sitemap files //also used to write out the sitemap index file $cnt = 0; $urls = array_chunk($urls, $chunk_size); foreach ($urls as $chunk) { //each chunk contains 5k, so write this out as a file, sitemap_write_sitemap_file($chunk, "$sitemap_directory/sitemap$cnt.xml"); $cnt++; } if($cnt > 0) { sitemap_write_sitemap_index($cnt, "$sitemap_directory/sitemap.xml"); } } else { //no chunking, just write out all the urls into one file sitemap_write_sitemap_file($urls, "$sitemap_directory/sitemap.xml"); } } function sitemap_write_sitemap_index($num, $file) { if ($num == 0) { echo 'No sitemaps were generated. Skipping index file'; return; } //support last mod $content .= ''."\n"; $content .= ''."\n"; $content .= ' '."\n"; } $content .= ''; if (!$fp = fopen($file, 'wb')) { echo("The file $file could not be created."); return 0; } fwrite($fp, $content); fclose($fp); } /** * Write the urls into a file * @param $urls * @param $file * @param $gzip * @return unknown_type */ function sitemap_write_sitemap_file($urls, $file, $gzip = FALSE) { $content = ''."\n"; $content .= 'alias) .''."\n"; //support lastmod for nodes if ($url->type == 'node') { $content .= ' ' . gmdate('Y-m-d\TH:i:s+00:00', $url->changed) . ''."\n"; } $content .= ' '."\n"; } $content .= ''; if (!$fp = fopen($file, 'wb')) { echo("The file $file could not be created."); return 0; } fwrite($fp, $content); fclose($fp); } /** * Helper function to generate a url * @param $url * @return unknown_type */ function sitemap_url($url) { if (!empty($url)) { if($url == '') { //generate the homepage url $path = 'http://' . $_SERVER['HTTP_HOST']; } else { $path = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $url; } } return $path; } /** * Get all the nodes in the DB * @return unknown_type */ function sitemap_get_nodes() { $result = db_query("SELECT n.nid, n.changed, n.created from {node} n where n.status > 0 and n.type IN ('news', 'stock', 'stockvote', 'page', 'slideshow', 'blog') and n.nid <> 0"); while ($node = db_fetch_object($result)) { $alias = db_result(db_query("SELECT dst FROM {url_alias} where src = '%s'", "node/$node->nid")); $node->alias = $alias === FALSE ? "node/$node->nid" : $alias; $node->type = 'node'; $nodes[] = $node; } return $nodes; } /** * Get users profile links * @return unknown_type */ function sitemap_get_profiles() { $result = db_query("SELECT u.uid from {users} u where status = 1 and u.uid <> 0"); while ($user = db_fetch_object($result)) { $alias = db_result(db_query("SELECT dst FROM {url_alias} where src = '%s'", "user/$user->uid")); $user->alias = $alias === FALSE ? "user/$user->uid" : $alias; $user->type = 'user'; $users[] = $user; } return $users; } /** * Get the navigation elements of the site * @return unknown_type */ function sitemap_links() { $menu = menu_get_menu(); $root_menus = array(); foreach ($menu['items'][0]['children'] as $mid) { $root_menus[$mid] = $menu['items'][$mid]['title']; } //TODO do not hard-code the menus $sitemap_menus = array(2, 115, 270, 277); $menu_array = array(); foreach ($sitemap_menus as $mid) { if (isset($root_menus[$mid])) { $menu_array = array_merge(sitemap_menu_process_link($mid, $menu), $menu_array); } } return $menu_array; } /** * Recusively get menu items and links if available * @param $mid * @param $menu * @param $alias_array * @return unknown_type */ function sitemap_menu_process_link($mid, $menu, $alias_array = array()) { $item = $menu['visible'][$mid]; if (!empty($item['path']) && strpos($item['path'], '://') === FALSE && $item['path'] != variable_get('site_frontpage', 'node')) { $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $item['path'])); $link = new stdClass; $link->alias = $alias === FALSE ? $item['path'] : $alias; $link->type = 'menu'; $alias_array[] = $link; } foreach ($item['children'] as $mid) { $alias_array = array_merge(sitemap_menu_process_link($mid, $menu, $alias_array)); } return $alias_array; }