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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Attempt to import existing log pages into the log tables.
27 * @subpackage Maintenance
31 require_once( 'GlobalFunctions.php' );
32 require_once( 'Database.php' );
33 require_once( 'Article.php' );
34 require_once( 'LogPage.php' );
40 * @subpackage Maintenance
45 function LogImporter( $type ) {
47 $this->db =& wfGetDB( DB_MASTER );
48 $this->actions = $this->setupActions();
51 function setupActions() {
53 foreach( LogPage::validActions( $this->type ) as $action ) {
54 $key = "{$this->type}/$action";
55 $actions[$key] = $this->makeLineRegexp( $this->type, $action );
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>)?';
73 function importText( $text ) {
76 var_dump( $this->actions );
78 $lines = explode( '<li>', $text );
79 foreach( $lines as $line ) {
80 if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
81 $this->importLine( $matches[1] );
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 if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
92 $date = $matches[2] . ' ' . $matches[1];
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 );
100 function importLine( $line ) {
101 foreach( $this->actions as $action => $regexp ) {
102 if( preg_match( $regexp, $line, $matches ) ) {
104 #var_dump( $matches );
106 $date = $this->fixDate( $matches[1] );
107 $user = Title::newFromText( $matches[2] );
108 $target = Title::newFromText( $matches[3] );
109 if( isset( $matches[4] ) ) {
110 $comment = $matches[4];
116 'log_type' => $this->type,
117 'log_action' => preg_replace( '!^.*/!', '', $action ),
118 'log_timestamp' => $date,
119 'log_user' => intval( User::idFromName( $user->getText() ) ),
120 'log_namespace' => $target->getNamespace(),
121 'log_title' => $target->getDBkey(),
122 'log_comment' => wfUnescapeWikiText( $comment ),
127 # FIXME: avoid duplicates!
128 $this->db->insert( 'logging', $insert );
136 function wfUnescapeWikiText( $text ) {
138 array( '[', '|', ''', 'ISBN ', '://' , "\n=", '{{' ),
139 array( '[', '|', "'", 'ISBN ' , '://' , "\n=", '{{' ),