Old log page -> logging table importer.
authorBrion Vibber <brion@users.mediawiki.org>
Wed, 25 Aug 2004 17:24:31 +0000 (17:24 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Wed, 25 Aug 2004 17:24:31 +0000 (17:24 +0000)
Not yet fully functional.

includes/LogPage.php
maintenance/importLogs.inc [new file with mode: 0644]
maintenance/importLogs.php [new file with mode: 0644]

index d09d435..590daa0 100644 (file)
@@ -66,11 +66,23 @@ class LogPage {
                return true;
        }
 
-       /* static */ function &validTypes() {
+       /* static */ function validTypes() {
                static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload' );
                return $types;
        }
        
+       /* static */ function validActions( $type ) {
+               static $actions = array(
+                       '' => NULL,
+                       'block' => array( 'block', 'unblock' ),
+                       'protect' => array( 'protect', 'unprotect' ),
+                       'rights' => array( 'rights' ),
+                       'delete' => array( 'delete', 'restore' ),
+                       'upload' => array( 'upload' )
+               );
+               return $actions[$type];
+       }
+       
        /* static */ function isLogType( $type ) {
                return in_array( $type, LogPage::validTypes() );
        }
@@ -99,7 +111,7 @@ class LogPage {
                return wfMsg( $headerText[$type] );
        }
        
-       /* static */ function actionText( $type, $action, $titleLink ) {
+       /* static */ function actionText( $type, $action, $titleLink = NULL ) {
                static $actions = array(
                        'block/block' => 'blocklogentry',
                        'block/unblock' => 'blocklogentry',
@@ -113,7 +125,11 @@ class LogPage {
                );
                $key = "$type/$action";
                if( isset( $actions[$key] ) ) {
-                       return wfMsg( $actions[$key], $titleLink );
+                       if( is_null( $titleLink ) ) {
+                               return wfMsg( $actions[$key] );
+                       } else {
+                               return wfMsg( $actions[$key], $titleLink );
+                       }
                } else {
                        wfDebug( "LogPage::actionText - unknown action $key\n" );
                        return "$action $titleLink";
diff --git a/maintenance/importLogs.inc b/maintenance/importLogs.inc
new file mode 100644 (file)
index 0000000..948c686
--- /dev/null
@@ -0,0 +1,132 @@
+<?php
+# Copyright (C) 2004 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.,
+# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# http://www.gnu.org/copyleft/gpl.html
+
+# Attempt to import existing log pages into the log tables.
+
+# Not yet complete.
+
+require_once( 'GlobalFunctions.php' );
+require_once( 'Database.php' );
+require_once( 'Article.php' );
+require_once( 'LogPage.php' );
+
+# Log importer
+class LogImporter {
+       var $dummy = false;
+       
+       function LogImporter( $type ) {
+               $this->type = $type;
+               $this->db =& wfGetDB( DB_MASTER );
+               $this->actions = $this->setupActions();
+       }
+       
+       function setupActions() {
+               $actions = array();
+               foreach( LogPage::validActions( $this->type ) as $action ) {
+                       $key = "{$this->type}/$action";
+                       $actions[$key] = $this->makeLineRegexp( $this->type, $action );
+               }
+               return $actions;
+       }
+       
+       function makeLineRegexp( $type, $action ) {
+               $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
+               $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
+               
+               $text = LogPage::actionText( $type, $action );
+               $text = preg_quote( $text, '/' );
+               $text = str_replace( '\$1', $linkRegexp, $text );
+               $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text;
+               $text .= '(?: <em>\((.*)\)<\/em>)?';
+               $text = "/$text/";
+               return $text;
+       }
+       
+       function importText( $text ) {
+               if( $this->dummy ) {
+                       print $text;
+                       var_dump( $this->actions );
+               }
+               $lines = explode( '<li>', $text );
+               foreach( $lines as $line ) {
+                       if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
+                               $this->importLine( $matches[1] );
+                       }
+               }
+       }
+       
+       function fixDate( $date ) {
+               # Yuck! Parsing multilingual date formats??!!!!???!!??!
+               # 01:55, 23 Aug 2004 - won't take in strtotimr
+               # "Aug 23 2004 01:55" - seems ok
+               # TODO: multilingual attempt to extract from the data in Language
+               if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
+                       $date = $matches[2] . ' ' . $matches[1];
+               }
+               $n = strtotime( $date ) + date("Z");
+               # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n";
+               $timestamp = wfTimestamp( TS_MW, $n );
+               return $timestamp;
+       }
+       
+       function importLine( $line ) {
+               foreach( $this->actions as $action => $regexp ) {
+                       if( preg_match( $regexp, $line, $matches ) ) {
+                               if( $this->dummy ) {
+                                       #var_dump( $matches );
+                               }
+                               $date = $this->fixDate( $matches[1] );
+                               $user = Title::newFromText( $matches[2] );
+                               $target = Title::newFromText( $matches[3] );
+                               if( isset( $matches[4] ) ) {
+                                       $comment = $matches[4];
+                               } else {
+                                       $comment = '';
+                               }
+                               
+                               $insert = array(
+                                       'log_type' => $this->type,
+                                       'log_action' => preg_replace( '!^.*/!', '', $action ),
+                                       'log_timestamp' => $date,
+                                       'log_user' => IntVal( User::idFromName( $user->getText() ) ),
+                                       'log_namespace' => $target->getNamespace(),
+                                       'log_title' => $target->getDBkey(),
+                                       'log_comment' => wfUnescapeWikiText( $comment ),
+                               );
+                               if( $this->dummy ) {
+                                       var_dump( $insert );
+                               } else {
+                                       # FIXME: avoid duplicates!
+                                       $this->db->insertArray( 'logging', $insert );
+                               }
+                               break;
+                       }
+               }
+       }
+}
+
+function wfUnescapeWikiText( $text ) {
+       $text = str_replace( 
+               array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
+               array( '[',             '|',      "'",     'ISBN '        , '://'         , "\n=", '{{' ),
+               $text );
+       return $text;
+}
+
+?>
\ No newline at end of file
diff --git a/maintenance/importLogs.php b/maintenance/importLogs.php
new file mode 100644 (file)
index 0000000..78596c2
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+require_once( "commandLine.inc" );
+require_once( "importLogs.inc" );
+
+#print $text;
+#exit();
+
+foreach( LogPage::validTypes() as $type ) {
+       if( $type == '' ) continue;
+       
+       $page = LogPage::logName( $type );
+       $log = new Article( Title::makeTitleSafe( NS_PROJECT, $page ) );
+       $text = $log->getContentWithoutUsingSoManyDamnGlobals();
+
+       $importer = new LogImporter( $type );
+       $importer->dummy = true;
+       $importer->importText( $text );
+}
+
+?>
\ No newline at end of file