OSDN Git Service

tag: sqlite0904
[nucleus-jp/nucleus-plugins.git] / sqlite / tags / sqlite0904 / nucleus / sqlite / convert.php
1 <?php
2 /*******************************************
3 * mysql_xxx => nucleus_mysql_xxx converter *
4 *                              for Nucleus *
5 *     ver 0.8.5   Written by Katsumi       *
6 *******************************************/
7
8 // The license of this script is GPL
9
10 function modifyConfigInstall(){
11         // Modify config.php
12         $pattern=array();
13         $replace=array();
14         array_push($pattern,'/^([\s\S]*?)include([^\(]*?)\(([\s\S]*?)\$([\s\S]+)\'globalfunctions.php\'([\s\S]*?)$/');
15         array_push($replace,'$1include$2($3\$DIR_NUCLEUS.\'sqlite/sqlite.php\'$5'.'$1include$2($3\$$4\'globalfunctions.php\'$5');
16         if (file_exists('./config.php')) {
17                 $before=read_from_file('./config.php');
18                 if (strpos($before,'sqlite.php')===false) {
19                         $before=file('./config.php');
20                         $after='';
21                         foreach($before as $line) $after.=preg_replace($pattern,$replace,$line);
22                         if (!write_to_file(realpath('./config.php'),$after)) ExitWithError();
23                 }
24         }
25         
26         // Modify install.php
27         if (file_exists('./install.php')) {
28                 $before=read_from_file('./install.php');
29                 if (strpos($before,'sqlite.php')===false) {
30                 
31                         // The same pattern/replce is also used for install.php
32                         array_push($pattern,'/aConfPlugsToInstall([\s\S]+)\'NP_SkinFiles\'/i');
33                         array_push($replace,'aConfPlugsToInstall$1\'NP_SkinFiles\',\'NP_SQLite\'');
34                         array_push($pattern,'/<input[^>]+name="mySQL_host"([^\/]+)\/>/i');
35                         array_push($replace,'<input name="mySQL_host" type="hidden" value="dummy" />Not needed for SQLite');
36                         array_push($pattern,'/<input[^>]+name="mySQL_user"([^\/]+)\/>/i');
37                         array_push($replace,'<input name="mySQL_user" type="hidden" value="dummy" />Not needed for SQLite');
38                         array_push($pattern,'/<input[^>]+name="mySQL_password"([^\/]+)\/>/i');
39                         array_push($replace,'<input name="mySQL_password" type="hidden" value="dummy" />Not needed for SQLite');
40                         array_push($pattern,'/<input[^>]+name="mySQL_database"([^\/]+)\/>/i');
41                         array_push($replace,'<input name="mySQL_database" type="hidden" value="dummy" />Not needed for SQLite');
42                         array_push($pattern,'/<input[^>]+name="mySQL_create"([^\)]+)<\/label>/i');
43                         array_push($replace,'Database will be created if not exist.');
44                         $before=file('./install.php');
45                         $after='<?php include("nucleus/sqlite/sqlite.php"); ?>';
46                         foreach($before as $line) $after.=preg_replace($pattern,$replace,$line);
47                         if (!write_to_file(realpath('./install.php'),$after)) ExitWithError();
48                 }
49         }
50         
51         // Modify backup.php
52         if (!modifyBackup('./nucleus/libs/backup.php')) // less than version 3.3
53                 modifyBackup('./nucleus/plugins/backup/NP_BackupAdmin.php','class'); // more than version 3.4 (??)
54
55         // Modify install.sql
56         if (file_exists('./install.sql')) {
57                 $before=file('./install.sql');
58                 $pluginoptiontable=false;
59                 $after='';
60                 foreach($before as $line){
61                         if ($pluginoptiontable) {
62                                 if (preg_match('/TYPE\=MyISAM;/i',$line)) $pluginoptiontable=false;
63                                 else if (preg_match('/`oid`[\s]+int\(11\)[\s]+NOT[\s]+NULL[\s]+auto_increment/i',$line))
64                                         $line=preg_replace('/[\s]+auto_increment/i'," default '0'",$line);
65                         } else {
66                                 if (preg_match('/CREATE[\s]+TABLE[\s]+`nucleus_plugin_option`/i',$line)) $pluginoptiontable=true;
67                         }
68                         if (strlen($after)==0) {
69                                 $replace='PRAGMA synchronous = off;';
70                                 if (!strstr($line,$replace)) $after=preg_replace('/^[^\r\n]*([\r\n]*)$/',$replace.'$1$1',$line);
71                         }
72                         $after.=$line;
73                 }
74                 if ($after!=$before) {
75                         if (!write_to_file(realpath('./install.sql'),$after)) ExitWithError();
76                 }
77         }
78 }
79
80 function modifyBackup($file,$type='global'){
81         if (!file_exists($file)) return false;
82         $before=read_from_file($file);
83         if (strpos($before,'sqlite_restore_execute_queries')===false) {
84                 $pattern='/_execute_queries[\s]*\(([^\)]+)\)[\s]*;/i';
85                 if ($type=='class') $pattern='/\$this->_execute_queries[\s]*\(([^\)]+)\)[\s]*;/i';
86                 $replace='sqlite_restore_execute_queries($1);';
87                 $after=preg_replace($pattern,$replace,$before);
88                 if (!write_to_file(realpath($file),$after)) ExitWithError();
89         }
90         return true;    
91 }
92
93 function seekPhpFiles($dir,&$phpfiles,$myself){
94         if (!is_dir($dir)) return;
95         $d = dir($dir);
96         $dirpath=realpath($d->path);
97         $dirs=array();
98         if (substr($dirpath,-1)!='/' && substr($dirpath,-1)!="\\") $dirpath.='/';
99         while (false !== ($entry = $d->read())) {
100                 if ($entry=='.' || $entry=='..') continue;
101                 if (is_file($dirpath.$entry) && substr($entry,-4)=='.php' && $entry!==$myself) array_push($phpfiles,realpath($dirpath.$entry));
102                 if (is_dir($dirpath.$entry) && $entry!='language' && $entry!='sqlite' )  array_push($dirs,realpath($dirpath.$entry));
103         }
104         $d->close();
105         foreach($dirs as $dir) seekPhpFiles($dir,$phpfiles,$myself);
106 }
107 function changeFunctions($file){
108         if (!is_file($file=realpath($file))) return false;
109         if (!is_readable($file)) {
110                 echo "Cannot read: $file<br />\n";
111                 return false;
112         }
113         $before=read_from_file($file);
114         $after=do_replace($before);
115         if ($before!=$after) return write_to_file($file,$after);
116         return true;
117 }
118 function do_replace(&$text) {
119         // Do this process until change does not occur any more... 
120         // Otherwise, sometime file is not completely modified.
121         // This is indeed the case for BLOG.php.
122         $after=$text;
123         do $after=preg_replace('/([^_])mysql_([_a-z]+)([\s]*?)\(/','$1nucleus_mysql_$2(',($before=$after));
124         while ($before!=$after);
125         return $after;
126 }
127 function read_from_file($file) {
128         if (function_exists('file_get_contents') ) $ret=file_get_contents($file);
129         else {
130                 ob_start();
131                 readfile($file);
132                 $ret=ob_get_contents();
133                 ob_end_clean();
134         }
135         return $ret;
136 }
137 function write_to_file($file,&$text){
138         if (!$handle = @fopen($file, 'w')) {
139                 echo "Cannot change: $file<br />\n";
140                 return false;
141         }
142         fwrite($handle,$text);
143         fclose($handle);
144         echo "Changed: $file<br />\n";
145         return true;
146 }
147 function ExitWithError($text='Error occured.') {
148         echo "$text</body></html>";
149         exit;
150 }
151 ?>