(bug 454) Merge e-notif 2.00
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'archives/moveCustomMessages.inc';
12
13 $wgNewTables = array(
14 # table patch file (in maintenance/archives)
15 array( 'linkscc', 'patch-linkscc.sql' ),
16 array( 'hitcounter', 'patch-hitcounter.sql' ),
17 array( 'querycache', 'patch-querycache.sql' ),
18 array( 'objectcache', 'patch-objectcache.sql' ),
19 array( 'categorylinks', 'patch-categorylinks.sql' ),
20 array( 'logging', 'patch-logging.sql' ),
21 array( 'user_rights', 'patch-user_rights.sql' ),
22 array( 'user_groups', 'patch-userlevels.sql' ),
23 );
24
25 $wgNewFields = array(
26 # table field patch file (in maintenance/archives)
27 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
28 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
29 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
30 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
31 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
32 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
33 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
34 array( 'user', 'user_token', 'patch-user_token.sql' ),
35 array( 'user_rights', 'ur_user', 'patch-rename-user_groups-and_rights.sql' ),
36 array( 'group', 'group_rights', 'patch-userlevels-rights.sql' ),
37 );
38
39 function add_table( $name, $patch ) {
40 global $wgDatabase;
41 if ( $wgDatabase->tableExists( $name ) ) {
42 echo "...$name table already exists.\n";
43 } else {
44 echo "Creating $name table...";
45 dbsource( "maintenance/archives/$patch", $wgDatabase );
46 echo "ok\n";
47 }
48 }
49
50 function add_field( $table, $field, $patch ) {
51 global $wgDatabase;
52 if ( $wgDatabase->fieldExists( $table, $field ) ) {
53 echo "...have $field field in $table table.\n";
54 } else {
55 echo "Adding $field field to table $table...";
56 dbsource( "maintenance/archives/$patch" , $wgDatabase );
57 echo "ok\n";
58 }
59 }
60
61 function do_revision_updates() {
62 global $wgSoftwareRevision;
63 if ( $wgSoftwareRevision < 1001 ) {
64 update_passwords();
65 }
66 }
67
68 function update_passwords() {
69 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
70
71 global $wgDatabase;
72 $fname = "Update script: update_passwords()";
73 print "\nIt appears that you need to update the user passwords in your\n" .
74 "database. If you have already done this (if you've run this update\n" .
75 "script once before, for example), doing so again will make all your\n" .
76 "user accounts inaccessible, so be sure you only do this once.\n" .
77 "Update user passwords? (yes/no)";
78
79 $resp = readconsole();
80 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
81
82 $sql = "SELECT user_id,user_password FROM user";
83 $source = $wgDatabase->query( $sql, $fname );
84
85 while ( $row = $wgDatabase->fetchObject( $source ) ) {
86 $id = $row->user_id;
87 $oldpass = $row->user_password;
88 $newpass = md5( "{$id}-{$oldpass}" );
89
90 $sql = "UPDATE user SET user_password='{$newpass}' " .
91 "WHERE user_id={$id}";
92 $wgDatabase->query( $sql, $fname );
93 }
94 }
95
96 function do_interwiki_update() {
97 # Check that interwiki table exists; if it doesn't source it
98 global $wgDatabase;
99 if( $wgDatabase->tableExists( "interwiki" ) ) {
100 echo "...already have interwiki table\n";
101 return true;
102 }
103 echo "Creating interwiki table: ";
104 dbsource( "maintenance/archives/patch-interwiki.sql" );
105 echo "ok\n";
106 echo "Adding default interwiki definitions: ";
107 dbsource( "maintenance/interwiki.sql" );
108 echo "ok\n";
109 }
110
111 function do_index_update() {
112 # Check that proper indexes are in place
113 global $wgDatabase;
114 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
115 if( $meta->multiple_key == 0 ) {
116 echo "Updating indexes to 20031107: ";
117 dbsource( "maintenance/archives/patch-indexes.sql" );
118 echo "ok\n";
119 return true;
120 }
121 echo "...indexes seem up to 20031107 standards\n";
122 return false;
123 }
124
125 function do_linkscc_1_3_update() {
126 // Update linkscc table to 1.3 schema if necessary
127 global $wgDatabase, $wgVersion;
128 if( $wgDatabase->tableExists( "linkscc" )
129 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
130 echo "Altering lcc_title field from linkscc table... ";
131 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
132 echo "ok\n";
133 } else {
134 echo "...linkscc is up to date, or does not exist. Good.\n";
135 }
136 }
137
138 function do_image_name_unique_update() {
139 global $wgDatabase;
140 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
141 echo "...image primary key already set.\n";
142 } else {
143 echo "Making img_name the primary key... ";
144 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
145 echo "ok\n";
146 }
147 }
148
149 function do_watchlist_update() {
150 global $wgDatabase;
151 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
152 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
153 } else {
154 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
155 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
156 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
157 echo "ok\n";
158 }
159 }
160
161 function do_copy_newtalk_to_watchlist() {
162 global $wgDatabase;
163 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
164
165 $CommandLineMode_save = $wgCommandLineMode;
166 $wgCommandLineMode = false; # otherwise User:loadfromDatabase() early returns, but we need it herein
167
168 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
169 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
170 $wgDatabase->tableName( 'user_newtalk' ) );
171 $num_newtalks=$wgDatabase->numRows($res);
172 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
173
174 $user = new User();
175 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
176 $wluser = $wgDatabase->fetchObject( $res );
177 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
178 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
179 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
180 $wgDatabase->replace( 'watchlist',
181 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
182 array('wl_user' => 0,
183 'wl_namespace' => NS_USER_TALK,
184 'wl_title' => $wluser->user_ip,
185 'wl_notificationtimestamp' => '19700101000000'
186 ), 'updaters.inc::do_watchlist_update2'
187 );
188 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
189 }
190 } else { # normal users ... have user_ids
191 $user->setID($wluser->user_id);
192 $wgDatabase->replace( 'watchlist',
193 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
194 array('wl_user' => $user->getID(),
195 'wl_namespace' => NS_USER_TALK,
196 'wl_title' => $user->getName(),
197 'wl_notificationtimestamp' => '19700101000000'
198 ), 'updaters.inc::do_watchlist_update3'
199 );
200 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
201 }
202 }
203 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
204 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
205 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
206 } else {
207 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
208 }
209 $wgCommandLineMode = $CommandLineMode_save;
210 }
211
212
213 function do_user_update() {
214 global $wgDatabase;
215 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
216 echo "EAUTHENT: The user table is already set up for email authentication.\n";
217 } else {
218 echo "EAUTHENT: Adding user_emailauthenticationtimestamp field for email authentication management.";
219 /* ALTER TABLE user ADD (user_emailauthenticationtimestamp varchar(14) binary NOT NULL default '0'); */
220 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
221 echo "ok\n";
222 }
223 }
224
225 # Assumes that the group table has been added.
226 function do_group_update() {
227 global $wgDatabase;
228 $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
229 $wgDatabase->tableName( 'group' ) );
230 $row = $wgDatabase->fetchObject( $res );
231 $wgDatabase->freeResult( $res );
232 if( $row->c == 0 ) {
233 echo "Adding default group definitions... ";
234 dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
235 echo "ok\n";
236 } else {
237 echo "...group definitions already in place.\n";
238 $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
239 WHERE group_name IN ('Sysops','Bureaucrat')
240 AND group_rights NOT LIKE 'sysop'",
241 $wgDatabase->tableName( 'group' ) );
242 $row = $wgDatabase->fetchObject( $res );
243 $wgDatabase->freeResult( $res );
244 if( $row->n ) {
245 echo "Fixing sysops group permissions... ";
246 dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
247 echo "ok\n";
248 } else {
249 echo "...sysop group permissions look ok.\n";
250 }
251 }
252 }
253
254 function do_all_updates() {
255 global $wgNewTables, $wgNewFields;
256
257 # Add missing tables
258 foreach ( $wgNewTables as $tableRecord ) {
259 add_table( $tableRecord[0], $tableRecord[1] );
260 flush();
261 }
262
263 # Add missing fields
264 foreach ( $wgNewFields as $fieldRecord ) {
265 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
266 flush();
267 }
268
269 # Add default group data
270 do_group_update(); flush();
271
272 # Do schema updates which require special handling
273 do_interwiki_update(); flush();
274 do_index_update(); flush();
275 do_linkscc_1_3_update(); flush();
276 convertLinks(); flush();
277 do_image_name_unique_update(); flush();
278 do_watchlist_update(); flush();
279 do_copy_newtalk_to_watchlist(); flush();
280 do_user_update(); flush();
281
282 if ( isTemplateInitialised() ) {
283 print "Template namespace already initialised\n";
284 } else {
285 moveCustomMessages( 1 ); flush();
286 moveCustomMessages( 2 ); flush();
287 moveCustomMessages( 3 ); flush();
288 }
289
290 initialiseMessages(); flush();
291 }
292
293 ?>