OSDN Git Service

1e63ee2a82aa7a3fb5af8aa91aeff764722d4d95
[nucleus-jp/nucleus-jp-ancient.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  * Some functions common to all upgrade scripts
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2012 The Nucleus Group
18  * @version $Id$
19  * $NucleusJP: upgrade.functions.php,v 1.10 2007/04/26 06:20:19 kimitake Exp $
20  */
21
22 /*************************************************************
23  *       NOTE: With upgrade to 3.6, need to set this to use sql_* API              *
24  **************************************************************/
25
26         include('../../config.php');
27
28         // sql_table function did not exists in nucleus <= 2.0
29         if (!function_exists('sql_table'))
30         {
31                 function sql_table($name) {
32                         return 'nucleus_' . $name;
33                 }
34         }
35
36         //intGetVar did not exist in very early versions
37         if (!function_exists('intGetVar')) {
38                 function intGetVar($name) {
39                         if (defined($_GET)) {
40                                 return intval($_GET[$name]);
41                         } else {
42                                 global $HTTP_GET_VARS;
43                                 return intval($HTTP_GET_VARS[$name]);
44                         }
45                 }
46         }
47
48         function upgrade_checkinstall($version) {
49                 $installed = 0;
50
51                 switch($version) {
52                         case '95':
53                                 $query = 'SELECT bconvertbreaks FROM '.sql_table('blog').' LIMIT 1';
54                                 $minrows = -1;
55                                 break;
56                         case '96':
57                                 $query = 'SELECT cip FROM '.sql_table('comment').' LIMIT 1';
58                                 $minrows = -1;
59                                 break;
60                         case '100':
61                                 $query = 'SELECT mcookiekey FROM '.sql_table('member').' LIMIT 1';
62                                 $minrows = -1;
63                                 break;
64                         case '110':
65                                 $query = 'SELECT bnotifytype FROM '.sql_table('blog').' LIMIT 1';
66                                 $minrows = -1;
67                                 break;
68                         case '150':
69                                 $query = 'SELECT * FROM '.sql_table('plugin_option').' LIMIT 1';
70                                 $minrows = -1;
71                                 break;
72                         case '200':
73                                 $query = 'SELECT sdincpref FROM '.sql_table('skin_desc').' LIMIT 1';
74                                 $minrows = -1;
75                                 break;
76                         // dev only (v2.2)
77                         case '220':
78                                 $query = 'SELECT oid FROM '.sql_table('plugin_option_desc').' LIMIT 1';
79                                 $minrows = -1;
80                                 break;
81                         // v2.5 beta
82                         case '240':
83                                 $query = 'SELECT bincludesearch FROM ' . sql_table('blog') . ' LIMIT 1';
84                                 $minrows = -1;
85                                 break;
86                         case '250':
87                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 250 LIMIT 1';
88                                 $minrows = 1;
89                                 break;
90                         case '300':
91                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 300 LIMIT 1';
92                                 $minrows = 1;
93                                 break;
94                         case '310':
95                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 310 LIMIT 1';
96                                 $minrows = 1;
97                                 break;
98                         case '320':
99                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 320 LIMIT 1';
100                                 $minrows = 1;
101                                 break;
102                         case '330':
103                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 330 LIMIT 1';
104                                 $minrows = 1;
105                                 break;
106                         case '331':
107                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 331 LIMIT 1';
108                                 $minrows = 1;
109                                 break;
110                         case '340':
111                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 340 LIMIT 1';
112                                 $minrows = 1;
113                                 break;
114                         case '350':
115                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 350 LIMIT 1';
116                                 $minrows = 1;
117                                 break;
118                         case '360':
119                                 $query = 'SELECT * FROM '.sql_table('config').' WHERE name=\'DatabaseVersion\' and value >= 360 LIMIT 1';
120                                 $minrows = 1;
121                                 break;
122                 }
123                 
124                 $res = mysql_query($query);
125                 $installed = ($res != 0) && (mysql_num_rows($res) >= $minrows);
126                 
127                 return $installed;
128         }
129
130
131         /** this function gets the nucleus version, even if the getNucleusVersion
132          * function does not exist yet
133          * return 96 for all versions < 100
134          */
135         function upgrade_getNucleusVersion() {
136                 if (!function_exists('getNucleusVersion')) return 96;
137                 return getNucleusVersion();
138         }
139
140         function upgrade_showLogin($type) {
141                 upgrade_head();
142         ?>
143                 <h1>まずはログインして下さい</h1>
144                 <p>下記の情報を入力して下さい:</p>
145
146                 <form method="post" action="<?php echo $type?>">
147
148                         <ul>
149                                 <li>名前: <input name="login" /></li>
150                                 <li>パスワード <input name="password" type="password" /></li>
151                         </ul>
152
153                         <p>
154                                 <input name="action" value="login" type="hidden" />
155                                 <input type="submit" value="ログイン" />
156                         </p>
157
158                 </form>
159         <?php      upgrade_foot();
160                 exit;
161         }
162
163         function upgrade_head() {
164         ?>
165                         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
166                         <html xmlns="http://www.w3.org/1999/xhtml">
167                         <head>
168                                 <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
169                                 <title>Nucleus アップグレード</title>
170 <?php if (file_exists("../styles/manual.css")) { ?>
171                                 <link rel="stylesheet" href="../styles/manual.css" type="text/css" />
172 <?php }else{ ?>
173                                 <style type="text/css"><!--
174                                         .warning {
175                                                 color: red;
176                                         }
177                                         .ok {
178                                                 color: green;
179                                         }
180                                 --></style>
181 <?php } ?>
182                         </head>
183                         <body>
184         <?php   }
185
186         function upgrade_foot() {
187         ?>
188                         </body>
189                         </html>
190         <?php   }
191
192         function upgrade_error($msg) {
193                 upgrade_head();
194                 ?>
195                 <h1>エラー!</h1>
196
197                 <p>メッセージは以下の通り:</p>
198
199                 <blockquote><div>
200                 <?php echo $msg?>
201                 </div></blockquote>
202
203                 <p><a href="index.php" onclick="history.back();">戻る</a></p>
204                 <?php
205                 upgrade_foot();
206                 exit;
207         }
208
209
210         function upgrade_start() {
211                 global $upgrade_failures;
212                 $upgrade_failures = 0;
213
214                 upgrade_head();
215                 ?>
216                 <h1>アップグレードの実行</h1>
217                 <ul>
218                 <?php   }
219
220         function upgrade_end($msg = "") {
221                 global $upgrade_failures;
222                 $from = intGetVar('from');
223                 if ($upgrade_failures > 0)
224                         $msg = "いくつかのデータベース操作に失敗しました。もし以前にこのアップグレードスクリプトを実行していたのであれば、問題ないと思われます。";
225
226                 ?>
227                 </ul>
228
229                 <h1>アップグレード完了!</h1>
230
231                 <p><?php echo $msg?></p>
232
233                 <p><a href="index.php?from=<?php echo $from; ?>">アップグレード最初のページ</a>にもどる</p>
234
235                 <?php
236                 upgrade_foot();
237                 exit;
238         }
239
240         /**
241           * Tries to execute a query, gives a message when failed
242           *
243           * @param friendly name
244           * @param query
245           */
246         function upgrade_query($friendly, $query) {
247                 global $upgrade_failures;
248
249                 echo "<li>$friendly ... ";
250                 $res = mysql_query($query);
251                 if (!$res) {
252                         echo "<span style='color:red'>失敗</span>\n";
253                         echo "<blockquote>失敗の理由: " . mysql_error() . " </blockquote>";
254                         $upgrade_failures++;
255                 } else {
256                         echo "<span style='color:green'>成功!</span><br />\n";
257                 }
258                 echo "</li>";
259                 return $res;
260         }
261
262         /**
263           * Tries to update database version, gives a message when failed
264           *
265           * @param $version
266           *      Schema version the database has been upgraded to
267           */
268         function update_version($version) {
269                 global $upgrade_failures;
270                 $message='Updating DatabaseVersion in config table to '.$version;
271                 if(0==$upgrade_failures){
272                         $query = 'UPDATE ' . sql_table('config') . ' set value=\''.$version.'\' where name=\'DatabaseVersion\'';
273                         upgrade_query($message, $query);
274                 }else
275                         echo '<li>'.$message.' ... <span class="warning">NOT EXECUTED</span>\n<blockquote>Errors occurred during upgrade process.</blockquote>';
276         }
277
278         /**
279          * @param $table
280          *        table to check (without prefix)
281          * @param $aColumns
282          *        array of column names included
283          */
284         function upgrade_checkIfIndexExists($table, $aColumns) {
285                 // get info for indices from database
286
287                 $aIndices = array();
288                 $query = 'show index from ' . sql_table($table);
289                 $res = mysql_query($query);
290                 while ($o = mysql_fetch_object($res)) {
291                         if (!$aIndices[$o->Key_name]) {
292                                 $aIndices[$o->Key_name] = array();
293                         }
294                         array_push($aIndices[$o->Key_name], $o->Column_name);
295                 }
296
297                 // compare each index with parameter
298                 foreach ($aIndices as $keyName => $aIndexColumns) {
299                         $aDiff = array_diff($aIndexColumns, $aColumns);
300                         if (count($aDiff) == 0) return 1;
301                 }
302
303                 return 0;
304
305         }
306
307         /**
308           * Checks to see if a given table exists
309           *
310           * @param $table
311           *      Name of table to check for existance of
312           *      Uses sql_table internally
313           * @return true if table exists, false otherwise.
314           */
315         function upgrade_checkIfTableExists($table){
316                 $query = 'SHOW TABLES LIKE \''.sql_table($table).'\'';
317                 $res = mysql_query($query);
318                 return ($res != 0) && (mysql_num_rows($res) == 1);
319         }
320
321         /**
322           * Checks to see if a given configuration value exists
323           *
324           * @param $value
325           *      Config value to check for existance of.
326           *      Paramater must be MySQL escaped
327           * @return true if configuration value exists, false otherwise.
328           */
329         function upgrade_checkIfCVExists($value){
330                 $query = 'SELECT name from '.sql_table('config').' WHERE name = \''.$value.'\'';
331                 $res = mysql_query($query);
332                 return ($res != 0) && (mysql_num_rows($res) == 1);
333         }
334
335         /**
336           * Checks to see if a given column exists
337           *
338           * @param $table
339           *      Name of table to check for column in
340           *      Uses sql_table internally
341           * @param $col
342           *      Name of column to check for existance of
343           * @return true if column exists, false otherwise.
344           */
345         function upgrade_checkIfColumnExists($table, $col){
346                 $query = 'DESC `'.sql_table($table).'` `'.$col.'`';
347                 $res = mysql_query($query);
348                 return ($res != 0) && (mysql_num_rows($res) == 1);
349         }
350 ?>