4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2012 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
14 * @license http://nucleuscms.org/license.txt GNU General Public License
15 * @copyright Copyright (C) 2002-2012 The Nucleus Group
16 * @version $Id: upgrade.functions.php 1889 2012-06-17 08:46:45Z sakamocchi $
20 * Some functions common to all upgrade scripts
23 /*************************************************************
24 * NOTE: With upgrade to 4.0, need to set this to use DB::* API
25 **************************************************************/
27 include('../../config.php');
29 // sql_table function did not exists in nucleus <= 2.0
30 if ( !function_exists('sql_table') )
32 function sql_table($name)
34 return 'nucleus_' . $name;
38 //intGetVar did not exist in very early versions
39 if ( !function_exists('intGetVar') )
41 function intGetVar($name)
45 return intval($_GET[$name]);
49 global $HTTP_GET_VARS;
50 return intval($HTTP_GET_VARS[$name]);
56 function upgrade_checkinstall($version)
63 $query = 'SELECT bconvertbreaks FROM ' . sql_table('blog') . ' LIMIT 1';
68 $query = 'SELECT cip FROM ' . sql_table('comment') . ' LIMIT 1';
73 $query = 'SELECT mcookiekey FROM ' . sql_table('member') . ' LIMIT 1';
78 $query = 'SELECT bnotifytype FROM ' . sql_table('blog') . ' LIMIT 1';
83 $query = 'SELECT * FROM ' . sql_table('plugin_option') . ' LIMIT 1';
88 $query = 'SELECT sdincpref FROM ' . sql_table('skin_desc') . ' LIMIT 1';
94 $query = 'SELECT oid FROM ' . sql_table('plugin_option_desc') . ' LIMIT 1';
100 $query = 'SELECT bincludesearch FROM ' . sql_table('blog') . ' LIMIT 1';
105 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 250 LIMIT 1';
110 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 300 LIMIT 1';
115 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 310 LIMIT 1';
120 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 320 LIMIT 1';
125 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 330 LIMIT 1';
130 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 340 LIMIT 1';
135 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 350 LIMIT 1';
140 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 360 LIMIT 1';
145 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 400 LIMIT 1';
150 $result = mysql_query($query);
151 $installed = ( $result != 0 ) && (mysql_num_rows($result) >= $minrows);
158 * Get the Nucleus version. If getNucleusVersion() doesn't exist, default to version 0.96
161 function upgrade_getNucleusVersion()
163 if ( !function_exists('getNucleusVersion') )
168 return getNucleusVersion();
172 * Show the login form
173 * @param string $action
175 function upgrade_showLogin($action)
179 echo "<h1> Log In </h1>\n";
180 echo "<p>Please enter your login name and password. </p>\n";
182 echo "<form method=\"POST\" action=\"{$action}\">\n";
184 echo "<li><label for=\"i_login\">Name:</label> <input type=\"text\" name=\"login\" id=\"i_login\" size=\"20\" /></li>\n";
185 echo "<li><label for=\"i_password\">Password:</label> <input type=\"password\" name=\"password\" id=\"i_password\" size=\"20\" /></li>\n";
187 echo "<p><input type=\"submit\" value=\"Log In\" /></p>\n";
188 echo "<input name=\"action\" value=\"login\" type=\"hidden\" />\n";
196 * Display the HTML header
198 function upgrade_head()
200 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
201 echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
203 echo "<title> Nucleus Upgrade </title>\n";
205 if ( file_exists('../documentation/styles/manual.css') )
207 echo "<link rel=\"stylesheet\" href=\"../documentation/styles/manual.css\" type=\"text/css\" />\n";
211 echo "<style type=\"text/css\">\n";
212 echo ".warning { color: red; }\n";
213 echo ".ok { color: green; }\n";
223 * Display the HTML footer
225 function upgrade_foot()
227 echo '</body></html>';
232 * Display an error page
233 * @param string $message
235 function upgrade_error($message)
241 <p> The following message was returned: </p>
242 <p> <?php echo $message?> </p>
243 <p> <a href="index.php" onclick="history.back(); return false;">Go Back</a> </p>
251 function upgrade_start()
253 global $upgrade_failures;
254 $upgrade_failures = 0;
258 <h1> Executing Upgrades </h1>
265 function upgrade_end($message = '')
267 global $upgrade_failures;
269 $from = intGetVar('from');
271 if ( $upgrade_failures > 0 )
273 $message = 'Some queries have failed. Try reverting to a backup or reparing things manually, then re-run this script.';
278 <h1> Upgrade Completed! </h1>
280 <p> <?php echo $message?> </p>
282 <p> Back to the <a href="index.php?from=<?php echo $from; ?>">Upgrades Overview</a> </p>
291 * Executes a query, displaying the user-friendly explanation and a success / fail message. Query errors are displayed, too.
293 * @param string $friendly
294 * @param string $query
295 * @return resource (is this return necessary?)
297 function upgrade_query($friendly, $query)
299 global $upgrade_failures;
301 # output the friendly message
302 echo "<li> $friendly — ";
305 $result = @mysql_query($query);
307 // begin if: error executing query
308 if ( $result === FALSE )
310 echo '<span class="warning"> FAILED </span> <br />';
311 echo 'Error: <code>', mysql_error(), '</code>';
314 // else: query was successful
317 echo '<span class="ok"> SUCCESS! </span>';
326 * Tries to update database version, gives a message when failed
329 * Schema version the database has been upgraded to
331 function update_version($version)
333 global $upgrade_failures;
335 $message = 'Updating DatabaseVersion in config table to ' . $version;
337 // begin if: no upgrade failures; update the database version in the config table
338 if ( $upgrade_failures == 0 )
340 $query = 'UPDATE %s SET value = "%s" WHERE name = "DatabaseVersion";';
342 $query = sprintf($query, sql_table('config'), $version);
343 upgrade_query($message, $query);
345 // else: display 'not executed' message
348 echo '<li>', $message, ' — <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
357 * @param string $table table to check (without prefix)
358 * @param array $columns array of column names included
361 function upgrade_checkIfIndexExists($table, $columns)
363 // get info for indices from database
366 $query = 'SHOW INDEX FROM ' . sql_table($table);
367 $result = @mysql_query($query);
369 // begin loop: each result object
370 while ( $object = mysql_fetch_object($result) )
373 // begin if: key has not been added to the indeces array yet
374 if ( !isset($indices[$object->Key_name]) )
376 $indices[$object->Key_name] = array();
379 array_push($indices[$object->Key_name], $object->Column_name);
382 // compare each index with parameter
383 foreach ( $indices as $key_name => $index_columns )
385 $diff = array_diff($index_columns, $columns);
387 if ( count($diff) == 0 )
399 * Checks to see if a given table exists
401 * @param string $table name of table to check existence of
402 * @return bool TRUE if table exists, FALSE otherwise.
404 function upgrade_checkIfTableExists($table)
406 $query = 'SHOW TABLES LIKE ' . sql_table($table);
407 $result = @mysql_query($query);
409 // begin if: query executed successfully and one row was returned
410 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
414 // else: query error or no results returned
424 * Checks to see if a given configuration value exists
426 * @param string $value config value to check for existance of (paramater must be MySQL escaped already)
427 * @return bool TRUE if configuration value exists, FALSE otherwise.
429 function upgrade_checkIfCVExists($value)
431 $query = 'SELECT name FROM ' . sql_table('config') . ' WHERE name = "' . $value . '"';
432 $result = @mysql_query($query);
434 // begin if: query executed successfully and one row was returned
435 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
439 // else: query error or no results returned
449 * Checks to see if a given column exists
451 * @param string $table name of table to check for column in
452 * @param string $column name of column to check for existance of
453 * @return bool TRUE if column exists, FALSE otherwise.
455 function upgrade_checkIfColumnExists($table, $column)
457 $query = 'DESC ' . sql_table($table) . ' ' . $column;
458 $result = @mysql_query($query);
460 // begin if: query executed successfully and one row was returned
461 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
465 // else: query error or no results returned