fixed discussion namespace
[lhc/web/wiklou.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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @package MediaWiki
27 * @subpackage Maintenance
28 */
29
30 /** */
31 require_once( 'GlobalFunctions.php' );
32 require_once( 'Database.php' );
33 require_once( 'Article.php' );
34 require_once( 'LogPage.php' );
35
36 /**
37 * Log importer
38 * @todo document
39 * @package MediaWiki
40 * @subpackage Maintenance
41 */
42 class LogImporter {
43 var $dummy = false;
44
45 function LogImporter( $type ) {
46 $this->type = $type;
47 $this->db =& wfGetDB( DB_MASTER );
48 $this->actions = $this->setupActions();
49 }
50
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 }
59
60 function makeLineRegexp( $type, $action ) {
61 $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
62 $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
63
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 }
72
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 if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
81 $this->importLine( $matches[1] );
82 }
83 }
84 }
85
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];
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 if( preg_match( $regexp, $line, $matches ) ) {
103 if( $this->dummy ) {
104 #var_dump( $matches );
105 }
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];
111 } else {
112 $comment = '';
113 }
114
115 $insert = array(
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 ),
123 );
124 if( $this->dummy ) {
125 var_dump( $insert );
126 } else {
127 # FIXME: avoid duplicates!
128 $this->db->insert( 'logging', $insert );
129 }
130 break;
131 }
132 }
133 }
134 }
135
136 function wfUnescapeWikiText( $text ) {
137 $text = str_replace(
138 array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
139 array( '[', '|', "'", 'ISBN ' , '://' , "\n=", '{{' ),
140 $text );
141 return $text;
142 }
143
144 ?>