Go to the documentation of this file.
32 require_once __DIR__ .
'/Maintenance.php';
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',
182 $this->url_limit = 50000;
183 $this->size_limit = ( 2 ** 20 ) * 10;
185 # Create directory if needed
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' );
203 $encIdentifier = rawurlencode( $this->identifier );
204 $this->findex = fopen(
"{$this->fspath}sitemap-index-{$encIdentifier}.xml",
'wb' );
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;
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;
284 return $this->priorities[$namespace] ?? $this->
guessPriority( $namespace );
296 return MediaWikiServices::getInstance()->getNamespaceInfo()->isSubject( $namespace )
308 return $this->dbr->select(
'page',
315 [
'page_namespace' => $namespace ],
324 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
326 fwrite( $this->findex, $this->
openIndex() );
328 foreach ( $this->namespaces as $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 ) {
350 $this->
close( $this->file );
353 $this->file = $this->
open( $this->fspath . $filename,
'wb' );
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() ) {
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" );
389 $this->
close( $this->file );
393 fclose( $this->findex );
404 $resource = $this->compress ? gzopen(
$file, $flags ) : fopen(
$file, $flags );
405 if ( $resource ===
false ) {
407 .
" error opening file $file with flags $flags. Check permissions?" );
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 );
436 if ( $this->compress ) {
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";
471 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
480 return $this->
xmlHead() .
'<sitemapindex xmlns="' . $this->
xmlSchema() .
'">' .
"\n";
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" .
504 return "</sitemapindex>\n";
513 return $this->
xmlHead() .
'<urlset xmlns="' . $this->
xmlSchema() .
'">' .
"\n";
527 "\t\t<loc>" . htmlspecialchars( $url ) .
"</loc>\n" .
528 "\t\t<lastmod>$date</lastmod>\n" .
529 "\t\t<priority>$priority</priority>\n" .
539 return "</urlset>\n";
554 $title->getCanonicalURL(),
const RUN_MAINTENANCE_IF_MAIN
generateNamespaces()
Generate a one-dimensional array of existing namespaces.
$wgSitemapNamespacesPriorities
Custom namespace priorities for sitemaps.
static getCurrentWikiDbDomain()
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addDescription( $text)
Set the description text.
xmlHead()
Return the XML required to open an XML file.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
int $url_limit
The maximum amount of urls in a sitemap file.
generateLimit( $namespace)
Populate $this->limit.
string $timestamp
When this sitemap batch was generated.
__construct()
Default constructor.
close(&$handle)
gzclose() / fclose() wrapper
resource false $file
A resource pointing to a sitemap file.
resource $findex
A resource pointing to the sitemap index file.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
array $priorities
Key => value entries of namespaces and their priorities.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
bool $skipRedirects
Whether or not to include redirection pages.
guessPriority( $namespace)
If the namespace isn't listed on the priority list return the default priority for the namespace,...
bool $compress
Whether or not to use compression.
closeIndex()
Return the XML required to close a sitemap index file.
fileEntry( $url, $date, $priority)
Return the XML for a single sitemap entry.
sitemapFilename( $namespace, $count)
Get a sitemap filename.
closeFile()
Return the XML required to close sitemap file.
int $size_limit
The maximum size of a sitemap file.
getPageRes( $namespace)
Return a database resolution of all the pages in a given namespace.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
string $fspath
The path to prepend to the filename.
$wgSitemapNamespaces
Array of namespaces to generate a Google sitemap for when the maintenance/generateSitemap....
priority( $namespace)
Get the priority of a given namespace.
xmlSchema()
Return the XML schema being used.
array $namespaces
A one-dimensional array of namespaces in the wiki.
openIndex()
Return the XML required to open a sitemap index file.
getOption( $name, $default=null)
Get an option, or return the default.
openFile()
Return the XML required to open a sitemap file.
output( $out, $channel=null)
Throw some output to the user.
object $dbr
A database replica DB object.
if(!is_readable( $file)) $ext
array $limit
The number of entries to save in each sitemap file.
hasOption( $name)
Checks to see if a particular option exists.
wfGetServerUrl( $proto)
Get the wiki's "server", i.e.
Maintenance script that generates a sitemap for the site.
open( $file, $flags)
gzopen() / fopen() wrapper
string $urlpath
The URL path to prepend to filenames in the index; should resolve to the same directory as $fspath.
indexEntry( $filename)
Return the XML for a single sitemap indexfile entry.
string $identifier
Identifier to use in filenames, default $wgDBname.
write(&$handle, $str)
gzwrite() / fwrite() wrapper