2 define( 'GS_MAIN', -2 );
3 define( 'GS_TALK', -1 );
5 * Creates a sitemap for the site
7 * Copyright © 2005, Ævar Arnfjörð Bjarmason, Jens Frank <jeluf@gmx.de> and
8 * Brion Vibber <brion@pobox.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 * @ingroup Maintenance
27 * @see http://www.sitemaps.org/
28 * @see http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
31 require_once( dirname( __FILE__
) . '/Maintenance.php' );
33 class GenerateSitemap
extends Maintenance
{
35 * The maximum amount of urls in a sitemap file
37 * @link http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
44 * The maximum size of a sitemap file
46 * @link http://www.sitemaps.org/faq.php#faq_sitemap_size
53 * The path to prepend to the filename
60 * The path to append to the domain name
67 * Whether or not to use compression
74 * The number of entries to save in each sitemap file
81 * Key => value entries of namespaces and their priorities
85 var $priorities = array();
88 * A one-dimensional array of namespaces in the wiki
92 var $namespaces = array();
95 * When this sitemap batch was generated
102 * A database slave object
109 * A resource pointing to the sitemap index file
117 * A resource pointing to a sitemap file
126 public function __construct() {
127 parent
::__construct();
128 $this->mDescription
= "Creates a sitemap for the site";
129 $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap' .
130 "\n\t\tdefaults to current directory", false, true );
131 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g.\n" .
132 "\t\thttp://en.wikipedia.org. This is sometimes necessary because\n" .
133 "\t\tserver name detection may fail in command line scripts.", false, true );
134 $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes', false, true );
140 public function execute() {
141 $this->setNamespacePriorities();
142 $this->url_limit
= 50000;
143 $this->size_limit
= pow( 2, 20 ) * 10;
144 $this->fspath
= self
::init_path( $this->getOption( 'fspath', getcwd() ) );
145 $this->compress
= $this->getOption( 'compress', 'yes' ) !== 'no';
146 $this->dbr
= wfGetDB( DB_SLAVE
);
147 $this->generateNamespaces();
148 $this->timestamp
= wfTimestamp( TS_ISO_8601
, wfTimestampNow() );
149 $this->findex
= fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
153 private function setNamespacePriorities() {
154 // Custom main namespaces
155 $this->priorities
[GS_MAIN
] = '0.5';
156 // Custom talk namesspaces
157 $this->priorities
[GS_TALK
] = '0.1';
158 // MediaWiki standard namespaces
159 $this->priorities
[NS_MAIN
] = '1.0';
160 $this->priorities
[NS_TALK
] = '0.1';
161 $this->priorities
[NS_USER
] = '0.5';
162 $this->priorities
[NS_USER_TALK
] = '0.1';
163 $this->priorities
[NS_PROJECT
] = '0.5';
164 $this->priorities
[NS_PROJECT_TALK
] = '0.1';
165 $this->priorities
[NS_FILE
] = '0.5';
166 $this->priorities
[NS_FILE_TALK
] = '0.1';
167 $this->priorities
[NS_MEDIAWIKI
] = '0.0';
168 $this->priorities
[NS_MEDIAWIKI_TALK
] = '0.1';
169 $this->priorities
[NS_TEMPLATE
] = '0.0';
170 $this->priorities
[NS_TEMPLATE_TALK
] = '0.1';
171 $this->priorities
[NS_HELP
] = '0.5';
172 $this->priorities
[NS_HELP_TALK
] = '0.1';
173 $this->priorities
[NS_CATEGORY
] = '0.5';
174 $this->priorities
[NS_CATEGORY_TALK
] = '0.1';
178 * Create directory if it does not exist and return pathname with a trailing slash
180 private static function init_path( $fspath ) {
181 if ( !isset( $fspath ) ) {
184 # Create directory if needed
185 if ( $fspath && !is_dir( $fspath ) ) {
186 wfMkdirParents( $fspath ) or die( "Can not create directory $fspath.\n" );
189 return realpath( $fspath ) . DIRECTORY_SEPARATOR
;
193 * Generate a one-dimensional array of existing namespaces
195 function generateNamespaces() {
196 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
197 global $wgSitemapNamespaces;
198 if ( is_array( $wgSitemapNamespaces ) ) {
199 $this->namespaces
= $wgSitemapNamespaces;
203 $res = $this->dbr
->select( 'page',
204 array( 'page_namespace' ),
208 'GROUP BY' => 'page_namespace',
209 'ORDER BY' => 'page_namespace',
213 foreach ( $res as $row )
214 $this->namespaces
[] = $row->page_namespace
;
218 * Get the priority of a given namespace
220 * @param $namespace Integer: the namespace to get the priority for
223 function priority( $namespace ) {
224 return isset( $this->priorities
[$namespace] ) ?
$this->priorities
[$namespace] : $this->guessPriority( $namespace );
228 * If the namespace isn't listed on the priority list return the
229 * default priority for the namespace, varies depending on whether it's
232 * @param $namespace Integer: the namespace to get the priority for
235 function guessPriority( $namespace ) {
236 return MWNamespace
::isMain( $namespace ) ?
$this->priorities
[GS_MAIN
] : $this->priorities
[GS_TALK
];
240 * Return a database resolution of all the pages in a given namespace
242 * @param $namespace Integer: limit the query to this namespace
245 function getPageRes( $namespace ) {
246 return $this->dbr
->select( 'page',
252 array( 'page_namespace' => $namespace ),
260 public function main() {
263 fwrite( $this->findex
, $this->openIndex() );
265 foreach ( $this->namespaces
as $namespace ) {
266 $res = $this->getPageRes( $namespace );
268 $this->generateLimit( $namespace );
269 $length = $this->limit
[0];
272 $fns = $wgContLang->getFormattedNsText( $namespace );
273 $this->output( "$namespace ($fns)" );
274 foreach ( $res as $row ) {
275 if ( $i++
=== 0 ||
$i === $this->url_limit +
1 ||
$length +
$this->limit
[1] +
$this->limit
[2] > $this->size_limit
) {
276 if ( $this->file
!== false ) {
277 $this->write( $this->file
, $this->closeFile() );
278 $this->close( $this->file
);
280 $filename = $this->sitemapFilename( $namespace, $smcount++
);
281 $this->file
= $this->open( $this->fspath
. $filename, 'wb' );
282 $this->write( $this->file
, $this->openFile() );
283 fwrite( $this->findex
, $this->indexEntry( $filename ) );
284 $this->output( "\t$this->fspath$filename\n" );
285 $length = $this->limit
[0];
288 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
289 $date = wfTimestamp( TS_ISO_8601
, $row->page_touched
);
290 $entry = $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) );
291 $length +
= strlen( $entry );
292 $this->write( $this->file
, $entry );
293 // generate pages for language variants
294 if ( $wgContLang->hasVariants() ) {
295 $variants = $wgContLang->getVariants();
296 foreach ( $variants as $vCode ) {
297 if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
298 $entry = $this->fileEntry( $title->getFullURL( '', $vCode ), $date, $this->priority( $namespace ) );
299 $length +
= strlen( $entry );
300 $this->write( $this->file
, $entry );
305 $this->write( $this->file
, $this->closeFile() );
306 $this->close( $this->file
);
309 fwrite( $this->findex
, $this->closeIndex() );
310 fclose( $this->findex
);
314 * gzopen() / fopen() wrapper
318 function open( $file, $flags ) {
319 return $this->compress ?
gzopen( $file, $flags ) : fopen( $file, $flags );
323 * gzwrite() / fwrite() wrapper
325 function write( &$handle, $str ) {
326 if ( $this->compress
)
327 gzwrite( $handle, $str );
329 fwrite( $handle, $str );
333 * gzclose() / fclose() wrapper
335 function close( &$handle ) {
336 if ( $this->compress
)
343 * Get a sitemap filename
345 * @param $namespace Integer: the namespace
346 * @param $count Integer: the count
349 function sitemapFilename( $namespace, $count ) {
350 $ext = $this->compress ?
'.gz' : '';
351 return "sitemap-" . wfWikiID() . "-NS_$namespace-$count.xml$ext";
355 * Return the XML required to open an XML file
360 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
364 * Return the XML schema being used
368 function xmlSchema() {
369 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
373 * Return the XML required to open a sitemap index file
377 function openIndex() {
378 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
382 * Return the XML for a single sitemap indexfile entry
384 * @param $filename String: the filename of the sitemap file
387 function indexEntry( $filename ) {
390 "\t\t<loc>$filename</loc>\n" .
391 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
396 * Return the XML required to close a sitemap index file
400 function closeIndex() {
401 return "</sitemapindex>\n";
405 * Return the XML required to open a sitemap file
409 function openFile() {
410 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
414 * Return the XML for a single sitemap entry
416 * @param $url String: an RFC 2396 compliant URL
417 * @param $date String: a ISO 8601 date
418 * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
421 function fileEntry( $url, $date, $priority ) {
424 "\t\t<loc>$url</loc>\n" .
425 "\t\t<lastmod>$date</lastmod>\n" .
426 "\t\t<priority>$priority</priority>\n" .
431 * Return the XML required to close sitemap file
435 function closeFile() {
436 return "</urlset>\n";
440 * Populate $this->limit
442 function generateLimit( $namespace ) {
443 $title = Title
::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
445 $this->limit
= array(
446 strlen( $this->openFile() ),
447 strlen( $this->fileEntry( $title->getFullUrl(), wfTimestamp( TS_ISO_8601
, wfTimestamp() ), $this->priority( $namespace ) ) ),
448 strlen( $this->closeFile() )
453 $maintClass = "GenerateSitemap";
454 require_once( DO_MAINTENANCE
);