db_insert_index_once.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. include "./_pdo.php";
  3. include "../path.php";
  4. if (PHP_SAPI == "cli") {
  5. echo $argc;
  6. if($argc>=3){
  7. $from=$argv[1];
  8. $to=$argv[2];
  9. echo "From: {$from} To:{$to}";
  10. }
  11. else if($argc>=1){
  12. $from=0;
  13. $to = 216;
  14. echo "生成全部217本书";
  15. }
  16. else{
  17. echo "参数错误";
  18. exit;
  19. }
  20. }
  21. else{
  22. echo "<!DOCTYPE html><html><head></head>";
  23. echo "<body><h2>Insert to Index</h2>";
  24. if(isset($_GET["from"])==false){
  25. echo '<form action="db_insert_index_once.php" method="get">';
  26. echo 'From: <input type="text" name="from" value="0"><br>';
  27. echo 'To: <input type="text" name="to" value="216"><br>';
  28. echo '<input type="submit">';
  29. echo '</form>';
  30. exit;
  31. }
  32. else{
  33. $from=$_GET["from"];
  34. $to=$_GET["to"];
  35. }
  36. }
  37. $g_wordCounter=0;
  38. $g_wordIndexCounter=0;
  39. $iAllWordIndex=array();
  40. $sAllWord=array();
  41. $dirLog=_DIR_LOG_."/";
  42. $dirXmlBase=_DIR_PALI_CSV_."/";
  43. $filelist=array();
  44. $fileNums=0;
  45. $log="";
  46. echo "<h2>$from</h2>";
  47. function getWordEn($strIn){
  48. $out=$strIn;
  49. $out=str_replace("ā","a",$out);
  50. $out=str_replace("ī","i",$out);
  51. $out=str_replace("ū","u",$out);
  52. $out=str_replace("ṅ","n",$out);
  53. $out=str_replace("ñ","n",$out);
  54. $out=str_replace("ṭ","t",$out);
  55. $out=str_replace("ḍ","d",$out);
  56. $out=str_replace("ṇ","n",$out);
  57. $out=str_replace("ḷ","l",$out);
  58. $out=str_replace("ṃ","m",$out);
  59. return($out);
  60. }
  61. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  62. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  63. $fileNums++;
  64. }
  65. }
  66. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  67. $db_file = _FILE_DB_INDEX_;
  68. PDO_Connect("sqlite:$db_file");
  69. for($iFile=$from;$iFile<=$to;$iFile++){
  70. echo "<h3>{$iFile}</h3>";
  71. $FileName=$filelist[$iFile][1].".htm";
  72. $fileId=$filelist[$iFile][0];
  73. $inputFileName=$FileName;
  74. $outputFileNameHead=$filelist[$iFile][1];
  75. $bookId=$filelist[$iFile][2];
  76. $dirXml=$outputFileNameHead."/";
  77. $xmlfile = $inputFileName;
  78. echo "doing:".$xmlfile."<br>";
  79. $log=$log."$iFile,$FileName,open\r\n";
  80. $arrInserString=array();
  81. // 打开文件并读取数据
  82. $irow=0;
  83. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead.".csv", "r"))!==FALSE){
  84. while(($data=fgetcsv($fp,0,','))!==FALSE){
  85. $irow++;
  86. if($irow>1){
  87. $params=$data;
  88. $arrInserString[count($arrInserString)]=$params;
  89. }
  90. }
  91. fclose($fp);
  92. echo "单词表load:".$dirXmlBase.$dirXml.$outputFileNameHead.".csv<br>";
  93. }
  94. else{
  95. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  96. }
  97. // 开始一个事务,关闭自动提交
  98. $PDO->beginTransaction();
  99. $query="INSERT INTO word ('id','book','paragraph','wordindex','bold') VALUES (?,?,?,?,?)";
  100. $stmt = $PDO->prepare($query);
  101. $count=0;
  102. $count1=0;
  103. $sen="";
  104. $sen1="";
  105. $sen_en="";
  106. $sen_count=0;
  107. $book="";
  108. $paragraph="";
  109. foreach($arrInserString as $oneParam){
  110. if($oneParam[5]!=""){
  111. $g_wordCounter++;
  112. $book=substr($oneParam[2],1);
  113. $paragraph=$oneParam[3];
  114. $word=$oneParam[5];
  115. if($oneParam[15]=="bld" ){
  116. $bold=1;
  117. }
  118. else{
  119. $bold=0;
  120. }
  121. if(isset($sAllWord[$word])){
  122. $wordindex=$sAllWord[$word];
  123. $iAllWordIndex[$wordindex][1]++;
  124. if($bold==1){
  125. $iAllWordIndex[$wordindex][3]++;
  126. }
  127. else{
  128. $iAllWordIndex[$wordindex][2]++;
  129. }
  130. }
  131. else{
  132. $wordindex=$g_wordIndexCounter;
  133. $sAllWord[$word]=$g_wordIndexCounter;
  134. $iAllWordIndex[$g_wordIndexCounter][0]=$word;
  135. $iAllWordIndex[$g_wordIndexCounter][1]=1;//all word count
  136. if($bold==1){
  137. $iAllWordIndex[$g_wordIndexCounter][2]=0;
  138. $iAllWordIndex[$g_wordIndexCounter][3]=1;
  139. }
  140. else{
  141. $iAllWordIndex[$g_wordIndexCounter][2]=1;
  142. $iAllWordIndex[$g_wordIndexCounter][3]=0;
  143. }
  144. $g_wordIndexCounter++;
  145. }
  146. $newWord=array($g_wordCounter,$book,$paragraph,$wordindex,$bold);
  147. $stmt->execute($newWord);
  148. $count++;
  149. }
  150. }
  151. // 提交更改
  152. $PDO->commit();
  153. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  154. $error = PDO_ErrorInfo();
  155. echo "error - $error[2] <br>";
  156. $log.="$from, $FileName, error, $error[2] \r\n";
  157. }
  158. else{
  159. echo "updata $count recorders.<br />";
  160. $log.="updata $count recorders.\r\n";
  161. }
  162. }
  163. // 开始一个事务,关闭自动提交
  164. $PDO->beginTransaction();
  165. $query="INSERT INTO wordindex ('id','word','word_en','count','normal','bold','is_base','len') VALUES (?,?,?,?,?,?,?,?)";
  166. $stmt = $PDO->prepare($query);
  167. echo count($iAllWordIndex)."words<br>";
  168. for($iword=0;$iword<count($iAllWordIndex);$iword++){
  169. $wordindex=$iword;
  170. $newWord=array($wordindex,$iAllWordIndex[$iword][0],getWordEn($iAllWordIndex[$iword][0]),$iAllWordIndex[$iword][1],$iAllWordIndex[$iword][2],$iAllWordIndex[$iword][3],0,mb_strlen($iAllWordIndex[$iword][0],"UTF-8"));
  171. //echo "<br>{$newWord[0]}-{$newWord[1]}-{$newWord[2]}-{$newWord[3]}-{$newWord[4]}-{$newWord[5]}-<br />";
  172. $stmt->execute($newWord);
  173. }
  174. // 提交更改
  175. $PDO->commit();
  176. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  177. $error = PDO_ErrorInfo();
  178. echo "error - $error[2] <br>";
  179. $log.="$from, $FileName, error, $error[2] \r\n";
  180. }
  181. else{
  182. echo "updata iword recorders.<br />";
  183. $log.="updata iword recorders.\r\n";
  184. }
  185. $myLogFile = fopen($dirLog."insert_index.log", "a");
  186. fwrite($myLogFile, $log);
  187. fclose($myLogFile);
  188. echo "<h2>齐活!功德无量!all done!</h2>";
  189. ?>
  190. </body>
  191. </html>