Fixed some doxygen warnings
[lhc/web/wiklou.git] / maintenance / generateSitemap.php
1 <?php
2 define( 'GS_MAIN', -2 );
3 define( 'GS_TALK', -1 );
4 /**
5 * Creates a sitemap for the site
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @ingroup Maintenance
23 *
24 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
25 * @copyright Copyright © 2005, Jens Frank <jeluf@gmx.de>
26 * @copyright Copyright © 2005, Brion Vibber <brion@pobox.com>
27 *
28 * @see http://www.sitemaps.org/
29 * @see http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
30 *
31 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
32 */
33
34 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
35
36 class GenerateSitemap extends Maintenance {
37 /**
38 * The maximum amount of urls in a sitemap file
39 *
40 * @link http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
41 *
42 * @var int
43 */
44 var $url_limit;
45
46 /**
47 * The maximum size of a sitemap file
48 *
49 * @link http://www.sitemaps.org/faq.php#faq_sitemap_size
50 *
51 * @var int
52 */
53 var $size_limit;
54
55 /**
56 * The path to prepend to the filename
57 *
58 * @var string
59 */
60 var $fspath;
61
62 /**
63 * The path to append to the domain name
64 *
65 * @var string
66 */
67 var $path;
68
69 /**
70 * Whether or not to use compression
71 *
72 * @var bool
73 */
74 var $compress;
75
76 /**
77 * The number of entries to save in each sitemap file
78 *
79 * @var array
80 */
81 var $limit = array();
82
83 /**
84 * Key => value entries of namespaces and their priorities
85 *
86 * @var array
87 */
88 var $priorities = array();
89
90 /**
91 * A one-dimensional array of namespaces in the wiki
92 *
93 * @var array
94 */
95 var $namespaces = array();
96
97 /**
98 * When this sitemap batch was generated
99 *
100 * @var string
101 */
102 var $timestamp;
103
104 /**
105 * A database slave object
106 *
107 * @var object
108 */
109 var $dbr;
110
111 /**
112 * A resource pointing to the sitemap index file
113 *
114 * @var resource
115 */
116 var $findex;
117
118
119 /**
120 * A resource pointing to a sitemap file
121 *
122 * @var resource
123 */
124 var $file;
125
126 /**
127 * Constructor
128 */
129 public function __construct() {
130 parent::__construct();
131 $this->mDescription = "Creates a sitemap for the site";
132 $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap' .
133 "\n\t\tdefaults to current directory", false, true );
134 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g.\n" .
135 "\t\thttp://en.wikipedia.org. This is sometimes necessary because\n" .
136 "\t\tserver name detection may fail in command line scripts.", false, true );
137 $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes', false, true );
138 }
139
140 /**
141 * Execute
142 */
143 public function execute() {
144 global $wgScriptPath;
145 $this->setNamespacePriorities();
146 $this->url_limit = 50000;
147 $this->size_limit = pow( 2, 20 ) * 10;
148 $this->fspath = self::init_path( $this->getOption( 'fspath', getcwd() ) );
149 $this->compress = $this->getOption( 'compress', 'yes' ) !== 'no';
150 $this->dbr = wfGetDB( DB_SLAVE );
151 $this->generateNamespaces();
152 $this->timestamp = wfTimestamp( TS_ISO_8601, wfTimestampNow() );
153 $this->findex = fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
154 $this->main();
155 }
156
157 private function setNamespacePriorities() {
158 // Custom main namespaces
159 $this->priorities[GS_MAIN] = '0.5';
160 // Custom talk namesspaces
161 $this->priorities[GS_TALK] = '0.1';
162 // MediaWiki standard namespaces
163 $this->priorities[NS_MAIN] = '1.0';
164 $this->priorities[NS_TALK] = '0.1';
165 $this->priorities[NS_USER] = '0.5';
166 $this->priorities[NS_USER_TALK] = '0.1';
167 $this->priorities[NS_PROJECT] = '0.5';
168 $this->priorities[NS_PROJECT_TALK] = '0.1';
169 $this->priorities[NS_FILE] = '0.5';
170 $this->priorities[NS_FILE_TALK] = '0.1';
171 $this->priorities[NS_MEDIAWIKI] = '0.0';
172 $this->priorities[NS_MEDIAWIKI_TALK] = '0.1';
173 $this->priorities[NS_TEMPLATE] = '0.0';
174 $this->priorities[NS_TEMPLATE_TALK] = '0.1';
175 $this->priorities[NS_HELP] = '0.5';
176 $this->priorities[NS_HELP_TALK] = '0.1';
177 $this->priorities[NS_CATEGORY] = '0.5';
178 $this->priorities[NS_CATEGORY_TALK] = '0.1';
179 }
180
181 /**
182 * Create directory if it does not exist and return pathname with a trailing slash
183 */
184 private static function init_path( $fspath ) {
185 if ( !isset( $fspath ) ) {
186 return null;
187 }
188 # Create directory if needed
189 if ( $fspath && !is_dir( $fspath ) ) {
190 wfMkdirParents( $fspath ) or die( "Can not create directory $fspath.\n" );
191 }
192
193 return realpath( $fspath ) . DIRECTORY_SEPARATOR ;
194 }
195
196 /**
197 * Generate a one-dimensional array of existing namespaces
198 */
199 function generateNamespaces() {
200 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
201 global $wgSitemapNamespaces;
202 if ( is_array( $wgSitemapNamespaces ) ) {
203 $this->namespaces = $wgSitemapNamespaces;
204 return;
205 }
206
207 $res = $this->dbr->select( 'page',
208 array( 'page_namespace' ),
209 array(),
210 __METHOD__,
211 array(
212 'GROUP BY' => 'page_namespace',
213 'ORDER BY' => 'page_namespace',
214 )
215 );
216
217 foreach ( $res as $row )
218 $this->namespaces[] = $row->page_namespace;
219 }
220
221 /**
222 * Get the priority of a given namespace
223 *
224 * @param $namespace Integer: the namespace to get the priority for
225 * @return String
226 */
227 function priority( $namespace ) {
228 return isset( $this->priorities[$namespace] ) ? $this->priorities[$namespace] : $this->guessPriority( $namespace );
229 }
230
231 /**
232 * If the namespace isn't listed on the priority list return the
233 * default priority for the namespace, varies depending on whether it's
234 * a talkpage or not.
235 *
236 * @param $namespace Integer: the namespace to get the priority for
237 * @return String
238 */
239 function guessPriority( $namespace ) {
240 return MWNamespace::isMain( $namespace ) ? $this->priorities[GS_MAIN] : $this->priorities[GS_TALK];
241 }
242
243 /**
244 * Return a database resolution of all the pages in a given namespace
245 *
246 * @param $namespace Integer: limit the query to this namespace
247 * @return Resource
248 */
249 function getPageRes( $namespace ) {
250 return $this->dbr->select( 'page',
251 array(
252 'page_namespace',
253 'page_title',
254 'page_touched',
255 ),
256 array( 'page_namespace' => $namespace ),
257 __METHOD__
258 );
259 }
260
261 /**
262 * Main loop
263 */
264 public function main() {
265 global $wgContLang;
266
267 fwrite( $this->findex, $this->openIndex() );
268
269 foreach ( $this->namespaces as $namespace ) {
270 $res = $this->getPageRes( $namespace );
271 $this->file = false;
272 $this->generateLimit( $namespace );
273 $length = $this->limit[0];
274 $i = $smcount = 0;
275
276 $fns = $wgContLang->getFormattedNsText( $namespace );
277 $this->output( "$namespace ($fns)" );
278 foreach ( $res as $row ) {
279 if ( $i++ === 0 || $i === $this->url_limit + 1 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit ) {
280 if ( $this->file !== false ) {
281 $this->write( $this->file, $this->closeFile() );
282 $this->close( $this->file );
283 }
284 $filename = $this->sitemapFilename( $namespace, $smcount++ );
285 $this->file = $this->open( $this->fspath . $filename, 'wb' );
286 $this->write( $this->file, $this->openFile() );
287 fwrite( $this->findex, $this->indexEntry( $filename ) );
288 $this->output( "\t$this->fspath$filename\n" );
289 $length = $this->limit[0];
290 $i = 1;
291 }
292 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
293 $date = wfTimestamp( TS_ISO_8601, $row->page_touched );
294 $entry = $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) );
295 $length += strlen( $entry );
296 $this->write( $this->file, $entry );
297 // generate pages for language variants
298 if ( $wgContLang->hasVariants() ) {
299 $variants = $wgContLang->getVariants();
300 foreach ( $variants as $vCode ) {
301 if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
302 $entry = $this->fileEntry( $title->getFullURL( '', $vCode ), $date, $this->priority( $namespace ) );
303 $length += strlen( $entry );
304 $this->write( $this->file, $entry );
305 }
306 }
307 }
308 if ( $this->file ) {
309 $this->write( $this->file, $this->closeFile() );
310 $this->close( $this->file );
311 }
312 }
313 fwrite( $this->findex, $this->closeIndex() );
314 fclose( $this->findex );
315 }
316
317 /**
318 * gzopen() / fopen() wrapper
319 *
320 * @return Resource
321 */
322 function open( $file, $flags ) {
323 return $this->compress ? gzopen( $file, $flags ) : fopen( $file, $flags );
324 }
325
326 /**
327 * gzwrite() / fwrite() wrapper
328 */
329 function write( &$handle, $str ) {
330 if ( $this->compress )
331 gzwrite( $handle, $str );
332 else
333 fwrite( $handle, $str );
334 }
335
336 /**
337 * gzclose() / fclose() wrapper
338 */
339 function close( &$handle ) {
340 if ( $this->compress )
341 gzclose( $handle );
342 else
343 fclose( $handle );
344 }
345
346 /**
347 * Get a sitemap filename
348 *
349 * @param $namespace Integer: the namespace
350 * @param $count Integer: the count
351 * @return String
352 */
353 function sitemapFilename( $namespace, $count ) {
354 $ext = $this->compress ? '.gz' : '';
355 return "sitemap-" . wfWikiID() . "-NS_$namespace-$count.xml$ext";
356 }
357
358 /**
359 * Return the XML required to open an XML file
360 *
361 * @return string
362 */
363 function xmlHead() {
364 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
365 }
366
367 /**
368 * Return the XML schema being used
369 *
370 * @return String
371 */
372 function xmlSchema() {
373 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
374 }
375
376 /**
377 * Return the XML required to open a sitemap index file
378 *
379 * @return String
380 */
381 function openIndex() {
382 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
383 }
384
385 /**
386 * Return the XML for a single sitemap indexfile entry
387 *
388 * @param $filename String: the filename of the sitemap file
389 * @return String
390 */
391 function indexEntry( $filename ) {
392 return
393 "\t<sitemap>\n" .
394 "\t\t<loc>$filename</loc>\n" .
395 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
396 "\t</sitemap>\n";
397 }
398
399 /**
400 * Return the XML required to close a sitemap index file
401 *
402 * @return String
403 */
404 function closeIndex() {
405 return "</sitemapindex>\n";
406 }
407
408 /**
409 * Return the XML required to open a sitemap file
410 *
411 * @return String
412 */
413 function openFile() {
414 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
415 }
416
417 /**
418 * Return the XML for a single sitemap entry
419 *
420 * @param $url String: an RFC 2396 compliant URL
421 * @param $date String: a ISO 8601 date
422 * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
423 * @return String
424 */
425 function fileEntry( $url, $date, $priority ) {
426 return
427 "\t<url>\n" .
428 "\t\t<loc>$url</loc>\n" .
429 "\t\t<lastmod>$date</lastmod>\n" .
430 "\t\t<priority>$priority</priority>\n" .
431 "\t</url>\n";
432 }
433
434 /**
435 * Return the XML required to close sitemap file
436 *
437 * @return String
438 */
439 function closeFile() {
440 return "</urlset>\n";
441 }
442
443 /**
444 * Populate $this->limit
445 */
446 function generateLimit( $namespace ) {
447 $title = Title::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
448
449 $this->limit = array(
450 strlen( $this->openFile() ),
451 strlen( $this->fileEntry( $title->getFullUrl(), wfTimestamp( TS_ISO_8601, wfTimestamp() ), $this->priority( $namespace ) ) ),
452 strlen( $this->closeFile() )
453 );
454 }
455 }
456
457 $maintClass = "GenerateSitemap";
458 require_once( DO_MAINTENANCE );