]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - sitemap.php
cf7d3f1991e7f7e63ee4de0abfef7ad9a1e02cd3
[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                                                                  )
37                                                    );
38         }
39
40         write_file($output_paths['index_file'], urlset($index_urls));
41
42 }
43
44 # Generate sitemap of standard site elements.
45 function standard_map() {
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         global $output_paths;
84
85         $notices = DB_DataObject::factory('notice');
86
87         $notices->query('SELECT uri, modified FROM notice');
88
89         $notice_count = 0;
90         $map_count = 1;
91
92         while ($notices->fetch()) {
93
94                 # Maximum 50,000 URLs per sitemap file.
95                 if ($notice_count == 50000) {
96                         $notice_count = 0;
97                         $map_count++;
98                 }
99
100                 $notice = array(
101                                                 'url'        => $notices->uri,
102                                                 'lastmod'    => common_date_w3dtf($notices->modified),
103                                                 'changefreq' => 'daily',
104                                                 'priority'   => '1',
105                                                 );
106
107                 $notice_list[$map_count] .= url($notice);
108
109                 $notice_count++;
110         }
111
112         # Make full sitemaps from the lists and save them.
113         array_to_map($notice_list, 'notice');
114 }
115
116 # Generate sitemaps of all users.
117 function user_map() {
118         global $output_paths;
119
120         $users = DB_DataObject::factory('user');
121
122         $users->query('SELECT id, nickname FROM user');
123
124         $user_count = 0;
125         $map_count = 1;
126
127         while ($users->fetch()) {
128
129                 # Maximum 50,000 URLs per sitemap file.
130                 if ($user_count == 50000) {
131                         $user_count = 0;
132                         $map_count++;
133                 }
134
135                 $user_args = array('nickname' => $users->nickname);
136
137                 # Define parameters for generating <url></url> elements.
138                 $user = array(
139                                           'url'        => common_local_url('showstream', $user_args),
140                                           'changefreq' => 'daily',
141                                           'priority'   => '1',
142                                           );
143
144                 $user_rss = array(
145                                                   'url'        => common_local_url('userrss', $user_args),
146                                                   'changefreq' => 'daily',
147                                                   'priority'   => '0.3',
148                                                   );
149
150                 $all = array(
151                                          'url'        => common_local_url('all', $user_args),
152                                          'changefreq' => 'daily',
153                                          'priority'   => '1',
154                                          );
155
156                 $all_rss = array(
157                                                  'url'        => common_local_url('allrss', $user_args),
158                                                  'changefreq' => 'daily',
159                                                  'priority'   => '0.3',
160                                                  );
161
162                 $replies = array(
163                                                  'url'        => common_local_url('replies', $user_args),
164                                                  'changefreq' => 'daily',
165                                                  'priority'   => '1',
166                                                  );
167
168                 $replies_rss = array(
169                                                          'url'        => common_local_url('repliesrss', $user_args),
170                                                          'changefreq' => 'daily',
171                                                          'priority'   => '0.3',
172                                                          );
173
174                 $foaf = array(
175                                           'url'        => common_local_url('foaf', $user_args),
176                                           'changefreq' => 'weekly',
177                                           'priority'   => '0.5',
178                                           );
179
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);
189
190                 $user_count++;
191         }
192
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');
203 }
204
205 # Generate sitemaps of all avatars.
206 function avatar_map() {
207         global $output_paths;
208
209         $avatars = DB_DataObject::factory('avatar');
210
211         $avatars->query('SELECT url, modified FROM avatar');
212
213         $avatar_count = 0;
214         $map_count = 1;
215
216         while ($avatars->fetch()) {
217
218                 # We only want the original size and 24px thumbnail version - skip 96px.
219                 if (preg_match('/-96-/', $avatars->url)) {
220                         continue;
221                 }
222
223                 # Maximum 50,000 URLs per sitemap file.
224                 if ($avatar_count == 50000) {
225                         $avatar_count = 0;
226                         $map_count++;
227                 }
228 w3cdate($avatars->modified);
229                 $image = array(
230                                            'url'        => common_avatar_display_url($avatars),
231                                            'lastmod'    => common_date_w3dtf($avatars->modified),
232                                            'changefreq' => 'monthly',
233                                            'priority'   => '0.2',
234                                            );
235
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);
239         }
240
241         array_to_map($avatar_list, 'avatars');
242 }
243
244 # ------------------------------------------------------------------------------
245 # XML generation functions
246 # ------------------------------------------------------------------------------
247
248 # Generate a <url></url> element.
249 function url($url_args) {
250         $url        = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
251         $lastmod    = $url_args['lastmod'];
252         $changefreq = $url_args['changefreq'];
253         $priority   = $url_args['priority'];
254
255         if (is_null($url)) {
256                 error("url() arguments require 'url' value.");
257         }
258
259         $url_out = "\t<url>\n";
260         $url_out .= "\t\t<loc>$url</loc>\n";
261
262         if ($changefreq) {
263                 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
264         }
265
266         if ($lastmod) {
267                 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
268         }
269
270         if ($priority) {
271                 $url_out .= "\t\t<priority>$priority</priority>\n";
272         }
273
274         $url_out .= "\t</url>\n";
275
276         return $url_out;
277 }
278
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" .
283           $urlset_text .
284           '</urlset>';
285
286         return $urlset;
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         global $output_paths;
292
293         if ($url_list) {
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";
297                         
298                         write_file($urlset_path, urlset($map_urls));
299                 }
300         }
301 }
302
303 # ------------------------------------------------------------------------------
304 # Internal functions
305 # ------------------------------------------------------------------------------
306
307 # Parse command line arguments.
308 function parse_args() {
309         $args = getopt('f:d:u:');
310
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>');
313         }
314
315         if (is_null($args[f])) {
316                 error('You must specify an index file name with the -f option.');
317         }
318
319         if (is_null($args[d])) {
320                 error('You must specify a directory for the output file with the -d option.');
321         }
322
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.');
325         }
326
327         $index_file = $args[f];
328         $output_dir = $args[d];
329         $output_url = $args[u];
330
331         if (file_exists($output_dir)) {
332                 if (is_writable($output_dir) === FALSE) {
333                         error("$output_dir is not writable.");
334                 }
335         }        else {
336                 error("output directory $output_dir does not exist.");
337         }
338
339         $paths = array(
340                                    'index_file' => $index_file,
341                                    'output_dir' => trailing_slash($output_dir),
342                                    'output_url' => trailing_slash($output_url),
343                                    );
344
345         return $paths;
346 }
347
348 # Ensure paths end with a "/".
349 function trailing_slash($path) {
350         if (preg_match('/\/$/', $path) == 0) {
351                 $path .= '/';
352         }
353
354         return $path;
355 }
356
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.');
363         }
364
365         if (($fh_out = fopen($path,'w')) === FALSE) {
366                 error("couldn't open $path for writing.");
367         }
368
369         if (fwrite($fh_out, $data) === FALSE) {
370                 error("couldn't write to $path.");
371         }
372 }
373
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!';
378         }
379
380         echo "Error: $error_msg\n";
381         exit(1);
382 }
383
384 ?>