X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fsitemap.php;h=f8c3921465bdaa1cba98bc265c594c919205c69b;hb=122c8677b7004bae4cfe7e2bd49fc1bc3187c72c;hp=b49bfe2a5f60bfaa47d8b894bf707092520e1338;hpb=edbc0c665cc65875b4d14b79939233b1c9c06bb6;p=quix0rs-gnu-social.git diff --git a/scripts/sitemap.php b/scripts/sitemap.php old mode 100644 new mode 100755 index b49bfe2a5f..f8c3921465 --- a/scripts/sitemap.php +++ b/scripts/sitemap.php @@ -1,10 +1,37 @@ +#!/usr/bin/env php . + */ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); -define('LACONICA', true); -require_once(INSTALLDIR . '/lib/common.php'); -require_once(INSTALLDIR . '/lib/util.php'); +$shortoptions = 'f:d:u:'; + +$helptext = << Use as output file + -d Use for new sitemaps + -u Use as root for URLs + +END_OF_SITEMAP_HELP; + +require_once INSTALLDIR . '/scripts/commandline.inc'; $output_paths = parse_args(); @@ -13,19 +40,20 @@ notices_map(); user_map(); index_map(); -# ------------------------------------------------------------------------------ -# Main functions: get data out and turn them into sitemaps -# ------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ +// Main functions: get data out and turn them into sitemaps +// ------------------------------------------------------------------------------ -# Generate index sitemap of all other sitemaps. -function index_map() { +// Generate index sitemap of all other sitemaps. +function index_map() +{ global $output_paths; $output_dir = $output_paths['output_dir']; $output_url = $output_paths['output_url']; foreach (glob("$output_dir*.xml") as $file_name) { - # Just the file name please. + // Just the file name please. $file_name = preg_replace("|$output_dir|", '', $file_name); $index_urls .= sitemap( @@ -39,8 +67,9 @@ function index_map() { write_file($output_paths['index_file'], sitemapindex($index_urls)); } -# Generate sitemap of standard site elements. -function standard_map() { +// Generate sitemap of standard site elements. +function standard_map() +{ global $output_paths; $standard_map_urls .= url( @@ -59,7 +88,8 @@ function standard_map() { ) ); - $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source'); + $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', + 'privacy', 'source', 'badge'); foreach($docs as $title) { $standard_map_urls .= url( @@ -76,8 +106,9 @@ function standard_map() { write_file($urlset_path, urlset($standard_map_urls)); } -# Generate sitemaps of all notices. -function notices_map() { +// Generate sitemaps of all notices. +function notices_map() +{ global $output_paths; $notices = DB_DataObject::factory('notice'); @@ -89,14 +120,14 @@ function notices_map() { while ($notices->fetch()) { - # Maximum 50,000 URLs per sitemap file. + // Maximum 50,000 URLs per sitemap file. if ($notice_count == 50000) { $notice_count = 0; $map_count++; } - # remote notices have an URL - + // remote notices have an URL + if (!$notices->url && $notices->uri) { $notice = array( 'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)), @@ -110,12 +141,13 @@ function notices_map() { } } - # Make full sitemaps from the lists and save them. + // Make full sitemaps from the lists and save them. array_to_map($notice_list, 'notice'); } -# Generate sitemaps of all users. -function user_map() { +// Generate sitemaps of all users. +function user_map() +{ global $output_paths; $users = DB_DataObject::factory('user'); @@ -127,7 +159,7 @@ function user_map() { while ($users->fetch()) { - # Maximum 50,000 URLs per sitemap file. + // Maximum 50,000 URLs per sitemap file. if ($user_count == 50000) { $user_count = 0; $map_count++; @@ -135,7 +167,7 @@ function user_map() { $user_args = array('nickname' => $users->nickname); - # Define parameters for generating elements. + // Define parameters for generating elements. $user = array( 'url' => common_local_url('showstream', $user_args), 'changefreq' => 'daily', @@ -178,8 +210,8 @@ function user_map() { 'priority' => '0.5', ); - # Construct a element for each user facet and add it - # to our existing list of those. + // Construct a element for each user facet and add it + // to our existing list of those. $user_list[$map_count] .= url($user); $user_rss_list[$map_count] .= url($user_rss); $all_list[$map_count] .= url($all); @@ -191,9 +223,9 @@ function user_map() { $user_count++; } - # Make full sitemaps from the lists and save them. - # Possible factoring: put all the lists into a master array, thus allowing - # calling with single argument (i.e., array_to_map('user')). + // Make full sitemaps from the lists and save them. + // Possible factoring: put all the lists into a master array, thus allowing + // calling with single argument (i.e., array_to_map('user')). array_to_map($user_list, 'user'); array_to_map($user_rss_list, 'user_rss'); array_to_map($all_list, 'all'); @@ -203,13 +235,14 @@ function user_map() { array_to_map($foaf_list, 'foaf'); } -# ------------------------------------------------------------------------------ -# XML generation functions -# ------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ +// XML generation functions +// ------------------------------------------------------------------------------ -# Generate a element. -function url($url_args) { - $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML +// Generate a element. +function url($url_args) +{ + $url = preg_replace('/&/', '&', $url_args['url']); // escape ampersands for XML $lastmod = $url_args['lastmod']; $changefreq = $url_args['changefreq']; $priority = $url_args['priority']; @@ -238,8 +271,9 @@ function url($url_args) { return $url_out; } -function sitemap($sitemap_args) { - $url = preg_replace('/&/', '&', $sitemap_args['url']); # escape ampersands for XML +function sitemap($sitemap_args) +{ + $url = preg_replace('/&/', '&', $sitemap_args['url']); // escape ampersands for XML $lastmod = $sitemap_args['lastmod']; if (is_null($url)) { @@ -258,8 +292,9 @@ function sitemap($sitemap_args) { return $sitemap_out; } -# Generate a element. -function urlset($urlset_text) { +// Generate a element. +function urlset($urlset_text) +{ $urlset = '' . "\n" . '' . "\n" . $urlset_text . @@ -268,8 +303,9 @@ function urlset($urlset_text) { return $urlset; } -# Generate a element. -function sitemapindex($sitemapindex_text) { +// Generate a element. +function sitemapindex($sitemapindex_text) +{ $sitemapindex = '' . "\n" . '' . "\n" . $sitemapindex_text . @@ -278,50 +314,34 @@ function sitemapindex($sitemapindex_text) { return $sitemapindex; } -# Generate a sitemap from an array containing elements and write it to a file. -function array_to_map($url_list, $filename_prefix) { +// Generate a sitemap from an array containing elements and write it to a file. +function array_to_map($url_list, $filename_prefix) +{ global $output_paths; if ($url_list) { - # $map_urls is a long string containing concatenated elements. + // $map_urls is a long string containing concatenated elements. while (list($map_idx, $map_urls) = each($url_list)) { $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml"; - + write_file($urlset_path, urlset($map_urls)); } } } -# ------------------------------------------------------------------------------ -# Internal functions -# ------------------------------------------------------------------------------ - -# Parse command line arguments. -function parse_args() { - $args = getopt('f:d:u:'); - - if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) { - error('Mandatory arguments: -f -d -u '); - } - - if (is_null($args[f])) { - error('You must specify an index file name with the -f option.'); - } - - if (is_null($args[d])) { - error('You must specify a directory for the output file with the -d option.'); - } - - if (is_null($args[u])) { - error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.'); - } +// ------------------------------------------------------------------------------ +// Internal functions +// ------------------------------------------------------------------------------ - $index_file = $args[f]; - $output_dir = $args[d]; - $output_url = $args[u]; +// Parse command line arguments. +function parse_args() +{ + $index_file = get_option_value('f'); + $output_dir = get_option_value('d'); + $output_url = get_option_value('u'); if (file_exists($output_dir)) { - if (is_writable($output_dir) === FALSE) { + if (is_writable($output_dir) === false) { error("$output_dir is not writable."); } } else { @@ -337,8 +357,9 @@ function parse_args() { return $paths; } -# Ensure paths end with a "/". -function trailing_slash($path) { +// Ensure paths end with a "/". +function trailing_slash($path) +{ if (preg_match('/\/$/', $path) == 0) { $path .= '/'; } @@ -346,25 +367,27 @@ function trailing_slash($path) { return $path; } -# Write data to disk. -function write_file($path, $data) { +// Write data to disk. +function write_file($path, $data) +{ if (is_null($path)) { error('No path specified for writing to.'); } elseif (is_null($data)) { error('No data specified for writing.'); } - if (($fh_out = fopen($path,'w')) === FALSE) { + if (($fh_out = fopen($path,'w')) === false) { error("couldn't open $path for writing."); } - if (fwrite($fh_out, $data) === FALSE) { + if (fwrite($fh_out, $data) === false) { error("couldn't write to $path."); } } -# Display an error message and exit. -function error ($error_msg) { +// Display an error message and exit. +function error ($error_msg) +{ if (is_null($error_msg)) { $error_msg = 'error() was called without any explanation!'; }