]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/reassignEdits.inc.php
MediaWiki 1.14.0-scripts
[autoinstallsdev/mediawiki.git] / maintenance / reassignEdits.inc.php
1 <?php
2
3 /**
4  * Support functions for the reassignEdits script
5  *
6  * @file
7  * @ingroup Maintenance
8  * @author Rob Church <robchur@gmail.com>
9  * @licence GNU General Public Licence 2.0 or later
10  */
11
12 /**
13  * Reassign edits from one user to another
14  *
15  * @param $from User to take edits from
16  * @param $to User to assign edits to
17  * @param $rc Update the recent changes table
18  * @param $report Don't change things; just echo numbers
19  * @return integer Number of entries changed, or that would be changed
20  */
21 function reassignEdits( &$from, &$to, $rc = false, $report = false ) {
22         $dbw = wfGetDB( DB_MASTER );
23         $dbw->immediateBegin();
24         $fname = 'reassignEdits';
25                 
26         # Count things
27         out( "Checking current edits..." );
28         $res = $dbw->select( 'revision', 'COUNT(*) AS count', userConditions( $from, 'rev_user', 'rev_user_text' ), $fname );
29         $row = $dbw->fetchObject( $res );
30         $cur = $row->count;
31         out( "found {$cur}.\n" );
32         
33         out( "Checking deleted edits..." );
34         $res = $dbw->select( 'archive', 'COUNT(*) AS count', userConditions( $from, 'ar_user', 'ar_user_text' ), $fname );
35         $row = $dbw->fetchObject( $res );
36         $del = $row->count;
37         out( "found {$del}.\n" );
38         
39         # Don't count recent changes if we're not supposed to
40         if( $rc ) {
41                 out( "Checking recent changes..." );
42                 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', userConditions( $from, 'rc_user', 'rc_user_text' ), $fname );
43                 $row = $dbw->fetchObject( $res );
44                 $rec = $row->count;
45                 out( "found {$rec}.\n" );
46         } else {
47                 $rec = 0;
48         }
49         
50         $total = $cur + $del + $rec;
51         out( "\nTotal entries to change: {$total}\n" );
52         
53         if( !$report ) {
54                 if( $total ) {
55                         # Reassign edits
56                         out( "\nReassigning current edits..." );
57                         $res = $dbw->update( 'revision', userSpecification( $to, 'rev_user', 'rev_user_text' ), userConditions( $from, 'rev_user', 'rev_user_text' ), $fname );
58                         out( "done.\nReassigning deleted edits..." );
59                         $res = $dbw->update( 'archive', userSpecification( $to, 'ar_user', 'ar_user_text' ), userConditions( $from, 'ar_user', 'ar_user_text' ), $fname );
60                         out( "done.\n" );
61                         # Update recent changes if required
62                         if( $rc ) {
63                                 out( "Updating recent changes..." );
64                                 $res = $dbw->update( 'recentchanges', userSpecification( $to, 'rc_user', 'rc_user_text' ), userConditions( $from, 'rc_user', 'rc_user_text' ), $fname );
65                                 out( "done.\n" );
66                         }
67                 }       
68         }
69         
70         $dbw->immediateCommit();
71         return (int)$total;     
72 }
73
74 /**
75  * Return the most efficient set of user conditions
76  * i.e. a user => id mapping, or a user_text => text mapping
77  *
78  * @param $user User for the condition
79  * @param $idfield Field name containing the identifier
80  * @param $utfield Field name containing the user text
81  * @return array
82  */
83 function userConditions( &$user, $idfield, $utfield ) {
84         return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
85 }
86
87 /**
88  * Return user specifications
89  * i.e. user => id, user_text => text
90  *
91  * @param $user User for the spec
92  * @param $idfield Field name containing the identifier
93  * @param $utfield Field name containing the user text
94  * @return array
95  */
96 function userSpecification( &$user, $idfield, $utfield ) {
97         return array( $idfield => $user->getId(), $utfield => $user->getName() );
98 }
99
100 /**
101  * Echo output if $wgSilent is off
102  *
103  * @param $output Output to echo
104  * @return bool True if the output was echoed
105  */
106 function out( $output ) {
107         global $wgSilent;
108         if( !$wgSilent ) {
109                 echo( $output );
110                 return true;
111         } else {
112                 return false;
113         }
114 }
115
116 /**
117  * Mutator for $wgSilent
118  *
119  * @param $silent Switch on $wgSilent
120  */
121 function silent( $silent = true ) {
122         global $wgSilent;
123         $wgSilent = $silent;
124 }
125
126 /**
127  * Initialise the user object
128  *
129  * @param $username Username or IP address
130  * @return User
131  */
132 function initialiseUser( $username ) {
133         if( User::isIP( $username ) ) {
134                 $user = new User();
135                 $user->setId( 0 );
136                 $user->setName( $username );
137         } else {
138                 $user = User::newFromName( $username );
139         }
140         $user->load();
141         return $user;
142 }
143