OSDN Git Service

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