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
21 * Attempt to import existing log pages into the log tables.
26 * @addtogroup Maintenance
30 require_once( 'GlobalFunctions.php' );
31 require_once( 'Database.php' );
32 require_once( 'Article.php' );
33 require_once( 'LogPage.php' );
38 * @addtogroup Maintenance
43 function LogImporter( $type ) {
45 $this->db = wfGetDB( DB_MASTER );
46 $this->actions = $this->setupActions();
49 function setupActions() {
51 foreach( LogPage::validActions( $this->type ) as $action ) {
52 $key = "{$this->type}/$action";
53 $actions[$key] = $this->makeLineRegexp( $this->type, $action );
58 function makeLineRegexp( $type, $action ) {
59 $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
60 $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
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>)?';
71 function importText( $text ) {
74 var_dump( $this->actions );
76 $lines = explode( '<li>', $text );
77 foreach( $lines as $line ) {
79 if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
80 $this->importLine( $matches[1] );
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
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 ) {
103 if( preg_match( $regexp, $line, $matches ) ) {
105 #var_dump( $matches );
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];
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 ),
128 # FIXME: avoid duplicates!
129 $this->db->insert( 'logging', $insert );
137 function wfUnescapeWikiText( $text ) {
139 array( '[', '|', ''', 'ISBN ', '://' , "\n=", '{{' ),
140 array( '[', '|', "'", 'ISBN ' , '://' , "\n=", '{{' ),