]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - sitemap.php
lcase tname
[quix0rs-gnu-social.git] / sitemap.php
1 <?php
2
3 define('INSTALLDIR', 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 avatar_map();
15 index_map();
16
17 # ------------------------------------------------------------------------------
18 # Main functions: get data out and turn them into sitemaps
19 # ------------------------------------------------------------------------------
20
21 # Generate index sitemap of all other sitemaps.
22 function index_map() {
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 .= url(
33                                                    array(
34                                                                  'url' => $output_url . $file_name,
35                                                                  'changefreq' => 'daily',
36                                                                  'priority' => '1',
37                                                                  )
38                                                    );
39         }
40
41         write_file($output_paths['index_file'], urlset($index_urls));
42
43 }
44
45 # Generate sitemap of standard site elements.
46 function standard_map() {
47         global $output_paths;
48
49         $standard_map_urls .= url(
50                                                           array(
51                                                                         'url' => common_local_url('public'),
52                                                                         'changefreq' => 'daily',
53                                                                         'priority' => '1',
54                                                                         )
55                                                           );
56
57         $standard_map_urls .= url(
58                                                           array(
59                                                                         'url' => common_local_url('publicrss'),
60                                                                         'changefreq' => 'daily',
61                                                                         'priority' => '0.3',
62                                                                         )
63                                                           );
64
65         $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
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         global $output_paths;
85
86         $notices = DB_DataObject::factory('notice');
87
88         $notices->query('SELECT uri, modified FROM notice');
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                 $notice = array(
102                                                 'url'        => $notices->uri,
103                                                 'lastmod'    => w3cdate($notices->modified),
104                                                 'changefreq' => 'daily',
105                                                 'priority'   => '1',
106                                                 );
107
108                 $notice_list[$map_count] .= url($notice);
109
110                 $notice_count++;
111         }
112
113         # Make full sitemaps from the lists and save them.
114         array_to_map($notice_list, 'notice');
115 }
116
117 # Generate sitemaps of all users.
118 function user_map() {
119         global $output_paths;
120
121         $users = DB_DataObject::factory('user');
122
123         $users->query('SELECT id, nickname FROM user');
124
125         $user_count = 0;
126         $map_count = 1;
127
128         while ($users->fetch()) {
129
130                 # Maximum 50,000 URLs per sitemap file.
131                 if ($user_count == 50000) {
132                         $user_count = 0;
133                         $map_count++;
134                 }
135
136                 $user_args = array('nickname' => $users->nickname);
137
138                 # Define parameters for generating <url></url> elements.
139                 $user = array(
140                                           'url'        => common_local_url('showstream', $user_args),
141                                           'changefreq' => 'daily',
142                                           'priority'   => '1',
143                                           );
144
145                 $user_rss = array(
146                                                   'url'        => common_local_url('userrss', $user_args),
147                                                   'changefreq' => 'daily',
148                                                   'priority'   => '0.3',
149                                                   );
150
151                 $all = array(
152                                          'url'        => common_local_url('all', $user_args),
153                                          'changefreq' => 'daily',
154                                          'priority'   => '1',
155                                          );
156
157                 $all_rss = array(
158                                                  'url'        => common_local_url('allrss', $user_args),
159                                                  'changefreq' => 'daily',
160                                                  'priority'   => '0.3',
161                                                  );
162
163                 $replies = array(
164                                                  'url'        => common_local_url('replies', $user_args),
165                                                  'changefreq' => 'daily',
166                                                  'priority'   => '1',
167                                                  );
168
169                 $replies_rss = array(
170                                                          'url'        => common_local_url('repliesrss', $user_args),
171                                                          'changefreq' => 'daily',
172                                                          'priority'   => '0.3',
173                                                          );
174
175                 $foaf = array(
176                                           'url'        => common_local_url('foaf', $user_args),
177                                           'changefreq' => 'weekly',
178                                           'priority'   => '0.5',
179                                           );
180
181                 # Construct a <url></url> element for each user facet and add it
182                 # to our existing list of those.
183                 $user_list[$map_count]        .= url($user);
184                 $user_rss_list[$map_count]    .= url($user_rss);
185                 $all_list[$map_count]         .= url($all);
186                 $all_rss_list[$map_count]     .= url($all_rss);
187                 $replies_list[$map_count]     .= url($replies);
188                 $replies_rss_list[$map_count] .= url($replies_rss);
189                 $foaf_list[$map_count]        .= url($foaf);
190
191                 $user_count++;
192         }
193
194         # Make full sitemaps from the lists and save them.
195         # Possible factoring: put all the lists into a master array, thus allowing
196         # calling with single argument (i.e., array_to_map('user')).
197         array_to_map($user_list, 'user');
198         array_to_map($user_rss_list, 'user_rss');
199         array_to_map($all_list, 'all');
200         array_to_map($all_rss_list, 'all_rss');
201         array_to_map($replies_list, 'replies');
202         array_to_map($replies_rss_list, 'replies_rss');
203         array_to_map($foaf_list, 'foaf');
204 }
205
206 # Generate sitemaps of all avatars.
207 function avatar_map() {
208         global $output_paths;
209
210         $avatars = DB_DataObject::factory('avatar');
211
212         $avatars->query('SELECT url, modified FROM avatar');
213
214         $avatar_count = 0;
215         $map_count = 1;
216
217         while ($avatars->fetch()) {
218
219                 # We only want the original size and 24px thumbnail version - skip 96px.
220                 if (preg_match('/-96-/', $avatars->url)) {
221                         continue;
222                 }
223
224                 # Maximum 50,000 URLs per sitemap file.
225                 if ($avatar_count == 50000) {
226                         $avatar_count = 0;
227                         $map_count++;
228                 }
229 w3cdate($avatars->modified);
230                 $image = array(
231                                            'url'        => $avatars->url,
232                                            'lastmod'    => w3cdate($avatars->modified),
233                                            'changefreq' => 'monthly',
234                                            'priority'   => '0.2',
235                                            );
236
237                 # Construct a <url></url> element for each avatar and add it
238                 # to our existing list of those.
239                 $avatar_list[$map_count] .= url($image);
240         }
241
242         array_to_map($avatar_list, 'avatars');
243 }
244
245 # ------------------------------------------------------------------------------
246 # XML generation functions
247 # ------------------------------------------------------------------------------
248
249 # Generate a <url></url> element.
250 function url($url_args) {
251         $url        = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
252         $lastmod    = $url_args['lastmod'];
253         $changefreq = $url_args['changefreq'];
254         $priority   = $url_args['priority'];
255
256         if (is_null($url)) {
257                 error("url() arguments require 'url' value.");
258         }
259
260         $url_out = "\t<url>\n";
261         $url_out .= "\t\t<loc>$url</loc>\n";
262
263         if ($changefreq) {
264                 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
265         }
266
267         if ($lastmod) {
268                 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
269         }
270
271         if ($priority) {
272                 $url_out .= "\t\t<priority>$priority</priority>\n";
273         }
274
275         $url_out .= "\t</url>\n";
276
277         return $url_out;
278 }
279
280 # Generate a <urlset></urlset> element.
281 function urlset($urlset_text) {
282         $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
283           '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
284           $urlset_text .
285           '</urlset>';
286
287         return $urlset;
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         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         $args = getopt('f:d:u:');
311
312         if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
313                 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
314         }
315
316         if (is_null($args[f])) {
317                 error('You must specify an index file name with the -f option.');
318         }
319
320         if (is_null($args[d])) {
321                 error('You must specify a directory for the output file with the -d option.');
322         }
323
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.');
326         }
327
328         $index_file = $args[f];
329         $output_dir = $args[d];
330         $output_url = $args[u];
331
332         if (file_exists($output_dir)) {
333                 if (is_writable($output_dir) === FALSE) {
334                         error("$output_dir is not writable.");
335                 }
336         }        else {
337                 error("output directory $output_dir does not exist.");
338         }
339
340         $paths = array(
341                                    'index_file' => $index_file,
342                                    'output_dir' => trailing_slash($output_dir),
343                                    'output_url' => trailing_slash($output_url),
344                                    );
345
346         return $paths;
347 }
348
349 # Format database timestamps as W3C DTF.
350 function w3cdate ($timestamp) {
351         preg_match('/(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/', $timestamp, $date);
352         
353         return date(DATE_W3C, mktime($date[4], $date[5], $date[6], $date[2], $date[3], $date[1]));
354 }
355
356 # Ensure paths end with a "/".
357 function trailing_slash($path) {
358         if (preg_match('/\/$/', $path) == 0) {
359                 $path .= '/';
360         }
361
362         return $path;
363 }
364
365 # Write data to disk.
366 function write_file($path, $data) {
367         if (is_null($path)) {
368                 error('No path specified for writing to.');
369         }        elseif (is_null($data)) {
370                 error('No data specified for writing.');
371         }
372
373         if (($fh_out = fopen($path,'w')) === FALSE) {
374                 error("couldn't open $path for writing.");
375         }
376
377         if (fwrite($fh_out, $data) === FALSE) {
378                 error("couldn't write to $path.");
379         }
380 }
381
382 # Display an error message and exit.
383 function error ($error_msg) {
384         if (is_null($error_msg)) {
385                 $error_msg = 'error() was called without any explanation!';
386         }
387
388         echo "Error: $error_msg\n";
389         exit(1);
390 }
391
392 ?>