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' => 'daily'
40 write_file($output_paths['index_file'], urlset($index_urls));
44 # Generate sitemap of standard site elements.
45 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', 'privacy', 'source');
66 foreach($docs as $title) {
67 $standard_map_urls .= url(
69 'url' => common_local_url('doc', array('title' => $title)),
70 'changefreq' => 'monthly',
76 $urlset_path = $output_paths['output_dir'] . 'standard.xml';
78 write_file($urlset_path, urlset($standard_map_urls));
81 # Generate sitemaps of all notices.
82 function notices_map() {
85 $notices = DB_DataObject::factory('notice');
87 $notices->query('SELECT uri, modified FROM notice');
92 while ($notices->fetch()) {
94 # Maximum 50,000 URLs per sitemap file.
95 if ($notice_count == 50000) {
101 'url' => $notices->uri,
102 'lastmod' => common_date_w3dtf($notices->modified),
103 'changefreq' => 'daily',
107 $notice_list[$map_count] .= url($notice);
112 # Make full sitemaps from the lists and save them.
113 array_to_map($notice_list, 'notice');
116 # Generate sitemaps of all users.
117 function user_map() {
118 global $output_paths;
120 $users = DB_DataObject::factory('user');
122 $users->query('SELECT id, nickname FROM user');
127 while ($users->fetch()) {
129 # Maximum 50,000 URLs per sitemap file.
130 if ($user_count == 50000) {
135 $user_args = array('nickname' => $users->nickname);
137 # Define parameters for generating <url></url> elements.
139 'url' => common_local_url('showstream', $user_args),
140 'changefreq' => 'daily',
145 'url' => common_local_url('userrss', $user_args),
146 'changefreq' => 'daily',
151 'url' => common_local_url('all', $user_args),
152 'changefreq' => 'daily',
157 'url' => common_local_url('allrss', $user_args),
158 'changefreq' => 'daily',
163 'url' => common_local_url('replies', $user_args),
164 'changefreq' => 'daily',
168 $replies_rss = array(
169 'url' => common_local_url('repliesrss', $user_args),
170 'changefreq' => 'daily',
175 'url' => common_local_url('foaf', $user_args),
176 'changefreq' => 'weekly',
180 # Construct a <url></url> element for each user facet and add it
181 # to our existing list of those.
182 $user_list[$map_count] .= url($user);
183 $user_rss_list[$map_count] .= url($user_rss);
184 $all_list[$map_count] .= url($all);
185 $all_rss_list[$map_count] .= url($all_rss);
186 $replies_list[$map_count] .= url($replies);
187 $replies_rss_list[$map_count] .= url($replies_rss);
188 $foaf_list[$map_count] .= url($foaf);
193 # Make full sitemaps from the lists and save them.
194 # Possible factoring: put all the lists into a master array, thus allowing
195 # calling with single argument (i.e., array_to_map('user')).
196 array_to_map($user_list, 'user');
197 array_to_map($user_rss_list, 'user_rss');
198 array_to_map($all_list, 'all');
199 array_to_map($all_rss_list, 'all_rss');
200 array_to_map($replies_list, 'replies');
201 array_to_map($replies_rss_list, 'replies_rss');
202 array_to_map($foaf_list, 'foaf');
205 # Generate sitemaps of all avatars.
206 function avatar_map() {
207 global $output_paths;
209 $avatars = new Avatar();
210 $avatars->whereAdd('original = 1', "OR");
211 $avatars->whereAdd('width = ' . AVATAR_MINI_SIZE, 'OR');
213 if (!$avatars->find()) {
220 while ($avatars->fetch()) {
222 # Maximum 50,000 URLs per sitemap file.
223 if ($avatar_count == 50000) {
228 'url' => common_avatar_display_url($avatars),
229 'lastmod' => common_date_w3dtf($avatars->modified),
230 'changefreq' => 'never',
234 # Construct a <url></url> element for each avatar and add it
235 # to our existing list of those.
236 $avatar_list[$map_count] .= url($image);
239 array_to_map($avatar_list, 'avatars');
242 # ------------------------------------------------------------------------------
243 # XML generation functions
244 # ------------------------------------------------------------------------------
246 # Generate a <url></url> element.
247 function url($url_args) {
248 $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
249 $lastmod = $url_args['lastmod'];
250 $changefreq = $url_args['changefreq'];
251 $priority = $url_args['priority'];
254 error("url() arguments require 'url' value.");
257 $url_out = "\t<url>\n";
258 $url_out .= "\t\t<loc>$url</loc>\n";
261 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
265 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
269 $url_out .= "\t\t<priority>$priority</priority>\n";
272 $url_out .= "\t</url>\n";
277 # Generate a <urlset></urlset> element.
278 function urlset($urlset_text) {
279 $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
280 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
287 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
288 function array_to_map($url_list, $filename_prefix) {
289 global $output_paths;
292 # $map_urls is a long string containing concatenated <url></url> elements.
293 while (list($map_idx, $map_urls) = each($url_list)) {
294 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
296 write_file($urlset_path, urlset($map_urls));
301 # ------------------------------------------------------------------------------
303 # ------------------------------------------------------------------------------
305 # Parse command line arguments.
306 function parse_args() {
307 $args = getopt('f:d:u:');
309 if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
310 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
313 if (is_null($args[f])) {
314 error('You must specify an index file name with the -f option.');
317 if (is_null($args[d])) {
318 error('You must specify a directory for the output file with the -d option.');
321 if (is_null($args[u])) {
322 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
325 $index_file = $args[f];
326 $output_dir = $args[d];
327 $output_url = $args[u];
329 if (file_exists($output_dir)) {
330 if (is_writable($output_dir) === FALSE) {
331 error("$output_dir is not writable.");
334 error("output directory $output_dir does not exist.");
338 'index_file' => $index_file,
339 'output_dir' => trailing_slash($output_dir),
340 'output_url' => trailing_slash($output_url),
346 # Ensure paths end with a "/".
347 function trailing_slash($path) {
348 if (preg_match('/\/$/', $path) == 0) {
355 # Write data to disk.
356 function write_file($path, $data) {
357 if (is_null($path)) {
358 error('No path specified for writing to.');
359 } elseif (is_null($data)) {
360 error('No data specified for writing.');
363 if (($fh_out = fopen($path,'w')) === FALSE) {
364 error("couldn't open $path for writing.");
367 if (fwrite($fh_out, $data) === FALSE) {
368 error("couldn't write to $path.");
372 # Display an error message and exit.
373 function error ($error_msg) {
374 if (is_null($error_msg)) {
375 $error_msg = 'error() was called without any explanation!';
378 echo "Error: $error_msg\n";