OSDN Git Service

0e2f667a5fa5232c9528f987fae4ccf0dc7ab7d4
[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 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 = DB::getResult($query);
151                 $installed = ( $result !== FALSE ) && ($result->rowCount() >= $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 = @DB::execute($query);
306
307                 // begin if: error executing query
308                 if ( $result === FALSE )
309                 {
310                         echo '<span class="warning"> FAILED </span> <br />';
311                         $err = DB::getError();
312                         echo 'Error: <code>', $err[2], '</code>';
313                         $upgrade_failures++;
314                 }
315                 // else: query was successful
316                 else
317                 {
318                         echo '<span class="ok"> SUCCESS! </span>';
319                 } // end if
320
321                 echo '</li>', "\n";
322                 return $result;
323         }
324
325
326         /**
327           * Tries to update database version, gives a message when failed
328           *
329           * @param $version
330           *     Schema version the database has been upgraded to
331           */
332         function update_version($version)
333         {
334                 global $upgrade_failures;
335
336                 $message = 'Updating DatabaseVersion in config table to ' . $version;
337
338                 // begin if: no upgrade failures; update the database version in the config table
339                 if ( $upgrade_failures == 0 )
340                 {
341                         $query = 'UPDATE %s SET value = "%s" WHERE name = "DatabaseVersion";';
342                         
343                         $query = sprintf($query, sql_table('config'), $version);
344                         upgrade_query($message, $query);
345                 }
346                 // else: display 'not executed' message
347                 else
348                 {
349                         echo '<li>', $message, ' &mdash; <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
350                 } // end if
351
352         }
353
354
355         /**
356          * 
357          *
358          * @param string $table table to check (without prefix)
359          * @param array $columns array of column names included
360          * @return int
361          */
362         function upgrade_checkIfIndexExists($table, $columns)
363         {
364                 // get info for indices from database
365                 $indices = array();
366
367                 $query = 'SHOW INDEX FROM ' . sql_table($table);
368                 $result = @DB::getResult($query);
369
370                 // begin loop: each result object
371                 foreach ( $result as $row )
372                 {
373
374                         // begin if: key has not been added to the indeces array yet
375                         if ( !isset($indices[$row['Key_name']]) )
376                         {
377                                 $indices[$row['Key_name']] = array();
378                         } // end if
379
380                         array_push($indices[$row['Key_name']], $row['Column_name']);
381                 }
382
383                 // compare each index with parameter
384                 foreach ( $indices as $key_name => $index_columns )
385                 {
386                         $diff = array_diff($index_columns, $columns);
387
388                         if ( count($diff) == 0 )
389                         {
390                                 return 1;
391                         } // end if
392
393                 } // end loop
394
395                 return 0;
396         }
397
398
399         /**
400          * Checks to see if a given table exists
401          *
402          * @param string $table name of table to check existence of
403          * @return bool TRUE if table exists, FALSE otherwise.
404          */
405         function upgrade_checkIfTableExists($table)
406         {
407                 $query = 'SHOW TABLES LIKE ' . sql_table($table);
408                 $result = DB::getResult($query);
409
410                 // begin if: query executed successfully and one row was returned
411                 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
412                 {
413                         return TRUE;
414                 }
415                 // else: query error or no results returned
416                 else
417                 {
418                         return FALSE;
419                 } // end if
420
421         }
422
423
424         /**
425           * Checks to see if a given configuration value exists
426           *
427           * @param string $value config value to check for existance of (paramater must be MySQL escaped already)
428           * @return bool TRUE if configuration value exists, FALSE otherwise.
429           */
430         function upgrade_checkIfCVExists($value)
431         {
432                 $query = 'SELECT name FROM ' . sql_table('config') . ' WHERE name = "' . $value . '"';
433                 $result = DB::getResult($query);
434
435                 // begin if: query executed successfully and one row was returned
436                 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
437                 {
438                         return TRUE;
439                 }
440                 // else: query error or no results returned
441                 else
442                 {
443                         return FALSE;
444                 } // end if
445
446         }
447
448
449         /**
450           * Checks to see if a given column exists
451           *
452           * @param string $table name of table to check for column in
453           * @param string $column name of column to check for existance of
454           * @return bool TRUE if column exists, FALSE otherwise.
455           */
456         function upgrade_checkIfColumnExists($table, $column)
457         {
458                 $query = 'DESC ' . sql_table($table) . ' ' . $column;
459                 $result = DB::getResult($query);
460
461                 // begin if: query executed successfully and one row was returned
462                 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
463                 {
464                         return TRUE;
465                 }
466                 // else: query error or no results returned
467                 else
468                 {
469                         return FALSE;
470                 } // end if
471
472         }