3 define('INSTALLDIR', 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();
17 # ------------------------------------------------------------------------------
18 # Main functions: get data out and turn them into sitemaps
19 # ------------------------------------------------------------------------------
21 # Generate index sitemap of all other sitemaps.
22 function index_map() {
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() {
47 $standard_map_urls .= url(
49 'url' => common_local_url('public'),
50 'changefreq' => 'daily',
55 $standard_map_urls .= url(
57 'url' => common_local_url('publicrss'),
58 'changefreq' => 'daily',
63 $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
65 foreach($docs as $title) {
66 $standard_map_urls .= url(
68 'url' => common_local_url('doc', array('title' => $title)),
69 'changefreq' => 'monthly',
75 $urlset_path = $output_paths['output_dir'] . 'standard.xml';
77 write_file($urlset_path, urlset($standard_map_urls));
80 # Generate sitemaps of all notices.
81 function notices_map() {
84 $notices = DB_DataObject::factory('notice');
86 $notices->query('SELECT uri, modified FROM notice');
91 while ($notices->fetch()) {
93 # Maximum 50,000 URLs per sitemap file.
94 if ($notice_count == 50000) {
100 'url' => $notices->uri,
101 'lastmod' => common_date_w3dtf($notices->modified),
102 'changefreq' => 'daily',
106 $notice_list[$map_count] .= url($notice);
111 # Make full sitemaps from the lists and save them.
112 array_to_map($notice_list, 'notice');
115 # Generate sitemaps of all users.
116 function user_map() {
117 global $output_paths;
119 $users = DB_DataObject::factory('user');
121 $users->query('SELECT id, nickname FROM user');
126 while ($users->fetch()) {
128 # Maximum 50,000 URLs per sitemap file.
129 if ($user_count == 50000) {
134 $user_args = array('nickname' => $users->nickname);
136 # Define parameters for generating <url></url> elements.
138 'url' => common_local_url('showstream', $user_args),
139 'changefreq' => 'daily',
144 'url' => common_local_url('userrss', $user_args),
145 'changefreq' => 'daily',
150 'url' => common_local_url('all', $user_args),
151 'changefreq' => 'daily',
156 'url' => common_local_url('allrss', $user_args),
157 'changefreq' => 'daily',
162 'url' => common_local_url('replies', $user_args),
163 'changefreq' => 'daily',
167 $replies_rss = array(
168 'url' => common_local_url('repliesrss', $user_args),
169 'changefreq' => 'daily',
174 'url' => common_local_url('foaf', $user_args),
175 'changefreq' => 'weekly',
179 # Construct a <url></url> element for each user facet and add it
180 # to our existing list of those.
181 $user_list[$map_count] .= url($user);
182 $user_rss_list[$map_count] .= url($user_rss);
183 $all_list[$map_count] .= url($all);
184 $all_rss_list[$map_count] .= url($all_rss);
185 $replies_list[$map_count] .= url($replies);
186 $replies_rss_list[$map_count] .= url($replies_rss);
187 $foaf_list[$map_count] .= url($foaf);
192 # Make full sitemaps from the lists and save them.
193 # Possible factoring: put all the lists into a master array, thus allowing
194 # calling with single argument (i.e., array_to_map('user')).
195 array_to_map($user_list, 'user');
196 array_to_map($user_rss_list, 'user_rss');
197 array_to_map($all_list, 'all');
198 array_to_map($all_rss_list, 'all_rss');
199 array_to_map($replies_list, 'replies');
200 array_to_map($replies_rss_list, 'replies_rss');
201 array_to_map($foaf_list, 'foaf');
204 # Generate sitemaps of all avatars.
205 function avatar_map() {
206 global $output_paths;
208 $avatars = new Avatar();
209 $avatars->whereAdd('original = 1', "OR");
210 $avatars->whereAdd('width = ' . AVATAR_MINI_SIZE, 'OR');
212 if (!$avatars->find()) {
219 while ($avatars->fetch()) {
221 # Maximum 50,000 URLs per sitemap file.
222 if ($avatar_count == 50000) {
227 'url' => common_avatar_display_url($avatars),
228 'lastmod' => common_date_w3dtf($avatars->modified),
229 'changefreq' => 'never',
233 # Construct a <url></url> element for each avatar and add it
234 # to our existing list of those.
235 $avatar_list[$map_count] .= url($image);
238 array_to_map($avatar_list, 'avatars');
241 # ------------------------------------------------------------------------------
242 # XML generation functions
243 # ------------------------------------------------------------------------------
245 # Generate a <url></url> element.
246 function url($url_args) {
247 $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
248 $lastmod = $url_args['lastmod'];
249 $changefreq = $url_args['changefreq'];
250 $priority = $url_args['priority'];
253 error("url() arguments require 'url' value.");
256 $url_out = "\t<url>\n";
257 $url_out .= "\t\t<loc>$url</loc>\n";
260 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
264 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
268 $url_out .= "\t\t<priority>$priority</priority>\n";
271 $url_out .= "\t</url>\n";
276 function sitemap($sitemap_args) {
277 $url = preg_replace('/&/', '&', $sitemap_args['url']); # escape ampersands for XML
278 $lastmod = $sitemap_args['lastmod'];
281 error("url() arguments require 'url' value.");
284 $sitemap_out = "\t<sitemap>\n";
285 $sitemap_out .= "\t\t<loc>$url</loc>\n";
288 $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
291 $sitemap_out .= "\t</sitemap>\n";
296 # Generate a <urlset></urlset> element.
297 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) {
308 $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
309 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
313 return $sitemapindex;
316 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
317 function array_to_map($url_list, $filename_prefix) {
318 global $output_paths;
321 # $map_urls is a long string containing concatenated <url></url> elements.
322 while (list($map_idx, $map_urls) = each($url_list)) {
323 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
325 write_file($urlset_path, urlset($map_urls));
330 # ------------------------------------------------------------------------------
332 # ------------------------------------------------------------------------------
334 # Parse command line arguments.
335 function parse_args() {
336 $args = getopt('f:d:u:');
338 if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
339 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
342 if (is_null($args[f])) {
343 error('You must specify an index file name with the -f option.');
346 if (is_null($args[d])) {
347 error('You must specify a directory for the output file with the -d option.');
350 if (is_null($args[u])) {
351 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
354 $index_file = $args[f];
355 $output_dir = $args[d];
356 $output_url = $args[u];
358 if (file_exists($output_dir)) {
359 if (is_writable($output_dir) === FALSE) {
360 error("$output_dir is not writable.");
363 error("output directory $output_dir does not exist.");
367 'index_file' => $index_file,
368 'output_dir' => trailing_slash($output_dir),
369 'output_url' => trailing_slash($output_url),
375 # Ensure paths end with a "/".
376 function trailing_slash($path) {
377 if (preg_match('/\/$/', $path) == 0) {
384 # Write data to disk.
385 function write_file($path, $data) {
386 if (is_null($path)) {
387 error('No path specified for writing to.');
388 } elseif (is_null($data)) {
389 error('No data specified for writing.');
392 if (($fh_out = fopen($path,'w')) === FALSE) {
393 error("couldn't open $path for writing.");
396 if (fwrite($fh_out, $data) === FALSE) {
397 error("couldn't write to $path.");
401 # Display an error message and exit.
402 function error ($error_msg) {
403 if (is_null($error_msg)) {
404 $error_msg = 'error() was called without any explanation!';
407 echo "Error: $error_msg\n";