Folowup r77763, add documentation for $wgFooterIcons.
[lhc/web/wiklou.git] / includes / Export.php
index 3372cec..bdb9d4a 100644 (file)
@@ -1,21 +1,27 @@
 <?php
-# Copyright (C) 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
-# http://www.mediawiki.org/
-#
-# 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
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
+/**
+ * Base classes for dumps and export
+ *
+ * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
+ * http://www.mediawiki.org/
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 /**
  * @defgroup Dump Dump
@@ -55,6 +61,7 @@ class WikiExporter {
         *                   limit: maximum number of rows to return
         *                   dir: "asc" or "desc" timestamp order
         * @param $buffer Int: one of WikiExporter::BUFFER or WikiExporter::STREAM
+        * @param $text Int: one of WikiExporter::TEXT or WikiExporter::STUB
         */
        function __construct( &$db, $history = WikiExporter::CURRENT,
                        $buffer = WikiExporter::BUFFER, $text = WikiExporter::TEXT ) {
@@ -151,17 +158,16 @@ class WikiExporter {
        # Not called by default (depends on $this->list_authors)
        # Can be set by Special:Export when not exporting whole history
        protected function do_list_authors( $page , $revision , $cond ) {
-               $fname = "do_list_authors" ;
-               wfProfileIn( $fname );
+               wfProfileIn( __METHOD__ );
                $this->author_list = "<contributors>";
                //rev_deleted
                $nothidden = '('.$this->db->bitAnd('rev_deleted', Revision::DELETED_USER) . ') = 0';
 
                $sql = "SELECT DISTINCT rev_user_text,rev_user FROM {$page},{$revision} 
                WHERE page_id=rev_page AND $nothidden AND " . $cond ;
-               $result = $this->db->query( $sql, $fname );
+               $result = $this->db->query( $sql, __METHOD__ );
                $resultset = $this->db->resultObject( $result );
-               while( $row = $resultset->fetchObject() ) {
+               foreach ( $resultset as $row ) {
                        $this->author_list .= "<contributor>" .
                                "<username>" .
                                htmlentities( $row->rev_user_text )  .
@@ -171,7 +177,7 @@ class WikiExporter {
                                "</id>" .
                                "</contributor>";
                }
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                $this->author_list .= "</contributors>";
        }
 
@@ -207,27 +213,8 @@ class WikiExporter {
                        $opts = array( 'ORDER BY' => 'page_id ASC' );
                        $opts['USE INDEX'] = array();
                        $join = array();
-                       # Full history dumps...
-                       if( $this->history & WikiExporter::FULL ) {
-                               $join['revision'] = array('INNER JOIN','page_id=rev_page');
-                       # Latest revision dumps...
-                       } elseif( $this->history & WikiExporter::CURRENT ) {
-                               if( $this->list_authors && $cond != '' )  { // List authors, if so desired
-                                       list($page,$revision) = $this->db->tableNamesN('page','revision');
-                                       $this->do_list_authors( $page, $revision, $cond );
-                               }
-                               $join['revision'] = array('INNER JOIN','page_id=rev_page AND page_latest=rev_id');
-                       # "Stable" revision dumps...
-                       } elseif( $this->history & WikiExporter::STABLE ) {
-                               # Default JOIN, to be overridden...
-                               $join['revision'] = array('INNER JOIN','page_id=rev_page AND page_latest=rev_id');
-                               # One, and only one hook should set this, and return false
-                               if( wfRunHooks( 'WikiExporter::dumpStableQuery', array(&$tables,&$opts,&$join) ) ) {
-                                       wfProfileOut( __METHOD__ );
-                                       return new WikiError( __METHOD__." given invalid history dump type." );
-                               }
-                       # Time offset/limit for all pages/history...
-                       } elseif( is_array( $this->history ) ) {
+                       if( is_array( $this->history ) ) {
+                               # Time offset/limit for all pages/history...
                                $revJoin = 'page_id=rev_page';
                                # Set time order
                                if( $this->history['dir'] == 'asc' ) {
@@ -247,8 +234,27 @@ class WikiExporter {
                                if( !empty( $this->history['limit'] ) ) {
                                        $opts['LIMIT'] = intval( $this->history['limit'] );
                                }
-                       # Uknown history specification parameter?
+                       } elseif( $this->history & WikiExporter::FULL ) {
+                               # Full history dumps...
+                               $join['revision'] = array('INNER JOIN','page_id=rev_page');
+                       } elseif( $this->history & WikiExporter::CURRENT ) {
+                               # Latest revision dumps...
+                               if( $this->list_authors && $cond != '' )  { // List authors, if so desired
+                                       list($page,$revision) = $this->db->tableNamesN('page','revision');
+                                       $this->do_list_authors( $page, $revision, $cond );
+                               }
+                               $join['revision'] = array('INNER JOIN','page_id=rev_page AND page_latest=rev_id');
+                       } elseif( $this->history & WikiExporter::STABLE ) {
+                               # "Stable" revision dumps...
+                               # Default JOIN, to be overridden...
+                               $join['revision'] = array('INNER JOIN','page_id=rev_page AND page_latest=rev_id');
+                               # One, and only one hook should set this, and return false
+                               if( wfRunHooks( 'WikiExporter::dumpStableQuery', array(&$tables,&$opts,&$join) ) ) {
+                                       wfProfileOut( __METHOD__ );
+                                       return new WikiError( __METHOD__." given invalid history dump type." );
+                               }
                        } else {
+                               # Uknown history specification parameter?
                                wfProfileOut( __METHOD__ );
                                return new WikiError( __METHOD__." given invalid history dump type." );
                        }
@@ -266,6 +272,9 @@ class WikiExporter {
                        if( $this->buffer == WikiExporter::STREAM ) {
                                $prev = $this->db->bufferResults( false );
                        }
+                       
+                       wfRunHooks( 'ModifyExportQuery',
+                                               array( $this->db, &$tables, &$cond, &$opts, &$join ) );
 
                        # Do the query!
                        $result = $this->db->select( $tables, '*', $cond, __METHOD__, $opts, $join );
@@ -297,7 +306,7 @@ class WikiExporter {
         */
        protected function outputPageStream( $resultset ) {
                $last = null;
-               while( $row = $resultset->fetchObject() ) {
+               foreach ( $resultset as $row ) {
                        if( is_null( $last ) ||
                                $last->page_namespace != $row->page_namespace ||
                                $last->page_title     != $row->page_title ) {
@@ -328,7 +337,7 @@ class WikiExporter {
        }
        
        protected function outputLogStream( $resultset ) {
-               while( $row = $resultset->fetchObject() ) {
+               foreach ( $resultset as $row ) {
                        $output = $this->writer->writeLogItem( $row );
                        $this->sink->writeLogItem( $row, $output );
                }
@@ -345,7 +354,7 @@ class XmlDumpWriter {
         * @return string
         */
        function schemaVersion() {
-               return "0.3"; // FIXME: upgrade to 0.4 when updated XSD is ready, for the revision deletion bits
+               return "0.4";
        }
 
        /**
@@ -359,7 +368,7 @@ class XmlDumpWriter {
         * @return string
         */
        function openStream() {
-               global $wgContLanguageCode;
+               global $wgLanguageCode;
                $ver = $this->schemaVersion();
                return Xml::element( 'mediawiki', array(
                        'xmlns'              => "http://www.mediawiki.org/xml/export-$ver/",
@@ -367,7 +376,7 @@ class XmlDumpWriter {
                        'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
                                                "http://www.mediawiki.org/xml/export-$ver.xsd",
                        'version'            => $ver,
-                       'xml:lang'           => $wgContLanguageCode ),
+                       'xml:lang'           => $wgLanguageCode ),
                        null ) .
                        "\n" .
                        $this->siteInfo();
@@ -410,7 +419,11 @@ class XmlDumpWriter {
                global $wgContLang;
                $spaces = "<namespaces>\n";
                foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
-                       $spaces .= '      ' . Xml::element( 'namespace', array( 'key' => $ns ), $title ) . "\n";
+                       $spaces .= '      ' . 
+                               Xml::element( 'namespace', 
+                                       array(  'key' => $ns,
+                                                       'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
+                                       ), $title ) . "\n";
                }
                $spaces .= "    </namespaces>";
                return $spaces;
@@ -439,12 +452,15 @@ class XmlDumpWriter {
                $out .= '    ' . Xml::elementClean( 'title', array(), $title->getPrefixedText() ) . "\n";
                $out .= '    ' . Xml::element( 'id', array(), strval( $row->page_id ) ) . "\n";
                if( $row->page_is_redirect ) {
-                       $out .= '    ' . Xml::element( 'redirect', array() ). "\n";
+                       $out .= '    ' . Xml::element( 'redirect', array() ) . "\n";
                }
-               if( '' != $row->page_restrictions ) {
+               if( $row->page_restrictions != '' ) {
                        $out .= '    ' . Xml::element( 'restrictions', array(),
                                strval( $row->page_restrictions ) ) . "\n";
                }
+               
+               wfRunHooks( 'XmlDumpWriterOpenPage', array( $this, &$out, $row, $title ) );
+               
                return $out;
        }
 
@@ -466,8 +482,7 @@ class XmlDumpWriter {
         * @access private
         */
        function writeRevision( $row ) {
-               $fname = 'WikiExporter::dumpRev';
-               wfProfileIn( $fname );
+               wfProfileIn( __METHOD__ );
 
                $out  = "    <revision>\n";
                $out .= "      " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
@@ -489,6 +504,7 @@ class XmlDumpWriter {
                        $out .= "      " . Xml::elementClean( 'comment', null, strval( $row->rev_comment ) ) . "\n";
                }
 
+               $text = '';
                if( $row->rev_deleted & Revision::DELETED_TEXT ) {
                        $out .= "      " . Xml::element( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
                } elseif( isset( $row->old_text ) ) {
@@ -504,9 +520,11 @@ class XmlDumpWriter {
                                "" ) . "\n";
                }
 
+               wfRunHooks( 'XmlDumpWriterWriteRevision', array( &$this, &$out, $row, $text ) );
+
                $out .= "    </revision>\n";
 
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                return $out;
        }
        
@@ -519,8 +537,7 @@ class XmlDumpWriter {
         * @access private
         */
        function writeLogItem( $row ) {
-               $fname = 'WikiExporter::writeLogItem';
-               wfProfileIn( $fname );
+               wfProfileIn( __METHOD__ );
 
                $out  = "    <logitem>\n";
                $out .= "      " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
@@ -554,7 +571,7 @@ class XmlDumpWriter {
 
                $out .= "    </logitem>\n";
 
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                return $out;
        }
 
@@ -652,7 +669,7 @@ class DumpOutput {
 class DumpFileOutput extends DumpOutput {
        var $handle;
 
-       function DumpFileOutput( $file ) {
+       function __construct( $file ) {
                $this->handle = fopen( $file, "wt" );
        }
 
@@ -668,7 +685,7 @@ class DumpFileOutput extends DumpOutput {
  * @ingroup Dump
  */
 class DumpPipeOutput extends DumpFileOutput {
-       function DumpPipeOutput( $command, $file = null ) {
+       function __construct( $command, $file = null ) {
                if( !is_null( $file ) ) {
                        $command .=  " > " . wfEscapeShellArg( $file );
                }
@@ -681,8 +698,8 @@ class DumpPipeOutput extends DumpFileOutput {
  * @ingroup Dump
  */
 class DumpGZipOutput extends DumpPipeOutput {
-       function DumpGZipOutput( $file ) {
-               parent::DumpPipeOutput( "gzip", $file );
+       function __construct( $file ) {
+               parent::__construct( "gzip", $file );
        }
 }
 
@@ -691,8 +708,8 @@ class DumpGZipOutput extends DumpPipeOutput {
  * @ingroup Dump
  */
 class DumpBZip2Output extends DumpPipeOutput {
-       function DumpBZip2Output( $file ) {
-               parent::DumpPipeOutput( "bzip2", $file );
+       function __construct( $file ) {
+               parent::__construct( "bzip2", $file );
        }
 }
 
@@ -701,12 +718,12 @@ class DumpBZip2Output extends DumpPipeOutput {
  * @ingroup Dump
  */
 class Dump7ZipOutput extends DumpPipeOutput {
-       function Dump7ZipOutput( $file ) {
+       function __construct( $file ) {
                $command = "7za a -bd -si " . wfEscapeShellArg( $file );
                // Suppress annoying useless crap from p7zip
                // Unfortunately this could suppress real error messages too
                $command .= ' >' . wfGetNull() . ' 2>&1';
-               parent::DumpPipeOutput( $command );
+               parent::__construct( $command );
        }
 }
 
@@ -719,7 +736,7 @@ class Dump7ZipOutput extends DumpPipeOutput {
  * @ingroup Dump
  */
 class DumpFilter {
-       function DumpFilter( &$sink ) {
+       function __construct( &$sink ) {
                $this->sink =& $sink;
        }
 
@@ -782,8 +799,8 @@ class DumpNamespaceFilter extends DumpFilter {
        var $invert = false;
        var $namespaces = array();
 
-       function DumpNamespaceFilter( &$sink, $param ) {
-               parent::DumpFilter( $sink );
+       function __construct( &$sink, $param ) {
+               parent::__construct( $sink );
 
                $constants = array(
                        "NS_MAIN"           => NS_MAIN,
@@ -868,7 +885,7 @@ class DumpLatestFilter extends DumpFilter {
  * @ingroup Dump
  */
 class DumpMultiWriter {
-       function DumpMultiWriter( $sinks ) {
+       function __construct( $sinks ) {
                $this->sinks = $sinks;
                $this->count = count( $sinks );
        }
@@ -905,8 +922,7 @@ class DumpMultiWriter {
 }
 
 function xmlsafe( $string ) {
-       $fname = 'xmlsafe';
-       wfProfileIn( $fname );
+       wfProfileIn( __FUNCTION__ );
 
        /**
         * The page may contain old data which has not been properly normalized.
@@ -916,6 +932,6 @@ function xmlsafe( $string ) {
        $string = UtfNormal::cleanUp( $string );
 
        $string = htmlspecialchars( $string );
-       wfProfileOut( $fname );
+       wfProfileOut( __FUNCTION__ );
        return $string;
 }