OSDN Git Service

Merge branch 'skinnable-master'
[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 <<<<<<< HEAD
151                 $result = DB::getResult($query);
152                 $installed = ( $result !== FALSE ) && ($result->rowCount() >= $minrows);
153
154 =======
155                 $result = mysql_query($query);
156                 $installed = ( $result != 0 ) && (mysql_num_rows($result) >= $minrows);
157                 
158 >>>>>>> skinnable-master
159                 return $installed;
160         }
161
162
163         /**
164          * Get the Nucleus version. If getNucleusVersion() doesn't exist, default to version 0.96
165          * @return int
166          */
167         function upgrade_getNucleusVersion()
168         {
169                 if ( !function_exists('getNucleusVersion') )
170                 {
171                         return 96;
172                 }
173
174                 return getNucleusVersion();
175         }
176
177         /**
178          * Show the login form
179          * @param string $action
180          */
181         function upgrade_showLogin($action)
182         {
183                 upgrade_head();
184                 
185                 echo "<h1> Log In </h1>\n";
186                 echo "<p>Please enter your login name and password. </p>\n";
187         
188                 echo "<form method=\"POST\" action=\"{$action}\">\n";
189                 echo "<ul>\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";
192                 echo "</ul>\n";
193                 echo "<p><input type=\"submit\" value=\"Log In\" /></p>\n";
194                 echo "<input name=\"action\" value=\"login\" type=\"hidden\" />\n";
195                 echo "</form>\n";
196                 
197                 upgrade_foot();
198                 exit;
199         }
200
201         /**
202          * Display the HTML header
203          */
204         function upgrade_head()
205         {
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";
208                 echo "<head>\n";
209                 echo "<title> Nucleus Upgrade </title>\n";
210                 
211                 if ( file_exists('../documentation/styles/manual.css') )
212                 {
213                         echo "<link rel=\"stylesheet\" href=\"../documentation/styles/manual.css\" type=\"text/css\" />\n";
214                 }
215                 else
216                 {
217                         echo "<style type=\"text/css\">\n";
218                         echo ".warning { color: red; }\n";
219                         echo ".ok { color: green; }\n";
220                         echo "</style>\n";
221                 }
222                 
223                 echo "</head>\n";
224                 echo "<body>\n";
225         }
226
227
228         /**
229          * Display the HTML footer
230          */
231         function upgrade_foot()
232         {
233                 echo '</body></html>';
234         }
235
236
237         /**
238          * Display an error page
239          * @param string $message
240          */
241         function upgrade_error($message)
242         {
243                 upgrade_head();
244 ?>
245                 <h1> Error </h1>
246
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>
250
251 <?php
252                 upgrade_foot();
253                 exit;
254         }
255
256
257         function upgrade_start()
258         {
259                 global $upgrade_failures;
260                 $upgrade_failures = 0;
261
262                 upgrade_head();
263 ?>
264                 <h1> Executing Upgrades </h1>
265
266                 <ul>
267 <?php
268         }
269
270
271         function upgrade_end($message = '')
272         {
273                 global $upgrade_failures;
274
275                 $from = intGetVar('from');
276
277                 if ( $upgrade_failures > 0 )
278                 {
279                         $message = 'Some queries have failed. Try reverting to a backup or reparing things manually, then re-run this script.';
280                 }
281 ?>
282                 </ul>
283
284                 <h1> Upgrade Completed! </h1>
285
286                 <p> <?php echo $message?> </p>
287
288                 <p> Back to the <a href="index.php?from=<?php echo $from; ?>">Upgrades Overview</a> </p>
289
290 <?php
291                 upgrade_foot();
292                 exit;
293         }
294
295
296         /**
297          * Executes a query, displaying the user-friendly explanation and a success / fail message. Query errors are displayed, too.
298          *
299          * @param string $friendly
300          * @param string $query
301          * @return resource (is this return necessary?)
302          */
303         function upgrade_query($friendly, $query)
304         {
305                 global $upgrade_failures;
306
307                 # output the friendly message
308                 echo "<li> $friendly &mdash; ";
309
310                 # execute the query
311                 $result = @DB::execute($query);
312
313                 // begin if: error executing query
314                 if ( $result === FALSE )
315                 {
316                         echo '<span class="warning"> FAILED </span> <br />';
317                         $err = DB::getError();
318                         echo 'Error: <code>', $err[2], '</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 SET value = "%s" WHERE name = "DatabaseVersion";';
348                         
349                         $query = sprintf($query, sql_table('config'), $version);
350                         upgrade_query($message, $query);
351                 }
352                 // else: display 'not executed' message
353                 else
354                 {
355                         echo '<li>', $message, ' &mdash; <span class="warning">NOT EXECUTED</span> Errors occurred during upgrade process. </li>';
356                 } // end if
357
358         }
359
360
361         /**
362          * 
363          *
364          * @param string $table table to check (without prefix)
365          * @param array $columns array of column names included
366          * @return int
367          */
368         function upgrade_checkIfIndexExists($table, $columns)
369         {
370                 // get info for indices from database
371                 $indices = array();
372
373                 $query = 'SHOW INDEX FROM ' . sql_table($table);
374                 $result = @DB::getResult($query);
375
376                 // begin loop: each result object
377                 foreach ( $result as $row )
378                 {
379
380                         // begin if: key has not been added to the indeces array yet
381                         if ( !isset($indices[$row['Key_name']]) )
382                         {
383                                 $indices[$row['Key_name']] = array();
384                         } // end if
385
386                         array_push($indices[$row['Key_name']], $row['Column_name']);
387                 }
388
389                 // compare each index with parameter
390                 foreach ( $indices as $key_name => $index_columns )
391                 {
392                         $diff = array_diff($index_columns, $columns);
393
394                         if ( count($diff) == 0 )
395                         {
396                                 return 1;
397                         } // end if
398
399                 } // end loop
400
401                 return 0;
402         }
403
404
405         /**
406          * Checks to see if a given table exists
407          *
408          * @param string $table name of table to check existence of
409          * @return bool TRUE if table exists, FALSE otherwise.
410          */
411         function upgrade_checkIfTableExists($table)
412         {
413                 $query = 'SHOW TABLES LIKE ' . sql_table($table);
414                 $result = DB::getResult($query);
415
416                 // begin if: query executed successfully and one row was returned
417                 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
418                 {
419                         return TRUE;
420                 }
421                 // else: query error or no results returned
422                 else
423                 {
424                         return FALSE;
425                 } // end if
426
427         }
428
429
430         /**
431           * Checks to see if a given configuration value exists
432           *
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.
435           */
436         function upgrade_checkIfCVExists($value)
437         {
438                 $query = 'SELECT name FROM ' . sql_table('config') . ' WHERE name = "' . $value . '"';
439                 $result = DB::getResult($query);
440
441                 // begin if: query executed successfully and one row was returned
442                 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
443                 {
444                         return TRUE;
445                 }
446                 // else: query error or no results returned
447                 else
448                 {
449                         return FALSE;
450                 } // end if
451
452         }
453
454
455         /**
456           * Checks to see if a given column exists
457           *
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.
461           */
462         function upgrade_checkIfColumnExists($table, $column)
463         {
464                 $query = 'DESC ' . sql_table($table) . ' ' . $column;
465                 $result = DB::getResult($query);
466
467                 // begin if: query executed successfully and one row was returned
468                 if ( ($result !== FALSE) && ($result->rowCount() == 1) )
469                 {
470                         return TRUE;
471                 }
472                 // else: query error or no results returned
473                 else
474                 {
475                         return FALSE;
476                 } // end if
477
478         }