r81913 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81912‎ | r81913 | r81914 >
Date:19:12, 10 February 2011
Author:platonides
Status:ok
Tags:
Comment:
Remove unused initial_setup(), last $wgDBts2schema user. Follow up to r81132
Modified paths:
  • /trunk/phase3/includes/db/DatabasePostgres.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabasePostgres.php
@@ -212,220 +212,6 @@
213213 return $s;
214214 }
215215
216 -
217 - function initial_setup( $superuser, $password, $dbName ) {
218 - // If this is the initial connection, setup the schema stuff and possibly create the user
219 - global $wgDBname, $wgDBuser, $wgDBpassword, $wgDBmwschema, $wgDBts2schema;
220 -
221 - $safeuser = $this->addIdentifierQuotes( $wgDBuser );
222 - // Are we connecting as a superuser for the first time?
223 - if ( $superuser ) {
224 - // Are we really a superuser? Check out our rights
225 - $SQL = "SELECT
226 - CASE WHEN usesuper IS TRUE THEN
227 - CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
228 - ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
229 - END AS rights
230 - FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes( $superuser );
231 - $rows = $this->numRows( $res = $this->doQuery( $SQL ) );
232 - if ( !$rows ) {
233 - print '<li>ERROR: Could not read permissions for user "' . htmlspecialchars( $superuser ) . "\"</li>\n";
234 - dieout( );
235 - }
236 - $perms = pg_fetch_result( $res, 0, 0 );
237 -
238 - $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes( $wgDBuser );
239 - $rows = $this->numRows( $this->doQuery( $SQL ) );
240 - if ( $rows ) {
241 - print '<li>User "' . htmlspecialchars( $wgDBuser ) . '" already exists, skipping account creation.</li>';
242 - } else {
243 - if ( $perms != 1 && $perms != 3 ) {
244 - print '<li>ERROR: the user "' . htmlspecialchars( $superuser ) . '" cannot create other users. ';
245 - print 'Please use a different Postgres user.</li>';
246 - dieout( );
247 - }
248 - print '<li>Creating user <b>' . htmlspecialchars( $wgDBuser ) . '</b>...';
249 - $safepass = $this->addQuotes( $wgDBpassword );
250 - $SQL = "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass";
251 - $this->doQuery( $SQL );
252 - print "OK</li>\n";
253 - }
254 - // User now exists, check out the database
255 - if ( $dbName != $wgDBname ) {
256 - $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this->addQuotes( $wgDBname );
257 - $rows = $this->numRows( $this->doQuery( $SQL ) );
258 - if ( $rows ) {
259 - print '<li>Database "' . htmlspecialchars( $wgDBname ) . '" already exists, skipping database creation.</li>';
260 - } else {
261 - if ( $perms < 1 ) {
262 - print '<li>ERROR: the user "' . htmlspecialchars( $superuser ) . '" cannot create databases. ';
263 - print 'Please use a different Postgres user.</li>';
264 - dieout( );
265 - }
266 - print '<li>Creating database <b>' . htmlspecialchars( $wgDBname ) . '</b>...';
267 - $safename = $this->addIdentifierQuotes( $wgDBname );
268 - $SQL = "CREATE DATABASE $safename OWNER $safeuser ";
269 - $this->doQuery( $SQL );
270 - print "OK</li>\n";
271 - // Hopefully tsearch2 and plpgsql are in template1...
272 - }
273 -
274 - // Reconnect to check out tsearch2 rights for this user
275 - print '<li>Connecting to "' . htmlspecialchars( $wgDBname ) . '" as superuser "' .
276 - htmlspecialchars( $superuser ) . '" to check rights...';
277 -
278 - $connectVars = array();
279 - if ( $this->mServer != false && $this->mServer != '' ) {
280 - $connectVars['host'] = $this->mServer;
281 - }
282 - if ( $this->mPort != false && $this->mPort != '' ) {
283 - $connectVars['port'] = $this->mPort;
284 - }
285 - $connectVars['dbname'] = $wgDBname;
286 - $connectVars['user'] = $superuser;
287 - $connectVars['password'] = $password;
288 -
289 - @$this->mConn = pg_connect( $this->makeConnectionString( $connectVars ) );
290 - if ( !$this->mConn ) {
291 - print "<b>FAILED TO CONNECT!</b></li>";
292 - dieout( );
293 - }
294 - print "OK</li>\n";
295 - }
296 -
297 - // Setup the schema for this user if needed
298 - $result = $this->schemaExists( $wgDBmwschema );
299 - $safeschema = $this->addIdentifierQuotes( $wgDBmwschema );
300 - if ( !$result ) {
301 - print '<li>Creating schema <b>' . htmlspecialchars( $wgDBmwschema ) . '</b> ...';
302 - $result = $this->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
303 - if ( !$result ) {
304 - print "<b>FAILED</b>.</li>\n";
305 - dieout( );
306 - }
307 - print "OK</li>\n";
308 - } else {
309 - print "<li>Schema already exists, explicitly granting rights...\n";
310 - $safeschema2 = $this->addQuotes( $wgDBmwschema );
311 - $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
312 - "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
313 - "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
314 - "AND p.relkind IN ('r','S','v')\n";
315 - $SQL .= "UNION\n";
316 - $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
317 - "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
318 - "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
319 - "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
320 - $res = $this->doQuery( $SQL );
321 - if ( !$res ) {
322 - print "<b>FAILED</b>. Could not set rights for the user.</li>\n";
323 - dieout( );
324 - }
325 - $this->doQuery( "SET search_path = $safeschema" );
326 - $rows = $this->numRows( $res );
327 - while ( $rows ) {
328 - $rows--;
329 - $this->doQuery( pg_fetch_result( $res, $rows, 0 ) );
330 - }
331 - print "OK</li>";
332 - }
333 -
334 - // Install plpgsql if needed
335 - $this->setup_plpgsql();
336 -
337 - return true; // Reconnect as regular user
338 -
339 - } // end superuser
340 -
341 - if ( !defined( 'POSTGRES_SEARCHPATH' ) ) {
342 - // Install plpgsql if needed
343 - $this->setup_plpgsql();
344 -
345 - // Does the schema already exist? Who owns it?
346 - $result = $this->schemaExists( $wgDBmwschema );
347 - if ( !$result ) {
348 - print '<li>Creating schema <b>' . htmlspecialchars( $wgDBmwschema ) . '</b> ...';
349 - error_reporting( 0 );
350 - $safeschema = $this->addIdentifierQuotes( $wgDBmwschema );
351 - $result = $this->doQuery( "CREATE SCHEMA $safeschema" );
352 - error_reporting( E_ALL );
353 - if ( !$result ) {
354 - print '<b>FAILED</b>. The user "' . htmlspecialchars( $wgDBuser ) .
355 - '" must be able to access the schema. '.
356 - 'You can try making them the owner of the database, or try creating the schema with a '.
357 - 'different user, and then grant access to the "' .
358 - htmlspecialchars( $wgDBuser ) . "\" user.</li>\n";
359 - dieout( );
360 - }
361 - print "OK</li>\n";
362 - } elseif ( $result != $wgDBuser ) {
363 - print '<li>Schema "' . htmlspecialchars( $wgDBmwschema ) . '" exists but is not owned by "' .
364 - htmlspecialchars( $wgDBuser ) . "\". Not ideal.</li>\n";
365 - } else {
366 - print '<li>Schema "' . htmlspecialchars( $wgDBmwschema ) . '" exists and is owned by "' .
367 - htmlspecialchars( $wgDBuser ) . "\". Excellent.</li>\n";
368 - }
369 -
370 - // Always return GMT time to accomodate the existing integer-based timestamp assumption
371 - print "<li>Setting the timezone to GMT for user \"" . htmlspecialchars( $wgDBuser ) . '" ...';
372 - $SQL = "ALTER USER $safeuser SET timezone = 'GMT'";
373 - $result = pg_query( $this->mConn, $SQL );
374 - if ( !$result ) {
375 - print "<b>FAILED</b>.</li>\n";
376 - dieout( );
377 - }
378 - print "OK</li>\n";
379 - // Set for the rest of this session
380 - $SQL = "SET timezone = 'GMT'";
381 - $result = pg_query( $this->mConn, $SQL );
382 - if ( !$result ) {
383 - print "<li>Failed to set timezone</li>\n";
384 - dieout( );
385 - }
386 -
387 - print '<li>Setting the datestyle to ISO, YMD for user "' . htmlspecialchars( $wgDBuser ) . '" ...';
388 - $SQL = "ALTER USER $safeuser SET datestyle = 'ISO, YMD'";
389 - $result = pg_query( $this->mConn, $SQL );
390 - if ( !$result ) {
391 - print "<b>FAILED</b>.</li>\n";
392 - dieout( );
393 - }
394 - print "OK</li>\n";
395 - // Set for the rest of this session
396 - $SQL = "SET datestyle = 'ISO, YMD'";
397 - $result = pg_query( $this->mConn, $SQL );
398 - if ( !$result ) {
399 - print "<li>Failed to set datestyle</li>\n";
400 - dieout( );
401 - }
402 -
403 - // Fix up the search paths if needed
404 - print '<li>Setting the search path for user "' . htmlspecialchars( $wgDBuser ) . '" ...';
405 - $path = $this->addIdentifierQuotes( $wgDBmwschema );
406 - if ( $wgDBts2schema !== $wgDBmwschema ) {
407 - $path .= ', '. $this->addIdentifierQuotes( $wgDBts2schema );
408 - }
409 - if ( $wgDBmwschema !== 'public' && $wgDBts2schema !== 'public' ) {
410 - $path .= ', public';
411 - }
412 - $SQL = "ALTER USER $safeuser SET search_path = $path";
413 - $result = pg_query( $this->mConn, $SQL );
414 - if ( !$result ) {
415 - print "<b>FAILED</b>.</li>\n";
416 - dieout( );
417 - }
418 - print "OK</li>\n";
419 - // Set for the rest of this session
420 - $SQL = "SET search_path = $path";
421 - $result = pg_query( $this->mConn, $SQL );
422 - if ( !$result ) {
423 - print "<li>Failed to set search_path</li>\n";
424 - dieout( );
425 - }
426 - define( 'POSTGRES_SEARCHPATH', $path );
427 - }
428 - }
429 -
430216 /**
431217 * Closes a database connection, if it is open
432218 * Returns success, true if already closed

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r81132Per comment on bug 26612, we should drop the pre-Postgres 8.3 support with th...demon14:12, 28 January 2011

Status & tagging log