db_update_palitext.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. require_once "install_head.php";
  3. require_once "../public/_pdo.php";
  4. ?>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. </head>
  9. <body>
  10. <h2>Update Pali Text Use CSV</h2>
  11. <p><a href="index.php">Home</a></p>
  12. <?php
  13. if(isset($_GET["from"])==false){
  14. ?>
  15. <form action="db_update_palitext.php" method="get">
  16. From: <input type="text" name="from" value="0"><br>
  17. To: <input type="text" name="to" value="216"><br>
  18. <input type="submit">
  19. </form>
  20. <?php
  21. return;
  22. }
  23. $from=$_GET["from"];
  24. $to=$_GET["to"];
  25. $filelist=array();
  26. $fileNums=0;
  27. $log="";
  28. echo "<h2>$from</h2>";
  29. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  30. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  31. $fileNums++;
  32. }
  33. }
  34. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  35. $FileName=$filelist[$from][1].".htm";
  36. $fileId=$filelist[$from][0];
  37. $fileId=$filelist[$from][0];
  38. $dirLog=_DIR_LOG_."/";
  39. $dirDb="/";
  40. $inputFileName=$FileName;
  41. $outputFileNameHead=$filelist[$from][1];
  42. $bookId=$filelist[$from][2];
  43. $vriParNum=0;
  44. $wordOrder=1;
  45. $dirXmlBase=_DIR_PALI_CSV_."/";
  46. $dirPaliTextBase=_DIR_PALI_HTML_."/";
  47. $dirXml=$outputFileNameHead."/";
  48. $xmlfile = $inputFileName;
  49. echo "doing:".$xmlfile."<br>";
  50. $log=$log.date("Y-m-d h:i:sa").",$from,$FileName,open\r\n";
  51. $arrInserString=array();
  52. // 打开vri html文件并读取数据
  53. $pali_text_array=array();//vri text
  54. if(($fpPaliText=fopen($dirPaliTextBase.$xmlfile, "r"))!==FALSE){
  55. while(($data=fgets($fpPaliText))!==FALSE){
  56. array_push($pali_text_array,$data);
  57. }
  58. fclose($fpPaliText);
  59. echo "pali text load:".$dirPaliTextBase.$xmlfile."<br>";
  60. }
  61. else{
  62. echo "can not pali text file. filename=".$dirPaliTextBase.$xmlfile;
  63. }
  64. // 打开csv文件并读取数据
  65. $inputRow=0;
  66. if(($fp=fopen(_DIR_PALI_TITLE_."/".($from+1)."_title.csv", "r"))!==FALSE){
  67. while(($data=fgetcsv($fp,0,','))!==FALSE){
  68. if($inputRow>0){
  69. $params=$data;
  70. array_push($arrInserString,$params);
  71. }
  72. $inputRow++;
  73. }
  74. fclose($fp);
  75. echo "单词表load:".$dirXmlBase.$dirXml.$outputFileNameHead.".csv<br>";
  76. }
  77. else{
  78. echo "can not open csv file. filename=".$dirXmlBase.$dirXml.$outputFileNameHead.".csv";
  79. }
  80. if((count($arrInserString)) != count($pali_text_array)-2){
  81. $log=$log."$from, $FileName,error,文件行数不匹配 csv = ".(count($arrInserString)-1) ." pali_text_array=".(count($pali_text_array)-2)." \r\n";
  82. }
  83. $book=$from+1;
  84. //计算段落信息,如上一段
  85. PDO_Connect("sqlite:"._FILE_DB_PALITEXT_);
  86. $query="select * from pali_text where book = '$book' ";
  87. $title_data = PDO_FetchAll($query);
  88. echo "Paragraph Count:".count($title_data)."<br>";
  89. $paragraph_count = count($title_data);
  90. // 开始一个事务,关闭自动提交
  91. $PDO->beginTransaction();
  92. $query ="UPDATE pali_text SET level = ? , toc = ? , chapter_len = ? , next_chapter = ?, prev_chapter=? , parent= ? , chapter_strlen = ? WHERE book=? and paragraph=?";
  93. $stmt = $PDO->prepare($query);
  94. $paragraph_info = array();
  95. array_push($paragraph_info,array($from,-1,$paragraph_count,-1,-1,-1));
  96. for($iPar=0;$iPar < count($title_data); $iPar++){
  97. $title_data[$iPar]["level"] = $arrInserString[$iPar][3];
  98. }
  99. for($iPar=0;$iPar < count($title_data); $iPar++){
  100. $book=$from+1;
  101. $paragraph=$title_data[$iPar]["paragraph"];
  102. if((int)$title_data[$iPar]["level"]==8){
  103. $title_data[$iPar]["level"]=100;
  104. }
  105. $curr_level=(int)$title_data[$iPar]["level"];
  106. # j计算这个chapter的段落数量
  107. $length=-1;
  108. for($iPar1=$iPar+1;$iPar1<count($title_data); $iPar1++){
  109. $thislevel = (int)$title_data[$iPar1]["level"];
  110. if( $thislevel <= $curr_level){
  111. $length=(int)$title_data[$iPar1]["paragraph"]-$paragraph;
  112. break;
  113. }
  114. }
  115. if($length==-1){
  116. $length=$paragraph_count-$paragraph+1;
  117. }
  118. $prev=-1;
  119. if($iPar>0){
  120. for($iPar1=$iPar-1;$iPar1>=0; $iPar1--){
  121. if($title_data[$iPar1]["level"]==$curr_level){
  122. $prev=$title_data[$iPar1]["paragraph"];
  123. break;
  124. }
  125. }
  126. }
  127. $next=-1;
  128. if($iPar<count($title_data)-1){
  129. for($iPar1=$iPar+1;$iPar1<count($title_data); $iPar1++){
  130. if($title_data[$iPar1]["level"]==$curr_level){
  131. $next=$title_data[$iPar1]["paragraph"];
  132. break;
  133. }
  134. }
  135. }
  136. $parent=-1;
  137. if($iPar>0){
  138. for($iPar1=$iPar-1;$iPar1>=0; $iPar1--){
  139. if($title_data[$iPar1]["level"]<$curr_level){
  140. $parent=$title_data[$iPar1]["paragraph"];
  141. break;
  142. }
  143. }
  144. }
  145. //计算章节包含总字符数
  146. $iChapter_strlen = 0;
  147. for($i = $iPar ; $i < $iPar+$length; $i++ ){
  148. $iChapter_strlen += $title_data[$i]["lenght"];
  149. }
  150. $newData=array(
  151. $arrInserString[$iPar][3],
  152. $arrInserString[$iPar][5],
  153. $length,
  154. $next,
  155. $prev,
  156. $parent,
  157. $iChapter_strlen,
  158. $book,
  159. $paragraph
  160. );
  161. $stmt->execute($newData);
  162. if($curr_level>0 && $curr_level<8){
  163. array_push($paragraph_info,array($book,$paragraph,$length,$prev,$next,$parent));
  164. }
  165. }
  166. // 提交更改
  167. $PDO->commit();
  168. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  169. $error = PDO_ErrorInfo();
  170. echo "error - $error[2] <br>";
  171. $log=$log."$from, $FileName, error, $error[2] \r\n";
  172. }
  173. else{
  174. $count=count($title_data);
  175. echo "updata $count paragraph info recorders.<br>";
  176. echo count($paragraph_info)." Heading<br>";
  177. }
  178. //段落信息结束
  179. $myLogFile = fopen(_DIR_LOG_."/db_update_palitext.log", "a");
  180. fwrite($myLogFile, $log);
  181. fclose($myLogFile);
  182. ?>
  183. <?php
  184. if($from==$to){
  185. echo "<h2>齐活!功德无量!all done!</h2>";
  186. }
  187. else{
  188. echo "<script>";
  189. echo "window.location.assign(\"db_update_palitext.php?from=".($from+1)."&to=".$to."\")";
  190. echo "</script>";
  191. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  192. }
  193. ?>
  194. </body>
  195. </html>