]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/sitemap.php
New doc page for Identi.ca badge and minor updates to badge's js
[quix0rs-gnu-social.git] / scripts / sitemap.php
1 <?php
2
3 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
4 define('LACONICA', true);
5
6 require_once(INSTALLDIR . '/lib/common.php');
7 require_once(INSTALLDIR . '/lib/util.php');
8
9 $output_paths = parse_args();
10
11 standard_map();
12 notices_map();
13 user_map();
14 index_map();
15
16 # ------------------------------------------------------------------------------
17 # Main functions: get data out and turn them into sitemaps
18 # ------------------------------------------------------------------------------
19
20 # Generate index sitemap of all other sitemaps.
21 function index_map()
22 {
23     global $output_paths;
24     $output_dir = $output_paths['output_dir'];
25     $output_url = $output_paths['output_url'];
26
27     foreach (glob("$output_dir*.xml") as $file_name) {
28
29         # Just the file name please.
30         $file_name = preg_replace("|$output_dir|", '', $file_name);
31
32         $index_urls .= sitemap(
33                            array(
34                                  'url' => $output_url . $file_name,
35                                  'changefreq' => 'daily'
36                                  )
37                            );
38     }
39
40     write_file($output_paths['index_file'], sitemapindex($index_urls));
41 }
42
43 # Generate sitemap of standard site elements.
44 function standard_map()
45 {
46     global $output_paths;
47
48     $standard_map_urls .= url(
49                               array(
50                                     'url' => common_local_url('public'),
51                                     'changefreq' => 'daily',
52                                     'priority' => '1',
53                                     )
54                               );
55
56     $standard_map_urls .= url(
57                               array(
58                                     'url' => common_local_url('publicrss'),
59                                     'changefreq' => 'daily',
60                                     'priority' => '0.3',
61                                     )
62                               );
63
64     $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 
65         'privacy', 'source', 'badge');
66
67     foreach($docs as $title) {
68         $standard_map_urls .= url(
69                                   array(
70                                         'url' => common_local_url('doc', array('title' => $title)),
71                                         'changefreq' => 'monthly',
72                                         'priority'   => '0.2',
73                                         )
74                                   );
75     }
76
77     $urlset_path = $output_paths['output_dir'] . 'standard.xml';
78
79     write_file($urlset_path, urlset($standard_map_urls));
80 }
81
82 # Generate sitemaps of all notices.
83 function notices_map()
84 {
85     global $output_paths;
86
87     $notices = DB_DataObject::factory('notice');
88
89     $notices->query('SELECT id, uri, url, modified FROM notice where is_local = 1');
90
91     $notice_count = 0;
92     $map_count = 1;
93
94     while ($notices->fetch()) {
95
96         # Maximum 50,000 URLs per sitemap file.
97         if ($notice_count == 50000) {
98             $notice_count = 0;
99             $map_count++;
100         }
101
102         # remote notices have an URL
103         
104         if (!$notices->url && $notices->uri) {
105             $notice = array(
106                         'url'        => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
107                         'lastmod'    => common_date_w3dtf($notices->modified),
108                         'changefreq' => 'never',
109                         'priority'   => '1',
110                         );
111
112             $notice_list[$map_count] .= url($notice);
113             $notice_count++;
114         }
115     }
116
117     # Make full sitemaps from the lists and save them.
118     array_to_map($notice_list, 'notice');
119 }
120
121 # Generate sitemaps of all users.
122 function user_map()
123 {
124     global $output_paths;
125
126     $users = DB_DataObject::factory('user');
127
128     $users->query('SELECT id, nickname FROM user');
129
130     $user_count = 0;
131     $map_count = 1;
132
133     while ($users->fetch()) {
134
135         # Maximum 50,000 URLs per sitemap file.
136         if ($user_count == 50000) {
137             $user_count = 0;
138             $map_count++;
139         }
140
141         $user_args = array('nickname' => $users->nickname);
142
143         # Define parameters for generating <url></url> elements.
144         $user = array(
145                       'url'        => common_local_url('showstream', $user_args),
146                       'changefreq' => 'daily',
147                       'priority'   => '1',
148                       );
149
150         $user_rss = array(
151                           'url'        => common_local_url('userrss', $user_args),
152                           'changefreq' => 'daily',
153                           'priority'   => '0.3',
154                           );
155
156         $all = array(
157                      'url'        => common_local_url('all', $user_args),
158                      'changefreq' => 'daily',
159                      'priority'   => '1',
160                      );
161
162         $all_rss = array(
163                          'url'        => common_local_url('allrss', $user_args),
164                          'changefreq' => 'daily',
165                          'priority'   => '0.3',
166                          );
167
168         $replies = array(
169                          'url'        => common_local_url('replies', $user_args),
170                          'changefreq' => 'daily',
171                          'priority'   => '1',
172                          );
173
174         $replies_rss = array(
175                              'url'        => common_local_url('repliesrss', $user_args),
176                              'changefreq' => 'daily',
177                              'priority'   => '0.3',
178                              );
179
180         $foaf = array(
181                       'url'        => common_local_url('foaf', $user_args),
182                       'changefreq' => 'weekly',
183                       'priority'   => '0.5',
184                       );
185
186         # Construct a <url></url> element for each user facet and add it
187         # to our existing list of those.
188         $user_list[$map_count]        .= url($user);
189         $user_rss_list[$map_count]    .= url($user_rss);
190         $all_list[$map_count]         .= url($all);
191         $all_rss_list[$map_count]     .= url($all_rss);
192         $replies_list[$map_count]     .= url($replies);
193         $replies_rss_list[$map_count] .= url($replies_rss);
194         $foaf_list[$map_count]        .= url($foaf);
195
196         $user_count++;
197     }
198
199     # Make full sitemaps from the lists and save them.
200     # Possible factoring: put all the lists into a master array, thus allowing
201     # calling with single argument (i.e., array_to_map('user')).
202     array_to_map($user_list, 'user');
203     array_to_map($user_rss_list, 'user_rss');
204     array_to_map($all_list, 'all');
205     array_to_map($all_rss_list, 'all_rss');
206     array_to_map($replies_list, 'replies');
207     array_to_map($replies_rss_list, 'replies_rss');
208     array_to_map($foaf_list, 'foaf');
209 }
210
211 # ------------------------------------------------------------------------------
212 # XML generation functions
213 # ------------------------------------------------------------------------------
214
215 # Generate a <url></url> element.
216 function url($url_args)
217 {
218     $url        = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
219     $lastmod    = $url_args['lastmod'];
220     $changefreq = $url_args['changefreq'];
221     $priority   = $url_args['priority'];
222
223     if (is_null($url)) {
224         error("url() arguments require 'url' value.");
225     }
226
227     $url_out = "\t<url>\n";
228     $url_out .= "\t\t<loc>$url</loc>\n";
229
230     if ($changefreq) {
231         $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
232     }
233
234     if ($lastmod) {
235         $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
236     }
237
238     if ($priority) {
239         $url_out .= "\t\t<priority>$priority</priority>\n";
240     }
241
242     $url_out .= "\t</url>\n";
243
244     return $url_out;
245 }
246
247 function sitemap($sitemap_args)
248 {
249     $url        = preg_replace('/&/', '&amp;', $sitemap_args['url']); # escape ampersands for XML
250     $lastmod    = $sitemap_args['lastmod'];
251
252     if (is_null($url)) {
253         error("url() arguments require 'url' value.");
254     }
255
256     $sitemap_out = "\t<sitemap>\n";
257     $sitemap_out .= "\t\t<loc>$url</loc>\n";
258
259     if ($lastmod) {
260         $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
261     }
262
263     $sitemap_out .= "\t</sitemap>\n";
264
265     return $sitemap_out;
266 }
267
268 # Generate a <urlset></urlset> element.
269 function urlset($urlset_text)
270 {
271     $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
272       '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
273       $urlset_text .
274       '</urlset>';
275
276     return $urlset;
277 }
278
279 # Generate a <urlset></urlset> element.
280 function sitemapindex($sitemapindex_text)
281 {
282     $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
283       '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
284       $sitemapindex_text .
285       '</sitemapindex>';
286
287     return $sitemapindex;
288 }
289
290 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
291 function array_to_map($url_list, $filename_prefix)
292 {
293     global $output_paths;
294
295     if ($url_list) {
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";
299             
300             write_file($urlset_path, urlset($map_urls));
301         }
302     }
303 }
304
305 # ------------------------------------------------------------------------------
306 # Internal functions
307 # ------------------------------------------------------------------------------
308
309 # Parse command line arguments.
310 function parse_args()
311 {
312     $args = getopt('f:d:u:');
313
314     if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
315         error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
316     }
317
318     if (is_null($args[f])) {
319         error('You must specify an index file name with the -f option.');
320     }
321
322     if (is_null($args[d])) {
323         error('You must specify a directory for the output file with the -d option.');
324     }
325
326     if (is_null($args[u])) {
327         error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
328     }
329
330     $index_file = $args[f];
331     $output_dir = $args[d];
332     $output_url = $args[u];
333
334     if (file_exists($output_dir)) {
335         if (is_writable($output_dir) === false) {
336             error("$output_dir is not writable.");
337         }
338     }     else {
339         error("output directory $output_dir does not exist.");
340     }
341
342     $paths = array(
343                    'index_file' => $index_file,
344                    'output_dir' => trailing_slash($output_dir),
345                    'output_url' => trailing_slash($output_url),
346                    );
347
348     return $paths;
349 }
350
351 # Ensure paths end with a "/".
352 function trailing_slash($path)
353 {
354     if (preg_match('/\/$/', $path) == 0) {
355         $path .= '/';
356     }
357
358     return $path;
359 }
360
361 # Write data to disk.
362 function write_file($path, $data)
363 {
364     if (is_null($path)) {
365         error('No path specified for writing to.');
366     }     elseif (is_null($data)) {
367         error('No data specified for writing.');
368     }
369
370     if (($fh_out = fopen($path,'w')) === false) {
371         error("couldn't open $path for writing.");
372     }
373
374     if (fwrite($fh_out, $data) === false) {
375         error("couldn't write to $path.");
376     }
377 }
378
379 # Display an error message and exit.
380 function error ($error_msg)
381 {
382     if (is_null($error_msg)) {
383         $error_msg = 'error() was called without any explanation!';
384     }
385
386     echo "Error: $error_msg\n";
387     exit(1);
388 }
389
390 ?>