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 = DB_DataObject::factory('avatar');
211 $avatars->query('SELECT url, modified FROM avatar');
216 while ($avatars->fetch()) {
218 # We only want the original size and 24px thumbnail version - skip 96px.
219 if (preg_match('/-96-/', $avatars->url)) {
223 # Maximum 50,000 URLs per sitemap file.
224 if ($avatar_count == 50000) {
228 w3cdate($avatars->modified);
230 'url' => common_avatar_display_url($avatars),
231 'lastmod' => common_date_w3dtf($avatars->modified),
232 'changefreq' => 'monthly',
236 # Construct a <url></url> element for each avatar and add it
237 # to our existing list of those.
238 $avatar_list[$map_count] .= url($image);
241 array_to_map($avatar_list, 'avatars');
244 # ------------------------------------------------------------------------------
245 # XML generation functions
246 # ------------------------------------------------------------------------------
248 # Generate a <url></url> element.
249 function url($url_args) {
250 $url = preg_replace('/&/', '&', $url_args['url']); # escape ampersands for XML
251 $lastmod = $url_args['lastmod'];
252 $changefreq = $url_args['changefreq'];
253 $priority = $url_args['priority'];
256 error("url() arguments require 'url' value.");
259 $url_out = "\t<url>\n";
260 $url_out .= "\t\t<loc>$url</loc>\n";
263 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
267 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
271 $url_out .= "\t\t<priority>$priority</priority>\n";
274 $url_out .= "\t</url>\n";
279 # Generate a <urlset></urlset> element.
280 function urlset($urlset_text) {
281 $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
282 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
289 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
290 function array_to_map($url_list, $filename_prefix) {
291 global $output_paths;
294 # $map_urls is a long string containing concatenated <url></url> elements.
295 while (list($map_idx, $map_urls) = each($url_list)) {
296 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
298 write_file($urlset_path, urlset($map_urls));
303 # ------------------------------------------------------------------------------
305 # ------------------------------------------------------------------------------
307 # Parse command line arguments.
308 function parse_args() {
309 $args = getopt('f:d:u:');
311 if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
312 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
315 if (is_null($args[f])) {
316 error('You must specify an index file name with the -f option.');
319 if (is_null($args[d])) {
320 error('You must specify a directory for the output file with the -d option.');
323 if (is_null($args[u])) {
324 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
327 $index_file = $args[f];
328 $output_dir = $args[d];
329 $output_url = $args[u];
331 if (file_exists($output_dir)) {
332 if (is_writable($output_dir) === FALSE) {
333 error("$output_dir is not writable.");
336 error("output directory $output_dir does not exist.");
340 'index_file' => $index_file,
341 'output_dir' => trailing_slash($output_dir),
342 'output_url' => trailing_slash($output_url),
348 # Ensure paths end with a "/".
349 function trailing_slash($path) {
350 if (preg_match('/\/$/', $path) == 0) {
357 # Write data to disk.
358 function write_file($path, $data) {
359 if (is_null($path)) {
360 error('No path specified for writing to.');
361 } elseif (is_null($data)) {
362 error('No data specified for writing.');
365 if (($fh_out = fopen($path,'w')) === FALSE) {
366 error("couldn't open $path for writing.");
369 if (fwrite($fh_out, $data) === FALSE) {
370 error("couldn't write to $path.");
374 # Display an error message and exit.
375 function error ($error_msg) {
376 if (is_null($error_msg)) {
377 $error_msg = 'error() was called without any explanation!';
380 echo "Error: $error_msg\n";