192 $this->setNamespacePriorities();
193 $this->url_limit = 50000;
194 $this->size_limit = ( 2 ** 20 ) * 10;
196 # Create directory if needed
197 $fspath = $this->getOption(
'fspath', getcwd() );
199 $this->fatalError(
"Can not create directory $fspath." );
202 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
203 $this->fspath = realpath(
$fspath ) . DIRECTORY_SEPARATOR;
204 $this->urlpath = $this->getOption(
'urlpath',
"" );
205 if ( $this->urlpath !==
"" && substr( $this->urlpath, -1 ) !==
'/' ) {
206 $this->urlpath .=
'/';
208 $this->identifier = $this->getOption(
'identifier', $dbDomain );
209 $this->compress = $this->getOption(
'compress',
'yes' ) !==
'no';
210 $this->skipRedirects = $this->hasOption(
'skip-redirects' );
211 $this->dbr = $this->getReplicaDB();
212 $this->generateNamespaces();
214 $encIdentifier = rawurlencode( $this->identifier );
215 $this->findex = fopen(
"{$this->fspath}sitemap-index-{$encIdentifier}.xml",
'wb' );
336 $services = $this->getServiceContainer();
337 $contLang = $services->getContentLanguage();
338 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
339 $serverUrl = $services->getUrlUtils()->getServer(
PROTO_CANONICAL ) ??
'';
341 fwrite( $this->findex, $this->openIndex() );
343 foreach ( $this->namespaces as $namespace ) {
344 $res = $this->getPageRes( $namespace );
346 $this->generateLimit( $namespace );
347 $length = $this->limit[0];
350 $fns = $contLang->getFormattedNsText( $namespace );
351 $this->output(
"$namespace ($fns)\n" );
352 $skippedRedirects = 0;
354 foreach ( $res as $row ) {
355 if ( $row->pp_propname ===
'noindex' ) {
360 if ( $this->skipRedirects && $row->page_is_redirect ) {
366 || $i === $this->url_limit + 1
367 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit
369 if ( $this->file !==
false ) {
370 $this->write( $this->file, $this->closeFile() );
371 $this->close( $this->file );
373 $filename = $this->sitemapFilename( $namespace, $smcount++ );
374 $this->file = $this->open( $this->fspath . $filename,
'wb' );
375 $this->write( $this->file, $this->openFile() );
376 fwrite( $this->findex, $this->indexEntry( $filename, $serverUrl ) );
377 $this->output(
"\t$this->fspath$filename\n" );
378 $length = $this->limit[0];
381 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
382 $date =
wfTimestamp( TS_ISO_8601, $row->page_touched );
383 $entry = $this->fileEntry( $title->getCanonicalURL(), $date, $this->priority( $namespace ) );
384 $length += strlen( $entry );
385 $this->write( $this->file, $entry );
387 if ( $langConverter->hasVariants() ) {
388 $variants = $langConverter->getVariants();
389 foreach ( $variants as $vCode ) {
390 if ( $vCode == $contLang->getCode() ) {
393 $entry = $this->fileEntry(
394 $title->getCanonicalURL( [
'variant' => $vCode ] ),
396 $this->priority( $namespace )
398 $length += strlen( $entry );
399 $this->write( $this->file, $entry );
404 if ( $skippedNoindex > 0 ) {
405 $this->output(
" skipped $skippedNoindex page(s) with __NOINDEX__ switch\n" );
408 if ( $this->skipRedirects && $skippedRedirects > 0 ) {
409 $this->output(
" skipped $skippedRedirects redirect(s)\n" );
413 $this->write( $this->file, $this->closeFile() );
414 $this->close( $this->file );
417 fwrite( $this->findex, $this->closeIndex() );
418 fclose( $this->findex );