31 require_once __DIR__ .
'/Maintenance.php';
87 public $skipRedirects;
101 public $priorities = [];
146 parent::__construct();
150 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory',
156 'The URL path corresponding to --fspath, prepended to filenames in the index; '
157 .
'defaults to an empty string',
163 'Compress the sitemap files, can take value yes|no, default yes',
167 $this->
addOption(
'skip-redirects',
'Do not include redirecting articles in the sitemap' );
170 'What site identifier to use for the wiki, defaults to $wgDBname',
180 $this->setNamespacePriorities();
181 $this->url_limit = 50000;
182 $this->size_limit = ( 2 ** 20 ) * 10;
184 # Create directory if needed
185 $fspath = $this->
getOption(
'fspath', getcwd() );
187 $this->
fatalError(
"Can not create directory $fspath." );
190 $this->fspath = realpath( $fspath ) . DIRECTORY_SEPARATOR;
191 $this->urlpath = $this->
getOption(
'urlpath',
"" );
192 if ( $this->urlpath !==
"" && substr( $this->urlpath, -1 ) !==
'/' ) {
193 $this->urlpath .=
'/';
196 $this->compress = $this->
getOption(
'compress',
'yes' ) !==
'no';
197 $this->skipRedirects = $this->
hasOption(
'skip-redirects' );
199 $this->generateNamespaces();
201 $this->findex = fopen(
"{$this->fspath}sitemap-index-{$this->identifier}.xml",
'wb' );
205 private function setNamespacePriorities() {
209 $this->priorities[self::GS_MAIN] =
'0.5';
211 $this->priorities[self::GS_TALK] =
'0.1';
213 $this->priorities[
NS_MAIN] =
'1.0';
214 $this->priorities[
NS_TALK] =
'0.1';
215 $this->priorities[
NS_USER] =
'0.5';
219 $this->priorities[
NS_FILE] =
'0.5';
225 $this->priorities[
NS_HELP] =
'0.5';
236 $float = floatval( $priority );
237 if ( $float > 1.0 ) {
239 } elseif ( $float < 0.0 ) {
242 $this->priorities[$namespace] = $priority;
250 function generateNamespaces() {
259 $res = $this->dbr->select(
'page',
260 [
'page_namespace' ],
264 'GROUP BY' =>
'page_namespace',
265 'ORDER BY' =>
'page_namespace',
269 foreach (
$res as $row ) {
280 function priority( $namespace ) {
281 return $this->priorities[$namespace] ?? $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() {
321 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
323 fwrite( $this->findex, $this->openIndex() );
326 $res = $this->getPageRes( $namespace );
328 $this->generateLimit( $namespace );
329 $length = $this->limit[0];
332 $fns = $contLang->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 );
363 if ( $contLang->hasVariants() ) {
364 $variants = $contLang->getVariants();
365 foreach ( $variants
as $vCode ) {
366 if ( $vCode == $contLang->getCode() ) {
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 ) {
487 return "\t<sitemap>\n" .
489 ( substr( $this->urlpath, 0, 1 ) ===
"/" ?
"" :
"/" ) .
490 "{$this->urlpath}$filename</loc>\n" .
491 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
500 function closeIndex() {
501 return "</sitemapindex>\n";
509 function openFile() {
510 return $this->xmlHead() .
'<urlset xmlns="' . $this->xmlSchema() .
'">' .
"\n";
521 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() )