]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - sitemap.php
unique key on a boolean value is not scalable
[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' => 'hourly',
36                                                                  'priority' => '1',
37                                                                  )
38                                                    );
39         }
40
41         $index_path = $output_dir . $output_paths['index_file'];
42
43         write_file($index_path, urlset($index_urls));
44
45 }
46
47 # Generate sitemap of standard site elements.
48 function standard_map() {
49         global $output_paths;
50
51         $standard_map_urls .= url(
52                                                           array(
53                                                                         'url' => common_local_url('public'),
54                                                                         'changefreq' => 'hourly',
55                                                                         'priority' => '1',
56                                                                         )
57                                                           );
58
59         $standard_map_urls .= url(
60                                                           array(
61                                                                         'url' => common_local_url('publicrss'),
62                                                                         'changefreq' => 'hourly',
63                                                                         'priority' => '0.3',
64                                                                         )
65                                                           );
66
67         $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
68
69         foreach($docs as $title) {
70                 $standard_map_urls .= url(
71                                                                   array(
72                                                                                 'url' => common_local_url('doc', array('title' => $title)),
73                                                                                 'changefreq' => 'monthly',
74                                                                                 'priority'   => '0.2',
75                                                                                 )
76                                                                   );
77         }
78
79         $urlset_path = $output_paths['output_dir'] . 'standard.xml';
80
81         write_file($urlset_path, urlset($standard_map_urls));
82 }
83
84 # Generate sitemaps of all notices.
85 function notices_map() {
86         global $output_paths;
87
88         $notices = DB_DataObject::factory('notice');
89
90         $notices->query('SELECT uri, modified FROM notice');
91
92         $notice_count = 0;
93         $map_count = 1;
94
95         while ($notices->fetch()) {
96
97                 # Maximum 50,000 URLs per sitemap file.
98                 if ($notice_count == 50000) {
99                         $notice_count = 0;
100                         $map_count++;
101                 }
102
103                 $notice = array(
104                                                 'url'        => $notices->uri,
105                                                 'lastmod'    => preg_replace('/ /', 'T', $notices->modified), # W3C DTF requires "T" separator
106                                                 'changefreq' => 'hourly',
107                                                 'priority'   => '1',
108                                                 );
109
110                 $notice_list[$map_count] .= url($notice);
111
112                 $notice_count++;
113         }
114
115         # Make full sitemaps from the lists and save them.
116         array_to_map($notice_list, 'notice');
117 }
118
119 # Generate sitemaps of all users.
120 function user_map() {
121         global $output_paths;
122
123         $users = DB_DataObject::factory('user');
124
125         $users->query('SELECT id, nickname FROM user');
126
127         $user_count = 0;
128         $map_count = 1;
129
130         while ($users->fetch()) {
131
132                 # Maximum 50,000 URLs per sitemap file.
133                 if ($user_count == 50000) {
134                         $user_count = 0;
135                         $map_count++;
136                 }
137
138                 $user_args = array('nickname' => $users->nickname);
139
140                 # Define parameters for generating <url></url> elements.
141                 $user = array(
142                                           'url'        => common_local_url('showstream', $user_args),
143                                           'changefreq' => 'hourly',
144                                           'priority'   => '1',
145                                           );
146
147                 $user_rss = array(
148                                                   'url'        => common_local_url('userrss', $user_args),
149                                                   'changefreq' => 'hourly',
150                                                   'priority'   => '0.3',
151                                                   );
152
153                 $all = array(
154                                          'url'        => common_local_url('all', $user_args),
155                                          'changefreq' => 'hourly',
156                                          'priority'   => '1',
157                                          );
158
159                 $all_rss = array(
160                                                  'url'        => common_local_url('allrss', $user_args),
161                                                  'changefreq' => 'hourly',
162                                                  'priority'   => '0.3',
163                                                  );
164
165                 $replies = array(
166                                                  'url'        => common_local_url('replies', $user_args),
167                                                  'changefreq' => 'hourly',
168                                                  'priority'   => '1',
169                                                  );
170
171                 $replies_rss = array(
172                                                          'url'        => common_local_url('repliesrss', $user_args),
173                                                          'changefreq' => 'hourly',
174                                                          'priority'   => '0.3',
175                                                          );
176
177                 $foaf = array(
178                                           'url'        => common_local_url('foaf', $user_args),
179                                           'changefreq' => 'weekly',
180                                           'priority'   => '0.5',
181                                           );
182
183                 # Construct a <url></url> element for each user facet and add it
184                 # to our existing list of those.
185                 $user_list[$map_count]        .= url($user);
186                 $user_rss_list[$map_count]    .= url($user_rss);
187                 $all_list[$map_count]         .= url($all);
188                 $all_rss_list[$map_count]     .= url($all_rss);
189                 $replies_list[$map_count]     .= url($replies);
190                 $replies_rss_list[$map_count] .= url($replies_rss);
191                 $foaf_list[$map_count]        .= url($foaf);
192
193                 $user_count++;
194         }
195
196         # Make full sitemaps from the lists and save them.
197         # Possible factoring: put all the lists into a master array, thus allowing
198         # calling with single argument (i.e., array_to_map('user')).
199         array_to_map($user_list, 'user');
200         array_to_map($user_rss_list, 'user_rss');
201         array_to_map($all_list, 'all');
202         array_to_map($all_rss_list, 'all_rss');
203         array_to_map($replies_list, 'replies');
204         array_to_map($replies_rss_list, 'replies_rss');
205         array_to_map($foaf_list, 'foaf');
206 }
207
208 # Generate sitemaps of all avatars.
209 function avatar_map() {
210         global $output_paths;
211
212         $avatars = DB_DataObject::factory('avatar');
213
214         $avatars->query('SELECT url, modified FROM avatar');
215
216         $avatar_count = 0;
217         $map_count = 1;
218
219         while ($avatars->fetch()) {
220
221                 # We only want the original size and 24px thumbnail version - skip 96px.
222                 if (preg_match('/-96-/', $avatars->url)) {
223                         continue;
224                 }
225
226                 # Maximum 50,000 URLs per sitemap file.
227                 if ($avatar_count == 50000) {
228                         $avatar_count = 0;
229                         $map_count++;
230                 }
231
232                 $image = array(
233                                            'url'        => $avatars->url,
234                                            'lastmod'    => preg_replace('/ /', 'T', $avatars->modified), # W3C DTF requires "T" separator
235                                            'changefreq' => 'monthly',
236                                            'priority'   => '0.2',
237                                            );
238
239                 # Construct a <url></url> element for each avatar and add it
240                 # to our existing list of those.
241                 $avatar_list[$map_count] .= url($image);
242         }
243
244         array_to_map($avatar_list, 'avatars');
245 }
246
247 # ------------------------------------------------------------------------------
248 # XML generation functions
249 # ------------------------------------------------------------------------------
250
251 # Generate a <url></url> element.
252 function url($url_args) {
253         $url        = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
254         $lastmod    = $url_args['lastmod'];
255         $changefreq = $url_args['changefreq'];
256         $priority   = $url_args['priority'];
257
258         if (is_null($url)) {
259                 error("url() arguments require 'url' value.");
260         }
261
262         $url_out = "\t<url>\n";
263         $url_out .= "\t\t<loc>$url</loc>\n";
264
265         if ($changefreq) {
266                 $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
267         }
268
269         if ($lastmod) {
270                 $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
271         }
272
273         if ($priority) {
274                 $url_out .= "\t\t<priority>$priority</priority>\n";
275         }
276
277         $url_out .= "\t</url>\n";
278
279         return $url_out;
280 }
281
282 # Generate a <urlset></urlset> element.
283 function urlset($urlset_text) {
284         $urlset = '<?xml version="1.0" encoding="UTF-8"?>'.
285           '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.
286           $urlset_text.
287           '</urlset>';
288
289         return $urlset;
290 }
291
292 # Generate a sitemap from an array containing <url></url> elements and write it to a file.
293 function array_to_map($url_list, $filename_prefix) {
294         global $output_paths;
295
296         # $map_urls is a long string containing concatenated <url></url> elements.
297         while (list($map_idx, $map_urls) = each($url_list)) {
298                 $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
299
300                 write_file($urlset_path, urlset($map_urls));
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 name> -d <output directory> -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 # Ensure paths end with a "/".
350 function trailing_slash($path) {
351         if (preg_match('/\/$/', $path) == 0) {
352                 $path .= '/';
353         }
354
355         return $path;
356 }
357
358 # Write data to disk.
359 function write_file($path, $data) {
360         if (is_null($path)) {
361                 error('No path specified for writing to.');
362         }        elseif (is_null($data)) {
363                 error('No data specified for writing.');
364         }
365
366         if (($fh_out = fopen($path,'w')) === FALSE) {
367                 error("couldn't open $path for writing.");
368         }
369
370         if (fwrite($fh_out, $data) === FALSE) {
371                 error("couldn't write to $path.");
372         }
373 }
374
375 # Display an error message and exit.
376 function error ($error_msg) {
377         if (is_null($error_msg)) {
378                 $error_msg = 'error() was called without any explanation!';
379         }
380
381         echo "Error: $error_msg\n";
382         exit(1);
383 }
384
385 ?>