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 require_once( dirname( __FILE__
) . '/Maintenance.php' );
31 class GenerateSitemap
extends Maintenance
{
36 * The maximum amount of urls in a sitemap file
38 * @link http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
45 * The maximum size of a sitemap file
47 * @link http://www.sitemaps.org/faq.php#faq_sitemap_size
54 * The path to prepend to the filename
61 * The URL path to prepend to filenames in the index; should resolve to the same directory as $fspath
68 * Whether or not to use compression
75 * The number of entries to save in each sitemap file
82 * Key => value entries of namespaces and their priorities
86 var $priorities = array();
89 * A one-dimensional array of namespaces in the wiki
93 var $namespaces = array();
96 * When this sitemap batch was generated
103 * A database slave object
110 * A resource pointing to the sitemap index file
118 * A resource pointing to a sitemap file
125 * Identifier to use in filenames, default $wgDBname
134 public function __construct() {
135 parent
::__construct();
136 $this->mDescription
= "Creates a sitemap for the site";
137 $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory', false, true );
138 $this->addOption( 'urlpath', 'The URL path corresponding to --fspath, prepended to filenames in the index; defaults to an empty string', false, true );
139 $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes', false, true );
140 $this->addOption( 'identifier', 'What site identifier to use for the wiki, defaults to $wgDBname', false, true );
146 public function execute() {
147 $this->setNamespacePriorities();
148 $this->url_limit
= 50000;
149 $this->size_limit
= pow( 2, 20 ) * 10;
150 $this->fspath
= self
::init_path( $this->getOption( 'fspath', getcwd() ) );
151 $this->urlpath
= $this->getOption( 'urlpath', "" );
152 if ( $this->urlpath
!== "" && substr( $this->urlpath
, -1 ) !== '/' ) {
153 $this->urlpath
.= '/';
155 $this->identifier
= $this->getOption( 'identifier', wfWikiID() );
156 $this->compress
= $this->getOption( 'compress', 'yes' ) !== 'no';
157 $this->dbr
= wfGetDB( DB_SLAVE
);
158 $this->generateNamespaces();
159 $this->timestamp
= wfTimestamp( TS_ISO_8601
, wfTimestampNow() );
160 $this->findex
= fopen( "{$this->fspath}sitemap-index-{$this->identifier}.xml", 'wb' );
164 private function setNamespacePriorities() {
165 global $wgSitemapNamespacesPriorities;
167 // Custom main namespaces
168 $this->priorities
[self
::GS_MAIN
] = '0.5';
169 // Custom talk namesspaces
170 $this->priorities
[self
::GS_TALK
] = '0.1';
171 // MediaWiki standard namespaces
172 $this->priorities
[NS_MAIN
] = '1.0';
173 $this->priorities
[NS_TALK
] = '0.1';
174 $this->priorities
[NS_USER
] = '0.5';
175 $this->priorities
[NS_USER_TALK
] = '0.1';
176 $this->priorities
[NS_PROJECT
] = '0.5';
177 $this->priorities
[NS_PROJECT_TALK
] = '0.1';
178 $this->priorities
[NS_FILE
] = '0.5';
179 $this->priorities
[NS_FILE_TALK
] = '0.1';
180 $this->priorities
[NS_MEDIAWIKI
] = '0.0';
181 $this->priorities
[NS_MEDIAWIKI_TALK
] = '0.1';
182 $this->priorities
[NS_TEMPLATE
] = '0.0';
183 $this->priorities
[NS_TEMPLATE_TALK
] = '0.1';
184 $this->priorities
[NS_HELP
] = '0.5';
185 $this->priorities
[NS_HELP_TALK
] = '0.1';
186 $this->priorities
[NS_CATEGORY
] = '0.5';
187 $this->priorities
[NS_CATEGORY_TALK
] = '0.1';
190 if ( $wgSitemapNamespacesPriorities !== false ) {
191 foreach ( $wgSitemapNamespacesPriorities as $namespace => $priority ) {
192 $float = floatval( $priority );
193 if ( $float > 1.0 ) {
195 } elseif ( $float < 0.0 ) {
198 $this->priorities
[$namespace] = $priority;
204 * Create directory if it does not exist and return pathname with a trailing slash
206 private static function init_path( $fspath ) {
207 if ( !isset( $fspath ) ) {
210 # Create directory if needed
211 if ( $fspath && !is_dir( $fspath ) ) {
212 wfMkdirParents( $fspath, null, __METHOD__
) or die( "Can not create directory $fspath.\n" );
215 return realpath( $fspath ) . DIRECTORY_SEPARATOR
;
219 * Generate a one-dimensional array of existing namespaces
221 function generateNamespaces() {
222 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
223 global $wgSitemapNamespaces;
224 if ( is_array( $wgSitemapNamespaces ) ) {
225 $this->namespaces
= $wgSitemapNamespaces;
229 $res = $this->dbr
->select( 'page',
230 array( 'page_namespace' ),
234 'GROUP BY' => 'page_namespace',
235 'ORDER BY' => 'page_namespace',
239 foreach ( $res as $row )
240 $this->namespaces
[] = $row->page_namespace
;
244 * Get the priority of a given namespace
246 * @param $namespace Integer: the namespace to get the priority for
249 function priority( $namespace ) {
250 return isset( $this->priorities
[$namespace] ) ?
$this->priorities
[$namespace] : $this->guessPriority( $namespace );
254 * If the namespace isn't listed on the priority list return the
255 * default priority for the namespace, varies depending on whether it's
258 * @param $namespace Integer: the namespace to get the priority for
261 function guessPriority( $namespace ) {
262 return MWNamespace
::isMain( $namespace ) ?
$this->priorities
[self
::GS_MAIN
] : $this->priorities
[self
::GS_TALK
];
266 * Return a database resolution of all the pages in a given namespace
268 * @param $namespace Integer: limit the query to this namespace
271 function getPageRes( $namespace ) {
272 return $this->dbr
->select( 'page',
278 array( 'page_namespace' => $namespace ),
286 public function main() {
289 fwrite( $this->findex
, $this->openIndex() );
291 foreach ( $this->namespaces
as $namespace ) {
292 $res = $this->getPageRes( $namespace );
294 $this->generateLimit( $namespace );
295 $length = $this->limit
[0];
298 $fns = $wgContLang->getFormattedNsText( $namespace );
299 $this->output( "$namespace ($fns)\n" );
300 foreach ( $res as $row ) {
301 if ( $i++
=== 0 ||
$i === $this->url_limit +
1 ||
$length +
$this->limit
[1] +
$this->limit
[2] > $this->size_limit
) {
302 if ( $this->file
!== false ) {
303 $this->write( $this->file
, $this->closeFile() );
304 $this->close( $this->file
);
306 $filename = $this->sitemapFilename( $namespace, $smcount++
);
307 $this->file
= $this->open( $this->fspath
. $filename, 'wb' );
308 $this->write( $this->file
, $this->openFile() );
309 fwrite( $this->findex
, $this->indexEntry( $filename ) );
310 $this->output( "\t$this->fspath$filename\n" );
311 $length = $this->limit
[0];
314 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
315 $date = wfTimestamp( TS_ISO_8601
, $row->page_touched
);
316 $entry = $this->fileEntry( $title->getCanonicalURL(), $date, $this->priority( $namespace ) );
317 $length +
= strlen( $entry );
318 $this->write( $this->file
, $entry );
319 // generate pages for language variants
320 if ( $wgContLang->hasVariants() ) {
321 $variants = $wgContLang->getVariants();
322 foreach ( $variants as $vCode ) {
323 if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
324 $entry = $this->fileEntry( $title->getCanonicalURL( '', $vCode ), $date, $this->priority( $namespace ) );
325 $length +
= strlen( $entry );
326 $this->write( $this->file
, $entry );
331 $this->write( $this->file
, $this->closeFile() );
332 $this->close( $this->file
);
335 fwrite( $this->findex
, $this->closeIndex() );
336 fclose( $this->findex
);
340 * gzopen() / fopen() wrapper
344 function open( $file, $flags ) {
345 return $this->compress ?
gzopen( $file, $flags ) : fopen( $file, $flags );
349 * gzwrite() / fwrite() wrapper
351 function write( &$handle, $str ) {
352 if ( $this->compress
)
353 gzwrite( $handle, $str );
355 fwrite( $handle, $str );
359 * gzclose() / fclose() wrapper
361 function close( &$handle ) {
362 if ( $this->compress
)
369 * Get a sitemap filename
371 * @param $namespace Integer: the namespace
372 * @param $count Integer: the count
375 function sitemapFilename( $namespace, $count ) {
376 $ext = $this->compress ?
'.gz' : '';
377 return "sitemap-{$this->identifier}-NS_$namespace-$count.xml$ext";
381 * Return the XML required to open an XML file
386 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
390 * Return the XML schema being used
394 function xmlSchema() {
395 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
399 * Return the XML required to open a sitemap index file
403 function openIndex() {
404 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
408 * Return the XML for a single sitemap indexfile entry
410 * @param $filename String: the filename of the sitemap file
413 function indexEntry( $filename ) {
416 "\t\t<loc>{$this->urlpath}$filename</loc>\n" .
417 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
422 * Return the XML required to close a sitemap index file
426 function closeIndex() {
427 return "</sitemapindex>\n";
431 * Return the XML required to open a sitemap file
435 function openFile() {
436 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
440 * Return the XML for a single sitemap entry
442 * @param $url String: an RFC 2396 compliant URL
443 * @param $date String: a ISO 8601 date
444 * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
447 function fileEntry( $url, $date, $priority ) {
450 "\t\t<loc>$url</loc>\n" .
451 "\t\t<lastmod>$date</lastmod>\n" .
452 "\t\t<priority>$priority</priority>\n" .
457 * Return the XML required to close sitemap file
461 function closeFile() {
462 return "</urlset>\n";
466 * Populate $this->limit
468 function generateLimit( $namespace ) {
469 // bug 17961: make a title with the longest possible URL in this namespace
470 $title = Title
::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
472 $this->limit
= array(
473 strlen( $this->openFile() ),
474 strlen( $this->fileEntry( $title->getCanonicalURL(), wfTimestamp( TS_ISO_8601
, wfTimestamp() ), $this->priority( $namespace ) ) ),
475 strlen( $this->closeFile() )
480 $maintClass = "GenerateSitemap";
481 require_once( RUN_MAINTENANCE_IF_MAIN
);