typo
[mediawiki.git] / maintenance / updaters.inc
blob8f83a480b599e170625dfe9b0a6f8784dd824d2e
1 <?php
2 /**
3  * @package MediaWiki
4  * @subpackage Maintenance
5  */
6  
7  /** */
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'archives/moveCustomMessages.inc';
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' ),
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' ),
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         }
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         }
61 function do_revision_updates() {
62         global $wgSoftwareRevision;
63         if ( $wgSoftwareRevision < 1001 ) {
64                 update_passwords();
65         }
68 function update_passwords() {
69         global $wgDatabase;
70         $fname = "Update script: update_passwords()";
71         print "\nIt appears that you need to update the user passwords in your\n" .
72           "database. If you have already done this (if you've run this update\n" .
73           "script once before, for example), doing so again will make all your\n" .
74           "user accounts inaccessible, so be sure you only do this once.\n" .
75           "Update user passwords? (yes/no)";
77         $resp = readconsole();
78     if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
80         $sql = "SELECT user_id,user_password FROM user";
81         $source = $wgDatabase->query( $sql, $fname );
83         while ( $row = $wgDatabase->fetchObject( $source ) ) {
84                 $id = $row->user_id;
85                 $oldpass = $row->user_password;
86                 $newpass = md5( "{$id}-{$oldpass}" );
88                 $sql = "UPDATE user SET user_password='{$newpass}' " .
89                   "WHERE user_id={$id}";
90                 $wgDatabase->query( $sql, $fname );
91         }
94 function do_interwiki_update() {
95         # Check that interwiki table exists; if it doesn't source it
96         global $wgDatabase;
97         if( $wgDatabase->tableExists( "interwiki" ) ) {
98                 echo "...already have interwiki table\n";
99                 return true;
100         }
101         echo "Creating interwiki table: ";
102         dbsource( "maintenance/archives/patch-interwiki.sql" );
103         echo "ok\n";
104         echo "Adding default interwiki definitions: ";
105         dbsource( "maintenance/interwiki.sql" );
106         echo "ok\n";
109 function do_index_update() {
110         # Check that proper indexes are in place
111         global $wgDatabase;
112         $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
113         if( $meta->multiple_key == 0 ) {
114                 echo "Updating indexes to 20031107: ";
115                 dbsource( "maintenance/archives/patch-indexes.sql" );
116                 echo "ok\n";
117                 return true;
118         }
119         echo "...indexes seem up to 20031107 standards\n";
120         return false;
123 function do_linkscc_1_3_update() {
124         // Update linkscc table to 1.3 schema if necessary
125         global $wgDatabase, $wgVersion;
126         if( ( strpos( "1.3", $wgVersion ) === 0 ) && $wgDatabase->tableExists( "linkscc" )
127                 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
128                 echo "Altering lcc_title field from linkscc table... ";
129                 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
130                 echo "ok\n";
131         } else {
132                 echo "...linkscc is up to date, or does not exist. Good.\n";
133         }
136 function do_image_name_unique_update() {
137         global $wgDatabase;
138         if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
139                 echo "...image primary key already set.\n";
140         } else {
141                 echo "Making img_name the primary key... ";
142                 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
143                 echo "ok\n";
144         }
147 # Assumes that the group table has been added.
148 function do_group_update() {
149         global $wgDatabase;
150         $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
151                 $wgDatabase->tableName( 'group' ) );
152         $row = $wgDatabase->fetchObject( $res );
153         $wgDatabase->freeResult( $res );
154         if( $row->c == 0 ) {
155                 echo "Adding default group definitions... ";
156                 dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
157                 echo "ok\n";
158         } else {
159                 echo "...group definitions already in place.\n";
160                 $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
161                                                  WHERE group_name IN ('Sysops','Bureaucrat')
162                                                    AND group_rights NOT LIKE 'sysop'",
163                                                $wgDatabase->tableName( 'group' ) );
164                 $row = $wgDatabase->fetchObject( $res );
165                 $wgDatabase->freeResult( $res );
166                 if( $row->n ) {
167                         echo "Fixing sysops group permissions... ";
168                         dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
169                         echo "ok\n";
170                 } else {
171                         echo "...sysop group permissions look ok.\n";
172                 }
173         }
176 function do_all_updates() {
177         global $wgNewTables, $wgNewFields;
178         
179         # Add missing tables
180         foreach ( $wgNewTables as $tableRecord ) {
181                 add_table( $tableRecord[0], $tableRecord[1] );
182                 flush();
183         }
185         # Add missing fields
186         foreach ( $wgNewFields as $fieldRecord ) {
187                 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
188                 flush();
189         }
190         
191         # Add default group data
192         do_group_update(); flush();
194         # Do schema updates which require special handling
195         do_interwiki_update(); flush();
196         do_index_update(); flush();
197         do_linkscc_1_3_update(); flush();
198         convertLinks(); flush();
199         do_image_name_unique_update(); flush();
201         if ( isTemplateInitialised() ) {
202                 print "Template namespace already initialised\n";
203         } else {
204                 moveCustomMessages( 1 ); flush();
205                 moveCustomMessages( 2 ); flush();
206                 moveCustomMessages( 3 ); flush();
207         }
209         initialiseMessages(); flush();