Index: xmlsitemap/xmlsitemap.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/xmlsitemap/xmlsitemap.install,v retrieving revision 1.4 diff -u -r1.4 xmlsitemap.install --- xmlsitemap/xmlsitemap.install 11 Dec 2007 22:57:45 -0000 1.4 +++ xmlsitemap/xmlsitemap.install 3 May 2008 12:01:59 -0000 @@ -32,35 +32,28 @@ } /** - * Implementation of hook_enable(). + * Implementation of hook_disable(). */ -function xmlsitemap_enable() { - global $base_path; - $xsl = file_get_contents(drupal_get_path('module', 'xmlsitemap') .'/gss/gss.xsl'); - - $css_start = strpos($xsl, '') - $css_start; - $xsl = substr_replace($xsl, $base_path . drupal_get_path('module', 'xmlsitemap') .'/gss/gss.css', $css_start, $css_length); - - $js_start = strpos($xsl, '') - $js_start; - $xsl = substr_replace($xsl, $base_path . drupal_get_path('module', 'xmlsitemap') .'/gss/gss.js', $js_start, $js_length); - - if (file_check_directory(($path = file_directory_path() .'/xmlsitemap'), FILE_CREATE_DIRECTORY)) { - file_save_data($xsl, "$path/gss.xsl", FILE_EXISTS_REPLACE); - } +function xmlsitemap_disable() { + $path = file_directory_path() .'/xmlsitemap'; + xmlsitemap_rmdir_recursive($path); } /** - * Implementation of hook_disable(). + * Recursively delete a directory's contents */ -function xmlsitemap_disable() { - $path = file_directory_path() .'/xmlsitemap'; +function xmlsitemap_rmdir_recursive($path) { if ($dir = @opendir($path)) { while (($file = readdir($dir)) !== FALSE) { - if ($file != '.' && $file != '..') { - unlink("$path/$file"); + if ($file == '.' || $file == '..') { + continue; + } + $fullpath = "$path/$file"; + if (is_dir($fullpath)) { + xmlsitemap_rmdir_recursive($fullpath); + continue; } + unlink($fullpath); } closedir($dir); rmdir($path); Index: xmlsitemap/xmlsitemap.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/xmlsitemap/xmlsitemap.module,v retrieving revision 1.16 diff -u -r1.16 xmlsitemap.module --- xmlsitemap/xmlsitemap.module 18 Dec 2007 22:54:51 -0000 1.16 +++ xmlsitemap/xmlsitemap.module 3 May 2008 12:02:04 -0000 @@ -12,7 +12,7 @@ switch ($section) { case 'admin/settings/xmlsitemap': case 'admin/settings/xmlsitemap/settings': - return t('Configure the site map. Your site map is at !url.', array('!url' => ''. xmlsitemap_url('sitemap.xml', drupal_lookup_path('alias', 'sitemap.xml') ? drupal_lookup_path('alias', 'sitemap.xml') : NULL, NULL, NULL, TRUE) .'')); + return t('Configure the site map. Your site map is at !url.', array('!url' => ''. _xmlsitemap_url_basic() .'')); case 'admin/settings/xmlsitemap/engines': return t('Configure behavior for search engines.'); case 'admin/settings/xmlsitemap/additional': @@ -61,7 +61,7 @@ 'weight' => 1, ); $items[] = array( - 'path' => 'sitemap.xml', + 'path' => _xmlsitemap_get_basic_path(), 'title' => t('Site map index'), 'callback' => '_xmlsitemap_output', 'type' => MENU_CALLBACK, @@ -72,7 +72,7 @@ $chunk_count = variable_get('xmlsitemap_chunk_count', 0); for ($chunk = 0; $chunk < $chunk_count; ++$chunk) { $items[] = array( - 'path' => "sitemap$chunk.xml", + 'path' => _xmlsitemap_get_basic_path(0, $chunk), 'title' => t('Site map !number', array('!number' => $chunk)), 'callback' => '_xmlsitemap_output', 'callback arguments' => array($chunk), @@ -317,7 +317,7 @@ * Implementation of hook_robotstxt(). */ function xmlsitemap_robotstxt() { - return array("Sitemap: ". xmlsitemap_url('sitemap.xml', drupal_lookup_path('alias', 'sitemap.xml') ? drupal_lookup_path('alias', 'sitemap.xml') : NULL, NULL, NULL, TRUE)); + return array("Sitemap: ". _xmlsitemap_url_basic()); } /** @@ -331,13 +331,13 @@ drupal_set_header('Content-type: text/xml; charset=utf-8'); global $user; $dest = file_directory_path() .'/xmlsitemap'; - file_check_directory($dest, FILE_CREATE_DIRECTORY); + _xmlsitemap_ensure_folder($dest); if (isset($chunk)) { - $dest .= "/sitemap$chunk.xml.gz"; + $dest .= '/'. _xmlsitemap_get_basic_path(1, $chunk) .'.gz'; $type = t('Site map @chunk', array('@chunk' => $chunk)); } else { - $dest .= '/sitemap.xml.gz'; + $dest .= '/'. _xmlsitemap_get_basic_path(1) .'.gz'; $link_count = _xmlsitemap_link_count(); $chunk_size = variable_get('xmlsitemap_chunk_size', 50000); $type = $link_count > $chunk_size ? t('Site map index') : t('Site map'); @@ -389,9 +389,9 @@ $current_user = $user; $user = user_load(array('uid' => 0)); $path = file_directory_path() .'/xmlsitemap'; - file_check_directory($path, FILE_CREATE_DIRECTORY); + _xmlsitemap_ensure_folder($path); $node_count = db_result(db_query("SELECT COUNT(*) FROM {node}")); - $dest = $path .'/sitemap.xml.gz'; + $dest = $path .'/'. _xmlsitemap_get_basic_path(1) .'.gz'; $link_count = _xmlsitemap_link_count(); $chunk_size = variable_get('xmlsitemap_chunk_size', 50000); $status = TRUE; @@ -401,7 +401,7 @@ $status = FALSE; } for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) { - $dest = $path ."/sitemap$chunk.xml.gz"; + $dest = $path .'/'. _xmlsitemap_get_basic_path(1, $chunk) .'.gz'; $data = gzencode(_xmlsitemap_output_chunk($chunk)); if (file_save_data($data, $dest, FILE_EXISTS_REPLACE) === 0 && ($page == $chunk || !isset($page))) { $status = FALSE; @@ -425,28 +425,53 @@ } /** + * Returns the standard site map xml + * @param $link_type: Specifies whether this is a sitemapindex or a urlset + * @param $filler: Specifies the content of the site map + */ +function _xmlsitemap_xml_text($link_type, $filler) { + switch ($link_type) { + case 'sitemapindex': + $mapname = 'siteindex'; + break; + case 'urlset': + $mapname = 'sitemap'; + break; + default: + form_set_error('xmlsitemap_invalid_type', t('In _xmlsitemap_header, link type must be sitemapindex or urlset')); + break; + } + $xsl_path = _xmlsitemap_generate_xsl(); + $output = << + +<$link_type xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/$mapname.xsd"> +$filler + +EOH; + return ($output); +} + +/** * Generate the site map index. * @param $link_count: * An integer containing the total number of links in the site * @return A string containing the site map index */ function _xmlsitemap_output_index($link_count) { - $output = ''; - global $base_path; - $xsl_path = file_directory_path() .'/xmlsitemap/gss.xsl'; - $xsl_path = file_exists($xsl_path) ? _xmlsitemap_file_create_url($xsl_path) : $base_path . drupal_get_path('module', 'xmlsitemap') .'/gss/gss.xsl'; - $output .= ''."\n"; - $output .= ''."\n"; - $output .= ''."\n"; $chunk_size = variable_get('xmlsitemap_chunk_size', 50000); for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) { - $output .= ''. xmlsitemap_url("sitemap$chunk.xml", NULL, NULL, NULL, TRUE) .''; + $loc = xmlsitemap_url(_xmlsitemap_get_basic_path(1, $chunk), NULL, NULL, NULL, TRUE); $previous = $chunk * $chunk_size; $links = array_slice(_xmlsitemap_links(), $previous, $chunk_size); - $output .= ''. gmdate('Y-m-d\TH:i:s+00:00', array_reduce($links, '_xmlsitemap_chunk_last_change')) .''; - $output .= "\n"; + $lastmod = gmdate('Y-m-d\TH:i:s+00:00', array_reduce($links, '_xmlsitemap_chunk_last_change')); + $output .= <<$loc +$lastmod + +EOH; } - $output .= ''; + $output = _xmlsitemap_xml_text('sitemapindex', $output); return $output; } @@ -472,20 +497,14 @@ * @return A string containing a chunk of the site map */ function _xmlsitemap_output_chunk($chunk) { - $output = ''; - global $base_path; if (!ini_get('safe_mode')) { set_time_limit(240); } - $xsl_path = file_directory_path() .'/xmlsitemap/gss.xsl'; - $xsl_path = file_exists($xsl_path) ? _xmlsitemap_file_create_url($xsl_path) : $base_path . drupal_get_path('module', 'xmlsitemap') .'/gss/gss.xsl'; - $output .= ''."\n"; - $output .= ''."\n"; - $output .= ''."\n"; + $chunk_size = variable_get('xmlsitemap_chunk_size', 50000); $previous = $chunk * $chunk_size; - $output .= implode("\n", _xmlsitemap_format(array_slice(_xmlsitemap_links(), $previous, $chunk_size))); - $output .= "\n"; + $output = implode("\n", _xmlsitemap_format(array_slice(_xmlsitemap_links(), $previous, $chunk_size))); + $output = _xmlsitemap_xml_text('urlset', $output); return $output; } @@ -509,7 +528,7 @@ * Implementation of hook_file_download(). */ function xmlsitemap_file_download($file) { - if ($file == 'xmlsitemap/gss.xsl') { + if (preg_match("/xmlsitemap\/[^\/]\/gss.xsl/", $file)) { return array('Content-Type:'); } } @@ -737,6 +756,50 @@ } /** + * Gets the basic URL, including the sitemap path + */ +function _xmlsitemap_url_basic() { + $path = _xmlsitemap_get_basic_path(); + $alias = drupal_lookup_path('alias', $path); + return xmlsitemap_url($path, ($alias ? $alias : NULL), NULL, NULL, TRUE); +} + +/** + * Gets the basic path for the sitemap file + * @param $domain: A boolean indicating whether we want the domain inserted into the path. Defaults to 0. + * @param $chunk: The chunk. Defaults to ''. + * @returns The path to the sitemap. + */ +function _xmlsitemap_get_basic_path($domain = 0, $chunk = '') { + $thispath = "sitemap$chunk.xml"; + if ($domain) { + $proto = $_SERVER['HTTPS'] ? 'https' : 'http'; + $thispath = $_SERVER['HTTP_HOST'] ."/$proto/". $_SERVER['SERVER_PORT'] ."/$thispath"; + } + return ($thispath); +} + +/** + * Ensures that the folder exists, and contains a folder with the name + * @param $path: The path to the folder + */ +function _xmlsitemap_ensure_folder($path) { + $host = $_SERVER['HTTP_HOST']; + $proto = $_SERVER['HTTPS'] ? 'https' : 'http'; + $port = $_SERVER['SERVER_PORT']; + if (! file_check_directory($path, FILE_CREATE_DIRECTORY)) { + return 0; + } + if (! file_check_directory(($fullpath = "$path/$host"), FILE_CREATE_DIRECTORY)) { + return 0; + } + if (! file_check_directory(($fullpath = "$fullpath/$proto"), FILE_CREATE_DIRECTORY)) { + return 0; + } + return (file_check_directory(($fullpath = "$fullpath/$port"), FILE_CREATE_DIRECTORY)); +} + +/** * Modified version of drupal_get_path_alias() for xmlsitemap_url(). * @param $path: An internal Drupal path * @param $alias: The URL alias. Default is NULL. @@ -811,3 +874,44 @@ } } +/** + * Generates gss.xsl + * @param $overwrite: Whether to overwrite an existing file -- defaults to 0 (No) + */ +function _xmlsitemap_generate_xsl($overwrite = 0) { + $dest_file = file_directory_path() .'/xmlsitemap'; + if (! _xmlsitemap_ensure_folder($dest_file)) { + form_set_error('xmlsitemap_no_folder', t("Can't create folder: ") . $dest_file); + } + $proto = $_SERVER['HTTPS'] ? 'https' : 'http'; + $dest_file .= '/'. $_SERVER['HTTP_HOST'] ."/$proto/". $_SERVER['SERVER_PORT'] .'/gss.xsl'; + if (file_exists($dest_file) && ! $overwrite) { + return ($dest_file); + } + + global $base_path; + + $source_path = drupal_get_path('module', 'xmlsitemap') .'/gss'; + $url_source_path = $base_path . $source_path; + + $xsl = file_get_contents("$source_path/gss.xsl"); + + // Have replaced the code below with one preg_replace call +// $css_start = strpos($xsl, '') - $css_start; +// $xsl = substr_replace($xsl, $base_path . drupal_get_path('module', 'xmlsitemap') .'/gss/gss.css', $css_start, $css_length); + +// $js_start = strpos($xsl, '') - $js_start; +// $xsl = substr_replace($xsl, $base_path . drupal_get_path('module', 'xmlsitemap') .'/gss/gss.js', $js_start, $js_length); + + $xsl = preg_replace( + array('/()/', '/(