3 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
4 define('LACONICA', true);
6 require_once(INSTALLDIR . '/lib/common.php');
7 require_once(INSTALLDIR . '/lib/util.php');
9 $output_paths = parse_args();
16 # ------------------------------------------------------------------------------
17 # Main functions: get data out and turn them into sitemaps
18 # ------------------------------------------------------------------------------
20 # Generate index sitemap of all other sitemaps.
24 $output_dir = $output_paths['output_dir'];
25 $output_url = $output_paths['output_url'];
27 foreach (glob("$output_dir*.xml") as $file_name) {
29 # Just the file name please.
30 $file_name = preg_replace("|$output_dir|", '', $file_name);
32 $index_urls .= sitemap(
34 'url' => $output_url . $file_name,
35 'changefreq' => 'daily'
40 write_file($output_paths['index_file'], sitemapindex($index_urls));
43 # Generate sitemap of standard site elements.
44 function standard_map()
48 $standard_map_urls .= url(
50 'url' => common_local_url('public'),
51 'changefreq' => 'daily',
56 $standard_map_urls .= url(
58 'url' => common_local_url('publicrss'),
59 'changefreq' => 'daily',
64 $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog',
65 'privacy', 'source', 'badge');
67 foreach($docs as $title) {
68 $standard_map_urls .= url(
70 'url' => common_local_url('doc', array('title' => $title)),
71 'changefreq' => 'monthly',
77 $urlset_path = $output_paths['output_dir'] . 'standard.xml';
79 write_file($urlset_path, urlset($standard_map_urls));
82 # Generate sitemaps of all notices.
83 function notices_map()
87 $notices = DB_DataObject::factory('notice');
89 $notices->query('SELECT id, uri, url, modified FROM notice where is_local = 1');
94 while ($notices->fetch()) {
96 # Maximum 50,000 URLs per sitemap file.
97 if ($notice_count == 50000) {
102 # remote notices have an URL
104 if (!$notices->url && $notices->uri) {
106 'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
107 'lastmod' => common_date_w3dtf($notices->modified),
108 'changefreq' => 'never',
112 $notice_list[$map_count] .= url($notice);
117 # Make full sitemaps from the lists and save them.
118 array_to_map($notice_list, 'notice');
121 # Generate sitemaps of all users.
124 global $output_paths;
126 $users = DB_DataObject::factory('user');
128 $users->query('SELECT id, nickname FROM user');
133 while ($users->fetch()) {
135 # Maximum 50,000 URLs per sitemap file.
136 if ($user_count == 50000) {
141 $user_args = array('nickname' => $users->nickname);
143 # Define parameters for generating <url></url> elements.
145 'url' => common_local_url('showstream', $user_args),
146 'changefreq' => 'daily',
151 'url' => common_local_url('userrss', $user_args),
152 'changefreq' => 'daily',
157 'url' => common_local_url('all', $user_args),
158 'changefreq' => 'daily',
163 'url' => common_local_url('allrss', $user_args),
164 'changefreq' => 'daily',
169 'url' => common_local_url('replies', $user_args),
170 'changefreq' => 'daily',
174 $replies_rss = array(
175 'url' => common_local_url('repliesrss', $user_args),
176 'changefreq' => 'daily',
181 'url' => common_local_url('foaf', $user_args),
182 'changefreq' => 'weekly',
186 # Construct a <url></url> element for each user facet and add it
187 # to our existing list of those.
188 $user_list[$map_count] .= url($user);
189 $user_rss_list[$map_count] .= url($user_rss);
190 $all_list[$map_count] .= url($all);
191 $all_rss_list[$map_count] .= url($all_rss);
192 $replies_list[$map_count] .= url($replies);
193 $replies_rss_list[$map_count] .= url($replies_rss);
194 $foaf_list[$map_count] .= url($foaf);
199 # Make full sitemaps from the lists and save them.
200 # Possible factoring: put all the lists into a master array, thus allowing
201 # calling with single argument (i.e., array_to_map('user')).
202 array_to_map($user_list, 'user');
203 array_to_map($user_rss_list, 'user_rss');
204 array_to_map($all_list, 'all');
205 array_to_map($all_rss_list, 'all_rss');
206 array_to_map($replies_list, 'replies');
207 array_to_map($replies_rss_list, 'replies_rss');
208 array_to_map($foaf_list, 'foaf');
211 # ------------------------------------------------------------------------------
212 # XML generation functions
213 # ------------------------------------------------------------------------------
215 # Generate a <url></url> element.
216 function url($url_args)
218 $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
219 $lastmod = $url_args['lastmod'];
220 $changefreq = $url_args['changefreq'];
221 $priority = $url_args['priority'];
224 error("url() arguments require 'url' value.");
227 $url_out = "\t<url>\n";
228 $url_out .= "\t\t<loc>$url</loc>\n";
231 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
235 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
239 $url_out .= "\t\t<priority>$priority</priority>\n";
242 $url_out .= "\t</url>\n";
247 function sitemap($sitemap_args)
249 $url = preg_replace('/&/', '&', $sitemap_args['url']); # escape ampersands for XML
250 $lastmod = $sitemap_args['lastmod'];
253 error("url() arguments require 'url' value.");
256 $sitemap_out = "\t<sitemap>\n";
257 $sitemap_out .= "\t\t<loc>$url</loc>\n";
260 $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
263 $sitemap_out .= "\t</sitemap>\n";
268 # Generate a <urlset></urlset> element.
269 function urlset($urlset_text)
271 $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
272 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
279 # Generate a <urlset></urlset> element.
280 function sitemapindex($sitemapindex_text)
282 $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
283 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
287 return $sitemapindex;
290 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
291 function array_to_map($url_list, $filename_prefix)
293 global $output_paths;
296 # $map_urls is a long string containing concatenated <url></url> elements.
297 while (list($map_idx, $map_urls) = each($url_list)) {
298 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
300 write_file($urlset_path, urlset($map_urls));
305 # ------------------------------------------------------------------------------
307 # ------------------------------------------------------------------------------
309 # Parse command line arguments.
310 function parse_args()
312 $args = getopt('f:d:u:');
314 if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
315 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
318 if (is_null($args[f])) {
319 error('You must specify an index file name with the -f option.');
322 if (is_null($args[d])) {
323 error('You must specify a directory for the output file with the -d option.');
326 if (is_null($args[u])) {
327 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
330 $index_file = $args[f];
331 $output_dir = $args[d];
332 $output_url = $args[u];
334 if (file_exists($output_dir)) {
335 if (is_writable($output_dir) === false) {
336 error("$output_dir is not writable.");
339 error("output directory $output_dir does not exist.");
343 'index_file' => $index_file,
344 'output_dir' => trailing_slash($output_dir),
345 'output_url' => trailing_slash($output_url),
351 # Ensure paths end with a "/".
352 function trailing_slash($path)
354 if (preg_match('/\/$/', $path) == 0) {
361 # Write data to disk.
362 function write_file($path, $data)
364 if (is_null($path)) {
365 error('No path specified for writing to.');
366 } elseif (is_null($data)) {
367 error('No data specified for writing.');
370 if (($fh_out = fopen($path,'w')) === false) {
371 error("couldn't open $path for writing.");
374 if (fwrite($fh_out, $data) === false) {
375 error("couldn't write to $path.");
379 # Display an error message and exit.
380 function error ($error_msg)
382 if (is_null($error_msg)) {
383 $error_msg = 'error() was called without any explanation!';
386 echo "Error: $error_msg\n";