4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
23 $shortoptions = 'f:d:u:';
25 $helptext = <<<END_OF_SITEMAP_HELP
26 Script for creating sitemaps files per http://sitemaps.org/
28 -f <indexfile> Use <indexfile> as output file
29 -d <outputdir> Use <outputdir> for new sitemaps
30 -u <outputurl> Use <outputurl> as root for URLs
34 require_once INSTALLDIR . '/scripts/commandline.inc';
36 $output_paths = parse_args();
43 // ------------------------------------------------------------------------------
44 // Main functions: get data out and turn them into sitemaps
45 // ------------------------------------------------------------------------------
47 // Generate index sitemap of all other sitemaps.
51 $output_dir = $output_paths['output_dir'];
52 $output_url = $output_paths['output_url'];
54 foreach (glob("$output_dir*.xml") as $file_name) {
56 // Just the file name please.
57 $file_name = preg_replace("|$output_dir|", '', $file_name);
59 $index_urls .= sitemap(
61 'url' => $output_url . $file_name,
62 'changefreq' => 'daily'
67 write_file($output_paths['index_file'], sitemapindex($index_urls));
70 // Generate sitemap of standard site elements.
71 function standard_map()
75 $standard_map_urls .= url(
77 'url' => common_local_url('public'),
78 'changefreq' => 'daily',
83 $standard_map_urls .= url(
85 'url' => common_local_url('publicrss'),
86 'changefreq' => 'daily',
91 $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog',
92 'privacy', 'source', 'badge');
94 foreach($docs as $title) {
95 $standard_map_urls .= url(
97 'url' => common_local_url('doc', array('title' => $title)),
98 'changefreq' => 'monthly',
104 $urlset_path = $output_paths['output_dir'] . 'standard.xml';
106 write_file($urlset_path, urlset($standard_map_urls));
109 // Generate sitemaps of all notices.
110 function notices_map()
112 global $output_paths;
114 $notices = DB_DataObject::factory('notice');
116 $notices->query('SELECT id, uri, url, modified FROM notice where is_local = 1');
121 while ($notices->fetch()) {
123 // Maximum 50,000 URLs per sitemap file.
124 if ($notice_count == 50000) {
129 // remote notices have an URL
131 if (!$notices->url && $notices->uri) {
133 'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
134 'lastmod' => common_date_w3dtf($notices->modified),
135 'changefreq' => 'never',
139 $notice_list[$map_count] .= url($notice);
144 // Make full sitemaps from the lists and save them.
145 array_to_map($notice_list, 'notice');
148 // Generate sitemaps of all users.
151 global $output_paths;
153 $users = DB_DataObject::factory('user');
155 $users->query('SELECT id, nickname FROM user');
160 while ($users->fetch()) {
162 // Maximum 50,000 URLs per sitemap file.
163 if ($user_count == 50000) {
168 $user_args = array('nickname' => $users->nickname);
170 // Define parameters for generating <url></url> elements.
172 'url' => common_local_url('showstream', $user_args),
173 'changefreq' => 'daily',
178 'url' => common_local_url('userrss', $user_args),
179 'changefreq' => 'daily',
184 'url' => common_local_url('all', $user_args),
185 'changefreq' => 'daily',
190 'url' => common_local_url('allrss', $user_args),
191 'changefreq' => 'daily',
196 'url' => common_local_url('replies', $user_args),
197 'changefreq' => 'daily',
201 $replies_rss = array(
202 'url' => common_local_url('repliesrss', $user_args),
203 'changefreq' => 'daily',
208 'url' => common_local_url('foaf', $user_args),
209 'changefreq' => 'weekly',
213 // Construct a <url></url> element for each user facet and add it
214 // to our existing list of those.
215 $user_list[$map_count] .= url($user);
216 $user_rss_list[$map_count] .= url($user_rss);
217 $all_list[$map_count] .= url($all);
218 $all_rss_list[$map_count] .= url($all_rss);
219 $replies_list[$map_count] .= url($replies);
220 $replies_rss_list[$map_count] .= url($replies_rss);
221 $foaf_list[$map_count] .= url($foaf);
226 // Make full sitemaps from the lists and save them.
227 // Possible factoring: put all the lists into a master array, thus allowing
228 // calling with single argument (i.e., array_to_map('user')).
229 array_to_map($user_list, 'user');
230 array_to_map($user_rss_list, 'user_rss');
231 array_to_map($all_list, 'all');
232 array_to_map($all_rss_list, 'all_rss');
233 array_to_map($replies_list, 'replies');
234 array_to_map($replies_rss_list, 'replies_rss');
235 array_to_map($foaf_list, 'foaf');
238 // ------------------------------------------------------------------------------
239 // XML generation functions
240 // ------------------------------------------------------------------------------
242 // Generate a <url></url> element.
243 function url($url_args)
245 $url = preg_replace('/&/', '&', $url_args['url']); // escape ampersands for XML
246 $lastmod = $url_args['lastmod'];
247 $changefreq = $url_args['changefreq'];
248 $priority = $url_args['priority'];
251 error("url() arguments require 'url' value.");
254 $url_out = "\t<url>\n";
255 $url_out .= "\t\t<loc>$url</loc>\n";
258 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
262 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
266 $url_out .= "\t\t<priority>$priority</priority>\n";
269 $url_out .= "\t</url>\n";
274 function sitemap($sitemap_args)
276 $url = preg_replace('/&/', '&', $sitemap_args['url']); // escape ampersands for XML
277 $lastmod = $sitemap_args['lastmod'];
280 error("url() arguments require 'url' value.");
283 $sitemap_out = "\t<sitemap>\n";
284 $sitemap_out .= "\t\t<loc>$url</loc>\n";
287 $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
290 $sitemap_out .= "\t</sitemap>\n";
295 // Generate a <urlset></urlset> element.
296 function urlset($urlset_text)
298 $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
299 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
306 // Generate a <urlset></urlset> element.
307 function sitemapindex($sitemapindex_text)
309 $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
310 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
314 return $sitemapindex;
317 // Generate a sitemap from an array containing <url></url> elements and write it to a file.
318 function array_to_map($url_list, $filename_prefix)
320 global $output_paths;
323 // $map_urls is a long string containing concatenated <url></url> elements.
324 while (list($map_idx, $map_urls) = each($url_list)) {
325 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
327 write_file($urlset_path, urlset($map_urls));
332 // ------------------------------------------------------------------------------
333 // Internal functions
334 // ------------------------------------------------------------------------------
336 // Parse command line arguments.
337 function parse_args()
339 $index_file = get_option_value('f');
340 $output_dir = get_option_value('d');
341 $output_url = get_option_value('u');
343 if (file_exists($output_dir)) {
344 if (is_writable($output_dir) === false) {
345 error("$output_dir is not writable.");
348 error("output directory $output_dir does not exist.");
352 'index_file' => $index_file,
353 'output_dir' => trailing_slash($output_dir),
354 'output_url' => trailing_slash($output_url),
360 // Ensure paths end with a "/".
361 function trailing_slash($path)
363 if (preg_match('/\/$/', $path) == 0) {
370 // Write data to disk.
371 function write_file($path, $data)
373 if (is_null($path)) {
374 error('No path specified for writing to.');
375 } elseif (is_null($data)) {
376 error('No data specified for writing.');
379 if (($fh_out = fopen($path,'w')) === false) {
380 error("Could not open $path for writing.");
383 if (fwrite($fh_out, $data) === false) {
384 error("Could not write to $path.");
388 // Display an error message and exit.
389 function error ($error_msg)
391 if (is_null($error_msg)) {
392 $error_msg = 'error() was called without any explanation!';
395 echo "Error: $error_msg\n";