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);
34 'url' => $output_url . $file_name,
35 'changefreq' => 'hourly',
41 $index_path = $output_dir . $output_paths['index_file'];
43 write_file($index_path, urlset($index_urls));
47 # Generate sitemap of standard site elements.
48 function standard_map() {
51 $standard_map_urls .= url(
53 'url' => common_local_url('public'),
54 'changefreq' => 'hourly',
59 $standard_map_urls .= url(
61 'url' => common_local_url('publicrss'),
62 'changefreq' => 'hourly',
67 $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
69 foreach($docs as $title) {
70 $standard_map_urls .= url(
72 'url' => common_local_url('doc', array('title' => $title)),
73 'changefreq' => 'monthly',
79 $urlset_path = $output_paths['output_dir'] . 'standard.xml';
81 write_file($urlset_path, urlset($standard_map_urls));
84 # Generate sitemaps of all notices.
85 function notices_map() {
88 $notices = DB_DataObject::factory('notice');
90 $notices->query('SELECT uri, modified FROM notice');
95 while ($notices->fetch()) {
97 # Maximum 50,000 URLs per sitemap file.
98 if ($notice_count == 50000) {
104 'url' => $notices->uri,
105 'lastmod' => preg_replace('/ /', 'T', $notices->modified), # W3C DTF requires "T" separator
106 'changefreq' => 'hourly',
110 $notice_list[$map_count] .= url($notice);
115 # Make full sitemaps from the lists and save them.
116 array_to_map($notice_list, 'notice');
119 # Generate sitemaps of all users.
120 function user_map() {
121 global $output_paths;
123 $users = DB_DataObject::factory('user');
125 $users->query('SELECT id, nickname FROM user');
130 while ($users->fetch()) {
132 # Maximum 50,000 URLs per sitemap file.
133 if ($user_count == 50000) {
138 $user_args = array('nickname' => $users->nickname);
140 # Define parameters for generating <url></url> elements.
142 'url' => common_local_url('showstream', $user_args),
143 'changefreq' => 'hourly',
148 'url' => common_local_url('userrss', $user_args),
149 'changefreq' => 'hourly',
154 'url' => common_local_url('all', $user_args),
155 'changefreq' => 'hourly',
160 'url' => common_local_url('allrss', $user_args),
161 'changefreq' => 'hourly',
166 'url' => common_local_url('replies', $user_args),
167 'changefreq' => 'hourly',
171 $replies_rss = array(
172 'url' => common_local_url('repliesrss', $user_args),
173 'changefreq' => 'hourly',
178 'url' => common_local_url('foaf', $user_args),
179 'changefreq' => 'weekly',
183 # Construct a <url></url> element for each user facet and add it
184 # to our existing list of those.
185 $user_list[$map_count] .= url($user);
186 $user_rss_list[$map_count] .= url($user_rss);
187 $all_list[$map_count] .= url($all);
188 $all_rss_list[$map_count] .= url($all_rss);
189 $replies_list[$map_count] .= url($replies);
190 $replies_rss_list[$map_count] .= url($replies_rss);
191 $foaf_list[$map_count] .= url($foaf);
196 # Make full sitemaps from the lists and save them.
197 # Possible factoring: put all the lists into a master array, thus allowing
198 # calling with single argument (i.e., array_to_map('user')).
199 array_to_map($user_list, 'user');
200 array_to_map($user_rss_list, 'user_rss');
201 array_to_map($all_list, 'all');
202 array_to_map($all_rss_list, 'all_rss');
203 array_to_map($replies_list, 'replies');
204 array_to_map($replies_rss_list, 'replies_rss');
205 array_to_map($foaf_list, 'foaf');
208 # Generate sitemaps of all avatars.
209 function avatar_map() {
210 global $output_paths;
212 $avatars = DB_DataObject::factory('avatar');
214 $avatars->query('SELECT url, modified FROM avatar');
219 while ($avatars->fetch()) {
221 # We only want the original size and 24px thumbnail version - skip 96px.
222 if (preg_match('/-96-/', $avatars->url)) {
226 # Maximum 50,000 URLs per sitemap file.
227 if ($avatar_count == 50000) {
233 'url' => $avatars->url,
234 'lastmod' => preg_replace('/ /', 'T', $avatars->modified), # W3C DTF requires "T" separator
235 'changefreq' => 'monthly',
239 # Construct a <url></url> element for each avatar and add it
240 # to our existing list of those.
241 $avatar_list[$map_count] .= url($image);
244 array_to_map($avatar_list, 'avatars');
247 # ------------------------------------------------------------------------------
248 # XML generation functions
249 # ------------------------------------------------------------------------------
251 # Generate a <url></url> element.
252 function url($url_args) {
253 $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
254 $lastmod = $url_args['lastmod'];
255 $changefreq = $url_args['changefreq'];
256 $priority = $url_args['priority'];
259 error("url() arguments require 'url' value.");
262 $url_out = "\t<url>\n";
263 $url_out .= "\t\t<loc>$url</loc>\n";
266 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
270 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
274 $url_out .= "\t\t<priority>$priority</priority>\n";
277 $url_out .= "\t</url>\n";
282 # Generate a <urlset></urlset> element.
283 function urlset($urlset_text) {
284 $urlset = '<?xml version="1.0" encoding="UTF-8"?>'.
285 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.
292 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
293 function array_to_map($url_list, $filename_prefix) {
294 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));
304 # ------------------------------------------------------------------------------
306 # ------------------------------------------------------------------------------
308 # Parse command line arguments.
309 function parse_args() {
310 $args = getopt('f:d:u:');
312 if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
313 error('Mandatory arguments: -f <index file name> -d <output directory> -u <URL of sitemaps directory>');
316 if (is_null($args[f])) {
317 error('You must specify an index file name with the -f option.');
320 if (is_null($args[d])) {
321 error('You must specify a directory for the output file with the -d option.');
324 if (is_null($args[u])) {
325 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
328 $index_file = $args[f];
329 $output_dir = $args[d];
330 $output_url = $args[u];
332 if (file_exists($output_dir)) {
333 if (is_writable($output_dir) === FALSE) {
334 error("$output_dir is not writable.");
337 error("output directory $output_dir does not exist.");
341 'index_file' => $index_file,
342 'output_dir' => trailing_slash($output_dir),
343 'output_url' => trailing_slash($output_url),
349 # Ensure paths end with a "/".
350 function trailing_slash($path) {
351 if (preg_match('/\/$/', $path) == 0) {
358 # Write data to disk.
359 function write_file($path, $data) {
360 if (is_null($path)) {
361 error('No path specified for writing to.');
362 } elseif (is_null($data)) {
363 error('No data specified for writing.');
366 if (($fh_out = fopen($path,'w')) === FALSE) {
367 error("couldn't open $path for writing.");
370 if (fwrite($fh_out, $data) === FALSE) {
371 error("couldn't write to $path.");
375 # Display an error message and exit.
376 function error ($error_msg) {
377 if (is_null($error_msg)) {
378 $error_msg = 'error() was called without any explanation!';
381 echo "Error: $error_msg\n";