Somewhat experimental tool for pulling edit data from recentchanges & attached tables.
[lhc/web/wiklou.git] / maintenance / dumpReplayLog.php
1 <?php
2
3 error_reporting(E_ALL);
4
5 require_once( "commandLine.inc" );
6 require_once( 'includes/SpecialExport.php' );
7
8 function dumpReplayLog( $start ) {
9 $dbw =& wfGetDB( DB_MASTER );
10 $recentchanges = $dbw->tableName( 'recentchanges' );
11 $result =& $dbw->safeQuery( "SELECT * FROM $recentchanges WHERE rc_timestamp >= "
12 . $dbw->timestamp( $start ) . ' ORDER BY rc_timestamp');
13
14 global $wgInputEncoding;
15 echo '<' . '?xml version="1.0" encoding="' . $wgInputEncoding . '" ?' . ">\n";
16 echo "<wikilog version='experimental'>\n";
17 echo "<!-- Do not use this script for any purpose. It's scary. -->\n";
18 while( $row = $dbw->fetchObject( $result ) ) {
19 echo dumpReplayEntry( $row );
20 }
21 echo "</wikilog>\n";
22 $dbw->freeResult( $result );
23 }
24
25 function dumpReplayEntry( $row ) {
26 $title = Title::MakeTitle( $row->rc_namespace, $row->rc_title );
27 switch( $row->rc_type ) {
28 case RC_EDIT:
29 case RC_NEW:
30 # Edit
31 $dbr =& wfGetDB( DB_MASTER );
32
33 $out = " <edit>\n";
34 $out .= " <title>" . xmlsafe( $title->getPrefixedText() ) . "</title>\n";
35
36 # Get previous edit timestamp
37 if( $row->rc_last_oldid ) {
38 $s = $dbr->selectRow( 'old',
39 array( 'old_timestamp' ),
40 array( 'old_id' => $row->rc_last_oldid ) );
41 $out .= " <lastedit>" . wfTimestamp2ISO8601( $s->old_timestamp ) . "</lastedit>\n";
42 } else {
43 $out .= " <newpage/>\n";
44 }
45
46 if( $row->rc_this_oldid ) {
47 $s = $dbr->selectRow( 'old', array( 'old_id as id','old_timestamp as timestamp',
48 'old_user as user', 'old_user_text as user_text', 'old_comment as comment',
49 'old_text as text', 'old_flags as flags' ),
50 array( 'old_id' => $row->rc_this_oldid ) );
51 $out .= revision2xml( $s, true, false );
52 } else {
53 $s = $dbr->selectRow( 'cur', array( 'cur_id as id','cur_timestamp as timestamp','cur_user as user',
54 'cur_user_text as user_text', 'cur_restrictions as restrictions','cur_comment as comment',
55 'cur_text as text' ),
56 array( 'cur_id' => $row->rc_cur_id ) );
57 $out .= revision2xml( $s, true, true );
58 }
59 $out .= " </edit>\n";
60 break;
61 case RC_LOG:
62 $dbr =& wfGetDB( DB_MASTER );
63 $s = $dbr->selectRow( 'logging',
64 array( 'log_type', 'log_action', 'log_timestamp', 'log_user',
65 'log_namespace', 'log_title', 'log_comment' ),
66 array( 'log_timestamp' => $row->rc_timestamp,
67 'log_user' => $row->rc_user ) );
68 $ts = wfTimestamp2ISO8601( $row->rc_timestamp );
69 $target = Title::MakeTitle( $s->log_namespace, $s->log_title );
70 $out = " <log>\n";
71 $out .= " <type>" . xmlsafe( $s->log_type ) . "</type>\n";
72 $out .= " <action>" . xmlsafe( $s->log_action ) . "</action>\n";
73 $out .= " <timestamp>" . $ts . "</timestamp>\n";
74 $out .= " <contributor><username>" . xmlsafe( $row->rc_user_text ) . "</username></contributor>\n";
75 $out .= " <target>" . xmlsafe( $target->getPrefixedText() ) . "</target>\n";
76 $out .= " <comment>" . xmlsafe( $s->log_comment ) . "</comment>\n";
77 $out .= " </log>\n";
78 break;
79 case RC_MOVE:
80 case RC_MOVE_OVER_REDIRECT:
81 $target = Title::MakeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
82 $out = " <move>\n";
83 $out .= " <title>" . xmlsafe( $title->getPrefixedText() ) . "</title>\n";
84 $out .= " <target>" . xmlsafe( $target->getPrefixedText() ) . "</target>\n";
85 if( $row->rc_type == RC_MOVE_OVER_REDIRECT ) {
86 $out .= " <override/>\n";
87 }
88 $ts = wfTimestamp2ISO8601( $row->rc_timestamp );
89 $out .= " <id>$row->rc_cur_id</id>\n";
90 $out .= " <timestamp>$ts</timestamp>\n";
91 if($row->rc_user_text) {
92 $u = "<username>" . xmlsafe( $row->rc_user_text ) . "</username>";
93 $u .= "<id>$row->rc_user</id>";
94 } else {
95 $u = "<ip>" . xmlsafe( $row->rc_user_text ) . "</ip>";
96 }
97 $out .= " <contributor>$u</contributor>\n";
98 $out .= " </move>\n";
99 }
100 return $out;
101 }
102
103
104 if( isset( $options['start'] ) ) {
105 $start = wfTimestamp( TS_MW, $options['start'] );
106 dumpReplayLog( $start );
107 } else {
108 echo "This is an experimental script to encapsulate data from recent edits.\n";
109 echo "Usage: php dumpReplayLog.php --start=20050118032544\n";
110 }
111
112 ?>