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';
151 $result = DB::getResult($query);
152 $installed = ( $result !== FALSE ) && ($result->rowCount() >= $minrows);
155 $result = mysql_query($query);
156 $installed = ( $result != 0 ) && (mysql_num_rows($result) >= $minrows);
158 >>>>>>> skinnable-master
164 * Get the Nucleus version. If getNucleusVersion() doesn't exist, default to version 0.96
167 function upgrade_getNucleusVersion()
169 if ( !function_exists('getNucleusVersion') )
174 return getNucleusVersion();
178 * Show the login form
179 * @param string $action
181 function upgrade_showLogin($action)
185 echo "<h1> Log In </h1>\n";
186 echo "<p>Please enter your login name and password. </p>\n";
188 echo "<form method=\"POST\" action=\"{$action}\">\n";
190 echo "<li><label for=\"i_login\">Name:</label> <input type=\"text\" name=\"login\" id=\"i_login\" size=\"20\" /></li>\n";
191 echo "<li><label for=\"i_password\">Password:</label> <input type=\"password\" name=\"password\" id=\"i_password\" size=\"20\" /></li>\n";
193 echo "<p><input type=\"submit\" value=\"Log In\" /></p>\n";
194 echo "<input name=\"action\" value=\"login\" type=\"hidden\" />\n";
202 * Display the HTML header
204 function upgrade_head()
206 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
207 echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
209 echo "<title> Nucleus Upgrade </title>\n";
211 if ( file_exists('../documentation/styles/manual.css') )
213 echo "<link rel=\"stylesheet\" href=\"../documentation/styles/manual.css\" type=\"text/css\" />\n";
217 echo "<style type=\"text/css\">\n";
218 echo ".warning { color: red; }\n";
219 echo ".ok { color: green; }\n";
229 * Display the HTML footer
231 function upgrade_foot()
233 echo '</body></html>';
238 * Display an error page
239 * @param string $message
241 function upgrade_error($message)
247 <p> The following message was returned: </p>
248 <p> <?php echo $message?> </p>
249 <p> <a href="index.php" onclick="history.back(); return false;">Go Back</a> </p>
257 function upgrade_start()
259 global $upgrade_failures;
260 $upgrade_failures = 0;
264 <h1> Executing Upgrades </h1>
271 function upgrade_end($message = '')
273 global $upgrade_failures;
275 $from = intGetVar('from');
277 if ( $upgrade_failures > 0 )
279 $message = 'Some queries have failed. Try reverting to a backup or reparing things manually, then re-run this script.';
284 <h1> Upgrade Completed! </h1>
286 <p> <?php echo $message?> </p>
288 <p> Back to the <a href="index.php?from=<?php echo $from; ?>">Upgrades Overview</a> </p>
297 * Executes a query, displaying the user-friendly explanation and a success / fail message. Query errors are displayed, too.
299 * @param string $friendly
300 * @param string $query
301 * @return resource (is this return necessary?)
303 function upgrade_query($friendly, $query)
305 global $upgrade_failures;
307 # output the friendly message
308 echo "<li> $friendly — ";
311 $result = @DB::execute($query);
313 // begin if: error executing query
314 if ( $result === FALSE )
316 echo '<span class="warning"> FAILED </span> <br />';
317 $err = DB::getError();
318 echo 'Error: <code>', $err[2], '</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 SET value = "%s" WHERE name = "DatabaseVersion";';
349 $query = sprintf($query, sql_table('config'), $version);
350 upgrade_query($message, $query);
352 // else: display 'not executed' message
355 echo '<li>', $message, ' — <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
364 * @param string $table table to check (without prefix)
365 * @param array $columns array of column names included
368 function upgrade_checkIfIndexExists($table, $columns)
370 // get info for indices from database
373 $query = 'SHOW INDEX FROM ' . sql_table($table);
374 $result = @DB::getResult($query);
376 // begin loop: each result object
377 foreach ( $result as $row )
380 // begin if: key has not been added to the indeces array yet
381 if ( !isset($indices[$row['Key_name']]) )
383 $indices[$row['Key_name']] = array();
386 array_push($indices[$row['Key_name']], $row['Column_name']);
389 // compare each index with parameter
390 foreach ( $indices as $key_name => $index_columns )
392 $diff = array_diff($index_columns, $columns);
394 if ( count($diff) == 0 )
406 * Checks to see if a given table exists
408 * @param string $table name of table to check existence of
409 * @return bool TRUE if table exists, FALSE otherwise.
411 function upgrade_checkIfTableExists($table)
413 $query = 'SHOW TABLES LIKE ' . sql_table($table);
414 $result = DB::getResult($query);
416 // begin if: query executed successfully and one row was returned
417 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
421 // else: query error or no results returned
431 * Checks to see if a given configuration value exists
433 * @param string $value config value to check for existance of (paramater must be MySQL escaped already)
434 * @return bool TRUE if configuration value exists, FALSE otherwise.
436 function upgrade_checkIfCVExists($value)
438 $query = 'SELECT name FROM ' . sql_table('config') . ' WHERE name = "' . $value . '"';
439 $result = DB::getResult($query);
441 // begin if: query executed successfully and one row was returned
442 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
446 // else: query error or no results returned
456 * Checks to see if a given column exists
458 * @param string $table name of table to check for column in
459 * @param string $column name of column to check for existance of
460 * @return bool TRUE if column exists, FALSE otherwise.
462 function upgrade_checkIfColumnExists($table, $column)
464 $query = 'DESC ' . sql_table($table) . ' ' . $column;
465 $result = DB::getResult($query);
467 // begin if: query executed successfully and one row was returned
468 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
472 // else: query error or no results returned