ISO 8601 timestamps can have decimal seconds
[mediawiki.git] / maintenance / importLogs.inc
bloba008e6c77a9a2dd16172165eee1ca385a2847372
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  * @file
26  * @todo document
27  * @ingroup 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  * @ingroup Maintenance
40  */
41 class LogImporter {
42         var $dummy = false;
44         function LogImporter( $type ) {
45                 $this->type = $type;
46                 $this->db = wfGetDB( DB_MASTER );
47                 $this->actions = $this->setupActions();
48         }
50         function setupActions() {
51                 $actions = array();
52                 foreach( LogPage::validActions( $this->type ) as $action ) {
53                         $key = "{$this->type}/$action";
54                         $actions[$key] = $this->makeLineRegexp( $this->type, $action );
55                 }
56                 return $actions;
57         }
59         function makeLineRegexp( $type, $action ) {
60                 $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
61                 $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
63                 $text = LogPage::actionText( $type, $action );
64                 $text = preg_quote( $text, '/' );
65                 $text = str_replace( '\$1', $linkRegexp, $text );
66                 $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text;
67                 $text .= '(?: <em>\((.*)\)<\/em>)?';
68                 $text = "/$text/";
69                 return $text;
70         }
72         function importText( $text ) {
73                 if( $this->dummy ) {
74                         print $text;
75                         var_dump( $this->actions );
76                 }
77                 $lines = explode( '<li>', $text );
78                 foreach( $lines as $line ) {
79                         $matches = array();
80                         if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
81                                 $this->importLine( $matches[1] );
82                         }
83                 }
84         }
86         function fixDate( $date ) {
87                 # Yuck! Parsing multilingual date formats??!!!!???!!??!
88                 # 01:55, 23 Aug 2004 - won't take in strtotimr
89                 # "Aug 23 2004 01:55" - seems ok
90                 # TODO: multilingual attempt to extract from the data in Language
91                 $matches = array();
92                 if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
93                         $date = $matches[2] . ' ' . $matches[1];
94                 }
95                 $n = strtotime( $date ) + date("Z");
96                 # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n";
97                 $timestamp = wfTimestamp( TS_MW, $n );
98                 return $timestamp;
99         }
101         function importLine( $line ) {
102                 foreach( $this->actions as $action => $regexp ) {
103                         $matches = array();
104                         if( preg_match( $regexp, $line, $matches ) ) {
105                                 if( $this->dummy ) {
106                                         #var_dump( $matches );
107                                 }
108                                 $date = $this->fixDate( $matches[1] );
109                                 $user = Title::newFromText( $matches[2] );
110                                 $target = Title::newFromText( $matches[3] );
111                                 if( isset( $matches[4] ) ) {
112                                         $comment = $matches[4];
113                                 } else {
114                                         $comment = '';
115                                 }
117                                 $insert = array(
118                                         'log_type' => $this->type,
119                                         'log_action' => preg_replace( '!^.*/!', '', $action ),
120                                         'log_timestamp' => $date,
121                                         'log_user' => intval( User::idFromName( $user->getText() ) ),
122                                         'log_namespace' => $target->getNamespace(),
123                                         'log_title' => $target->getDBkey(),
124                                         'log_comment' => wfUnescapeWikiText( $comment ),
125                                 );
126                                 if( $this->dummy ) {
127                                         var_dump( $insert );
128                                 } else {
129                                         # FIXME: avoid duplicates!
130                                         $this->db->insert( 'logging', $insert );
131                                 }
132                                 break;
133                         }
134                 }
135         }
138 function wfUnescapeWikiText( $text ) {
139         $text = str_replace(
140                 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
141                 array( '[',             '|',      "'",     'ISBN '        , '://'         , "\n=", '{{' ),
142                 $text );
143         return $text;