4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 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-2009 The Nucleus Group
16 * @version $Id: upgrade.functions.php 1592 2011-10-29 22:24:24Z gregorlove $
20 * Some functions common to all upgrade scripts
23 /*************************************************************
24 * NOTE: With upgrade to 3.6, need to set this to use sql_* 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 $result = mysql_query($query);
146 $installed = ( $result != 0 ) && (mysql_num_rows($result) >= $minrows);
153 * Get the Nucleus version. If getNucleusVersion() doesn't exist, default to version 0.96
156 function upgrade_getNucleusVersion()
158 if ( !function_exists('getNucleusVersion') )
163 return getNucleusVersion();
168 * Show the login form
169 * @param string $action
171 function upgrade_showLogin($action)
176 <p> Please enter your login name and password. </p>
178 <form method="POST" action="<?php echo $action; ?>">
180 <li> <label for="i_login">Name:</label> <input type="text" name="login" id="i_login" size="20" /> </li>
181 <li> <label for="i_password">Password:</label> <input type="password" name="password" id="i_password" size="20" /> </li>
183 <p> <input type="submit" value="Log In" /> </p>
184 <input name="action" value="login" type="hidden" />
190 } // end function upgrade_showLogin()
194 * Display the HTML header
196 function upgrade_head()
199 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
200 <html xmlns="http://www.w3.org/1999/xhtml">
202 <title> Nucleus Upgrade </title>
204 // begin if: manual.css exists - <link> it
205 if ( file_exists('../styles/manual.css') )
208 <link rel="stylesheet" href="../styles/manual.css" type="text/css" />
211 // else: include some CSS
215 <style type="text/css">
216 .warning { color: red; }
217 .ok { color: green; }
226 } // end function upgrade_head()
230 * Display the HTML footer
232 function upgrade_foot()
234 echo '</body></html>';
239 * Display an error page
240 * @param string $message
242 function upgrade_error($message)
248 <p> The following message was returned: </p>
249 <p> <?php echo $message?> </p>
250 <p> <a href="index.php" onclick="history.back(); return false;">Go Back</a> </p>
258 function upgrade_start()
260 global $upgrade_failures;
261 $upgrade_failures = 0;
265 <h1> Executing Upgrades </h1>
272 function upgrade_end($message = '')
274 global $upgrade_failures;
276 $from = intGetVar('from');
278 if ( $upgrade_failures > 0 )
280 $message = 'Some queries have failed. Try reverting to a backup or reparing things manually, then re-run this script.';
285 <h1> Upgrade Completed! </h1>
287 <p> <?php echo $message?> </p>
289 <p> Back to the <a href="index.php?from=<?php echo $from; ?>">Upgrades Overview</a> </p>
298 * Executes a query, displaying the user-friendly explanation and a success / fail message. Query errors are displayed, too.
300 * @param string $friendly
301 * @param string $query
302 * @return resource (is this return necessary?)
304 function upgrade_query($friendly, $query)
306 global $upgrade_failures;
308 # output the friendly message
309 echo "<li> $friendly — ";
312 $result = @mysql_query($query);
314 // begin if: error executing query
315 if ( $result === FALSE )
317 echo '<span class="warning"> FAILED </span> <br />';
318 echo 'Error: <code>', mysql_error(), '</code>';
321 // else: query was successful
324 echo '<span class="ok"> SUCCESS! </span>';
333 * Tries to update database version, gives a message when failed
336 * Schema version the database has been upgraded to
338 function update_version($version)
340 global $upgrade_failures;
342 $message = 'Updating DatabaseVersion in config table to ' . $version;
344 // begin if: no upgrade failures; update the database version in the config table
345 if ( $upgrade_failures == 0 )
347 $query = 'UPDATE `%s` ' .
348 'SET `value` = "%s" ' .
349 'WHERE `name` = "DatabaseVersion"';
351 $query = sprintf($query, sql_table('config'), $version);
352 upgrade_query($message, $query);
354 // else: display 'not executed' message
357 echo '<li>', $message, ' — <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
366 * @param string $table table to check (without prefix)
367 * @param array $columns array of column names included
370 function upgrade_checkIfIndexExists($table, $columns)
372 // get info for indices from database
375 $query = 'SHOW INDEX FROM `' . sql_table($table) . '`';
376 $result = @mysql_query($query);
378 // begin loop: each result object
379 while ( $object = mysql_fetch_object($result) )
382 // begin if: key has not been added to the indeces array yet
383 if ( !isset($indices[$object->Key_name]) )
385 $indices[$object->Key_name] = array();
388 array_push($indices[$object->Key_name], $object->Column_name);
391 // compare each index with parameter
392 foreach ( $indices as $key_name => $index_columns )
394 $diff = array_diff($index_columns, $columns);
396 if ( count($diff) == 0 )
408 * Checks to see if a given table exists
410 * @param string $table name of table to check existence of
411 * @return bool TRUE if table exists, FALSE otherwise.
413 function upgrade_checkIfTableExists($table)
415 $query = 'SHOW TABLES LIKE `' . sql_table($table) . '`';
416 $result = @mysql_query($query);
418 // begin if: query executed successfully and one row was returned
419 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
423 // else: query error or no results returned
433 * Checks to see if a given configuration value exists
435 * @param string $value config value to check for existance of (paramater must be MySQL escaped already)
436 * @return bool TRUE if configuration value exists, FALSE otherwise.
438 function upgrade_checkIfCVExists($value)
440 $query = 'SELECT `name` FROM `' . sql_table('config') . '` WHERE `name` = "' . $value . '"';
441 $result = @mysql_query($query);
443 // begin if: query executed successfully and one row was returned
444 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
448 // else: query error or no results returned
458 * Checks to see if a given column exists
460 * @param string $table name of table to check for column in
461 * @param string $column name of column to check for existance of
462 * @return bool TRUE if column exists, FALSE otherwise.
464 function upgrade_checkIfColumnExists($table, $column)
466 $query = 'DESC `' . sql_table($table) . '` `' . $column . '`';
467 $result = @mysql_query($query);
469 // begin if: query executed successfully and one row was returned
470 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
474 // else: query error or no results returned