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