29 require_once __DIR__ .
'/Maintenance.php';
85 public $skipRedirects;
99 public $priorities = [];
144 parent::__construct();
148 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory',
154 'The URL path corresponding to --fspath, prepended to filenames in the index; '
155 .
'defaults to an empty string',
161 'Compress the sitemap files, can take value yes|no, default yes',
165 $this->
addOption(
'skip-redirects',
'Do not include redirecting articles in the sitemap' );
168 'What site identifier to use for the wiki, defaults to $wgDBname',
178 $this->setNamespacePriorities();
179 $this->url_limit = 50000;
180 $this->size_limit = pow( 2, 20 ) * 10;
182 # Create directory if needed
183 $fspath = $this->
getOption(
'fspath', getcwd() );
185 $this->
error(
"Can not create directory $fspath.", 1 );
188 $this->fspath = realpath( $fspath ) . DIRECTORY_SEPARATOR;
189 $this->urlpath = $this->
getOption(
'urlpath',
"" );
190 if ( $this->urlpath !==
"" && substr( $this->urlpath, -1 ) !==
'/' ) {
191 $this->urlpath .=
'/';
194 $this->compress = $this->
getOption(
'compress',
'yes' ) !==
'no';
195 $this->skipRedirects = $this->
hasOption(
'skip-redirects' );
197 $this->generateNamespaces();
199 $this->findex = fopen(
"{$this->fspath}sitemap-index-{$this->identifier}.xml",
'wb' );
203 private function setNamespacePriorities() {
207 $this->priorities[self::GS_MAIN] =
'0.5';
209 $this->priorities[self::GS_TALK] =
'0.1';
211 $this->priorities[
NS_MAIN] =
'1.0';
212 $this->priorities[
NS_TALK] =
'0.1';
213 $this->priorities[
NS_USER] =
'0.5';
217 $this->priorities[
NS_FILE] =
'0.5';
223 $this->priorities[
NS_HELP] =
'0.5';
234 $float = floatval( $priority );
235 if ( $float > 1.0 ) {
237 } elseif ( $float < 0.0 ) {
240 $this->priorities[$namespace] = $priority;
248 function generateNamespaces() {
257 $res = $this->dbr->select(
'page',
258 [
'page_namespace' ],
262 'GROUP BY' =>
'page_namespace',
263 'ORDER BY' =>
'page_namespace',
267 foreach (
$res as $row ) {
278 function priority( $namespace ) {
279 return isset( $this->priorities[$namespace] )
280 ? $this->priorities[$namespace]
281 : $this->guessPriority( $namespace );
292 function guessPriority( $namespace ) {
294 ? $this->priorities[self::GS_MAIN]
295 : $this->priorities[self::GS_TALK];
304 function getPageRes( $namespace ) {
305 return $this->dbr->select(
'page',
312 [
'page_namespace' => $namespace ],
320 public function main() {
323 fwrite( $this->findex, $this->openIndex() );
326 $res = $this->getPageRes( $namespace );
328 $this->generateLimit( $namespace );
329 $length = $this->limit[0];
332 $fns =
$wgContLang->getFormattedNsText( $namespace );
333 $this->
output(
"$namespace ($fns)\n" );
334 $skippedRedirects = 0;
335 foreach (
$res as $row ) {
336 if ( $this->skipRedirects && $row->page_is_redirect ) {
342 || $i === $this->url_limit + 1
343 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit
345 if ( $this->
file !==
false ) {
346 $this->write( $this->
file, $this->closeFile() );
347 $this->close( $this->
file );
349 $filename = $this->sitemapFilename( $namespace, $smcount++ );
350 $this->
file = $this->
open( $this->fspath . $filename,
'wb' );
351 $this->write( $this->
file, $this->openFile() );
352 fwrite( $this->findex, $this->indexEntry( $filename ) );
353 $this->
output(
"\t$this->fspath$filename\n" );
354 $length = $this->limit[0];
358 $date =
wfTimestamp( TS_ISO_8601, $row->page_touched );
359 $entry = $this->fileEntry(
$title->getCanonicalURL(), $date, $this->priority( $namespace ) );
360 $length += strlen( $entry );
361 $this->write( $this->
file, $entry );
365 foreach ( $variants
as $vCode ) {
369 $entry = $this->fileEntry(
370 $title->getCanonicalURL(
'', $vCode ),
372 $this->priority( $namespace )
374 $length += strlen( $entry );
375 $this->write( $this->
file, $entry );
380 if ( $this->skipRedirects && $skippedRedirects > 0 ) {
381 $this->
output(
" skipped $skippedRedirects redirect(s)\n" );
385 $this->write( $this->
file, $this->closeFile() );
386 $this->close( $this->
file );
389 fwrite( $this->findex, $this->closeIndex() );
390 fclose( $this->findex );
401 $resource = $this->compress ? gzopen( $file,
$flags ) : fopen( $file,
$flags );
402 if ( $resource ===
false ) {
404 .
" error opening file $file with flags $flags. Check permissions?" );
416 function write( &$handle, $str ) {
417 if ( $handle ===
true || $handle ===
false ) {
418 throw new MWException( __METHOD__ .
" was passed a boolean as a file handle.\n" );
420 if ( $this->compress ) {
421 gzwrite( $handle, $str );
423 fwrite( $handle, $str );
432 function close( &$handle ) {
433 if ( $this->compress ) {
447 function sitemapFilename( $namespace, $count ) {
448 $ext = $this->compress ?
'.gz' :
'';
450 return "sitemap-{$this->identifier}-NS_$namespace-$count.xml$ext";
459 return '<?xml version="1.0" encoding="UTF-8"?>' .
"\n";
467 function xmlSchema() {
468 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
476 function openIndex() {
477 return $this->xmlHead() .
'<sitemapindex xmlns="' . $this->xmlSchema() .
'">' .
"\n";
486 function indexEntry( $filename ) {
489 "\t\t<loc>{$this->urlpath}$filename</loc>\n" .
490 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
499 function closeIndex() {
500 return "</sitemapindex>\n";
508 function openFile() {
509 return $this->xmlHead() .
'<urlset xmlns="' . $this->xmlSchema() .
'">' .
"\n";
520 function fileEntry( $url, $date, $priority ) {
524 "\t\t<loc>" . htmlspecialchars( $url ) .
"</loc>\n" .
525 "\t\t<lastmod>$date</lastmod>\n" .
526 "\t\t<priority>$priority</priority>\n" .
535 function closeFile() {
536 return "</urlset>\n";
544 function generateLimit( $namespace ) {
549 strlen( $this->openFile() ),
550 strlen( $this->fileEntry(
551 $title->getCanonicalURL(),
553 $this->priority( $namespace )
555 strlen( $this->closeFile() )