00001 <?php 00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com> 00003 # http://www.mediawiki.org/ 00004 # 00005 # This program is free software; you can redistribute it and/or modify 00006 # it under the terms of the GNU General Public License as published by 00007 # the Free Software Foundation; either version 2 of the License, or 00008 # (at your option) any later version. 00009 # 00010 # This program is distributed in the hope that it will be useful, 00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 # GNU General Public License for more details. 00014 # 00015 # You should have received a copy of the GNU General Public License along 00016 # with this program; if not, write to the Free Software Foundation, Inc., 00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 # http://www.gnu.org/copyleft/gpl.html 00019 00031 require_once( 'GlobalFunctions.php' ); 00032 require_once( 'Database.php' ); 00033 require_once( 'Article.php' ); 00034 require_once( 'LogPage.php' ); 00035 00041 class LogImporter { 00042 var $dummy = false; 00043 00044 function LogImporter( $type ) { 00045 $this->type = $type; 00046 $this->db = wfGetDB( DB_MASTER ); 00047 $this->actions = $this->setupActions(); 00048 } 00049 00050 function setupActions() { 00051 $actions = array(); 00052 foreach( LogPage::validActions( $this->type ) as $action ) { 00053 $key = "{$this->type}/$action"; 00054 $actions[$key] = $this->makeLineRegexp( $this->type, $action ); 00055 } 00056 return $actions; 00057 } 00058 00059 function makeLineRegexp( $type, $action ) { 00060 $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?'; 00061 $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]'; 00062 00063 $text = LogPage::actionText( $type, $action ); 00064 $text = preg_quote( $text, '/' ); 00065 $text = str_replace( '\$1', $linkRegexp, $text ); 00066 $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text; 00067 $text .= '(?: <em>\((.*)\)<\/em>)?'; 00068 $text = "/$text/"; 00069 return $text; 00070 } 00071 00072 function importText( $text ) { 00073 if( $this->dummy ) { 00074 print $text; 00075 var_dump( $this->actions ); 00076 } 00077 $lines = explode( '<li>', $text ); 00078 foreach( $lines as $line ) { 00079 $matches = array(); 00080 if( preg_match( '!^(.*)</li>!', $line, $matches ) ) { 00081 $this->importLine( $matches[1] ); 00082 } 00083 } 00084 } 00085 00086 function fixDate( $date ) { 00087 # Yuck! Parsing multilingual date formats??!!!!???!!??! 00088 # 01:55, 23 Aug 2004 - won't take in strtotimr 00089 # "Aug 23 2004 01:55" - seems ok 00090 # TODO: multilingual attempt to extract from the data in Language 00091 $matches = array(); 00092 if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) { 00093 $date = $matches[2] . ' ' . $matches[1]; 00094 } 00095 $n = strtotime( $date ) + date("Z"); 00096 # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n"; 00097 $timestamp = wfTimestamp( TS_MW, $n ); 00098 return $timestamp; 00099 } 00100 00101 function importLine( $line ) { 00102 foreach( $this->actions as $action => $regexp ) { 00103 $matches = array(); 00104 if( preg_match( $regexp, $line, $matches ) ) { 00105 if( $this->dummy ) { 00106 #var_dump( $matches ); 00107 } 00108 $date = $this->fixDate( $matches[1] ); 00109 $user = Title::newFromText( $matches[2] ); 00110 $target = Title::newFromText( $matches[3] ); 00111 if( isset( $matches[4] ) ) { 00112 $comment = $matches[4]; 00113 } else { 00114 $comment = ''; 00115 } 00116 00117 $insert = array( 00118 'log_type' => $this->type, 00119 'log_action' => preg_replace( '!^.*/!', '', $action ), 00120 'log_timestamp' => $date, 00121 'log_user' => intval( User::idFromName( $user->getText() ) ), 00122 'log_namespace' => $target->getNamespace(), 00123 'log_title' => $target->getDBkey(), 00124 'log_comment' => wfUnescapeWikiText( $comment ), 00125 ); 00126 if( $this->dummy ) { 00127 var_dump( $insert ); 00128 } else { 00129 # FIXME: avoid duplicates! 00130 $this->db->insert( 'logging', $insert ); 00131 } 00132 break; 00133 } 00134 } 00135 } 00136 } 00137 00138 function wfUnescapeWikiText( $text ) { 00139 $text = str_replace( 00140 array( '[', '|', ''', 'ISBN ', '://' , "\n=", '{{' ), 00141 array( '[', '|', "'", 'ISBN ' , '://' , "\n=", '{{' ), 00142 $text ); 00143 return $text; 00144 }