create_wbw.php 7.5 KB

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