]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - maintenance/generateSitemap.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / generateSitemap.php
index 04dbbc4d02cf9f235a83419845ee8c72dbb29a38..e483f7c95d7386b1ca7bc95d587c0b9b0c0a54b1 100644 (file)
@@ -4,6 +4,9 @@ define( 'GS_TALK', -1 );
 /**
  * Creates a sitemap for the site
  *
+ * Copyright © 2005, Ævar Arnfjörð Bjarmason, Jens Frank <jeluf@gmx.de> and
+ * Brion Vibber <brion@pobox.com>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -19,19 +22,13 @@ define( 'GS_TALK', -1 );
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  * @ingroup Maintenance
- *
- * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
- * @copyright Copyright © 2005, Jens Frank <jeluf@gmx.de>
- * @copyright Copyright © 2005, Brion Vibber <brion@pobox.com>
- *
  * @see http://www.sitemaps.org/
  * @see http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
- *
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  */
 
-require_once( dirname(__FILE__) . '/Maintenance.php' );
+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
 class GenerateSitemap extends Maintenance {
        /**
@@ -60,11 +57,11 @@ class GenerateSitemap extends Maintenance {
        var $fspath;
 
        /**
-        * The path to append to the domain name
+        * The URL path to prepend to filenames in the index; should resolve to the same directory as $fspath
         *
         * @var string
         */
-       var $path;
+       var $urlpath;
 
        /**
         * Whether or not to use compression
@@ -129,8 +126,8 @@ class GenerateSitemap extends Maintenance {
        public function __construct() {
                parent::__construct();
                $this->mDescription = "Creates a sitemap for the site";
-               $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap' .
-                                                                       "\n\t\tdefaults to current directory", false, true );
+               $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap; defaults to current directory', false, true );
+               $this->addOption( 'urlpath', 'The URL path corresponding to --fspath, prepended to filenames in the index; defaults to an empty string', false, true );
                $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes', false, true );
        }
 
@@ -138,11 +135,14 @@ class GenerateSitemap extends Maintenance {
         * Execute
         */
        public function execute() {
-               global $wgScriptPath;
                $this->setNamespacePriorities();
                $this->url_limit = 50000;
                $this->size_limit = pow( 2, 20 ) * 10;
                $this->fspath = self::init_path( $this->getOption( 'fspath', getcwd() ) );
+               $this->urlpath = $this->getOption( 'urlpath', "" );
+               if ( $this->urlpath !== "" && substr( $this->urlpath, -1 ) !== '/' ) {
+                       $this->urlpath .= '/';
+               }
                $this->compress = $this->getOption( 'compress', 'yes' ) !== 'no';
                $this->dbr = wfGetDB( DB_SLAVE );
                $this->generateNamespaces();
@@ -179,15 +179,15 @@ class GenerateSitemap extends Maintenance {
         * Create directory if it does not exist and return pathname with a trailing slash
         */
        private static function init_path( $fspath ) {
-               if( !isset( $fspath ) ) {
+               if ( !isset( $fspath ) ) {
                        return null;
                }
                # Create directory if needed
-               if( $fspath && !is_dir( $fspath ) ) {
-                       wfMkdirParents( $fspath ) or die("Can not create directory $fspath.\n");
+               if ( $fspath && !is_dir( $fspath ) ) {
+                       wfMkdirParents( $fspath ) or die( "Can not create directory $fspath.\n" );
                }
 
-               return realpath( $fspath ). DIRECTORY_SEPARATOR ;
+               return realpath( $fspath ) . DIRECTORY_SEPARATOR ;
        }
 
        /**
@@ -196,7 +196,7 @@ class GenerateSitemap extends Maintenance {
        function generateNamespaces() {
                // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
                global $wgSitemapNamespaces;
-               if( is_array( $wgSitemapNamespaces ) ) {
+               if ( is_array( $wgSitemapNamespaces ) ) {
                        $this->namespaces = $wgSitemapNamespaces;
                        return;
                }
@@ -218,11 +218,9 @@ class GenerateSitemap extends Maintenance {
        /**
         * Get the priority of a given namespace
         *
-        * @param int $namespace The namespace to get the priority for
-        +
-        * @return string
+        * @param $namespace Integer: the namespace to get the priority for
+        * @return String
         */
-
        function priority( $namespace ) {
                return isset( $this->priorities[$namespace] ) ? $this->priorities[$namespace] : $this->guessPriority( $namespace );
        }
@@ -232,9 +230,8 @@ class GenerateSitemap extends Maintenance {
         * default priority for the namespace, varies depending on whether it's
         * a talkpage or not.
         *
-        * @param int $namespace The namespace to get the priority for
-        *
-        * @return string
+        * @param $namespace Integer: the namespace to get the priority for
+        * @return String
         */
        function guessPriority( $namespace ) {
                return MWNamespace::isMain( $namespace ) ? $this->priorities[GS_MAIN] : $this->priorities[GS_TALK];
@@ -243,9 +240,8 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return a database resolution of all the pages in a given namespace
         *
-        * @param int $namespace Limit the query to this namespace
-        *
-        * @return resource
+        * @param $namespace Integer: limit the query to this namespace
+        * @return Resource
         */
        function getPageRes( $namespace ) {
                return $this->dbr->select( 'page',
@@ -261,10 +257,8 @@ class GenerateSitemap extends Maintenance {
 
        /**
         * Main loop
-        *
-        * @access public
         */
-       function main() {
+       public function main() {
                global $wgContLang;
 
                fwrite( $this->findex, $this->openIndex() );
@@ -298,11 +292,11 @@ class GenerateSitemap extends Maintenance {
                                $length += strlen( $entry );
                                $this->write( $this->file, $entry );
                                // generate pages for language variants
-                               if($wgContLang->hasVariants()){
+                               if ( $wgContLang->hasVariants() ) {
                                        $variants = $wgContLang->getVariants();
-                                       foreach($variants as $vCode){
-                                               if($vCode==$wgContLang->getCode()) continue; // we don't want default variant
-                                               $entry = $this->fileEntry( $title->getFullURL('',$vCode), $date, $this->priority( $namespace ) );
+                                       foreach ( $variants as $vCode ) {
+                                               if ( $vCode == $wgContLang->getCode() ) continue; // we don't want default variant
+                                               $entry = $this->fileEntry( $title->getFullURL( '', $vCode ), $date, $this->priority( $namespace ) );
                                                $length += strlen( $entry );
                                                $this->write( $this->file, $entry );
                                        }
@@ -320,7 +314,7 @@ class GenerateSitemap extends Maintenance {
        /**
         * gzopen() / fopen() wrapper
         *
-        * @return resource
+        * @return Resource
         */
        function open( $file, $flags ) {
                return $this->compress ? gzopen( $file, $flags ) : fopen( $file, $flags );
@@ -349,23 +343,18 @@ class GenerateSitemap extends Maintenance {
        /**
         * Get a sitemap filename
         *
-        * @static
-        *
-        * @param int $namespace The namespace
-        * @param int $count The count
-        *
-        * @return string
+        * @param $namespace Integer: the namespace
+        * @param $count Integer: the count
+        * @return String
         */
        function sitemapFilename( $namespace, $count ) {
                $ext = $this->compress ? '.gz' : '';
-               return "sitemap-".wfWikiID()."-NS_$namespace-$count.xml$ext";
+               return "sitemap-" . wfWikiID() . "-NS_$namespace-$count.xml$ext";
        }
 
        /**
         * Return the XML required to open an XML file
         *
-        * @static
-        *
         * @return string
         */
        function xmlHead() {
@@ -375,9 +364,7 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML schema being used
         *
-        * @static
-        *
-        * @returns string
+        * @return String
         */
        function xmlSchema() {
                return 'http://www.sitemaps.org/schemas/sitemap/0.9';
@@ -386,7 +373,7 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML required to open a sitemap index file
         *
-        * @return string
+        * @return String
         */
        function openIndex() {
                return $this->xmlHead() . '<sitemapindex xmlns="' . $this->xmlSchema() . '">' . "\n";
@@ -395,16 +382,13 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML for a single sitemap indexfile entry
         *
-        * @static
-        *
-        * @param string $filename The filename of the sitemap file
-        *
-        * @return string
+        * @param $filename String: the filename of the sitemap file
+        * @return String
         */
        function indexEntry( $filename ) {
                return
                        "\t<sitemap>\n" .
-                       "\t\t<loc>$filename</loc>\n" .
+                       "\t\t<loc>{$this->urlpath}$filename</loc>\n" .
                        "\t\t<lastmod>{$this->timestamp}</lastmod>\n" .
                        "\t</sitemap>\n";
        }
@@ -412,9 +396,7 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML required to close a sitemap index file
         *
-        * @static
-        *
-        * @return string
+        * @return String
         */
        function closeIndex() {
                return "</sitemapindex>\n";
@@ -423,7 +405,7 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML required to open a sitemap file
         *
-        * @return string
+        * @return String
         */
        function openFile() {
                return $this->xmlHead() . '<urlset xmlns="' . $this->xmlSchema() . '">' . "\n";
@@ -432,13 +414,10 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML for a single sitemap entry
         *
-        * @static
-        *
-        * @param string $url An RFC 2396 compliant URL
-        * @param string $date A ISO 8601 date
-        * @param string $priority A priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
-        *
-        * @return string
+        * @param $url String: an RFC 2396 compliant URL
+        * @param $date String: a ISO 8601 date
+        * @param $priority String: a priority indicator, 0.0 - 1.0 inclusive with a 0.1 stepsize
+        * @return String
         */
        function fileEntry( $url, $date, $priority ) {
                return
@@ -452,8 +431,7 @@ class GenerateSitemap extends Maintenance {
        /**
         * Return the XML required to close sitemap file
         *
-        * @static
-        * @return string
+        * @return String
         */
        function closeFile() {
                return "</urlset>\n";
@@ -474,4 +452,4 @@ class GenerateSitemap extends Maintenance {
 }
 
 $maintClass = "GenerateSitemap";
-require_once( DO_MAINTENANCE );
+require_once( RUN_MAINTENANCE_IF_MAIN );