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.
21 function index_map() {
23 $output_dir = $output_paths['output_dir'];
24 $output_url = $output_paths['output_url'];
26 foreach (glob("$output_dir*.xml") as $file_name) {
28 # Just the file name please.
29 $file_name = preg_replace("|$output_dir|", '', $file_name);
31 $index_urls .= sitemap(
33 'url' => $output_url . $file_name,
34 'changefreq' => 'daily'
39 write_file($output_paths['index_file'], sitemapindex($index_urls));
42 # Generate sitemap of standard site elements.
43 function standard_map() {
46 $standard_map_urls .= url(
48 'url' => common_local_url('public'),
49 'changefreq' => 'daily',
54 $standard_map_urls .= url(
56 'url' => common_local_url('publicrss'),
57 'changefreq' => 'daily',
62 $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
64 foreach($docs as $title) {
65 $standard_map_urls .= url(
67 'url' => common_local_url('doc', array('title' => $title)),
68 'changefreq' => 'monthly',
74 $urlset_path = $output_paths['output_dir'] . 'standard.xml';
76 write_file($urlset_path, urlset($standard_map_urls));
79 # Generate sitemaps of all notices.
80 function notices_map() {
83 $notices = DB_DataObject::factory('notice');
85 $notices->query('SELECT id, uri, url, modified FROM notice');
90 while ($notices->fetch()) {
92 # Maximum 50,000 URLs per sitemap file.
93 if ($notice_count == 50000) {
98 # remote notices have an URL
100 if (!$notices->url && $notices->uri) {
102 'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
103 'lastmod' => common_date_w3dtf($notices->modified),
104 'changefreq' => 'never',
108 $notice_list[$map_count] .= url($notice);
113 # Make full sitemaps from the lists and save them.
114 array_to_map($notice_list, 'notice');
117 # Generate sitemaps of all users.
118 function user_map() {
119 global $output_paths;
121 $users = DB_DataObject::factory('user');
123 $users->query('SELECT id, nickname FROM user');
128 while ($users->fetch()) {
130 # Maximum 50,000 URLs per sitemap file.
131 if ($user_count == 50000) {
136 $user_args = array('nickname' => $users->nickname);
138 # Define parameters for generating <url></url> elements.
140 'url' => common_local_url('showstream', $user_args),
141 'changefreq' => 'daily',
146 'url' => common_local_url('userrss', $user_args),
147 'changefreq' => 'daily',
152 'url' => common_local_url('all', $user_args),
153 'changefreq' => 'daily',
158 'url' => common_local_url('allrss', $user_args),
159 'changefreq' => 'daily',
164 'url' => common_local_url('replies', $user_args),
165 'changefreq' => 'daily',
169 $replies_rss = array(
170 'url' => common_local_url('repliesrss', $user_args),
171 'changefreq' => 'daily',
176 'url' => common_local_url('foaf', $user_args),
177 'changefreq' => 'weekly',
181 # Construct a <url></url> element for each user facet and add it
182 # to our existing list of those.
183 $user_list[$map_count] .= url($user);
184 $user_rss_list[$map_count] .= url($user_rss);
185 $all_list[$map_count] .= url($all);
186 $all_rss_list[$map_count] .= url($all_rss);
187 $replies_list[$map_count] .= url($replies);
188 $replies_rss_list[$map_count] .= url($replies_rss);
189 $foaf_list[$map_count] .= url($foaf);
194 # Make full sitemaps from the lists and save them.
195 # Possible factoring: put all the lists into a master array, thus allowing
196 # calling with single argument (i.e., array_to_map('user')).
197 array_to_map($user_list, 'user');
198 array_to_map($user_rss_list, 'user_rss');
199 array_to_map($all_list, 'all');
200 array_to_map($all_rss_list, 'all_rss');
201 array_to_map($replies_list, 'replies');
202 array_to_map($replies_rss_list, 'replies_rss');
203 array_to_map($foaf_list, 'foaf');
206 # ------------------------------------------------------------------------------
207 # XML generation functions
208 # ------------------------------------------------------------------------------
210 # Generate a <url></url> element.
211 function url($url_args) {
212 $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
213 $lastmod = $url_args['lastmod'];
214 $changefreq = $url_args['changefreq'];
215 $priority = $url_args['priority'];
218 error("url() arguments require 'url' value.");
221 $url_out = "\t<url>\n";
222 $url_out .= "\t\t<loc>$url</loc>\n";
225 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
229 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
233 $url_out .= "\t\t<priority>$priority</priority>\n";
236 $url_out .= "\t</url>\n";
241 function sitemap($sitemap_args) {
242 $url = preg_replace('/&/', '&', $sitemap_args['url']); # escape ampersands for XML
243 $lastmod = $sitemap_args['lastmod'];
246 error("url() arguments require 'url' value.");
249 $sitemap_out = "\t<sitemap>\n";
250 $sitemap_out .= "\t\t<loc>$url</loc>\n";
253 $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
256 $sitemap_out .= "\t</sitemap>\n";
261 # Generate a <urlset></urlset> element.
262 function urlset($urlset_text) {
263 $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
264 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
271 # Generate a <urlset></urlset> element.
272 function sitemapindex($sitemapindex_text) {
273 $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
274 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
278 return $sitemapindex;
281 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
282 function array_to_map($url_list, $filename_prefix) {
283 global $output_paths;
286 # $map_urls is a long string containing concatenated <url></url> elements.
287 while (list($map_idx, $map_urls) = each($url_list)) {
288 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
290 write_file($urlset_path, urlset($map_urls));
295 # ------------------------------------------------------------------------------
297 # ------------------------------------------------------------------------------
299 # Parse command line arguments.
300 function parse_args() {
301 $args = getopt('f:d:u:');
303 if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
304 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
307 if (is_null($args[f])) {
308 error('You must specify an index file name with the -f option.');
311 if (is_null($args[d])) {
312 error('You must specify a directory for the output file with the -d option.');
315 if (is_null($args[u])) {
316 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
319 $index_file = $args[f];
320 $output_dir = $args[d];
321 $output_url = $args[u];
323 if (file_exists($output_dir)) {
324 if (is_writable($output_dir) === FALSE) {
325 error("$output_dir is not writable.");
328 error("output directory $output_dir does not exist.");
332 'index_file' => $index_file,
333 'output_dir' => trailing_slash($output_dir),
334 'output_url' => trailing_slash($output_url),
340 # Ensure paths end with a "/".
341 function trailing_slash($path) {
342 if (preg_match('/\/$/', $path) == 0) {
349 # Write data to disk.
350 function write_file($path, $data) {
351 if (is_null($path)) {
352 error('No path specified for writing to.');
353 } elseif (is_null($data)) {
354 error('No data specified for writing.');
357 if (($fh_out = fopen($path,'w')) === FALSE) {
358 error("couldn't open $path for writing.");
361 if (fwrite($fh_out, $data) === FALSE) {
362 error("couldn't write to $path.");
366 # Display an error message and exit.
367 function error ($error_msg) {
368 if (is_null($error_msg)) {
369 $error_msg = 'error() was called without any explanation!';
372 echo "Error: $error_msg\n";