2
0

custom_book.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. require_once "../config.php";
  3. require_once "../db/table.php";
  4. require_once '../hostsetting/function.php';
  5. class CustomBook extends Table
  6. {
  7. function __construct($redis=false) {
  8. parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, "custom_book", "", "",$redis);
  9. }
  10. public function new($title,$data,$lang)
  11. {
  12. $respond['status']=0;
  13. $respond['message']="";
  14. $respond['content']="";
  15. $sent = explode("\n",$data);
  16. if($sent && count($sent)>0){
  17. $setting = new Hostsetting();
  18. $max_book = $setting->get("max_book_number");
  19. if($max_book){
  20. $currBook = $max_book+1;
  21. $setbooknum = $setting->set("max_book_number",$currBook);
  22. if($setbooknum==false){
  23. $respond["status"]=1;
  24. $respond["message"]="设置书号错误";
  25. return $respond;
  26. }
  27. }
  28. else{
  29. $respond["status"]=1;
  30. $respond["message"]="获取书号错误";
  31. return $respond;
  32. }
  33. $query="INSERT INTO {$this->table} ('book_id','title','owner','lang','status','modify_time','create_time') VALUES (?, ?, ?, ?, ?, ?, ?)";
  34. $stmt = $this->execute($query,array($currBook,$title,$_COOKIE["userid"],$lang,10,mTime(),mTime()));
  35. if($stmt){
  36. $CSent = new CustomBookSentence($this->redis);
  37. $respond = $CSent->insert($currBook,$sent,$lang);
  38. }
  39. else{
  40. $respond["status"]=1;
  41. $respond["message"]="插入新书失败";
  42. }
  43. }
  44. return $respond;
  45. }
  46. }
  47. class CustomBookSentence extends Table
  48. {
  49. function __construct($redis=false) {
  50. parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, "custom_book_sentence", "", "",$redis);
  51. }
  52. public function getAll($book,$para,$start,$end){
  53. $query="SELECT text,length,lang,modify_time,create_time,owner FROM custom_book_sentence WHERE book = ? AND paragraph = ? AND begin=? AND end = ?";
  54. $result = $this->fetch($query,array($book,$para,$start,$end));
  55. if($result){
  56. return $result;
  57. }
  58. else{
  59. return array("text"=>"","length"=>"","lang"=>"","modify_time"=>0,"create_time"=>0,"owner"=>"");
  60. }
  61. }
  62. #将句子插入数据库
  63. #包含句子识别算法,表格算一个句子,list算一个句子
  64. public function insert($book,$content,$lang)
  65. {
  66. $respond['status']=0;
  67. $respond['message']="";
  68. $respond['content']="";
  69. # 开始一个事务,关闭自动提交
  70. $this->dbh->beginTransaction();
  71. $query="INSERT INTO custom_book_sentence ('book','paragraph','begin','end','length','text','lang','owner','status','create_time','modify_time') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  72. $sth = $this->dbh->prepare($query);
  73. $para = 1;
  74. $sentNum = 1;
  75. $newText = "";
  76. $isTable=false;
  77. $isList=false;
  78. $newSent="";
  79. foreach ($content as $data) {
  80. $trimData = trim($data);
  81. $newSent .= $trimData;
  82. /*
  83. $left2 = mb_substr($trimData,0,2,"UTF-8");
  84. if($left2 == "- " || $left2 == "* " || $left2 == "+ "){
  85. $isList=true;
  86. }
  87. */
  88. if(mb_substr($trimData,0,1,"UTF-8")=="|"){
  89. $isTable=true;
  90. }
  91. if($trimData!="" && ($isTable == true || $isList == true)){
  92. $newSent .= "\n";
  93. continue;
  94. }
  95. #生成句子编号
  96. if($trimData==""){
  97. #空行
  98. if(strlen($newSent)>0){
  99. //之前有内容
  100. {
  101. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  102. $sth->execute(
  103. array(
  104. $book,
  105. $para,
  106. $sentNum,
  107. $sentNum,
  108. mb_strlen($data,"UTF-8"),
  109. $newSent,
  110. $lang,
  111. $_COOKIE["userid"],
  112. 10,
  113. mTime(),
  114. mTime()
  115. ));
  116. }
  117. $newSent="";
  118. }
  119. #新的段落 不插入数据库
  120. $para++;
  121. $sentNum = 1;
  122. $newText .="\n";
  123. $isTable = false;
  124. $isList = false;
  125. continue;
  126. }else{
  127. $sentNum=$sentNum+10;
  128. }
  129. if(mb_substr($trimData,0,2,"UTF-8")=="{{"){
  130. #已经有的句子链接不处理
  131. $newText .=$trimData."\n";
  132. }else{
  133. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  134. $sth->execute(
  135. array(
  136. $book,
  137. $para,
  138. $sentNum,
  139. $sentNum,
  140. mb_strlen($data,"UTF-8"),
  141. $newSent,
  142. $lang,
  143. $_COOKIE["userid"],
  144. 10,
  145. mTime(),
  146. mTime()
  147. ));
  148. $newSent="";
  149. }
  150. }
  151. if(strlen($newSent)>0){
  152. //最后一行是表格结束
  153. {
  154. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  155. $sth->execute(
  156. array(
  157. $book,
  158. $para,
  159. $sentNum,
  160. $sentNum,
  161. mb_strlen($data,"UTF-8"),
  162. $newSent,
  163. $lang,
  164. $_COOKIE["userid"],
  165. 10,
  166. mTime(),
  167. mTime()
  168. ));
  169. }
  170. $newSent="";
  171. }
  172. $this->dbh->commit();
  173. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  174. # 识别错误且回滚更改
  175. $this->dbh->rollBack();
  176. $error = $this->dbh->errorInfo();
  177. $respond['status']=1;
  178. $respond['message']=$error[2];
  179. $respond['content']="";
  180. }
  181. else{
  182. $respond['status']=0;
  183. $respond['message']="成功";
  184. $respond['content']=$newText;
  185. }
  186. return $respond;
  187. }
  188. public function getText($book,$para,$start,$end){
  189. $query="SELECT text FROM custom_book_sentence WHERE book = ? AND paragraph = ? AND begin=? AND end = ?";
  190. $result = $this->fetch($query,array($book,$para,$start,$end));
  191. if($result){
  192. return $result["text"];
  193. }
  194. else{
  195. return "unkow";
  196. }
  197. }
  198. }
  199. ?>