OSDN Git Service

FIX:アップグレードスクリプトがNucleus3.6上で動作するように修正
[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-2012 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-2012 The Nucleus Group
16  * @version $Id: upgrade.functions.php 1889 2012-06-17 08:46:45Z sakamocchi $
17  */
18
19         /**
20          * Some functions common to all upgrade scripts
21          */
22
23         /*************************************************************
24          * NOTE: With upgrade to 4.0, need to set this to use DB::* 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                         case '400':
145                                 $query = 'SELECT * FROM ' . sql_table('config') . ' WHERE name=\'DatabaseVersion\' and value >= 400 LIMIT 1';
146                                 $minrows = 1;
147                         break;
148                 }
149
150                 $result = mysql_query($query);
151                 $installed = ( $result != 0 ) && (mysql_num_rows($result) >= $minrows);
152                 
153                 return $installed;
154         }
155
156
157         /**
158          * Get the Nucleus version. If getNucleusVersion() doesn't exist, default to version 0.96
159          * @return int
160          */
161         function upgrade_getNucleusVersion()
162         {
163                 if ( !function_exists('getNucleusVersion') )
164                 {
165                         return 96;
166                 }
167
168                 return getNucleusVersion();
169         }
170
171         /**
172          * Show the login form
173          * @param string $action
174          */
175         function upgrade_showLogin($action)
176         {
177                 upgrade_head();
178                 
179                 echo "<h1> Log In </h1>\n";
180                 echo "<p>Please enter your login name and password. </p>\n";
181         
182                 echo "<form method=\"POST\" action=\"{$action}\">\n";
183                 echo "<ul>\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";
186                 echo "</ul>\n";
187                 echo "<p><input type=\"submit\" value=\"Log In\" /></p>\n";
188                 echo "<input name=\"action\" value=\"login\" type=\"hidden\" />\n";
189                 echo "</form>\n";
190                 
191                 upgrade_foot();
192                 exit;
193         }
194
195         /**
196          * Display the HTML header
197          */
198         function upgrade_head()
199         {
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";
202                 echo "<head>\n";
203                 echo "<title> Nucleus Upgrade </title>\n";
204                 
205                 if ( file_exists('../documentation/styles/manual.css') )
206                 {
207                         echo "<link rel=\"stylesheet\" href=\"../documentation/styles/manual.css\" type=\"text/css\" />\n";
208                 }
209                 else
210                 {
211                         echo "<style type=\"text/css\">\n";
212                         echo ".warning { color: red; }\n";
213                         echo ".ok { color: green; }\n";
214                         echo "</style>\n";
215                 }
216                 
217                 echo "</head>\n";
218                 echo "<body>\n";
219         }
220
221
222         /**
223          * Display the HTML footer
224          */
225         function upgrade_foot()
226         {
227                 echo '</body></html>';
228         }
229
230
231         /**
232          * Display an error page
233          * @param string $message
234          */
235         function upgrade_error($message)
236         {
237                 upgrade_head();
238 ?>
239                 <h1> Error </h1>
240
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>
244
245 <?php
246                 upgrade_foot();
247                 exit;
248         }
249
250
251         function upgrade_start()
252         {
253                 global $upgrade_failures;
254                 $upgrade_failures = 0;
255
256                 upgrade_head();
257 ?>
258                 <h1> Executing Upgrades </h1>
259
260                 <ul>
261 <?php
262         }
263
264
265         function upgrade_end($message = '')
266         {
267                 global $upgrade_failures;
268
269                 $from = intGetVar('from');
270
271                 if ( $upgrade_failures > 0 )
272                 {
273                         $message = 'Some queries have failed. Try reverting to a backup or reparing things manually, then re-run this script.';
274                 }
275 ?>
276                 </ul>
277
278                 <h1> Upgrade Completed! </h1>
279
280                 <p> <?php echo $message?> </p>
281
282                 <p> Back to the <a href="index.php?from=<?php echo $from; ?>">Upgrades Overview</a> </p>
283
284 <?php
285                 upgrade_foot();
286                 exit;
287         }
288
289
290         /**
291          * Executes a query, displaying the user-friendly explanation and a success / fail message. Query errors are displayed, too.
292          *
293          * @param string $friendly
294          * @param string $query
295          * @return resource (is this return necessary?)
296          */
297         function upgrade_query($friendly, $query)
298         {
299                 global $upgrade_failures;
300
301                 # output the friendly message
302                 echo "<li> $friendly &mdash; ";
303
304                 # execute the query
305                 $result = @mysql_query($query);
306
307                 // begin if: error executing query
308                 if ( $result === FALSE )
309                 {
310                         echo '<span class="warning"> FAILED </span> <br />';
311                         echo 'Error: <code>', mysql_error(), '</code>';
312                         $upgrade_failures++;
313                 }
314                 // else: query was successful
315                 else
316                 {
317                         echo '<span class="ok"> SUCCESS! </span>';
318                 } // end if
319
320                 echo '</li>', "\n";
321                 return $result;
322         }
323
324
325         /**
326           * Tries to update database version, gives a message when failed
327           *
328           * @param $version
329           *     Schema version the database has been upgraded to
330           */
331         function update_version($version)
332         {
333                 global $upgrade_failures;
334
335                 $message = 'Updating DatabaseVersion in config table to ' . $version;
336
337                 // begin if: no upgrade failures; update the database version in the config table
338                 if ( $upgrade_failures == 0 )
339                 {
340                         $query = 'UPDATE %s SET value = "%s" WHERE name = "DatabaseVersion";';
341                         
342                         $query = sprintf($query, sql_table('config'), $version);
343                         upgrade_query($message, $query);
344                 }
345                 // else: display 'not executed' message
346                 else
347                 {
348                         echo '<li>', $message, ' &mdash; <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
349                 } // end if
350
351         }
352
353
354         /**
355          * 
356          *
357          * @param string $table table to check (without prefix)
358          * @param array $columns array of column names included
359          * @return int
360          */
361         function upgrade_checkIfIndexExists($table, $columns)
362         {
363                 // get info for indices from database
364                 $indices = array();
365
366                 $query = 'SHOW INDEX FROM ' . sql_table($table);
367                 $result = @mysql_query($query);
368
369                 // begin loop: each result object
370                 while ( $object = mysql_fetch_object($result) )
371                 {
372
373                         // begin if: key has not been added to the indeces array yet
374                         if ( !isset($indices[$object->Key_name]) )
375                         {
376                                 $indices[$object->Key_name] = array();
377                         } // end if
378
379                         array_push($indices[$object->Key_name], $object->Column_name);
380                 }
381
382                 // compare each index with parameter
383                 foreach ( $indices as $key_name => $index_columns )
384                 {
385                         $diff = array_diff($index_columns, $columns);
386
387                         if ( count($diff) == 0 )
388                         {
389                                 return 1;
390                         } // end if
391
392                 } // end loop
393
394                 return 0;
395         }
396
397
398         /**
399          * Checks to see if a given table exists
400          *
401          * @param string $table name of table to check existence of
402          * @return bool TRUE if table exists, FALSE otherwise.
403          */
404         function upgrade_checkIfTableExists($table)
405         {
406                 $query = 'SHOW TABLES LIKE ' . sql_table($table);
407                 $result = @mysql_query($query);
408
409                 // begin if: query executed successfully and one row was returned
410                 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
411                 {
412                         return TRUE;
413                 }
414                 // else: query error or no results returned
415                 else
416                 {
417                         return FALSE;
418                 } // end if
419
420         }
421
422
423         /**
424           * Checks to see if a given configuration value exists
425           *
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.
428           */
429         function upgrade_checkIfCVExists($value)
430         {
431                 $query = 'SELECT name FROM ' . sql_table('config') . ' WHERE name = "' . $value . '"';
432                 $result = @mysql_query($query);
433
434                 // begin if: query executed successfully and one row was returned
435                 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
436                 {
437                         return TRUE;
438                 }
439                 // else: query error or no results returned
440                 else
441                 {
442                         return FALSE;
443                 } // end if
444
445         }
446
447
448         /**
449           * Checks to see if a given column exists
450           *
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.
454           */
455         function upgrade_checkIfColumnExists($table, $column)
456         {
457                 $query = 'DESC ' . sql_table($table) . ' ' . $column;
458                 $result = @mysql_query($query);
459
460                 // begin if: query executed successfully and one row was returned
461                 if ( ($result !== FALSE) && (@mysql_num_rows($result) == 1) )
462                 {
463                         return TRUE;
464                 }
465                 // else: query error or no results returned
466                 else
467                 {
468                         return FALSE;
469                 } // end if
470
471         }