* Updates
[mediawiki.git] / maintenance / importLogs.inc
bloba5b94cb93d3019c1566dab2e7485520e2fba15a7
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 /**
21  * Attempt to import existing log pages into the log tables.
22  *
23  * Not yet complete.
24  *
25  * @todo document
26  * @package MediaWiki
27  * @subpackage Maintenance
28  */
30 /** */
31 require_once( 'GlobalFunctions.php' );
32 require_once( 'Database.php' );
33 require_once( 'Article.php' );
34 require_once( 'LogPage.php' );
36 /**
37  * Log importer
38  * @todo document
39  * @package MediaWiki
40  * @subpackage Maintenance
41  */
42 class LogImporter {
43         var $dummy = false;
45         function LogImporter( $type ) {
46                 $this->type = $type;
47                 $this->db =& wfGetDB( DB_MASTER );
48                 $this->actions = $this->setupActions();
49         }
51         function setupActions() {
52                 $actions = array();
53                 foreach( LogPage::validActions( $this->type ) as $action ) {
54                         $key = "{$this->type}/$action";
55                         $actions[$key] = $this->makeLineRegexp( $this->type, $action );
56                 }
57                 return $actions;
58         }
60         function makeLineRegexp( $type, $action ) {
61                 $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
62                 $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
64                 $text = LogPage::actionText( $type, $action );
65                 $text = preg_quote( $text, '/' );
66                 $text = str_replace( '\$1', $linkRegexp, $text );
67                 $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text;
68                 $text .= '(?: <em>\((.*)\)<\/em>)?';
69                 $text = "/$text/";
70                 return $text;
71         }
73         function importText( $text ) {
74                 if( $this->dummy ) {
75                         print $text;
76                         var_dump( $this->actions );
77                 }
78                 $lines = explode( '<li>', $text );
79                 foreach( $lines as $line ) {
80                         $matches = array();
81                         if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
82                                 $this->importLine( $matches[1] );
83                         }
84                 }
85         }
87         function fixDate( $date ) {
88                 # Yuck! Parsing multilingual date formats??!!!!???!!??!
89                 # 01:55, 23 Aug 2004 - won't take in strtotimr
90                 # "Aug 23 2004 01:55" - seems ok
91                 # TODO: multilingual attempt to extract from the data in Language
92                 $matches = array();
93                 if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
94                         $date = $matches[2] . ' ' . $matches[1];
95                 }
96                 $n = strtotime( $date ) + date("Z");
97                 # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n";
98                 $timestamp = wfTimestamp( TS_MW, $n );
99                 return $timestamp;
100         }
102         function importLine( $line ) {
103                 foreach( $this->actions as $action => $regexp ) {
104                         $matches = array();
105                         if( preg_match( $regexp, $line, $matches ) ) {
106                                 if( $this->dummy ) {
107                                         #var_dump( $matches );
108                                 }
109                                 $date = $this->fixDate( $matches[1] );
110                                 $user = Title::newFromText( $matches[2] );
111                                 $target = Title::newFromText( $matches[3] );
112                                 if( isset( $matches[4] ) ) {
113                                         $comment = $matches[4];
114                                 } else {
115                                         $comment = '';
116                                 }
118                                 $insert = array(
119                                         'log_type' => $this->type,
120                                         'log_action' => preg_replace( '!^.*/!', '', $action ),
121                                         'log_timestamp' => $date,
122                                         'log_user' => intval( User::idFromName( $user->getText() ) ),
123                                         'log_namespace' => $target->getNamespace(),
124                                         'log_title' => $target->getDBkey(),
125                                         'log_comment' => wfUnescapeWikiText( $comment ),
126                                 );
127                                 if( $this->dummy ) {
128                                         var_dump( $insert );
129                                 } else {
130                                         # FIXME: avoid duplicates!
131                                         $this->db->insert( 'logging', $insert );
132                                 }
133                                 break;
134                         }
135                 }
136         }
139 function wfUnescapeWikiText( $text ) {
140         $text = str_replace(
141                 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
142                 array( '[',             '|',      "'",     'ISBN '        , '://'         , "\n=", '{{' ),
143                 $text );
144         return $text;