* (bug 21503) There's now a "reason" field when creating account for other users
[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 $this->setNamespacePriorities();
145 $this->url_limit = 50000;
146 $this->size_limit = pow( 2, 20 ) * 10;
147 $this->fspath = self::init_path( $this->getOption( 'fspath', getcwd() ) );
148 $this->compress = $this->getOption( 'compress', 'yes' ) !== 'no';
149 $this->dbr = wfGetDB( DB_SLAVE );
150 $this->generateNamespaces();
151 $this->timestamp = wfTimestamp( TS_ISO_8601, wfTimestampNow() );
152 $this->findex = fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
153 $this->main();
154 }
155
156 private function setNamespacePriorities() {
157 // Custom main namespaces
158 $this->priorities[GS_MAIN] = '0.5';
159 // Custom talk namesspaces
160 $this->priorities[GS_TALK] = '0.1';
161 // MediaWiki standard namespaces
162 $this->priorities[NS_MAIN] = '1.0';
163 $this->priorities[NS_TALK] = '0.1';
164 $this->priorities[NS_USER] = '0.5';
165 $this->priorities[NS_USER_TALK] = '0.1';
166 $this->priorities[NS_PROJECT] = '0.5';
167 $this->priorities[NS_PROJECT_TALK] = '0.1';
168 $this->priorities[NS_FILE] = '0.5';
169 $this->priorities[NS_FILE_TALK] = '0.1';
170 $this->priorities[NS_MEDIAWIKI] = '0.0';
171 $this->priorities[NS_MEDIAWIKI_TALK] = '0.1';
172 $this->priorities[NS_TEMPLATE] = '0.0';
173 $this->priorities[NS_TEMPLATE_TALK] = '0.1';
174 $this->priorities[NS_HELP] = '0.5';
175 $this->priorities[NS_HELP_TALK] = '0.1';
176 $this->priorities[NS_CATEGORY] = '0.5';
177 $this->priorities[NS_CATEGORY_TALK] = '0.1';
178 }
179
180 /**
181 * Create directory if it does not exist and return pathname with a trailing slash
182 */
183 private static function init_path( $fspath ) {
184 if ( !isset( $fspath ) ) {
185 return null;
186 }
187 # Create directory if needed
188 if ( $fspath && !is_dir( $fspath ) ) {
189 wfMkdirParents( $fspath ) or die( "Can not create directory $fspath.\n" );
190 }
191
192 return realpath( $fspath ) . DIRECTORY_SEPARATOR ;
193 }
194
195 /**
196 * Generate a one-dimensional array of existing namespaces
197 */
198 function generateNamespaces() {
199 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
200 global $wgSitemapNamespaces;
201 if ( is_array( $wgSitemapNamespaces ) ) {
202 $this->namespaces = $wgSitemapNamespaces;
203 return;
204 }
205
206 $res = $this->dbr->select( 'page',
207 array( 'page_namespace' ),
208 array(),
209 __METHOD__,
210 array(
211 'GROUP BY' => 'page_namespace',
212 'ORDER BY' => 'page_namespace',
213 )
214 );
215
216 foreach ( $res as $row )
217 $this->namespaces[] = $row->page_namespace;
218 }
219
220 /**
221 * Get the priority of a given namespace
222 *
223 * @param $namespace Integer: the namespace to get the priority for
224 * @return String
225 */
226 function priority( $namespace ) {
227 return isset( $this->priorities[$namespace] ) ? $this->priorities[$namespace] : $this->guessPriority( $namespace );
228 }
229
230 /**
231 * If the namespace isn't listed on the priority list return the
232 * default priority for the namespace, varies depending on whether it's
233 * a talkpage or not.
234 *
235 * @param $namespace Integer: the namespace to get the priority for
236 * @return String
237 */
238 function guessPriority( $namespace ) {
239 return MWNamespace::isMain( $namespace ) ? $this->priorities[GS_MAIN] : $this->priorities[GS_TALK];
240 }
241
242 /**
243 * Return a database resolution of all the pages in a given namespace
244 *
245 * @param $namespace Integer: limit the query to this namespace
246 * @return Resource
247 */
248 function getPageRes( $namespace ) {
249 return $this->dbr->select( 'page',
250 array(
251 'page_namespace',
252 'page_title',
253 'page_touched',
254 ),
255 array( 'page_namespace' => $namespace ),
256 __METHOD__
257 );
258 }
259
260 /**
261 * Main loop
262 */
263 public function main() {
264 global $wgContLang;
265
266 fwrite( $this->findex, $this->openIndex() );
267
268 foreach ( $this->namespaces as $namespace ) {
269 $res = $this->getPageRes( $namespace );
270 $this->file = false;
271 $this->generateLimit( $namespace );
272 $length = $this->limit[0];
273 $i = $smcount = 0;
274
275 $fns = $wgContLang->getFormattedNsText( $namespace );
276 $this->output( "$namespace ($fns)" );
277 foreach ( $res as $row ) {
278 if ( $i++ === 0 || $i === $this->url_limit + 1 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit ) {
279 if ( $this->file !== false ) {
280 $this->write( $this->file, $this->closeFile() );
281 $this->close( $this->file );
282 }
283 $filename = $this->sitemapFilename( $namespace, $smcount++ );
284 $this->file = $this->open( $this->fspath . $filename, 'wb' );
285 $this->write( $this->file, $this->openFile() );
286 fwrite( $this->findex, $this->indexEntry( $filename ) );
287 $this->output( "\t$this->fspath$filename\n" );
288 $length = $this->limit[0];
289 $i = 1;
290 }
291 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
292 $date = wfTimestamp( TS_ISO_8601, $row->page_touched );
293 $entry = $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) );
294 $length += strlen( $entry );
295 $this->write( $this->file, $entry );
296 // generate pages for language variants
297 if ( $wgContLang->hasVariants() ) {
298 $variants = $wgContLang->getVariants();
299 foreach ( $variants as $vCode ) {
300 if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
301 $entry = $this->fileEntry( $title->getFullURL( '', $vCode ), $date, $this->priority( $namespace ) );
302 $length += strlen( $entry );
303 $this->write( $this->file, $entry );
304 }
305 }
306 }
307 if ( $this->file ) {
308 $this->write( $this->file, $this->closeFile() );
309 $this->close( $this->file );
310 }
311 }
312 fwrite( $this->findex, $this->closeIndex() );
313 fclose( $this->findex );
314 }
315
316 /**
317 * gzopen() / fopen() wrapper
318 *
319 * @return Resource
320 */
321 function open( $file, $flags ) {
322 return $this->compress ? gzopen( $file, $flags ) : fopen( $file, $flags );
323 }
324
325 /**
326 * gzwrite() / fwrite() wrapper
327 */
328 function write( &$handle, $str ) {
329 if ( $this->compress )
330 gzwrite( $handle, $str );
331 else
332 fwrite( $handle, $str );
333 }
334
335 /**
336 * gzclose() / fclose() wrapper
337 */
338 function close( &$handle ) {
339 if ( $this->compress )
340 gzclose( $handle );
341 else
342 fclose( $handle );
343 }
344
345 /**
346 * Get a sitemap filename
347 *
348 * @param $namespace Integer: the namespace
349 * @param $count Integer: the count
350 * @return String
351 */
352 function sitemapFilename( $namespace, $count ) {
353 $ext = $this->compress ? '.gz' : '';
354 return "sitemap-" . wfWikiID() . "-NS_$namespace-$count.xml$ext";
355 }
356
357 /**
358 * Return the XML required to open an XML file
359 *
360 * @return string
361 */
362 function xmlHead() {
363 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
364 }
365
366 /**
367 * Return the XML schema being used
368 *
369 * @return String
370 */
371 function xmlSchema() {
372 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
373 }
374
375 /**
376 * Return the XML required to open a sitemap index file
377 *
378 * @return String
379 */
380 function openIndex() {
381 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
382 }
383
384 /**
385 * Return the XML for a single sitemap indexfile entry
386 *
387 * @param $filename String: the filename of the sitemap file
388 * @return String
389 */
390 function indexEntry( $filename ) {
391 return
392 "\t<sitemap>\n" .
393 "\t\t<loc>$filename</loc>\n" .
394 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
395 "\t</sitemap>\n";
396 }
397
398 /**
399 * Return the XML required to close a sitemap index file
400 *
401 * @return String
402 */
403 function closeIndex() {
404 return "</sitemapindex>\n";
405 }
406
407 /**
408 * Return the XML required to open a sitemap file
409 *
410 * @return String
411 */
412 function openFile() {
413 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
414 }
415
416 /**
417 * Return the XML for a single sitemap entry
418 *
419 * @param $url String: an RFC 2396 compliant URL
420 * @param $date String: a ISO 8601 date
421 * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
422 * @return String
423 */
424 function fileEntry( $url, $date, $priority ) {
425 return
426 "\t<url>\n" .
427 "\t\t<loc>$url</loc>\n" .
428 "\t\t<lastmod>$date</lastmod>\n" .
429 "\t\t<priority>$priority</priority>\n" .
430 "\t</url>\n";
431 }
432
433 /**
434 * Return the XML required to close sitemap file
435 *
436 * @return String
437 */
438 function closeFile() {
439 return "</urlset>\n";
440 }
441
442 /**
443 * Populate $this->limit
444 */
445 function generateLimit( $namespace ) {
446 $title = Title::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
447
448 $this->limit = array(
449 strlen( $this->openFile() ),
450 strlen( $this->fileEntry( $title->getFullUrl(), wfTimestamp( TS_ISO_8601, wfTimestamp() ), $this->priority( $namespace ) ) ),
451 strlen( $this->closeFile() )
452 );
453 }
454 }
455
456 $maintClass = "GenerateSitemap";
457 require_once( DO_MAINTENANCE );