SQLite-4
2006年3月8日
Nucleus 3.22 en/ja を、SQLite用に自動変換するスクリプト。ほぼできあがったので、ここにメモ。
'***********************************************
'* mysql_xxx() => sqlite_mysql_xxx() converter *
'* for Nucleus *
'* ver 0.2 Written by Katsumi *
'***********************************************
'
' The license of this script is GPL
Option Explicit
Dim sfo,fHandle,buff,ret,T,temp,curdir
set sfo=CreateObject("Scripting.FileSystemObject")
curdir=CreateObject("WScript.Shell").CurrentDirectory
if right(curdir,1)<>"\" then curdir=curdir+"\"
'//Avoide 2nd round
if sfo.FileExists("convert-result.txt") then
msgbox "This conversion has been probably done!",vbCritical
wscript.quit
end if
'//Check the language used in install.php
Dim jae,jau,lang,probably,encode
set fHandle=sfo.OpenTextFile("install.php")
jae=0
jau=0
Do Until fHandle.AtEndOfStream
buff=fHandle.ReadLine
if instr(1,buff,"charset=EUC-JP",1) then jae=jae+1
if instr(1,buff,"charset=UTF-8",1) then jau=jau+1
Loop
fHandle.close
'//Normally, the score of jae/jau is 4 if japanese is used
if (jae=0) and (jau=0) then
lang="english"
elseif 2<jae and jau<jae then
lang="japanese-euc"
elseif 2<jau and jae<jau then
lang="japanese-utf8"
elseif jau<jae then
lang="japanese-euc"
probably=" (not sure)"
elseif jae<jau then
lang="japanese-utf8"
probably=" probably (not sure)"
else
msgbox "Unknown language"
wscript.quit
end if
'//Set the encoding mode
select case lang
case "japanese-euc"
encode="euc-jp"
case "japanese-utf8"
encode="utf-8"
case else
encode="iso-8859-1"
end select
'//Prompt asking user to start process.
buff="Used language is "+lang+probably+vbcrlf+vbcrlf
buff=buff+"Continue?"+vbcrlf+"This may take a few minutes."
ret=msgbox(buff,vbyesno,"Scores: EUC-JP="+cstr(jae)+", UTF-8="+cstr(jau))
if ret=vbno then wscript.quit
'//Modify install.php
Dim firstLine,mySQLSettings(3),value,log
if encode<>"iso-8859-1" then call convertCode("install.php",encode,"shift_jis")
T=""
log=curdir+"install.php"+vbcrlf
mySQLSettings(0)="mySQL_host"
mySQLSettings(1)="mySQL_user"
mySQLSettings(2)="mySQL_password"
mySQLSettings(3)="mySQL_database"
firstLine=true
set fHandle=sfo.OpenTextFile("install.php")
Do Until fHandle.AtEndOfStream
buff=fHandle.ReadLine+vbcrlf
if firstLine then
firstLine=false
temp="include('nucleus/sqlite/sqlite.php');"+vbcrlf
buff=buff+temp
log=log+temp
end if
if 0<instr(1,buff,"<input name=") then
for each value in mySQLSettings
if 0<instr(1,buff,value) then
buff=ereg_replace("<input name="""+value+"""[\s\S]*?/>", _
"<input name="""+value+""" type=""hidden"" value=""dummy"" />",buff)
log=log+buff
end if
next
end if
if 0<instr(1,buff,"globalfunctions.php") and 0<instr(1,buff,"include") then
temp=ereg_replace("include[\s]*?\([\s]*?([\\]?)\$DIR_[\s\S]*?globalfunctions.php[^;]*?;", _
"include($1$DIR_NUCLEUS.'sqlite/sqlite.php');",buff)
if temp<>buff then
buff=temp+buff
log=log+temp
end if
end if
buff=ereg_replace("mysql_query","sqlite_mysql_query",buff)
T=T+buff
Loop
fHandle.close
set fHandle=sfo.OpenTextFile("install.php",2,true)
fHandle.Write T
fHandle.close
'//modify config.php
T=""
log=log+curdir+"config.php"+vbcrlf
set fHandle=sfo.OpenTextFile("config.php")
Do Until fHandle.AtEndOfStream
buff=fHandle.ReadLine+vbcrlf
if 0<instr(1,buff,"globalfunctions.php") and 0<instr(1,buff,"include") then
temp=ereg_replace("include[\s]*?\([\s]*?([\\]?)\$DIR_[\s\S]*?globalfunctions.php[^;]*?;", _
"include($1$DIR_NUCLEUS.'sqlite/sqlite.php');",buff)
if temp<>buff then
buff=temp+buff
log=log+temp
end if
end if
T=T+buff
Loop
fHandle.close
set fHandle=sfo.OpenTextFile("config.php",2,true)
fHandle.Write T
fHandle.close
set fHandle=sfo.OpenTextFile("convert-result.txt",8,True) 'Append
fHandle.Write log
fHandle.close
'//Check all PHP file for changing from mysql_xxx() to sqlite_mysql-xxx()
do
T=""
call SearchFolder(curdir)
set fHandle=sfo.OpenTextFile("convert-result.txt",8,True) 'Append
fHandle.Write T
fHandle.close
loop until T=""
'//Return to original encode of install.php
if encode<>"iso-8859-1" then call convertCode("install.php","shift_jis",encode)
MsgBox("Done. You may see the convert-result.txt file.")
WScript.quit
sub searchFolder(fpath)
Dim f1
if right(fpath,1)<>"\" then fpath=fpath+"\"
call checkPhpFiles(fpath)
For Each f1 in sfo.GetFolder(fpath).SubFolders
if f1.name<>"language" and f1.name<>"sqlite" then call searchFolder(fpath+f1.name)
Next
end sub
sub checkPhpFiles(fpath)
Dim f1
For Each f1 in sfo.GetFolder(fpath).Files
if lcase(right(f1.name,4))=".php" then call replaceFunctions(fpath+f1.name)
Next
end sub
sub replaceFunctions(fpath)
Dim phpfile,fHandle,temp
phpfile=""
set fHandle=sfo.OpenTextFile(fpath)
Do Until fHandle.AtEndOfStream
phpfile=phpfile+fHandle.ReadLine+vbcrlf
Loop
fHandle.close
Dim regEx, Match, Matches, RepText
Set regEx = New RegExp
regEx.Pattern = "([^_])mysql_([_a-z]*?)([\s]*?)\("
RepText="$1sqlite_mysql_$2("
regEx.IgnoreCase = False
regEx.Global = True
if regEx.Test(phpfile) then
T=T+fpath+vbcrlf
Set Matches = regEx.Execute(phpfile)
For Each Match in Matches
T=T&" "&Match.Value&")"&vbcrlf
Next
set fHandle=sfo.OpenTextFile(fpath,2,true)
fHandle.Write regEx.replace(phpfile,RepText)
fHandle.close
end if
end sub
sub convertCode(fName,enc1,enc2)
Dim Stream,Stream2
Set Stream=CreateObject("ADODB.Stream")
Set Stream2=CreateObject("ADODB.Stream")
Stream.Open
Stream.Type = 2
Stream.Charset = enc1
Stream.LoadFromFile fName
Stream2.Open
Stream2.Charset = enc2
Stream.CopyTo Stream2
Stream2.SaveToFile fName, 2
Stream2.Close
Stream.Close
end sub
function ereg_replace_sub(pattern,replacement,T,IgnoreCase)
with (New RegExp)
.Pattern = pattern
.IgnoreCase = IgnoreCase
.Global = True
ereg_replace_sub=.replace(T,replacement)
end with
end function
function ereg_replace(pattern,replacement,T)
ereg_replace=ereg_replace_sub(pattern,replacement,T,false)
end function
function eregi_replace(pattern,replacement,T)
eregi_replace=ereg_replace_sub(pattern,replacement,T,true)
end function