33require_once __DIR__ .
'/Maintenance.php';
148 parent::__construct();
152 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory',
158 'The URL path corresponding to --fspath, prepended to filenames in the index; '
159 .
'defaults to an empty string',
165 'Compress the sitemap files, can take value yes|no, default yes',
169 $this->
addOption(
'skip-redirects',
'Do not include redirecting articles in the sitemap' );
172 'What site identifier to use for the wiki, defaults to $wgDBname',
183 $this->url_limit = 50000;
184 $this->size_limit = ( 2 ** 20 ) * 10;
186 # Create directory if needed
189 $this->
fatalError(
"Can not create directory $fspath." );
192 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
193 $this->fspath = realpath(
$fspath ) . DIRECTORY_SEPARATOR;
194 $this->urlpath = $this->
getOption(
'urlpath',
"" );
195 if ( $this->urlpath !==
"" && substr( $this->urlpath, -1 ) !==
'/' ) {
196 $this->urlpath .=
'/';
198 $this->identifier = $this->
getOption(
'identifier', $dbDomain );
199 $this->compress = $this->
getOption(
'compress',
'yes' ) !==
'no';
200 $this->skipRedirects = $this->
hasOption(
'skip-redirects' );
204 $encIdentifier = rawurlencode( $this->identifier );
205 $this->findex = fopen(
"{$this->fspath}sitemap-index-{$encIdentifier}.xml",
'wb' );
217 $this->priorities[
NS_MAIN] =
'1.0';
218 $this->priorities[
NS_TALK] =
'0.1';
219 $this->priorities[
NS_USER] =
'0.5';
223 $this->priorities[
NS_FILE] =
'0.5';
229 $this->priorities[
NS_HELP] =
'0.5';
240 $float = floatval( $priority );
241 if ( $float > 1.0 ) {
243 } elseif ( $float < 0.0 ) {
246 $this->priorities[$namespace] = $priority;
263 $res = $this->dbr->select(
'page',
264 [
'page_namespace' ],
268 'GROUP BY' =>
'page_namespace',
269 'ORDER BY' =>
'page_namespace',
273 foreach (
$res as $row ) {
274 $this->namespaces[] = $row->page_namespace;
285 return $this->priorities[$namespace] ?? $this->
guessPriority( $namespace );
297 return MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $namespace )
309 return $this->dbr->select(
310 [
'page',
'page_props' ],
318 [
'page_namespace' => $namespace ],
326 'pp_propname' =>
'noindex'
337 $services = MediaWikiServices::getInstance();
338 $contLang = $services->getContentLanguage();
339 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
341 fwrite( $this->findex, $this->
openIndex() );
343 foreach ( $this->namespaces as $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 ) {
371 $this->
close( $this->file );
374 $this->file = $this->
open( $this->fspath . $filename,
'wb' );
376 fwrite( $this->findex, $this->
indexEntry( $filename ) );
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() ) {
394 $title->getCanonicalURL(
'', $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" );
414 $this->
close( $this->file );
418 fclose( $this->findex );
429 $resource = $this->compress ? gzopen(
$file, $flags ) : fopen(
$file, $flags );
430 if ( $resource ===
false ) {
432 .
" error opening file $file with flags $flags. Check permissions?" );
444 private function write( &$handle, $str ) {
445 if ( $handle ===
true || $handle ===
false ) {
446 throw new MWException( __METHOD__ .
" was passed a boolean as a file handle.\n" );
448 if ( $this->compress ) {
449 gzwrite( $handle, $str );
451 fwrite( $handle, $str );
460 private function close( &$handle ) {
461 if ( $this->compress ) {
476 $ext = $this->compress ?
'.gz' :
'';
478 return "sitemap-{$this->identifier}-NS_$namespace-$count.xml$ext";
487 return '<?xml version="1.0" encoding="UTF-8"?>' .
"\n";
496 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
505 return $this->
xmlHead() .
'<sitemapindex xmlns="' . $this->
xmlSchema() .
'">' .
"\n";
515 return "\t<sitemap>\n" .
517 ( substr( $this->urlpath, 0, 1 ) ===
"/" ?
"" :
"/" ) .
518 "{$this->urlpath}$filename</loc>\n" .
519 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
529 return "</sitemapindex>\n";
538 return $this->
xmlHead() .
'<urlset xmlns="' . $this->
xmlSchema() .
'">' .
"\n";
552 "\t\t<loc>" . htmlspecialchars( $url ) .
"</loc>\n" .
553 "\t\t<lastmod>$date</lastmod>\n" .
554 "\t\t<priority>$priority</priority>\n" .
564 return "</urlset>\n";
574 $title = Title::makeTitle( $namespace, str_repeat(
"\u{28B81}", 63 ) .
"\u{5583}" );
579 $title->getCanonicalURL(),
589require_once RUN_MAINTENANCE_IF_MAIN;
$wgSitemapNamespaces
Array of namespaces to generate a Google sitemap for when the maintenance/generateSitemap....
$wgSitemapNamespacesPriorities
Custom namespace priorities for sitemaps.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetServerUrl( $proto)
Get the wiki's "server", i.e.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script that generates a sitemap for the site.
generateLimit( $namespace)
Populate $this->limit.
string $timestamp
When this sitemap batch was generated.
xmlSchema()
Return the XML schema being used.
indexEntry( $filename)
Return the XML for a single sitemap indexfile entry.
close(&$handle)
gzclose() / fclose() wrapper
IDatabase $dbr
A database replica DB object.
generateNamespaces()
Generate a one-dimensional array of existing namespaces.
closeFile()
Return the XML required to close sitemap file.
string $identifier
Identifier to use in filenames, default $wgDBname.
string $fspath
The path to prepend to the filename.
closeIndex()
Return the XML required to close a sitemap index file.
write(&$handle, $str)
gzwrite() / fwrite() wrapper
array $priorities
Key => value entries of namespaces and their priorities.
bool $skipRedirects
Whether or not to include redirection pages.
__construct()
Default constructor.
array $limit
The number of entries to save in each sitemap file.
open( $file, $flags)
gzopen() / fopen() wrapper
int $size_limit
The maximum size of a sitemap file.
xmlHead()
Return the XML required to open an XML file.
getPageRes( $namespace)
Return a database resolution of all the pages in a given namespace.
priority( $namespace)
Get the priority of a given namespace.
bool $compress
Whether or not to use compression.
array $namespaces
A one-dimensional array of namespaces in the wiki.
string $urlpath
The URL path to prepend to filenames in the index; should resolve to the same directory as $fspath.
guessPriority( $namespace)
If the namespace isn't listed on the priority list return the default priority for the namespace,...
resource $findex
A resource pointing to the sitemap index file.
int $url_limit
The maximum amount of urls in a sitemap file.
resource false $file
A resource pointing to a sitemap file.
fileEntry( $url, $date, $priority)
Return the XML for a single sitemap entry.
openFile()
Return the XML required to open a sitemap file.
sitemapFilename( $namespace, $count)
Get a sitemap filename.
openIndex()
Return the XML required to open a sitemap index file.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
if(!is_readable( $file)) $ext