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