]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - sitemap.php
876c25251b7944fd981aba336c4c77b116c23f45
[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                 $image = array(
229                                            'url'        => common_avatar_display_url($avatars),
230                                            'lastmod'    => common_date_w3dtf($avatars->modified),
231                                            'changefreq' => 'monthly',
232                                            'priority'   => '0.2',
233                                            );
234
235                 # Construct a <url></url> element for each avatar and add it
236                 # to our existing list of those.
237                 $avatar_list[$map_count] .= url($image);
238         }
239
240         array_to_map($avatar_list, 'avatars');
241 }
242
243 # ------------------------------------------------------------------------------
244 # XML generation functions
245 # ------------------------------------------------------------------------------
246
247 # Generate a <url></url> element.
248 function url($url_args) {
249         $url        = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
250         $lastmod    = $url_args['lastmod'];
251         $changefreq = $url_args['changefreq'];
252         $priority   = $url_args['priority'];
253
254         if (is_null($url)) {
255                 error("url() arguments require 'url' value.");
256         }
257
258         $url_out = "\t<url>\n";
259         $url_out .= "\t\t<loc>$url</loc>\n";
260
261         if ($changefreq) {
262                 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
263         }
264
265         if ($lastmod) {
266                 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
267         }
268
269         if ($priority) {
270                 $url_out .= "\t\t<priority>$priority</priority>\n";
271         }
272
273         $url_out .= "\t</url>\n";
274
275         return $url_out;
276 }
277
278 # Generate a <urlset></urlset> element.
279 function urlset($urlset_text) {
280         $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
281           '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
282           $urlset_text .
283           '</urlset>';
284
285         return $urlset;
286 }
287
288 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
289 function array_to_map($url_list, $filename_prefix) {
290         global $output_paths;
291
292         if ($url_list) {
293                 # $map_urls is a long string containing concatenated <url></url> elements.
294                 while (list($map_idx, $map_urls) = each($url_list)) {
295                         $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
296                         
297                         write_file($urlset_path, urlset($map_urls));
298                 }
299         }
300 }
301
302 # ------------------------------------------------------------------------------
303 # Internal functions
304 # ------------------------------------------------------------------------------
305
306 # Parse command line arguments.
307 function parse_args() {
308         $args = getopt('f:d:u:');
309
310         if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
311                 error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
312         }
313
314         if (is_null($args[f])) {
315                 error('You must specify an index file name with the -f option.');
316         }
317
318         if (is_null($args[d])) {
319                 error('You must specify a directory for the output file with the -d option.');
320         }
321
322         if (is_null($args[u])) {
323                 error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
324         }
325
326         $index_file = $args[f];
327         $output_dir = $args[d];
328         $output_url = $args[u];
329
330         if (file_exists($output_dir)) {
331                 if (is_writable($output_dir) === FALSE) {
332                         error("$output_dir is not writable.");
333                 }
334         }        else {
335                 error("output directory $output_dir does not exist.");
336         }
337
338         $paths = array(
339                                    'index_file' => $index_file,
340                                    'output_dir' => trailing_slash($output_dir),
341                                    'output_url' => trailing_slash($output_url),
342                                    );
343
344         return $paths;
345 }
346
347 # Ensure paths end with a "/".
348 function trailing_slash($path) {
349         if (preg_match('/\/$/', $path) == 0) {
350                 $path .= '/';
351         }
352
353         return $path;
354 }
355
356 # Write data to disk.
357 function write_file($path, $data) {
358         if (is_null($path)) {
359                 error('No path specified for writing to.');
360         }        elseif (is_null($data)) {
361                 error('No data specified for writing.');
362         }
363
364         if (($fh_out = fopen($path,'w')) === FALSE) {
365                 error("couldn't open $path for writing.");
366         }
367
368         if (fwrite($fh_out, $data) === FALSE) {
369                 error("couldn't write to $path.");
370         }
371 }
372
373 # Display an error message and exit.
374 function error ($error_msg) {
375         if (is_null($error_msg)) {
376                 $error_msg = 'error() was called without any explanation!';
377         }
378
379         echo "Error: $error_msg\n";
380         exit(1);
381 }
382
383 ?>