3 * Creates a sitemap for the site.
5 * Copyright © 2005, Ævar Arnfjörð Bjarmason, Jens Frank <jeluf@gmx.de> and
6 * Brion Vibber <brion@pobox.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup Maintenance
25 * @see http://www.sitemaps.org/
26 * @see http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
29 use MediaWiki\MainConfigNames
;
30 use MediaWiki\Title\Title
;
31 use MediaWiki\WikiMap\WikiMap
;
32 use Wikimedia\Rdbms\IDatabase
;
33 use Wikimedia\Rdbms\IResultWrapper
;
35 require_once __DIR__
. '/Maintenance.php';
38 * Maintenance script that generates a sitemap for the site.
40 * @ingroup Maintenance
42 class GenerateSitemap
extends Maintenance
{
43 private const GS_MAIN
= -2;
44 private const GS_TALK
= -1;
47 * The maximum amount of urls in a sitemap file
49 * @link http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
56 * The maximum size of a sitemap file
58 * @link http://www.sitemaps.org/faq.php#faq_sitemap_size
65 * The path to prepend to the filename
72 * The URL path to prepend to filenames in the index;
73 * should resolve to the same directory as $fspath.
80 * Whether or not to use compression
87 * Whether or not to include redirection pages
91 public $skipRedirects;
94 * The number of entries to save in each sitemap file
101 * Key => value entries of namespaces and their priorities
105 public $priorities = [];
108 * A one-dimensional array of namespaces in the wiki
112 public $namespaces = [];
115 * When this sitemap batch was generated
122 * A database replica DB object
129 * A resource pointing to the sitemap index file
136 * A resource pointing to a sitemap file
138 * @var resource|false
143 * Identifier to use in filenames, default $wgDBname
149 public function __construct() {
150 parent
::__construct();
151 $this->addDescription( 'Creates a sitemap for the site' );
154 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory',
160 'The URL path corresponding to --fspath, prepended to filenames in the index; '
161 . 'defaults to an empty string',
167 'Compress the sitemap files, can take value yes|no, default yes',
171 $this->addOption( 'skip-redirects', 'Do not include redirecting articles in the sitemap' );
174 'What site identifier to use for the wiki, defaults to $wgDBname',
183 public function execute() {
184 $this->setNamespacePriorities();
185 $this->url_limit
= 50000;
186 $this->size_limit
= ( 2 ** 20 ) * 10;
188 # Create directory if needed
189 $fspath = $this->getOption( 'fspath', getcwd() );
190 if ( !wfMkdirParents( $fspath, null, __METHOD__
) ) {
191 $this->fatalError( "Can not create directory $fspath." );
194 $dbDomain = WikiMap
::getCurrentWikiDbDomain()->getId();
195 $this->fspath
= realpath( $fspath ) . DIRECTORY_SEPARATOR
;
196 $this->urlpath
= $this->getOption( 'urlpath', "" );
197 if ( $this->urlpath
!== "" && substr( $this->urlpath
, -1 ) !== '/' ) {
198 $this->urlpath
.= '/';
200 $this->identifier
= $this->getOption( 'identifier', $dbDomain );
201 $this->compress
= $this->getOption( 'compress', 'yes' ) !== 'no';
202 $this->skipRedirects
= $this->hasOption( 'skip-redirects' );
203 $this->dbr
= $this->getDB( DB_REPLICA
);
204 $this->generateNamespaces();
205 $this->timestamp
= wfTimestamp( TS_ISO_8601
, wfTimestampNow() );
206 $encIdentifier = rawurlencode( $this->identifier
);
207 $this->findex
= fopen( "{$this->fspath}sitemap-index-{$encIdentifier}.xml", 'wb' );
211 private function setNamespacePriorities() {
212 $sitemapNamespacesPriorities = $this->getConfig()->get( MainConfigNames
::SitemapNamespacesPriorities
);
214 // Custom main namespaces
215 $this->priorities
[self
::GS_MAIN
] = '0.5';
216 // Custom talk namesspaces
217 $this->priorities
[self
::GS_TALK
] = '0.1';
218 // MediaWiki standard namespaces
219 $this->priorities
[NS_MAIN
] = '1.0';
220 $this->priorities
[NS_TALK
] = '0.1';
221 $this->priorities
[NS_USER
] = '0.5';
222 $this->priorities
[NS_USER_TALK
] = '0.1';
223 $this->priorities
[NS_PROJECT
] = '0.5';
224 $this->priorities
[NS_PROJECT_TALK
] = '0.1';
225 $this->priorities
[NS_FILE
] = '0.5';
226 $this->priorities
[NS_FILE_TALK
] = '0.1';
227 $this->priorities
[NS_MEDIAWIKI
] = '0.0';
228 $this->priorities
[NS_MEDIAWIKI_TALK
] = '0.1';
229 $this->priorities
[NS_TEMPLATE
] = '0.0';
230 $this->priorities
[NS_TEMPLATE_TALK
] = '0.1';
231 $this->priorities
[NS_HELP
] = '0.5';
232 $this->priorities
[NS_HELP_TALK
] = '0.1';
233 $this->priorities
[NS_CATEGORY
] = '0.5';
234 $this->priorities
[NS_CATEGORY_TALK
] = '0.1';
237 if ( $sitemapNamespacesPriorities !== false ) {
239 * @var array $sitemapNamespacesPriorities
241 foreach ( $sitemapNamespacesPriorities as $namespace => $priority ) {
242 $float = floatval( $priority );
243 if ( $float > 1.0 ) {
245 } elseif ( $float < 0.0 ) {
248 $this->priorities
[$namespace] = $priority;
254 * Generate a one-dimensional array of existing namespaces
256 private function generateNamespaces() {
257 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
258 $sitemapNamespaces = $this->getConfig()->get( MainConfigNames
::SitemapNamespaces
);
259 if ( is_array( $sitemapNamespaces ) ) {
260 $this->namespaces
= $sitemapNamespaces;
265 $res = $this->dbr
->newSelectQueryBuilder()
266 ->select( [ 'page_namespace' ] )
268 ->groupBy( 'page_namespace' )
269 ->orderBy( 'page_namespace' )
270 ->caller( __METHOD__
)->fetchResultSet();
272 foreach ( $res as $row ) {
273 $this->namespaces
[] = $row->page_namespace
;
278 * Get the priority of a given namespace
280 * @param int $namespace The namespace to get the priority for
283 private function priority( $namespace ) {
284 return $this->priorities
[$namespace] ??
$this->guessPriority( $namespace );
288 * If the namespace isn't listed on the priority list return the
289 * default priority for the namespace, varies depending on whether it's
292 * @param int $namespace The namespace to get the priority for
295 private function guessPriority( $namespace ) {
296 return $this->getServiceContainer()->getNamespaceInfo()->isSubject( $namespace )
297 ?
$this->priorities
[self
::GS_MAIN
]
298 : $this->priorities
[self
::GS_TALK
];
302 * Return a database resolution of all the pages in a given namespace
304 * @param int $namespace Limit the query to this namespace
305 * @return IResultWrapper
307 private function getPageRes( $namespace ) {
308 return $this->dbr
->newSelectQueryBuilder()
309 ->select( [ 'page_namespace', 'page_title', 'page_touched', 'page_is_redirect', 'pp_propname' ] )
311 ->leftJoin( 'page_props', null, [ 'page_id = pp_page', 'pp_propname' => 'noindex' ] )
312 ->where( [ 'page_namespace' => $namespace ] )
313 ->caller( __METHOD__
)->fetchResultSet();
319 public function main() {
320 $services = $this->getServiceContainer();
321 $contLang = $services->getContentLanguage();
322 $langConverter = $services->getLanguageConverterFactory()->getLanguageConverter( $contLang );
324 fwrite( $this->findex
, $this->openIndex() );
326 foreach ( $this->namespaces
as $namespace ) {
327 $res = $this->getPageRes( $namespace );
329 $this->generateLimit( $namespace );
330 $length = $this->limit
[0];
333 $fns = $contLang->getFormattedNsText( $namespace );
334 $this->output( "$namespace ($fns)\n" );
335 $skippedRedirects = 0; // Number of redirects skipped for that namespace
336 $skippedNoindex = 0; // Number of pages with __NOINDEX__ switch for that NS
337 foreach ( $res as $row ) {
338 if ( $row->pp_propname
=== 'noindex' ) {
343 if ( $this->skipRedirects
&& $row->page_is_redirect
) {
349 ||
$i === $this->url_limit +
1
350 ||
$length +
$this->limit
[1] +
$this->limit
[2] > $this->size_limit
352 if ( $this->file
!== false ) {
353 $this->write( $this->file
, $this->closeFile() );
354 $this->close( $this->file
);
356 $filename = $this->sitemapFilename( $namespace, $smcount++
);
357 $this->file
= $this->open( $this->fspath
. $filename, 'wb' );
358 $this->write( $this->file
, $this->openFile() );
359 fwrite( $this->findex
, $this->indexEntry( $filename ) );
360 $this->output( "\t$this->fspath$filename\n" );
361 $length = $this->limit
[0];
364 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
365 $date = wfTimestamp( TS_ISO_8601
, $row->page_touched
);
366 $entry = $this->fileEntry( $title->getCanonicalURL(), $date, $this->priority( $namespace ) );
367 $length +
= strlen( $entry );
368 $this->write( $this->file
, $entry );
369 // generate pages for language variants
370 if ( $langConverter->hasVariants() ) {
371 $variants = $langConverter->getVariants();
372 foreach ( $variants as $vCode ) {
373 if ( $vCode == $contLang->getCode() ) {
374 continue; // we don't want default variant
376 $entry = $this->fileEntry(
377 $title->getCanonicalURL( [ 'variant' => $vCode ] ),
379 $this->priority( $namespace )
381 $length +
= strlen( $entry );
382 $this->write( $this->file
, $entry );
387 if ( $skippedNoindex > 0 ) {
388 $this->output( " skipped $skippedNoindex page(s) with __NOINDEX__ switch\n" );
391 if ( $this->skipRedirects
&& $skippedRedirects > 0 ) {
392 $this->output( " skipped $skippedRedirects redirect(s)\n" );
396 $this->write( $this->file
, $this->closeFile() );
397 $this->close( $this->file
);
400 fwrite( $this->findex
, $this->closeIndex() );
401 fclose( $this->findex
);
405 * gzopen() / fopen() wrapper
407 * @param string $file
408 * @param string $flags
411 private function open( $file, $flags ) {
412 $resource = $this->compress ?
gzopen( $file, $flags ) : fopen( $file, $flags );
413 if ( $resource === false ) {
414 throw new RuntimeException( __METHOD__
415 . " error opening file $file with flags $flags. Check permissions?" );
422 * gzwrite() / fwrite() wrapper
424 * @param resource &$handle
427 private function write( &$handle, $str ) {
428 if ( $handle === true ||
$handle === false ) {
429 throw new InvalidArgumentException( __METHOD__
. " was passed a boolean as a file handle.\n" );
431 if ( $this->compress
) {
432 gzwrite( $handle, $str );
434 fwrite( $handle, $str );
439 * gzclose() / fclose() wrapper
441 * @param resource &$handle
443 private function close( &$handle ) {
444 if ( $this->compress
) {
452 * Get a sitemap filename
454 * @param int $namespace
458 private function sitemapFilename( $namespace, $count ) {
459 $ext = $this->compress ?
'.gz' : '';
461 return "sitemap-{$this->identifier}-NS_$namespace-$count.xml$ext";
465 * Return the XML required to open an XML file
469 private function xmlHead() {
470 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
474 * Return the XML schema being used
478 private function xmlSchema() {
479 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
483 * Return the XML required to open a sitemap index file
487 private function openIndex() {
488 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
492 * Return the XML for a single sitemap indexfile entry
494 * @param string $filename The filename of the sitemap file
497 private function indexEntry( $filename ) {
498 return "\t<sitemap>\n" .
499 "\t\t<loc>" . wfGetServerUrl( PROTO_CANONICAL
) .
500 ( substr( $this->urlpath
, 0, 1 ) === "/" ?
"" : "/" ) .
501 "{$this->urlpath}$filename</loc>\n" .
502 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
507 * Return the XML required to close a sitemap index file
511 private function closeIndex() {
512 return "</sitemapindex>\n";
516 * Return the XML required to open a sitemap file
520 private function openFile() {
521 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
525 * Return the XML for a single sitemap entry
527 * @param string $url An RFC 2396 compliant URL
528 * @param string $date A ISO 8601 date
529 * @param string $priority A priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
532 private function fileEntry( $url, $date, $priority ) {
534 // T36666: $url may contain bad characters such as ampersands.
535 "\t\t<loc>" . htmlspecialchars( $url ) . "</loc>\n" .
536 "\t\t<lastmod>$date</lastmod>\n" .
537 "\t\t<priority>$priority</priority>\n" .
542 * Return the XML required to close sitemap file
546 private function closeFile() {
547 return "</urlset>\n";
551 * Populate $this->limit
553 * @param int $namespace
555 private function generateLimit( $namespace ) {
556 // T19961: make a title with the longest possible URL in this namespace
557 $title = Title
::makeTitle( $namespace, str_repeat( "\u{28B81}", 63 ) . "\u{5583}" );
560 strlen( $this->openFile() ),
561 strlen( $this->fileEntry(
562 $title->getCanonicalURL(),
563 wfTimestamp( TS_ISO_8601
, wfTimestamp() ),
564 $this->priority( $namespace )
566 strlen( $this->closeFile() )
571 $maintClass = GenerateSitemap
::class;
572 require_once RUN_MAINTENANCE_IF_MAIN
;