2
0

db_insert_templet.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /*
  3. 用拆分好的三藏数据 生成模板库
  4. */
  5. require_once "install_head.php";
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. </head>
  11. <body>
  12. <h2>Insert to DB</h2>
  13. <p><a href="index.php">Home</a></p>
  14. <?php
  15. require_once '../public/_pdo.php';
  16. require_once '../path.php';
  17. if (isset($_GET["from"]) == false) {
  18. ?>
  19. <form action="db_insert_templet.php" method="get">
  20. From: <input type="text" value="0" name="from"><br>
  21. To: <input type="text" value="216" name="to"><br>
  22. <input type="submit">
  23. </form>
  24. <?php
  25. return;
  26. }
  27. $from = $_GET["from"];
  28. $to = $_GET["to"];
  29. $filelist = array();
  30. $fileNums = 0;
  31. $log = "";
  32. echo "<h2>$from</h2>";
  33. if (($handle = fopen("filelist.csv", 'r')) !== false) {
  34. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  35. $fileNums++;
  36. }
  37. }
  38. if ($to == 0 || $to >= $fileNums) {
  39. $to = $fileNums - 1;
  40. }
  41. $FileName = $filelist[$from][1] . ".htm";
  42. $fileId = $filelist[$from][0];
  43. $fileId = $filelist[$from][0];
  44. $dirLog = _DIR_LOG_;
  45. $inputFileName = $FileName;
  46. $outputFileNameHead = $filelist[$from][1];
  47. $bookId = $filelist[$from][2];
  48. $vriParNum = 0;
  49. $wordOrder = 1;
  50. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  51. $dirXml = $outputFileNameHead . "/";
  52. $currChapter = "";
  53. $currParNum = "";
  54. $arrAllWords[0] = array("id", "wid", "book", "paragraph", "word", "real", "type", "gramma", "mean", "note", "part", "partmean", "bmc", "bmt", "un", "style", "vri", "sya", "si", "ka", "pi", "pa", "kam");
  55. $g_wordCounter = 0;
  56. $arrUnWords[0] = array("id", "word", "type", "gramma", "parent", "mean", "note", "part", "partmean", "cf", "state", "delete", "tag", "len");
  57. $g_unWordCounter = 0;
  58. $arrUnPart[0] = "word";
  59. $g_unPartCounter = -1;
  60. /*去掉标点符号的统计*/
  61. $arrAllPaliWordsCount = array();
  62. $g_paliWordCounter = 0;
  63. $g_wordCounterInSutta = 0;
  64. $g_paliWordCountCounter = 0;
  65. $xmlfile = $inputFileName;
  66. echo "doing:" . $xmlfile . "<br>";
  67. $log = $log . "$from,$FileName,open\r\n";
  68. $arrInserString = array();
  69. $db_file = _DIR_PALICANON_TEMPLET_ . "/" . $bookId . '_tpl.db3';
  70. PDO_Connect("sqlite:{$db_file}");
  71. PDO_Execute("DROP TABLE IF EXISTS main;");
  72. $query = "CREATE TABLE 'main' ( 'id' TEXT PRIMARY KEY NOT NULL,
  73. 'book' INTEGER,
  74. 'paragraph' INTEGER,
  75. 'wid' INTEGER,
  76. 'word' TEXT,
  77. 'real' TEXT,
  78. 'type' TEXT,
  79. 'gramma' TEXT,
  80. 'part' TEXT,
  81. 'style' TEXT)";
  82. $stmt = @PDO_Execute($query);
  83. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  84. $error = PDO_ErrorInfo();
  85. print_r($error[2]);
  86. }
  87. PDO_Execute("DROP INDEX IF EXISTS search;");
  88. $query = "CREATE INDEX 'search' ON \"main\" (\"book\", \"paragraph\", \"wid\" ASC)";
  89. $stmt = @PDO_Execute($query);
  90. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  91. $error = PDO_ErrorInfo();
  92. print_r($error[2]);
  93. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  94. }
  95. // 打开文件并读取数据
  96. if (($fp = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
  97. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  98. //id,wid,book,paragraph,word,real,type,gramma,mean,note,part,partmean,bmc,bmt,un,style,vri,sya,si,ka,pi,pa,kam
  99. $params = array($data[0],
  100. mb_substr($data[2], 1),
  101. $data[3],
  102. $data[16],
  103. $data[4],
  104. $data[5],
  105. $data[6],
  106. $data[7],
  107. $data[10],
  108. $data[15]);
  109. $arrInserString[] = $params;
  110. }
  111. fclose($fp);
  112. echo "单词表load:" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv<br>";
  113. } else {
  114. echo "can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv";
  115. }
  116. // 开始一个事务,关闭自动提交
  117. $PDO->beginTransaction();
  118. $query = "INSERT INTO main ('id','book','paragraph','wid','word','real','type','gramma','part','style') VALUES (?,?,?,?,?,?,?,?,?,?)";
  119. $stmt = $PDO->prepare($query);
  120. foreach ($arrInserString as $oneParam) {
  121. $stmt->execute($oneParam);
  122. }
  123. // 提交更改
  124. $PDO->commit();
  125. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  126. $error = PDO_ErrorInfo();
  127. echo "error - $error[2] <br>";
  128. $log = $log . "$from, $FileName, error, $error[2] \r\n";
  129. } else {
  130. $count = count($arrInserString);
  131. echo "updata $count recorders.";
  132. }
  133. $myLogFile = fopen($dirLog . "insert_db.log", "a");
  134. fwrite($myLogFile, $log);
  135. fclose($myLogFile);
  136. ?>
  137. <?php
  138. if ($from == $to) {
  139. echo "<h2>齐活!功德无量!all done!</h2>";
  140. } else {
  141. echo "<script>";
  142. echo "window.location.assign(\"db_insert_templet.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  143. echo "</script>";
  144. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  145. }
  146. ?>
  147. </body>
  148. </html>