3 * Creates a Google sitemap for the site
6 * @subpackage Maintenance
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @copyright Copyright © 2005, Jens Frank <jeluf@gmx.de>
10 * @copyright Copyright © 2005, Brion Vibber <brion@pobox.com>
12 * @link http://www.google.com/webmasters/sitemaps/docs/en/about.html
13 * @link http://www.google.com/schemas/sitemap/0.84/sitemap.xsd
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
18 if ( isset( $argv[1] ) )
19 $_SERVER['SERVER_NAME'] = $argv[1];
21 $optionsWithArgs = array( 'path' );
23 require_once 'commandLine.inc';
25 define( 'GS_MAIN', -2 );
26 define( 'GS_TALK', -1 );
28 $gs = new GenerateSitemap( @$options['path'] );
31 class GenerateSitemap
{
33 * The number of entries to save in each sitemap file
40 * Key => value entries of namespaces and their priorities
44 var $priorities = array(
45 // Custom main namespaces
47 // Custom talk namesspaces
49 // MediaWiki standard namespaces
53 NS_USER_TALK
=> '0.1',
55 NS_PROJECT_TALK
=> '0.1',
57 NS_IMAGE_TALK
=> '0.1',
58 NS_MEDIAWIKI
=> '0.0',
59 NS_MEDIAWIKI_TALK
=> '0.1',
61 NS_TEMPLATE_TALK
=> '0.1',
63 NS_HELP_TALK
=> '0.1',
65 NS_CATEGORY_TALK
=> '0.1',
69 * A one-dimensional array of namespaces in the wiki
73 var $namespaces = array();
76 * A database slave object
83 * A resource pointing to the sitemap index file
91 * A resource pointing to a sitemap file
98 * A resource pointing to php://stderr
107 * @param string $path The path to prepend to the filenames, used to
108 * save them somewhere else than in the root directory
110 function GenerateSitemap( $path ) {
113 $this->path
= isset( $path ) ?
$path : '';
114 $this->stderr
= fopen( 'php://stderr', 'wt' );
116 $this->dbr
=& wfGetDB( DB_SLAVE
);
117 $this->generateNamespaces();
118 $this->generateLimit( NS_MAIN
);
119 $this->findex
= fopen( "{$this->path}sitemap-index-$wgDBname.xml", 'wb' );
123 * Generate a one-dimensional array of existing namespaces
125 function generateNamespaces() {
126 $fname = 'GenerateSitemap::generateNamespaces';
128 $res = $this->dbr
->select( 'page',
129 array( 'page_namespace' ),
133 'GROUP BY' => 'page_namespace',
134 'ORDER BY' => 'page_namespace',
138 while ( $row = $this->dbr
->fetchObject( $res ) )
139 $this->namespaces
[] = $row->page_namespace
;
143 * Get the priority of a given namespace
145 * @param int $namespace The namespace to get the priority for
150 function priority( $namespace ) {
151 return isset( $this->priorities
[$namespace] ) ?
$this->priorities
[$namespace] : $this->guessPriority( $namespace );
155 * If the namespace isn't listed on the priority list return the
156 * default priority for the namespace, varies depending on whether it's
159 * @param int $namespace The namespace to get the priority for
163 function guessPriority( $namespace ) {
164 return Namespace::isMain( $namespace ) ?
$this->priorities
[GS_MAIN
] : $this->priorities
[GS_TALK
];
168 * Return a database resolution of all the pages in a given namespace
170 * @param int $namespace Limit the query to this namespace
174 function getPageRes( $namespace ) {
175 $fname = 'GenerateSitemap::getPageRes';
177 return $this->dbr
->select( 'page',
184 array( 'page_namespace' => $namespace ),
197 fwrite( $this->findex
, $this->openIndex() );
199 foreach ( $this->namespaces
as $namespace ) {
200 $res = $this->getPageRes( $namespace );
204 $this->debug( $namespace );
205 while ( $row = $this->dbr
->fetchObject( $res ) ) {
206 if ( $i %
$this->limit
=== 0 ) {
207 if ( $this->file
!== false ) {
208 gzwrite( $this->file
, $this->closeFile() );
209 gzclose( $this->file
);
211 $this->generateLimit( $namespace );
212 $filename = "sitemap-$wgDBname-NS_$namespace-$smcount.xml.gz";
214 $this->file
= gzopen( $this->path
. $filename, 'wb' );
215 gzwrite( $this->file
, $this->openFile() );
216 fwrite( $this->findex
, $this->indexEntry( $filename ) );
217 $this->debug( "\t$filename" );
220 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
221 $date = wfTimestamp( TS_ISO_8601
, $row->page_touched
);
222 gzwrite( $this->file
, $this->fileEntry( $title->getFullURL(), $date, $this->priority( $namespace ) ) );
225 gzwrite( $this->file
, $this->closeFile() );
226 gzclose( $this->file
);
229 fwrite( $this->findex
, $this->closeIndex() );
230 fclose( $this->findex
);
234 * Return the XML required to open an XML file
241 return '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
245 * Return the XML schema being used
251 function xmlSchema() {
252 return 'http://www.google.com/schemas/sitemap/0.84';
256 * Return the XML required to open a sitemap index file
260 function openIndex() {
261 return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
265 * Return the XML for a single sitemap indexfile entry
269 * @param string $filename The filename of the sitemap file
273 function indexEntry( $filename ) {
274 global $wgServer, $wgScriptPath;
278 "\t\t<loc>$wgServer$wgScriptPath/$filename</log>\n" .
283 * Return the XML required to close a sitemap index file
289 function closeIndex() {
290 return "</sitemapindex>\n";
294 * Return the XML required to open a sitemap file
298 function openFile() {
299 return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
303 * Return the XML for a single sitemap entry
307 * @param string $url An RFC 2396 compilant URL
308 * @param string $date A ISO 8601 date
309 * @param string $priority A priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
314 function fileEntry( $url, $date, $priority ) {
317 "\t\t<loc>$url</loc>\n" .
318 "\t\t<lastmod>$date</lastmod>\n" .
319 "\t\t<priority>$priority</priority>\n" .
324 * Return the XML required to close sitemap file
329 function closeFile() {
330 return "</urlset>\n";
334 * Write a string to stderr followed by a UNIX newline
336 function debug( $str ) {
337 fwrite( $this->stderr
, "$str\n" );
341 * According to the sitemap specification each sitemap must contain no
342 * more than 50,000 urls and no more than 2^20 bytes (10MB), this
343 * function calculates how many urls we can have in each file assuming
344 * that we have the worst case of 63 four byte characters and 1 three
345 * byte character in the title (63*4+1*3 = 255)
347 function generateLimit( $namespace ) {
348 $title = Title
::makeTitle( $namespace, str_repeat( "\xf0\xa8\xae\x81", 63 ) . "\xe5\x96\x83" );
350 $olen = strlen( $this->openFile() );
351 $elen = strlen( $this->fileEntry( $title->getFullUrl(), wfTimestamp( TS_ISO_8601
, wfTimestamp() ), '1.0' ) );
352 $clen = strlen( $this->closeFile() );
354 for ( $i = 1, $etot = $elen; ( $olen +
$clen +
$etot +
$elen ) <= pow( 2, 20 ); ++
$i )