2 define( 'GS_MAIN', -2 );
3 define( 'GS_TALK', -1 );
5 * Creates a sitemap for the site
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
24 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
25 * @copyright Copyright © 2005, Jens Frank <jeluf@gmx.de>
26 * @copyright Copyright © 2005, Brion Vibber <brion@pobox.com>
28 * @see http://www.sitemaps.org/
29 * @see http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
31 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
34 require_once( dirname( __FILE__
) . '/Maintenance.php' );
36 class GenerateSitemap
extends Maintenance
{
38 * The maximum amount of urls in a sitemap file
40 * @link http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
47 * The maximum size of a sitemap file
49 * @link http://www.sitemaps.org/faq.php#faq_sitemap_size
56 * The path to prepend to the filename
63 * The path to append to the domain name
70 * Whether or not to use compression
77 * The number of entries to save in each sitemap file
84 * Key => value entries of namespaces and their priorities
88 var $priorities = array();
91 * A one-dimensional array of namespaces in the wiki
95 var $namespaces = array();
98 * When this sitemap batch was generated
105 * A database slave object
112 * A resource pointing to the sitemap index file
120 * A resource pointing to a sitemap file
129 public function __construct() {
130 parent
::__construct();
131 $this->mDescription
= "Creates a sitemap for the site";
132 $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap' .
133 "\n\t\tdefaults to current directory", false, true );
134 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g.\n" .
135 "\t\thttp://en.wikipedia.org. This is sometimes necessary because\n" .
136 "\t\tserver name detection may fail in command line scripts.", false, true );
137 $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes', false, true );
143 public function execute() {
144 $this->setNamespacePriorities();
145 $this->url_limit
= 50000;
146 $this->size_limit
= pow( 2, 20 ) * 10;
147 $this->fspath
= self
::init_path( $this->getOption( 'fspath', getcwd() ) );
148 $this->compress
= $this->getOption( 'compress', 'yes' ) !== 'no';
149 $this->dbr
= wfGetDB( DB_SLAVE
);
150 $this->generateNamespaces();
151 $this->timestamp
= wfTimestamp( TS_ISO_8601
, wfTimestampNow() );
152 $this->findex
= fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
156 private function setNamespacePriorities() {
157 // Custom main namespaces
158 $this->priorities
[GS_MAIN
] = '0.5';
159 // Custom talk namesspaces
160 $this->priorities
[GS_TALK
] = '0.1';
161 // MediaWiki standard namespaces
162 $this->priorities
[NS_MAIN
] = '1.0';
163 $this->priorities
[NS_TALK
] = '0.1';
164 $this->priorities
[NS_USER
] = '0.5';
165 $this->priorities
[NS_USER_TALK
] = '0.1';
166 $this->priorities
[NS_PROJECT
] = '0.5';
167 $this->priorities
[NS_PROJECT_TALK
] = '0.1';
168 $this->priorities
[NS_FILE
] = '0.5';
169 $this->priorities
[NS_FILE_TALK
] = '0.1';
170 $this->priorities
[NS_MEDIAWIKI
] = '0.0';
171 $this->priorities
[NS_MEDIAWIKI_TALK
] = '0.1';
172 $this->priorities
[NS_TEMPLATE
] = '0.0';
173 $this->priorities
[NS_TEMPLATE_TALK
] = '0.1';
174 $this->priorities
[NS_HELP
] = '0.5';
175 $this->priorities
[NS_HELP_TALK
] = '0.1';
176 $this->priorities
[NS_CATEGORY
] = '0.5';
177 $this->priorities
[NS_CATEGORY_TALK
] = '0.1';
181 * Create directory if it does not exist and return pathname with a trailing slash
183 private static function init_path( $fspath ) {
184 if ( !isset( $fspath ) ) {
187 # Create directory if needed
188 if ( $fspath && !is_dir( $fspath ) ) {
189 wfMkdirParents( $fspath ) or die( "Can not create directory $fspath.\n" );
192 return realpath( $fspath ) . DIRECTORY_SEPARATOR
;
196 * Generate a one-dimensional array of existing namespaces
198 function generateNamespaces() {
199 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
200 global $wgSitemapNamespaces;
201 if ( is_array( $wgSitemapNamespaces ) ) {
202 $this->namespaces
= $wgSitemapNamespaces;
206 $res = $this->dbr
->select( 'page',
207 array( 'page_namespace' ),
211 'GROUP BY' => 'page_namespace',
212 'ORDER BY' => 'page_namespace',
216 foreach ( $res as $row )
217 $this->namespaces
[] = $row->page_namespace
;
221 * Get the priority of a given namespace
223 * @param $namespace Integer: the namespace to get the priority for
226 function priority( $namespace ) {
227 return isset( $this->priorities
[$namespace] ) ?
$this->priorities
[$namespace] : $this->guessPriority( $namespace );
231 * If the namespace isn't listed on the priority list return the
232 * default priority for the namespace, varies depending on whether it's
235 * @param $namespace Integer: the namespace to get the priority for
238 function guessPriority( $namespace ) {
239 return MWNamespace
::isMain( $namespace ) ?
$this->priorities
[GS_MAIN
] : $this->priorities
[GS_TALK
];
243 * Return a database resolution of all the pages in a given namespace
245 * @param $namespace Integer: limit the query to this namespace
248 function getPageRes( $namespace ) {
249 return $this->dbr
->select( 'page',
255 array( 'page_namespace' => $namespace ),
263 public function main() {
266 fwrite( $this->findex
, $this->openIndex() );
268 foreach ( $this->namespaces
as $namespace ) {
269 $res = $this->getPageRes( $namespace );
271 $this->generateLimit( $namespace );
272 $length = $this->limit
[0];
275 $fns = $wgContLang->getFormattedNsText( $namespace );
276 $this->output( "$namespace ($fns)" );
277 foreach ( $res as $row ) {
278 if ( $i++
=== 0 ||
$i === $this->url_limit +
1 ||
$length +
$this->limit
[1] +
$this->limit
[2] > $this->size_limit
) {
279 if ( $this->file
!== false ) {
280 $this->write( $this->file
, $this->closeFile() );
281 $this->close( $this->file
);
283 $filename = $this->sitemapFilename( $namespace, $smcount++
);
284 $this->file
= $this->open( $this->fspath
. $filename, 'wb' );
285 $this->write( $this->file
, $this->openFile() );
286 fwrite( $this->findex
, $this->indexEntry( $filename ) );
287 $this->output( "\t$this->fspath$filename\n" );
288 $length = $this->limit
[0];
291 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
292 $date = wfTimestamp( TS_ISO_8601
, $row->page_touched
);
293 $entry = $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) );
294 $length +
= strlen( $entry );
295 $this->write( $this->file
, $entry );
296 // generate pages for language variants
297 if ( $wgContLang->hasVariants() ) {
298 $variants = $wgContLang->getVariants();
299 foreach ( $variants as $vCode ) {
300 if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
301 $entry = $this->fileEntry( $title->getFullURL( '', $vCode ), $date, $this->priority( $namespace ) );
302 $length +
= strlen( $entry );
303 $this->write( $this->file
, $entry );
308 $this->write( $this->file
, $this->closeFile() );
309 $this->close( $this->file
);
312 fwrite( $this->findex
, $this->closeIndex() );
313 fclose( $this->findex
);
317 * gzopen() / fopen() wrapper
321 function open( $file, $flags ) {
322 return $this->compress ?
gzopen( $file, $flags ) : fopen( $file, $flags );
326 * gzwrite() / fwrite() wrapper
328 function write( &$handle, $str ) {
329 if ( $this->compress
)
330 gzwrite( $handle, $str );
332 fwrite( $handle, $str );
336 * gzclose() / fclose() wrapper
338 function close( &$handle ) {
339 if ( $this->compress
)
346 * Get a sitemap filename
348 * @param $namespace Integer: the namespace
349 * @param $count Integer: the count
352 function sitemapFilename( $namespace, $count ) {
353 $ext = $this->compress ?
'.gz' : '';
354 return "sitemap-" . wfWikiID() . "-NS_$namespace-$count.xml$ext";
358 * Return the XML required to open an XML file
363 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
367 * Return the XML schema being used
371 function xmlSchema() {
372 return 'http://www.sitemaps.org/schemas/sitemap/0.9';
376 * Return the XML required to open a sitemap index file
380 function openIndex() {
381 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
385 * Return the XML for a single sitemap indexfile entry
387 * @param $filename String: the filename of the sitemap file
390 function indexEntry( $filename ) {
393 "\t\t<loc>$filename</loc>\n" .
394 "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
399 * Return the XML required to close a sitemap index file
403 function closeIndex() {
404 return "</sitemapindex>\n";
408 * Return the XML required to open a sitemap file
412 function openFile() {
413 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
417 * Return the XML for a single sitemap entry
419 * @param $url String: an RFC 2396 compliant URL
420 * @param $date String: a ISO 8601 date
421 * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
424 function fileEntry( $url, $date, $priority ) {
427 "\t\t<loc>$url</loc>\n" .
428 "\t\t<lastmod>$date</lastmod>\n" .
429 "\t\t<priority>$priority</priority>\n" .
434 * Return the XML required to close sitemap file
438 function closeFile() {
439 return "</urlset>\n";
443 * Populate $this->limit
445 function generateLimit( $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( DO_MAINTENANCE
);