32 require_once __DIR__ .
'/Maintenance.php';
88 public $skipRedirects;
102 public $priorities = [];
109 public $namespaces = [];
147 parent::__construct();
151 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory',
157 'The URL path corresponding to --fspath, prepended to filenames in the index; '
158 .
'defaults to an empty string',
164 'Compress the sitemap files, can take value yes|no, default yes',
168 $this->
addOption(
'skip-redirects',
'Do not include redirecting articles in the sitemap' );
171 'What site identifier to use for the wiki, defaults to $wgDBname',
181 $this->setNamespacePriorities();
182 $this->url_limit = 50000;
183 $this->size_limit = ( 2 ** 20 ) * 10;
185 # Create directory if needed
186 $fspath = $this->
getOption(
'fspath', getcwd() );
188 $this->
fatalError(
"Can not create directory $fspath." );
192 $this->fspath = realpath( $fspath ) . DIRECTORY_SEPARATOR;
193 $this->urlpath = $this->
getOption(
'urlpath',
"" );
194 if ( $this->urlpath !==
"" && substr( $this->urlpath, -1 ) !==
'/' ) {
195 $this->urlpath .=
'/';
197 $this->identifier = $this->
getOption(
'identifier', $dbDomain );
198 $this->compress = $this->
getOption(
'compress',
'yes' ) !==
'no';
199 $this->skipRedirects = $this->
hasOption(
'skip-redirects' );
201 $this->generateNamespaces();
203 $encIdentifier = rawurlencode( $this->identifier );
204 $this->findex = fopen(
"{$this->fspath}sitemap-index-{$encIdentifier}.xml",
'wb' );
208 private function setNamespacePriorities() {
212 $this->priorities[self::GS_MAIN] =
'0.5';
214 $this->priorities[self::GS_TALK] =
'0.1';
216 $this->priorities[
NS_MAIN] =
'1.0';
217 $this->priorities[
NS_TALK] =
'0.1';
218 $this->priorities[
NS_USER] =
'0.5';
222 $this->priorities[
NS_FILE] =
'0.5';
228 $this->priorities[
NS_HELP] =
'0.5';
239 $float = floatval( $priority );
240 if ( $float > 1.0 ) {
242 } elseif ( $float < 0.0 ) {
245 $this->priorities[$namespace] = $priority;
253 function generateNamespaces() {
262 $res = $this->dbr->select(
'page',
263 [
'page_namespace' ],
267 'GROUP BY' =>
'page_namespace',
268 'ORDER BY' =>
'page_namespace',
272 foreach (
$res as $row ) {
273 $this->namespaces[] = $row->page_namespace;
283 function priority( $namespace ) {
284 return $this->priorities[$namespace] ?? $this->guessPriority( $namespace );
295 function guessPriority( $namespace ) {
296 return MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $namespace )
297 ? $this->priorities[self::GS_MAIN]
298 : $this->priorities[self::GS_TALK];
307 function getPageRes( $namespace ) {
308 return $this->dbr->select(
'page',
315 [
'page_namespace' => $namespace ],
323 public function main() {
324 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
326 fwrite( $this->findex, $this->openIndex() );
328 foreach ( $this->namespaces as $namespace ) {
329 $res = $this->getPageRes( $namespace );
331 $this->generateLimit( $namespace );
332 $length = $this->limit[0];
335 $fns = $contLang->getFormattedNsText( $namespace );
336 $this->
output(
"$namespace ($fns)\n" );
337 $skippedRedirects = 0;
338 foreach (
$res as $row ) {
339 if ( $this->skipRedirects && $row->page_is_redirect ) {
345 || $i === $this->url_limit + 1
346 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit
348 if ( $this->file !==
false ) {
349 $this->write( $this->file, $this->closeFile() );
350 $this->close( $this->file );
352 $filename = $this->sitemapFilename( $namespace, $smcount++ );
353 $this->file = $this->open( $this->fspath . $filename,
'wb' );
354 $this->write( $this->file, $this->openFile() );
355 fwrite( $this->findex, $this->indexEntry( $filename ) );
356 $this->
output(
"\t$this->fspath$filename\n" );
357 $length = $this->limit[0];
361 $date =
wfTimestamp( TS_ISO_8601, $row->page_touched );
362 $entry = $this->fileEntry(
$title->getCanonicalURL(), $date, $this->priority( $namespace ) );
363 $length += strlen( $entry );
364 $this->write( $this->file, $entry );
366 if ( $contLang->hasVariants() ) {
367 $variants = $contLang->getVariants();
368 foreach ( $variants as $vCode ) {
369 if ( $vCode == $contLang->getCode() ) {
372 $entry = $this->fileEntry(
373 $title->getCanonicalURL(
'', $vCode ),
375 $this->priority( $namespace )
377 $length += strlen( $entry );
378 $this->write( $this->file, $entry );
383 if ( $this->skipRedirects && $skippedRedirects > 0 ) {
384 $this->
output(
" skipped $skippedRedirects redirect(s)\n" );
388 $this->write( $this->file, $this->closeFile() );
389 $this->close( $this->file );
392 fwrite( $this->findex, $this->closeIndex() );
393 fclose( $this->findex );
403 function open(
$file, $flags ) {
404 $resource = $this->compress ? gzopen(
$file, $flags ) : fopen(
$file, $flags );
405 if ( $resource ===
false ) {
407 .
" error opening file $file with flags $flags. Check permissions?" );
419 function write( &$handle, $str ) {
420 if ( $handle ===
true || $handle ===
false ) {
421 throw new MWException( __METHOD__ .
" was passed a boolean as a file handle.\n" );
423 if ( $this->compress ) {
424 gzwrite( $handle, $str );
426 fwrite( $handle, $str );
435 function close( &$handle ) {
436 if ( $this->compress ) {
450 function sitemapFilename( $namespace, $count ) {
451 $ext = $this->compress ?
'.gz' :
'';
453 return "sitemap-{$this->identifier}-NS_$namespace-$count.xml$ext";
462 return '<?xml version="1.0" encoding="UTF-8"?>' .
"\n";
470 function xmlSchema() {
471 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
479 function openIndex() {
480 return $this->xmlHead() .
'<sitemapindex xmlns="' . $this->xmlSchema() .
'">' .
"\n";
489 function indexEntry( $filename ) {
490 return "\t<sitemap>\n" .
492 ( substr( $this->urlpath, 0, 1 ) ===
"/" ?
"" :
"/" ) .
493 "{$this->urlpath}$filename</loc>\n" .
494 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
503 function closeIndex() {
504 return "</sitemapindex>\n";
512 function openFile() {
513 return $this->xmlHead() .
'<urlset xmlns="' . $this->xmlSchema() .
'">' .
"\n";
524 function fileEntry( $url, $date, $priority ) {
527 "\t\t<loc>" . htmlspecialchars( $url ) .
"</loc>\n" .
528 "\t\t<lastmod>$date</lastmod>\n" .
529 "\t\t<priority>$priority</priority>\n" .
538 function closeFile() {
539 return "</urlset>\n";
547 function generateLimit( $namespace ) {
552 strlen( $this->openFile() ),
553 strlen( $this->fileEntry(
554 $title->getCanonicalURL(),
556 $this->priority( $namespace )
558 strlen( $this->closeFile() )