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