db_insert_index.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <h2>Insert to Index</h2>
  7. <p><a href="index.php">Home</a></p>
  8. <?php
  9. include "./_pdo.php";
  10. include "../path.php";
  11. if(isset($_GET["from"])==false){
  12. ?>
  13. <form action="db_insert_index.php" method="get">
  14. From: <input type="text" name="from" value="0"><br>
  15. To: <input type="text" name="to" value="216"><br>
  16. <input type="submit">
  17. </form>
  18. <?php
  19. return;
  20. }
  21. $from=$_GET["from"];
  22. $to=$_GET["to"];
  23. $dirLog=_DIR_LOG_."/";
  24. $dirDb="db/";
  25. $dirXmlBase=_DIR_PALI_CSV_."/";
  26. $filelist=array();
  27. $fileNums=0;
  28. $log="";
  29. echo "<h2>$from</h2>";
  30. //已经存在的词
  31. $g_wordCounter=0;
  32. $g_wordIndexCounter=0;
  33. $iAllWordIndex=array();
  34. $sAllWord=array();
  35. //新加入的词
  36. $wordindex_max_index = 0;
  37. $aNewWordIndex = array(); //词内容
  38. $sNewWord = array(); //词头索引
  39. global $dbh_word_index;
  40. $dns = "sqlite:"._FILE_DB_WORD_INDEX_;
  41. $dbh_word_index = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  42. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  43. $query = "select id from wordindex where 1 order by id DESC ";
  44. $stmt = $dbh_word_index->prepare($query);
  45. $stmt->execute(array());
  46. $id = $stmt->fetch(PDO::FETCH_ASSOC);
  47. if($id === FALSE){
  48. $wordindex_max_index = 0;
  49. }
  50. else{
  51. $wordindex_max_index =$id["id"];
  52. }
  53. $db_file = _FILE_DB_PALI_INDEX_;
  54. PDO_Connect("sqlite:$db_file");
  55. $query = "select id from word where 1 order by id DESC ";
  56. $stmt = $PDO->prepare($query);
  57. $stmt->execute(array());
  58. $id = $stmt->fetch(PDO::FETCH_ASSOC);
  59. if($id === FALSE){
  60. $g_wordCounter = 0;
  61. }
  62. else{
  63. $g_wordCounter = $id["id"];
  64. }
  65. function dict_lookup($word){
  66. global $dbh_word_index;
  67. $query = "select * from wordindex where \"word\" = ? ";
  68. $stmt = $dbh_word_index->prepare($query);
  69. $stmt->execute(array($word));
  70. return $stmt->fetch(PDO::FETCH_ASSOC);
  71. }
  72. function getWordEn($strIn){
  73. $out=$strIn;
  74. $out=str_replace("ā","a",$out);
  75. $out=str_replace("ī","i",$out);
  76. $out=str_replace("ū","u",$out);
  77. $out=str_replace("ṅ","n",$out);
  78. $out=str_replace("ñ","n",$out);
  79. $out=str_replace("ṭ","t",$out);
  80. $out=str_replace("ḍ","d",$out);
  81. $out=str_replace("ṇ","n",$out);
  82. $out=str_replace("ḷ","l",$out);
  83. $out=str_replace("ṃ","m",$out);
  84. return($out);
  85. }
  86. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  87. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  88. $fileNums++;
  89. }
  90. }
  91. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  92. //for($iFile=$from;$iFile<=$to;$iFile++)
  93. $iFile=$from;
  94. {
  95. $FileName=$filelist[$iFile][1].".htm";
  96. $fileId=$filelist[$iFile][0];
  97. $inputFileName=$FileName;
  98. $outputFileNameHead=$filelist[$iFile][1];
  99. $bookId=$filelist[$iFile][2];
  100. $dirXml=$outputFileNameHead."/";
  101. $xmlfile = $inputFileName;
  102. echo "doing:".$xmlfile."<br>";
  103. $log=$log."$iFile,$FileName,open\r\n";
  104. $arrInserString=array();
  105. // 打开文件并读取数据
  106. $irow=0;
  107. if(($fp=fopen($dirXmlBase.$dirXml.$outputFileNameHead.".csv", "r"))!==FALSE){
  108. while(($data=fgetcsv($fp,0,','))!==FALSE){
  109. $irow++;
  110. if($irow>1){
  111. $params=$data;
  112. $arrInserString[]=$params;
  113. }
  114. }
  115. fclose($fp);
  116. echo "单词表load:".$dirXmlBase.$dirXml.$outputFileNameHead.".csv<br>";
  117. }
  118. else{
  119. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  120. }
  121. // 开始一个事务,关闭自动提交
  122. $PDO->beginTransaction();
  123. $query="INSERT INTO word ('id','book','paragraph','wordindex','bold') VALUES (?,?,?,?,?)";
  124. $stmt = $PDO->prepare($query);
  125. $count=0;
  126. $count1=0;
  127. $sen="";
  128. $sen1="";
  129. $sen_en="";
  130. $sen_count=0;
  131. $book="";
  132. $paragraph="";
  133. foreach($arrInserString as $oneParam){
  134. if($oneParam[5]!=""){
  135. $g_wordCounter++;
  136. $book=substr($oneParam[2],1);
  137. $paragraph=$oneParam[3];
  138. $word=$oneParam[5];
  139. if($oneParam[15]=="bld" ){
  140. $bold=1;
  141. }
  142. else{
  143. $bold=0;
  144. }
  145. if(isset($sAllWord[$word])){
  146. //已经存在的词
  147. $wordindex=$sAllWord[$word];
  148. $iAllWordIndex[$wordindex][1]++;
  149. if($bold==1){
  150. $iAllWordIndex[$wordindex][3]++;
  151. }
  152. else{
  153. $iAllWordIndex[$wordindex][2]++;
  154. }
  155. }
  156. else if(isset($sNewWord[$word])){
  157. //是新家入的词
  158. $wordindex=$sNewWord[$word];
  159. $aNewWordIndex[$wordindex][1]++;
  160. if($bold==1){
  161. $aNewWordIndex[$wordindex][3]++;
  162. }
  163. else{
  164. $aNewWordIndex[$wordindex][2]++;
  165. }
  166. }
  167. else if(($lookup=dict_lookup($word)) !== FALSE){
  168. //在数据库中找到
  169. $wordindex=$lookup["id"];
  170. $sAllWord[$word]=$wordindex;
  171. $iAllWordIndex[$wordindex][0]=$word;
  172. $iAllWordIndex[$wordindex][1]=$lookup["count"] + 1;//all word count
  173. if($bold==1){
  174. $iAllWordIndex[$wordindex][2] = $lookup["normal"] ;
  175. $iAllWordIndex[$wordindex][3] = $lookup["bold"] + 1;
  176. }
  177. else{
  178. $iAllWordIndex[$wordindex][2] = $lookup["normal"] + 1;
  179. $iAllWordIndex[$wordindex][3] = $lookup["bold"] ;
  180. }
  181. }
  182. else{
  183. //数据库里也没找到 怎么办呢?我想呀想 想呀想
  184. $wordindex=$wordindex_max_index + 1;
  185. $sNewWord[$word]=$wordindex;
  186. $aNewWordIndex[$wordindex][0]=$word;
  187. $aNewWordIndex[$wordindex][1]=1;//all word count
  188. if($bold==1){
  189. $aNewWordIndex[$wordindex][2]=0;
  190. $aNewWordIndex[$wordindex][3]=1;
  191. }
  192. else{
  193. $aNewWordIndex[$wordindex][2]=1;
  194. $aNewWordIndex[$wordindex][3]=0;
  195. }
  196. $wordindex_max_index++;
  197. }
  198. $newWord=array($g_wordCounter,$book,$paragraph,$wordindex,$bold);
  199. $stmt->execute($newWord);
  200. $count++;
  201. }
  202. }
  203. // 提交更改
  204. $PDO->commit();
  205. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  206. $error = PDO_ErrorInfo();
  207. echo "error - $error[2] <br>";
  208. $log.="$from, $FileName, error, $error[2] \r\n";
  209. }
  210. else{
  211. echo "updata $count recorders.<br />";
  212. $log.="updata $count recorders.\r\n";
  213. }
  214. }
  215. //更新单词索引表
  216. //首先插入新的词
  217. // 开始一个事务,关闭自动提交
  218. $dbh_word_index->beginTransaction();
  219. $query="INSERT INTO wordindex ('id','word','word_en','count','normal','bold','is_base','len') VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  220. $stmt = $dbh_word_index->prepare($query);
  221. echo "INSERT:".count($aNewWordIndex)."words<br>";
  222. foreach($aNewWordIndex as $wIndex => $info){
  223. $wordindex=$iword;
  224. $newWord=array(
  225. $wIndex,
  226. $info[0],
  227. getWordEn($info[0]),
  228. $info[1],
  229. $info[2],
  230. $info[3],
  231. 0,
  232. mb_strlen($info[0],"UTF-8")
  233. );
  234. $stmt->execute($newWord);
  235. }
  236. // 提交更改
  237. $dbh_word_index->commit();
  238. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  239. $error = $dbh_word_index->errorInfo();
  240. echo "error - $error[2] <br>";
  241. $log.="$from, $FileName, error, $error[2] \r\n";
  242. }
  243. else{
  244. echo "updata iword recorders.<br />";
  245. $log.="updata iword recorders.\r\n";
  246. }
  247. //然后修改已经有的词
  248. // 开始一个事务,关闭自动提交
  249. $dbh_word_index->beginTransaction();
  250. $query="UPDATE wordindex SET count = ? , normal = ? , bold = ? where id = ? ";
  251. $stmt = $dbh_word_index->prepare($query);
  252. echo "UPDATE:".count($iAllWordIndex)."words<br>";
  253. foreach($iAllWordIndex as $wIndex => $info){
  254. $wordindex=$iword;
  255. $newWord=array(
  256. $info[1],
  257. $info[2],
  258. $info[3],
  259. $wIndex
  260. );
  261. $stmt->execute($newWord);
  262. }
  263. // 提交更改
  264. $dbh_word_index->commit();
  265. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  266. $error = $dbh_word_index->errorInfo();
  267. echo "error - $error[2] <br>";
  268. $log.="$from, $FileName, error, $error[2] \r\n";
  269. }
  270. else{
  271. echo "updata iword recorders.<br />";
  272. $log.="updata iword recorders.\r\n";
  273. }
  274. $myLogFile = fopen($dirLog."insert_index.log", "a");
  275. fwrite($myLogFile, $log);
  276. fclose($myLogFile);
  277. ?>
  278. <?php
  279. if($from>=$to){
  280. echo "<h2>齐活!功德无量!all done!</h2>";
  281. }
  282. else{
  283. echo "<script>";
  284. echo "window.location.assign(\"db_insert_index.php?from=".($from+1)."&to=".$to."\")";
  285. echo "</script>";
  286. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  287. }
  288. ?>
  289. </body>
  290. </html>