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
127 public function __construct() {
128 parent
::__construct();
129 $this->mDescription
= "Creates a sitemap for the site";
130 $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory', false, true );
131 $this->addOption( 'urlpath', 'The URL path corresponding to --fspath, prepended to filenames in the index; defaults to an empty string', false, true );
132 $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes', false, true );
138 public function execute() {
139 $this->setNamespacePriorities();
140 $this->url_limit
= 50000;
141 $this->size_limit
= pow( 2, 20 ) * 10;
142 $this->fspath
= self
::init_path( $this->getOption( 'fspath', getcwd() ) );
143 $this->urlpath
= $this->getOption( 'urlpath', "" );
144 if ( $this->urlpath
!== "" && substr( $this->urlpath
, -1 ) !== '/' ) {
145 $this->urlpath
.= '/';
147 $this->compress
= $this->getOption( 'compress', 'yes' ) !== 'no';
148 $this->dbr
= wfGetDB( DB_SLAVE
);
149 $this->generateNamespaces();
150 $this->timestamp
= wfTimestamp( TS_ISO_8601
, wfTimestampNow() );
151 $this->findex
= fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
155 private function setNamespacePriorities() {
156 // Custom main namespaces
157 $this->priorities
[self
::GS_MAIN
] = '0.5';
158 // Custom talk namesspaces
159 $this->priorities
[self
::GS_TALK
] = '0.1';
160 // MediaWiki standard namespaces
161 $this->priorities
[NS_MAIN
] = '1.0';
162 $this->priorities
[NS_TALK
] = '0.1';
163 $this->priorities
[NS_USER
] = '0.5';
164 $this->priorities
[NS_USER_TALK
] = '0.1';
165 $this->priorities
[NS_PROJECT
] = '0.5';
166 $this->priorities
[NS_PROJECT_TALK
] = '0.1';
167 $this->priorities
[NS_FILE
] = '0.5';
168 $this->priorities
[NS_FILE_TALK
] = '0.1';
169 $this->priorities
[NS_MEDIAWIKI
] = '0.0';
170 $this->priorities
[NS_MEDIAWIKI_TALK
] = '0.1';
171 $this->priorities
[NS_TEMPLATE
] = '0.0';
172 $this->priorities
[NS_TEMPLATE_TALK
] = '0.1';
173 $this->priorities
[NS_HELP
] = '0.5';
174 $this->priorities
[NS_HELP_TALK
] = '0.1';
175 $this->priorities
[NS_CATEGORY
] = '0.5';
176 $this->priorities
[NS_CATEGORY_TALK
] = '0.1';
180 * Create directory if it does not exist and return pathname with a trailing slash
182 private static function init_path( $fspath ) {
183 if ( !isset( $fspath ) ) {
186 # Create directory if needed
187 if ( $fspath && !is_dir( $fspath ) ) {
188 wfMkdirParents( $fspath ) or die( "Can not create directory $fspath.\n" );
191 return realpath( $fspath ) . DIRECTORY_SEPARATOR
;
195 * Generate a one-dimensional array of existing namespaces
197 function generateNamespaces() {
198 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
199 global $wgSitemapNamespaces;
200 if ( is_array( $wgSitemapNamespaces ) ) {
201 $this->namespaces
= $wgSitemapNamespaces;
205 $res = $this->dbr
->select( 'page',
206 array( 'page_namespace' ),
210 'GROUP BY' => 'page_namespace',
211 'ORDER BY' => 'page_namespace',
215 foreach ( $res as $row )
216 $this->namespaces
[] = $row->page_namespace
;
220 * Get the priority of a given namespace
222 * @param $namespace Integer: the namespace to get the priority for
225 function priority( $namespace ) {
226 return isset( $this->priorities
[$namespace] ) ?
$this->priorities
[$namespace] : $this->guessPriority( $namespace );
230 * If the namespace isn't listed on the priority list return the
231 * default priority for the namespace, varies depending on whether it's
234 * @param $namespace Integer: the namespace to get the priority for
237 function guessPriority( $namespace ) {
238 return MWNamespace
::isMain( $namespace ) ?
$this->priorities
[self
::GS_MAIN
] : $this->priorities
[self
::GS_TALK
];
242 * Return a database resolution of all the pages in a given namespace
244 * @param $namespace Integer: limit the query to this namespace
247 function getPageRes( $namespace ) {
248 return $this->dbr
->select( 'page',
254 array( 'page_namespace' => $namespace ),
262 public function main() {
265 fwrite( $this->findex
, $this->openIndex() );
267 foreach ( $this->namespaces
as $namespace ) {
268 $res = $this->getPageRes( $namespace );
270 $this->generateLimit( $namespace );
271 $length = $this->limit
[0];
274 $fns = $wgContLang->getFormattedNsText( $namespace );
275 $this->output( "$namespace ($fns)\n" );
276 foreach ( $res as $row ) {
277 if ( $i++
=== 0 ||
$i === $this->url_limit +
1 ||
$length +
$this->limit
[1] +
$this->limit
[2] > $this->size_limit
) {
278 if ( $this->file
!== false ) {
279 $this->write( $this->file
, $this->closeFile() );
280 $this->close( $this->file
);
282 $filename = $this->sitemapFilename( $namespace, $smcount++
);
283 $this->file
= $this->open( $this->fspath
. $filename, 'wb' );
284 $this->write( $this->file
, $this->openFile() );
285 fwrite( $this->findex
, $this->indexEntry( $filename ) );
286 $this->output( "\t$this->fspath$filename\n" );
287 $length = $this->limit
[0];
290 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
291 $date = wfTimestamp( TS_ISO_8601
, $row->page_touched
);
292 $entry = $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) );
293 $length +
= strlen( $entry );
294 $this->write( $this->file
, $entry );
295 // generate pages for language variants
296 if ( $wgContLang->hasVariants() ) {
297 $variants = $wgContLang->getVariants();
298 foreach ( $variants as $vCode ) {
299 if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
300 $entry = $this->fileEntry( $title->getFullURL( '', $vCode ), $date, $this->priority( $namespace ) );
301 $length +
= strlen( $entry );
302 $this->write( $this->file
, $entry );
307 $this->write( $this->file
, $this->closeFile() );
308 $this->close( $this->file
);
311 fwrite( $this->findex
, $this->closeIndex() );
312 fclose( $this->findex
);
316 * gzopen() / fopen() wrapper
320 function open( $file, $flags ) {
321 return $this->compress ?
gzopen( $file, $flags ) : fopen( $file, $flags );
325 * gzwrite() / fwrite() wrapper
327 function write( &$handle, $str ) {
328 if ( $this->compress
)
329 gzwrite( $handle, $str );
331 fwrite( $handle, $str );
335 * gzclose() / fclose() wrapper
337 function close( &$handle ) {
338 if ( $this->compress
)
345 * Get a sitemap filename
347 * @param $namespace Integer: the namespace
348 * @param $count Integer: the count
351 function sitemapFilename( $namespace, $count ) {
352 $ext = $this->compress ?
'.gz' : '';
353 return "sitemap-" . wfWikiID() . "-NS_$namespace-$count.xml$ext";
357 * Return the XML required to open an XML file
362 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
366 * Return the XML schema being used
370 function xmlSchema() {
371 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
375 * Return the XML required to open a sitemap index file
379 function openIndex() {
380 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
384 * Return the XML for a single sitemap indexfile entry
386 * @param $filename String: the filename of the sitemap file
389 function indexEntry( $filename ) {
392 "\t\t<loc>{$this->urlpath}$filename</loc>\n" .
393 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
398 * Return the XML required to close a sitemap index file
402 function closeIndex() {
403 return "</sitemapindex>\n";
407 * Return the XML required to open a sitemap file
411 function openFile() {
412 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
416 * Return the XML for a single sitemap entry
418 * @param $url String: an RFC 2396 compliant URL
419 * @param $date String: a ISO 8601 date
420 * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
423 function fileEntry( $url, $date, $priority ) {
426 "\t\t<loc>$url</loc>\n" .
427 "\t\t<lastmod>$date</lastmod>\n" .
428 "\t\t<priority>$priority</priority>\n" .
433 * Return the XML required to close sitemap file
437 function closeFile() {
438 return "</urlset>\n";
442 * Populate $this->limit
444 function generateLimit( $namespace ) {
445 // bug 17961: make a title with the longest possible URL in this namespace
446 $title = Title
::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
448 $this->limit
= array(
449 strlen( $this->openFile() ),
450 strlen( $this->fileEntry( $title->getFullUrl(), wfTimestamp( TS_ISO_8601
, wfTimestamp() ), $this->priority( $namespace ) ) ),
451 strlen( $this->closeFile() )
456 $maintClass = "GenerateSitemap";
457 require_once( RUN_MAINTENANCE_IF_MAIN
);