* Add XML log dump support
authorAaron Schulz <aaron@users.mediawiki.org>
Thu, 18 Sep 2008 00:02:57 +0000 (00:02 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Thu, 18 Sep 2008 00:02:57 +0000 (00:02 +0000)
* TODO: make importer

includes/Export.php
maintenance/backup.inc
maintenance/dumpBackup.php

index 7d0a824..61229e0 100644 (file)
@@ -32,6 +32,7 @@ class WikiExporter {
 
        const FULL = 0;
        const CURRENT = 1;
+       const LOGS = 2;
 
        const BUFFER = 0;
        const STREAM = 1;
@@ -108,6 +109,18 @@ class WikiExporter {
                }
                return $this->dumpFrom( $condition );
        }
+       
+       function allLogs() {
+               return $this->dumpFrom( '' );
+       }
+       
+       function logsByRange( $start, $end ) {
+               $condition = 'log_id >= ' . intval( $start );
+               if( $end ) {
+                       $condition .= ' AND log_id < ' . intval( $end );
+               }
+               return $this->dumpFrom( $condition );
+       }
 
        /**
         * @param $title Title
@@ -166,7 +179,25 @@ class WikiExporter {
        function dumpFrom( $cond = '' ) {
                $fname = 'WikiExporter::dumpFrom';
                wfProfileIn( $fname );
-
+               
+               # For logs dumps...
+               if( $this->history & self::LOGS ) {
+                       $where = array( 'user_id = log_user' );
+                       # Hide private logs
+                       $where[] = LogEventsList::getExcludeClause( $this->db );
+                       if( $cond ) $where[] = $cond;
+                       $result = $this->db->select( array('logging','user'), 
+                               '*',
+                               $where,
+                               $fname,
+                               array( 'ORDER BY' => 'log_id')
+                       );
+                       $wrapper = $this->db->resultObject( $result );
+                       $this->outputLogStream( $wrapper );
+                       wfProfileOut( $fname );
+                       return;
+               }
+               # For page dumps...
                $page     = $this->db->tableName( 'page' );
                $revision = $this->db->tableName( 'revision' );
                $text     = $this->db->tableName( 'text' );
@@ -234,10 +265,10 @@ class WikiExporter {
                }
                $result = $this->db->query( $sql, $fname );
                $wrapper = $this->db->resultObject( $result );
-               $this->outputStream( $wrapper );
+               $this->outputPageStream( $wrapper );
 
                if ( $this->list_authors ) {
-                       $this->outputStream( $wrapper );
+                       $this->outputPageStream( $wrapper );
                }
 
                if( $this->buffer == WikiExporter::STREAM ) {
@@ -260,7 +291,7 @@ class WikiExporter {
         * @param $resultset ResultWrapper
         * @access private
         */
-       function outputStream( $resultset ) {
+       function outputPageStream( $resultset ) {
                $last = null;
                while( $row = $resultset->fetchObject() ) {
                        if( is_null( $last ) ||
@@ -292,6 +323,14 @@ class WikiExporter {
                }
                $resultset->free();
        }
+       
+       function outputLogStream( $resultset ) {
+               while( $row = $resultset->fetchObject() ) {
+                       $output = $this->writer->writeLogItem( $row );
+                       $this->sink->writeLogItem( $row, $output );
+               }
+               $resultset->free();
+       }
 }
 
 /**
@@ -465,6 +504,54 @@ class XmlDumpWriter {
                wfProfileOut( $fname );
                return $out;
        }
+       
+       /**
+        * Dumps a <logitem> section on the output stream, with
+        * data filled in from the given database row.
+        *
+        * @param $row object
+        * @return string
+        * @access private
+        */
+       function writeLogItem( $row ) {
+               $fname = 'WikiExporter::writeLogItem';
+               wfProfileIn( $fname );
+
+               $out  = "    <logitem>\n";
+               $out .= "      " . wfElement( 'id', null, strval( $row->log_id ) ) . "\n";
+
+               $out .= $this->writeTimestamp( $row->log_timestamp );
+
+               if( $row->log_deleted & LogPage::DELETED_USER ) {
+                       $out .= "      " . wfElement( 'contributor', array( 'deleted' => 'deleted' ) ) . "\n";
+               } else {
+                       $out .= $this->writeContributor( $row->log_user, $row->user_name );
+               }
+
+               if( $row->log_deleted & LogPage::DELETED_COMMENT ) {
+                       $out .= "      " . wfElement( 'comment', array( 'deleted' => 'deleted' ) ) . "\n";
+               } elseif( $row->log_comment != '' ) {
+                       $out .= "      " . wfElementClean( 'comment', null, strval( $row->log_comment ) ) . "\n";
+               }
+               
+               $out .= "      " . wfElement( 'type', null, strval( $row->log_type ) ) . "\n";
+               $out .= "      " . wfElement( 'action', null, strval( $row->log_action ) ) . "\n";
+
+               if( $row->log_deleted & LogPage::DELETED_ACTION ) {
+                       $out .= "      " . wfElement( 'text', array( 'deleted' => 'deleted' ) ) . "\n";
+               } else {
+                       $title = Title::makeTitle( $row->log_namespace, $row->log_title );
+                       $out .= "      " . wfElementClean( 'title', null, $title->getPrefixedText() ) . "\n";
+                       $out .= "      " . wfElementClean( 'params',
+                               array( 'xml:space' => 'preserve' ),
+                               strval( $row->log_params ) ) . "\n";
+               }
+
+               $out .= "    </logitem>\n";
+
+               wfProfileOut( $fname );
+               return $out;
+       }
 
        function writeTimestamp( $timestamp ) {
                $ts = wfTimestamp( TS_ISO_8601, $timestamp );
@@ -539,6 +626,10 @@ class DumpOutput {
        function writeRevision( $rev, $string ) {
                $this->write( $string );
        }
+       
+       function writeLogItem( $rev, $string ) {
+               $this->write( $string );
+       }
 
        /**
         * Override to write to a different stream type.
@@ -654,6 +745,10 @@ class DumpFilter {
                        $this->sink->writeRevision( $rev, $string );
                }
        }
+       
+       function writeLogItem( $rev, $string ) {
+               $this->sink->writeRevision( $rev, $string );
+       }       
 
        /**
         * Override for page-based filter types.
index bf52c1f..e2e5363 100644 (file)
@@ -175,7 +175,7 @@ class BackupDumper {
                // extension point for subclasses to add options
        }
 
-       function dump( $history, $text = MW_EXPORT_TEXT ) {
+       function dump( $history, $text = WikiExporter::TEXT ) {
                # Notice messages will foul up your XML output even if they're
                # relatively harmless.
                if( ini_get( 'display_errors' ) )
@@ -192,13 +192,21 @@ class BackupDumper {
 
                if( !$this->skipHeader )
                        $exporter->openStream();
-
-               if( is_null( $this->pages ) ) {
+               # Log item dumps: all or by range
+               if( $history & WikiExporter::LOGS ) {
+                       if( $this->startId || $this->endId ) {
+                               $exporter->logsByRange( $this->startId, $this->endId );
+                       } else {
+                               $exporter->allLogs();
+                       }
+               # Page dumps: all or by page ID range
+               } else if( is_null( $this->pages ) ) {
                        if( $this->startId || $this->endId ) {
                                $exporter->pagesByRange( $this->startId, $this->endId );
                        } else {
                                $exporter->allPages();
                        }
+               # Dump of specific pages
                } else {
                        $exporter->pagesByName( $this->pages );
                }
index bb43124..de7ce65 100644 (file)
@@ -63,6 +63,8 @@ if( isset( $options['full'] ) ) {
        $dumper->dump( WikiExporter::FULL, $textMode );
 } elseif( isset( $options['current'] ) ) {
        $dumper->dump( WikiExporter::CURRENT, $textMode );
+} elseif( isset( $options['logs'] ) ) {
+       $dumper->dump( WikiExporter::LOGS );
 } else {
        $dumper->progress( <<<ENDS
 This script dumps the wiki page database into an XML interchange wrapper
@@ -74,6 +76,7 @@ Usage: php dumpBackup.php <action> [<options>]
 Actions:
   --full      Dump complete history of every page.
   --current   Includes only the latest revision of each page.
+  --logs      Dump action logs for every page.
 
 Options:
   --quiet     Don't dump status reports to stderr.