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