typo
[mediawiki.git] / maintenance / importLogs.inc
blob948c686aabe0db38d71e25de47905ad7f427cd08
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or 
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 # Attempt to import existing log pages into the log tables.
22 # Not yet complete.
24 require_once( 'GlobalFunctions.php' );
25 require_once( 'Database.php' );
26 require_once( 'Article.php' );
27 require_once( 'LogPage.php' );
29 # Log importer
30 class LogImporter {
31         var $dummy = false;
32         
33         function LogImporter( $type ) {
34                 $this->type = $type;
35                 $this->db =& wfGetDB( DB_MASTER );
36                 $this->actions = $this->setupActions();
37         }
38         
39         function setupActions() {
40                 $actions = array();
41                 foreach( LogPage::validActions( $this->type ) as $action ) {
42                         $key = "{$this->type}/$action";
43                         $actions[$key] = $this->makeLineRegexp( $this->type, $action );
44                 }
45                 return $actions;
46         }
47         
48         function makeLineRegexp( $type, $action ) {
49                 $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
50                 $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
51                 
52                 $text = LogPage::actionText( $type, $action );
53                 $text = preg_quote( $text, '/' );
54                 $text = str_replace( '\$1', $linkRegexp, $text );
55                 $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text;
56                 $text .= '(?: <em>\((.*)\)<\/em>)?';
57                 $text = "/$text/";
58                 return $text;
59         }
60         
61         function importText( $text ) {
62                 if( $this->dummy ) {
63                         print $text;
64                         var_dump( $this->actions );
65                 }
66                 $lines = explode( '<li>', $text );
67                 foreach( $lines as $line ) {
68                         if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
69                                 $this->importLine( $matches[1] );
70                         }
71                 }
72         }
73         
74         function fixDate( $date ) {
75                 # Yuck! Parsing multilingual date formats??!!!!???!!??!
76                 # 01:55, 23 Aug 2004 - won't take in strtotimr
77                 # "Aug 23 2004 01:55" - seems ok
78                 # TODO: multilingual attempt to extract from the data in Language
79                 if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
80                         $date = $matches[2] . ' ' . $matches[1];
81                 }
82                 $n = strtotime( $date ) + date("Z");
83                 # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n";
84                 $timestamp = wfTimestamp( TS_MW, $n );
85                 return $timestamp;
86         }
87         
88         function importLine( $line ) {
89                 foreach( $this->actions as $action => $regexp ) {
90                         if( preg_match( $regexp, $line, $matches ) ) {
91                                 if( $this->dummy ) {
92                                         #var_dump( $matches );
93                                 }
94                                 $date = $this->fixDate( $matches[1] );
95                                 $user = Title::newFromText( $matches[2] );
96                                 $target = Title::newFromText( $matches[3] );
97                                 if( isset( $matches[4] ) ) {
98                                         $comment = $matches[4];
99                                 } else {
100                                         $comment = '';
101                                 }
102                                 
103                                 $insert = array(
104                                         'log_type' => $this->type,
105                                         'log_action' => preg_replace( '!^.*/!', '', $action ),
106                                         'log_timestamp' => $date,
107                                         'log_user' => IntVal( User::idFromName( $user->getText() ) ),
108                                         'log_namespace' => $target->getNamespace(),
109                                         'log_title' => $target->getDBkey(),
110                                         'log_comment' => wfUnescapeWikiText( $comment ),
111                                 );
112                                 if( $this->dummy ) {
113                                         var_dump( $insert );
114                                 } else {
115                                         # FIXME: avoid duplicates!
116                                         $this->db->insertArray( 'logging', $insert );
117                                 }
118                                 break;
119                         }
120                 }
121         }
124 function wfUnescapeWikiText( $text ) {
125         $text = str_replace( 
126                 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
127                 array( '[',             '|',      "'",     'ISBN '        , '://'         , "\n=", '{{' ),
128                 $text );
129         return $text;