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