create_wbw.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. //工程文件操作
  3. //建立,
  4. include("../log/pref_log.php");
  5. require_once '../config.php';
  6. require_once "../public/_pdo.php";
  7. require_once "../public/function.php";
  8. require_once "../channal/function.php";
  9. require_once "../redis/function.php";
  10. define("MAX_LETTER" ,20000);
  11. $output["status"]=0;
  12. $output["error"]="";
  13. $output["book"]="";
  14. $output["para"]="";
  15. $output["channel"]="";
  16. if(isset($_POST["book"])){
  17. $_book = $_POST["book"];
  18. }
  19. else{
  20. $output["status"]=1;
  21. $output["error"]="#no_param book";
  22. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  23. exit;
  24. }
  25. if(isset($_POST["para"])){
  26. $_para = json_decode($_POST["para"]);
  27. }
  28. else{
  29. $output["status"]=1;
  30. $output["error"]="#no_param paragraph";
  31. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  32. exit;
  33. }
  34. if(isset($_POST["channel"])){
  35. $_channel = $_POST["channel"];
  36. }
  37. else{
  38. $output["status"]=1;
  39. $output["error"]="#no_param channel";
  40. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  41. exit;
  42. }
  43. $output["book"]=$_book;
  44. $output["para"]=$_para;
  45. $output["channel"]=$_channel;
  46. //判断单词数量 太大的不能加载
  47. PDO_Connect(_FILE_DB_PALITEXT_);
  48. /* 创建一个填充了和 _para 相同数量占位符的字符串 */
  49. $place_holders = implode(',', array_fill(0, count($_para), '?'));
  50. $query = "SELECT sum(lenght) FROM "._TABLE_PALI_TEXT_." WHERE paragraph IN ($place_holders) AND book = ?";
  51. $param_letter = $_para;
  52. $param_letter[] = $_book;
  53. $sum_len = PDO_FetchOne($query,$param_letter);
  54. if($sum_len>MAX_LETTER){
  55. $output["status"]=1;
  56. $output["error"]="#oversize_to_load";
  57. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  58. exit;
  59. }
  60. # 查询数据库是否有数据,没有就建立
  61. // 查询逐词解析库
  62. //PDO_Connect(_FILE_DB_USER_WBW_);
  63. //模板库
  64. PDO_Connect(_FILE_DB_PALICANON_TEMPLET_);
  65. $dbh_tpl = $PDO;
  66. $dbh_tpl->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  67. #用户逐词译库
  68. $db_wbw = _FILE_DB_USER_WBW_;
  69. $dbh_wbw= new PDO($db_wbw, _DB_USERNAME_, _DB_PASSWORD_);
  70. $dbh_wbw->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  71. $channelClass = new Channal(redis_connect());
  72. $channelInfo = $channelClass->getChannal($_channel);
  73. foreach ($_para as $key => $para) {
  74. # code...
  75. $query = "SELECT count(*) FROM "._TABLE_USER_WBW_BLOCK_." WHERE channel_uid = ? AND book_id= ? and paragraph = ? ";
  76. $stmt = $dbh_wbw->prepare($query);
  77. $stmt->execute(array($_channel,$_book,$para));
  78. $row = $stmt->fetch(PDO::FETCH_NUM);
  79. if ($row) {
  80. $FetchWBW = $row[0];
  81. } else {
  82. $FetchWBW = 0;
  83. }
  84. if($FetchWBW == 0){
  85. #建立
  86. //写入数据库
  87. // 开始一个事务,关闭自动提交
  88. #更新block库
  89. $block_id=UUID::v4();
  90. $trans_block_id = UUID::v4();
  91. $block_data = array($block_id,
  92. "",
  93. $_channel,
  94. $_COOKIE["userid"],
  95. $_book,
  96. $para,
  97. "",
  98. $channelInfo["lang"],
  99. $channelInfo["status"],
  100. mTime()
  101. );
  102. $block_list[] = array("channal"=>$_channel,
  103. "type"=>6,//word by word
  104. "book"=>$_book,
  105. "paragraph"=>$para,
  106. "block_id"=>$block_id,
  107. "readonly"=>false
  108. );
  109. $dbh_wbw->beginTransaction();
  110. $query="INSERT INTO "._TABLE_USER_WBW_BLOCK_." ( uid ,
  111. parent_id ,
  112. channel_uid ,
  113. creator_uid ,
  114. book_id ,
  115. paragraph ,
  116. style ,
  117. lang ,
  118. status ,
  119. modify_time ,
  120. updated_at )
  121. VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , now() )";
  122. $stmt_wbw = $dbh_wbw->prepare($query);
  123. $stmt_wbw->execute($block_data);
  124. // 提交更改
  125. $dbh_wbw->commit();
  126. if (!$stmt_wbw || ($stmt_wbw && $stmt_wbw->errorCode() != 0)) {
  127. $error = $dbh_wbw->errorInfo();
  128. $output["status"]=1;
  129. $output["error"]=$error[2];
  130. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  131. exit;
  132. }
  133. #逐词解析库
  134. $query="SELECT * FROM "._TABLE_PALICANON_TEMPLET_." WHERE book = ? AND paragraph = ? ";
  135. $sth = $dbh_tpl->prepare($query);
  136. $sth->execute(array($_book,$para));
  137. $level=100;
  138. $title="";
  139. #TODO 查不到数据报错
  140. while($result = $sth->fetch(PDO::FETCH_ASSOC))
  141. {
  142. if($result["gramma"]=="?"){
  143. $wGrammar="";
  144. }
  145. else{
  146. $wGrammar=$result["gramma"];
  147. }
  148. $strXml="<word>";
  149. $strXml.="<pali>{$result["word"]}</pali>";
  150. $strXml.="<real>{$result["real"]}</real>";
  151. $wordid = "p{$result["book"]}-{$result["paragraph"]}-{$result["wid"]}";
  152. $strXml.="<id>{$wordid}</id>";
  153. $strXml.="<type s=\"0\">{$result["type"]}</type>";
  154. $strXml.="<gramma s=\"0\">{$wGrammar}</gramma>";
  155. $strXml.="<mean s=\"0\"></mean>";
  156. $strXml.="<org s=\"0\">".mb_strtolower($result["part"], 'UTF-8')."</org>";
  157. $strXml.="<om s=\"0\"></om>";
  158. $strXml.="<case s=\"0\">{$result["type"]}#{$wGrammar}</case>";
  159. $strXml.="<style>{$result["style"]}</style>";
  160. $strXml.="<status>0</status>";
  161. $strXml.="</word>";
  162. $wbw_data[] = array(UUID::v4(),
  163. $block_id,
  164. $_book,
  165. $para,
  166. $result["wid"],
  167. $result["real"],
  168. $strXml,
  169. mTime(),
  170. 10,
  171. $_COOKIE["userid"]
  172. );
  173. }
  174. // 开始一个事务,关闭自动提交
  175. $dbh_wbw->beginTransaction();
  176. $query="INSERT INTO "._TABLE_USER_WBW_." ( uid ,
  177. block_uid ,
  178. book_id ,
  179. paragraph ,
  180. wid ,
  181. word ,
  182. data ,
  183. modify_time ,
  184. status ,
  185. creator_uid ,
  186. updated_at
  187. )
  188. VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,now())";
  189. $stmt_wbw = $dbh_wbw->prepare($query);
  190. foreach($wbw_data as $oneParam){
  191. $stmt_wbw->execute($oneParam);
  192. }
  193. // 提交更改
  194. $dbh_wbw->commit();
  195. if (!$stmt_wbw || ($stmt_wbw && $stmt_wbw->errorCode() != 0)) {
  196. $error = $dbh_wbw->errorInfo();
  197. $output["status"]=1;
  198. $output["error"]=$error[2];
  199. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  200. exit;
  201. }
  202. }
  203. }
  204. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  205. PrefLog();
  206. ?>