OSDN Git Service

6ea18c34cf4ffa920e6e202665db4b1e3dec3cf3
[nucleus-jp/nucleus-next.git] / nucleus / upgrades / upgrade.functions.php
1 <?php
2
3 /**
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2009 The Nucleus Group
6  *
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)
12  */
13 /**
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 $
17  */
18
19         /**
20          * Some functions common to all upgrade scripts
21          */
22
23         /*************************************************************
24          *     NOTE: With upgrade to 3.6, need to set this to use sql_* API
25          **************************************************************/
26
27         include('../../config.php');
28
29         // sql_table function did not exists in nucleus <= 2.0
30         if ( !function_exists('sql_table') )
31         {
32                 function sql_table($name)
33                 {
34                         return 'nucleus_' . $name;
35                 }
36         }
37
38         //intGetVar did not exist in very early versions
39         if ( !function_exists('intGetVar') )
40         {
41                 function intGetVar($name)
42                 {
43                         if ( defined($_GET) )
44                         {
45                                 return intval($_GET[$name]);
46                         }
47                         else
48                         {
49                                 global $HTTP_GET_VARS;
50                                 return intval($HTTP_GET_VARS[$name]);
51                         }
52                 }
53         }
54
55
56         function upgrade_checkinstall($version)
57         {
58                 $installed = 0;
59
60                 switch( $version )
61                 {
62                         case '95':
63                                 $query = 'SELECT bconvertbreaks FROM ' . sql_table('blog') . ' LIMIT 1';
64                                 $minrows = -1;
65                         break;
66
67                         case '96':
68                                 $query = 'SELECT cip FROM ' . sql_table('comment') . ' LIMIT 1';
69                                 $minrows = -1;
70                         break;
71
72                         case '100':
73                                 $query = 'SELECT mcookiekey FROM ' . sql_table('member') . ' LIMIT 1';
74                                 $minrows = -1;
75                         break;
76
77                         case '110':
78                                 $query = 'SELECT bnotifytype FROM ' . sql_table('blog') . ' LIMIT 1';
79                                 $minrows = -1;
80                         break;
81
82                         case '150':
83                                 $query = 'SELECT * FROM ' . sql_table('plugin_option') . ' LIMIT 1';
84                                 $minrows = -1;
85                         break;
86
87                         case '200':
88                                 $query = 'SELECT sdincpref FROM ' . sql_table('skin_desc') . ' LIMIT 1';
89                                 $minrows = -1;
90                         break;
91
92                         // dev only (v2.2)
93                         case '220':
94                                 $query = 'SELECT oid FROM ' . sql_table('plugin_option_desc') . ' LIMIT 1';
95                                 $minrows = -1;
96                         break;
97
98                         // v2.5 beta
99                         case '240':
100                                 $query = 'SELECT bincludesearch FROM ' . sql_table('blog') . ' LIMIT 1';
101                                 $minrows = -1;
102                         break;
103
104                         case '250':
105                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 250 LIMIT 1';
106                                 $minrows = 1;
107                         break;
108
109                         case '300':
110                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 300 LIMIT 1';
111                                 $minrows = 1;
112                         break;
113
114                         case '310':
115                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 310 LIMIT 1';
116                                 $minrows = 1;
117                         break;
118
119                         case '320':
120                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 320 LIMIT 1';
121                                 $minrows = 1;
122                         break;
123
124                         case '330':
125                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 330 LIMIT 1';
126                                 $minrows = 1;
127                         break;
128
129                         case '340':
130                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 340 LIMIT 1';
131                                 $minrows = 1;
132                         break;
133
134                         case '350':
135                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 350 LIMIT 1';
136                                 $minrows = 1;
137                         break;
138
139                         case '360':
140                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 360 LIMIT 1';
141                                 $minrows = 1;
142                         break;
143                 }
144
145                 $result = mysql_query($query);
146                 $installed = ( $result != 0 ) && (mysql_num_rows($result) >= $minrows);
147
148                 return $installed;
149         }
150
151
152         /**
153          * Get the Nucleus version. If getNucleusVersion() doesn't exist, default to version 0.96
154          * @return int
155          */
156         function upgrade_getNucleusVersion()
157         {
158                 if ( !function_exists('getNucleusVersion') )
159                 {
160                         return 96;
161                 }
162
163                 return getNucleusVersion();
164         }
165
166
167         /**
168          * Show the login form
169          * @param string $action
170          */
171         function upgrade_showLogin($action)
172         {
173                 upgrade_head();
174 ?>
175         <h1> Log In </h1>
176         <p> Please enter your login name and password. </p>
177
178         <form method="POST" action="<?php echo $action; ?>">
179         <ul>
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>
182         </ul>
183         <p> <input type="submit" value="Log In" /> </p>
184         <input name="action" value="login" type="hidden" />
185         </form>
186
187 <?php
188                 upgrade_foot();
189                 exit;
190         } // end function upgrade_showLogin()
191
192
193         /**
194          * Display the HTML header
195          */
196         function upgrade_head()
197         {
198 ?>
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">
201         <head>
202                 <title> Nucleus Upgrade </title>
203 <?php
204         // begin if: manual.css exists - <link> it
205         if ( file_exists('../styles/manual.css') )
206         {
207 ?>
208                 <link rel="stylesheet" href="../styles/manual.css" type="text/css" />
209 <?php
210         }
211         // else: include some CSS
212         else
213         {
214 ?>
215                 <style type="text/css">
216                         .warning { color: red; }
217                         .ok { color: green; }
218                 </style>
219 <?php
220         } // end if
221 ?>
222         </head>
223         <body>
224
225 <?php
226         } // end function upgrade_head()
227
228
229         /**
230          * Display the HTML footer
231          */
232         function upgrade_foot()
233         {
234                 echo '</body></html>';
235         }
236
237
238         /**
239          * Display an error page
240          * @param string $message
241          */
242         function upgrade_error($message)
243         {
244                 upgrade_head();
245 ?>
246                 <h1> Error </h1>
247
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>
251
252 <?php
253                 upgrade_foot();
254                 exit;
255         }
256
257
258         function upgrade_start()
259         {
260                 global $upgrade_failures;
261                 $upgrade_failures = 0;
262
263                 upgrade_head();
264 ?>
265                 <h1> Executing Upgrades </h1>
266
267                 <ul>
268 <?php
269         }
270
271
272         function upgrade_end($message = '')
273         {
274                 global $upgrade_failures;
275
276                 $from = intGetVar('from');
277
278                 if ( $upgrade_failures > 0 )
279                 {
280                         $message = 'Some queries have failed. Try reverting to a backup or reparing things manually, then re-run this script.';
281                 }
282 ?>
283                 </ul>
284
285                 <h1> Upgrade Completed! </h1>
286
287                 <p> <?php echo $message?> </p>
288
289                 <p> Back to the <a href="index.php?from=<?php echo $from; ?>">Upgrades Overview</a> </p>
290
291 <?php
292                 upgrade_foot();
293                 exit;
294         }
295
296
297         /**
298          * Executes a query, displaying the user-friendly explanation and a success / fail message. Query errors are displayed, too.
299          *
300          * @param string $friendly
301          * @param string $query
302          * @return resource (is this return necessary?)
303          */
304         function upgrade_query($friendly, $query)
305         {
306                 global $upgrade_failures;
307
308                 # output the friendly message
309                 echo "<li> $friendly &mdash; ";
310
311                 # execute the query
312                 $result = @mysql_query($query);
313
314                 // begin if: error executing query
315                 if ( $result === FALSE )
316                 {
317                         echo '<span class="warning"> FAILED </span> <br />';
318                         echo 'Error: <code>', mysql_error(), '</code>';
319                         $upgrade_failures++;
320                 }
321                 // else: query was successful
322                 else
323                 {
324                         echo '<span class="ok"> SUCCESS! </span>';
325                 } // end if
326
327                 echo '</li>', "\n";
328                 return $result;
329         }
330
331
332         /**
333           * Tries to update database version, gives a message when failed
334           *
335           * @param $version
336           *     Schema version the database has been upgraded to
337           */
338         function update_version($version)
339         {
340                 global $upgrade_failures;
341
342                 $message = 'Updating DatabaseVersion in config table to ' . $version;
343
344                 // begin if: no upgrade failures; update the database version in the config table
345                 if ( $upgrade_failures == 0 )
346                 {
347                         $query = 'UPDATE `%s` ' .
348                                 'SET `value` = "%s" ' .
349                                 'WHERE `name` = "DatabaseVersion"';
350
351                         $query = sprintf($query, sql_table('config'), $version);
352                         upgrade_query($message, $query);
353                 }
354                 // else: display 'not executed' message
355                 else
356                 {
357                         echo '<li>', $message, ' &mdash; <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
358                 } // end if
359
360         }
361
362
363         /**
364          * 
365          *
366          * @param string $table table to check (without prefix)
367          * @param array $columns array of column names included
368          * @return int
369          */
370         function upgrade_checkIfIndexExists($table, $columns)
371         {
372                 // get info for indices from database
373                 $indices = array();
374
375                 $query = 'SHOW INDEX FROM `' . sql_table($table) . '`';
376                 $result = @mysql_query($query);
377
378                 // begin loop: each result object
379                 while ( $object = mysql_fetch_object($result) )
380                 {
381
382                         // begin if: key has not been added to the indeces array yet
383                         if ( !isset($indices[$object->Key_name]) )
384                         {
385                                 $indices[$object->Key_name] = array();
386                         } // end if
387
388                         array_push($indices[$object->Key_name], $object->Column_name);
389                 }
390
391                 // compare each index with parameter
392                 foreach ( $indices as $key_name => $index_columns )
393                 {
394                         $diff = array_diff($index_columns, $columns);
395
396                         if ( count($diff) == 0 )
397                         {
398                                 return 1;
399                         } // end if
400
401                 } // end loop
402
403                 return 0;
404         }
405
406
407         /**
408          * Checks to see if a given table exists
409          *
410          * @param string $table name of table to check existence of
411          * @return bool TRUE if table exists, FALSE otherwise.
412          */
413         function upgrade_checkIfTableExists($table)
414         {
415                 $query = 'SHOW TABLES LIKE `' . sql_table($table) . '`';
416                 $result = @mysql_query($query);
417
418                 // begin if: query executed successfully and one row was returned
419                 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
420                 {
421                         return TRUE;
422                 }
423                 // else: query error or no results returned
424                 else
425                 {
426                         return FALSE;
427                 } // end if
428
429         }
430
431
432         /**
433           * Checks to see if a given configuration value exists
434           *
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.
437           */
438         function upgrade_checkIfCVExists($value)
439         {
440                 $query = 'SELECT `name` FROM `' . sql_table('config') . '` WHERE `name` = "' . $value . '"';
441                 $result = @mysql_query($query);
442
443                 // begin if: query executed successfully and one row was returned
444                 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
445                 {
446                         return TRUE;
447                 }
448                 // else: query error or no results returned
449                 else
450                 {
451                         return FALSE;
452                 } // end if
453
454         }
455
456
457         /**
458           * Checks to see if a given column exists
459           *
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.
463           */
464         function upgrade_checkIfColumnExists($table, $column)
465         {
466                 $query = 'DESC `' . sql_table($table) . '` `' . $column . '`';
467                 $result = @mysql_query($query);
468
469                 // begin if: query executed successfully and one row was returned
470                 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
471                 {
472                         return TRUE;
473                 }
474                 // else: query error or no results returned
475                 else
476                 {
477                         return FALSE;
478                 } // end if
479
480         }