* (bug 9476) Update Russian extension translations
[mediawiki.git] / maintenance / generateSitemap.php
blob164929197f93b5c5b39a61c4484431158a1928c2
1 <?php
2 define( 'GS_MAIN', -2 );
3 define( 'GS_TALK', -1 );
4 /**
5 * Creates a Google sitemap for the site
7 * @addtogroup Maintenance
9 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
10 * @copyright Copyright © 2005, Jens Frank <jeluf@gmx.de>
11 * @copyright Copyright © 2005, Brion Vibber <brion@pobox.com>
13 * @link http://www.google.com/webmasters/sitemaps/docs/en/about.html
14 * @link http://www.google.com/schemas/sitemap/0.84/sitemap.xsd
16 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
19 class GenerateSitemap {
20 /**
21 * The maximum amount of urls in a sitemap file
23 * @link http://www.google.com/schemas/sitemap/0.84/sitemap.xsd
25 * @var int
27 var $url_limit;
29 /**
30 * The maximum size of a sitemap file
32 * @link http://www.google.com/webmasters/sitemaps/docs/en/protocol.html#faq_sitemap_size
34 * @var int
36 var $size_limit;
38 /**
39 * The path to prepend to the filename
41 * @var string
43 var $fspath;
45 /**
46 * The path to append to the domain name
48 * @var string
50 var $path;
52 /**
53 * Whether or not to use compression
55 * @var bool
57 var $compress;
59 /**
60 * The number of entries to save in each sitemap file
62 * @var array
64 var $limit = array();
66 /**
67 * Key => value entries of namespaces and their priorities
69 * @var array
71 var $priorities = array(
72 // Custom main namespaces
73 GS_MAIN => '0.5',
74 // Custom talk namesspaces
75 GS_TALK => '0.1',
76 // MediaWiki standard namespaces
77 NS_MAIN => '1.0',
78 NS_TALK => '0.1',
79 NS_USER => '0.5',
80 NS_USER_TALK => '0.1',
81 NS_PROJECT => '0.5',
82 NS_PROJECT_TALK => '0.1',
83 NS_IMAGE => '0.5',
84 NS_IMAGE_TALK => '0.1',
85 NS_MEDIAWIKI => '0.0',
86 NS_MEDIAWIKI_TALK => '0.1',
87 NS_TEMPLATE => '0.0',
88 NS_TEMPLATE_TALK => '0.1',
89 NS_HELP => '0.5',
90 NS_HELP_TALK => '0.1',
91 NS_CATEGORY => '0.5',
92 NS_CATEGORY_TALK => '0.1',
95 /**
96 * A one-dimensional array of namespaces in the wiki
98 * @var array
100 var $namespaces = array();
103 * When this sitemap batch was generated
105 * @var string
107 var $timestamp;
110 * A database slave object
112 * @var object
114 var $dbr;
117 * A resource pointing to the sitemap index file
119 * @var resource
121 var $findex;
125 * A resource pointing to a sitemap file
127 * @var resource
129 var $file;
132 * A resource pointing to php://stderr
134 * @var resource
136 var $stderr;
139 * Constructor
141 * @param string $fspath The path to prepend to the filenames, used to
142 * save them somewhere else than in the root directory
143 * @param string $path The path to append to the domain name
144 * @param bool $compress Whether to compress the sitemap files
146 function GenerateSitemap( $fspath, $path, $compress ) {
147 global $wgScriptPath;
149 $this->url_limit = 50000;
150 $this->size_limit = pow( 2, 20 ) * 10;
151 $this->fspath = isset( $fspath ) ? $fspath : '';
152 $this->path = isset( $path ) ? $path : $wgScriptPath;
153 $this->compress = $compress;
155 $this->stderr = fopen( 'php://stderr', 'wt' );
156 $this->dbr = wfGetDB( DB_SLAVE );
157 $this->generateNamespaces();
158 $this->timestamp = wfTimestamp( TS_ISO_8601, wfTimestampNow() );
159 $this->findex = fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
163 * Generate a one-dimensional array of existing namespaces
165 function generateNamespaces() {
166 $fname = 'GenerateSitemap::generateNamespaces';
168 $res = $this->dbr->select( 'page',
169 array( 'page_namespace' ),
170 array(),
171 $fname,
172 array(
173 'GROUP BY' => 'page_namespace',
174 'ORDER BY' => 'page_namespace',
178 while ( $row = $this->dbr->fetchObject( $res ) )
179 $this->namespaces[] = $row->page_namespace;
183 * Get the priority of a given namespace
185 * @param int $namespace The namespace to get the priority for
187 * @return string
190 function priority( $namespace ) {
191 return isset( $this->priorities[$namespace] ) ? $this->priorities[$namespace] : $this->guessPriority( $namespace );
195 * If the namespace isn't listed on the priority list return the
196 * default priority for the namespace, varies depending on whether it's
197 * a talkpage or not.
199 * @param int $namespace The namespace to get the priority for
201 * @return string
203 function guessPriority( $namespace ) {
204 return Namespace::isMain( $namespace ) ? $this->priorities[GS_MAIN] : $this->priorities[GS_TALK];
208 * Return a database resolution of all the pages in a given namespace
210 * @param int $namespace Limit the query to this namespace
212 * @return resource
214 function getPageRes( $namespace ) {
215 $fname = 'GenerateSitemap::getPageRes';
217 return $this->dbr->select( 'page',
218 array(
219 'page_namespace',
220 'page_title',
221 'page_touched',
223 array( 'page_namespace' => $namespace ),
224 $fname
229 * Main loop
231 * @access public
233 function main() {
234 global $wgContLang;
236 fwrite( $this->findex, $this->openIndex() );
238 foreach ( $this->namespaces as $namespace ) {
239 $res = $this->getPageRes( $namespace );
240 $this->file = false;
241 $this->generateLimit( $namespace );
242 $length = $this->limit[0];
243 $i = $smcount = 0;
245 $fns = $wgContLang->getFormattedNsText( $namespace );
246 $this->debug( "$namespace ($fns)" );
247 while ( $row = $this->dbr->fetchObject( $res ) ) {
248 if ( $i++ === 0 || $i === $this->url_limit + 1 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit ) {
249 if ( $this->file !== false ) {
250 $this->write( $this->file, $this->closeFile() );
251 $this->close( $this->file );
253 $filename = $this->sitemapFilename( $namespace, $smcount++ );
254 $this->file = $this->open( $this->fspath . $filename, 'wb' );
255 $this->write( $this->file, $this->openFile() );
256 fwrite( $this->findex, $this->indexEntry( $filename ) );
257 $this->debug( "\t$filename" );
258 $length = $this->limit[0];
259 $i = 1;
261 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
262 $date = wfTimestamp( TS_ISO_8601, $row->page_touched );
263 $entry = $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) );
264 $length += strlen( $entry );
265 $this->write( $this->file, $entry );
266 // generate pages for language variants
267 if($wgContLang->hasVariants()){
268 $variants = $wgContLang->getVariants();
269 foreach($variants as $vCode){
270 if($vCode==$wgContLang->getCode()) continue; // we don't want default variant
271 $entry = $this->fileEntry( $title->getFullURL('',$vCode), $date, $this->priority( $namespace ) );
272 $length += strlen( $entry );
273 $this->write( $this->file, $entry );
277 if ( $this->file ) {
278 $this->write( $this->file, $this->closeFile() );
279 $this->close( $this->file );
282 fwrite( $this->findex, $this->closeIndex() );
283 fclose( $this->findex );
287 * gzopen() / fopen() wrapper
289 * @return resource
291 function open( $file, $flags ) {
292 return $this->compress ? gzopen( $file, $flags ) : fopen( $file, $flags );
296 * gzwrite() / fwrite() wrapper
298 function write( &$handle, $str ) {
299 if ( $this->compress )
300 gzwrite( $handle, $str );
301 else
302 fwrite( $handle, $str );
306 * gzclose() / fclose() wrapper
308 function close( &$handle ) {
309 if ( $this->compress )
310 gzclose( $handle );
311 else
312 fclose( $handle );
316 * Get a sitemap filename
318 * @static
320 * @param int $namespace The namespace
321 * @param int $count The count
323 * @return string
325 function sitemapFilename( $namespace, $count ) {
326 $ext = $this->compress ? '.gz' : '';
327 return "sitemap-".wfWikiID()."-NS_$namespace-$count.xml$ext";
331 * Return the XML required to open an XML file
333 * @static
335 * @return string
337 function xmlHead() {
338 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
342 * Return the XML schema being used
344 * @static
346 * @returns string
348 function xmlSchema() {
349 return 'http://www.google.com/schemas/sitemap/0.84';
353 * Return the XML required to open a sitemap index file
355 * @return string
357 function openIndex() {
358 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
362 * Return the XML for a single sitemap indexfile entry
364 * @static
366 * @param string $filename The filename of the sitemap file
368 * @return string
370 function indexEntry( $filename ) {
371 return
372 "\t<sitemap>\n" .
373 "\t\t<loc>$filename</loc>\n" .
374 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
375 "\t</sitemap>\n";
379 * Return the XML required to close a sitemap index file
381 * @static
383 * @return string
385 function closeIndex() {
386 return "</sitemapindex>\n";
390 * Return the XML required to open a sitemap file
392 * @return string
394 function openFile() {
395 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
399 * Return the XML for a single sitemap entry
401 * @static
403 * @param string $url An RFC 2396 compilant URL
404 * @param string $date A ISO 8601 date
405 * @param string $priority A priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
407 * @return string
409 function fileEntry( $url, $date, $priority ) {
410 return
411 "\t<url>\n" .
412 "\t\t<loc>$url</loc>\n" .
413 "\t\t<lastmod>$date</lastmod>\n" .
414 "\t\t<priority>$priority</priority>\n" .
415 "\t</url>\n";
419 * Return the XML required to close sitemap file
421 * @static
422 * @return string
424 function closeFile() {
425 return "</urlset>\n";
429 * Write a string to stderr followed by a UNIX newline
431 function debug( $str ) {
432 fwrite( $this->stderr, "$str\n" );
436 * Populate $this->limit
438 function generateLimit( $namespace ) {
439 $title = Title::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
441 $this->limit = array(
442 strlen( $this->openFile() ),
443 strlen( $this->fileEntry( $title->getFullUrl(), wfTimestamp( TS_ISO_8601, wfTimestamp() ), $this->priority( $namespace ) ) ),
444 strlen( $this->closeFile() )
449 if ( in_array( '--help', $argv ) ) {
450 echo
451 "Usage: php generateSitemap.php [host] [options]\n" .
452 "\thost = hostname\n" .
453 "\toptions:\n" .
454 "\t\t--help\tshow this message\n" .
455 "\t\t--fspath\tThe file system path to save to, e.g /tmp/sitemap/\n" .
456 "\t\t--path\tThe http path to use, e.g. /wiki\n" .
457 "\t\t--compress=[yes|no]\tcompress the sitemap files, default yes\n";
458 die( -1 );
461 if ( isset( $argv[1] ) && strpos( $argv[1], '--' ) !== 0 )
462 $_SERVER['SERVER_NAME'] = $argv[1];
464 $optionsWithArgs = array( 'fspath', 'path', 'compress' );
465 require_once 'commandLine.inc';
467 $gs = new GenerateSitemap( @$options['fspath'], @$options['path'], @$options['compress'] !== 'no' );
468 $gs->main();