custom_book.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. require_once "../path.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 .= $data;
  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. continue;
  93. }
  94. #生成句子编号
  95. if($trimData==""){
  96. #空行
  97. if(strlen($newSent)>0){
  98. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  99. $sth->execute(
  100. array(
  101. $book,
  102. $para,
  103. $sentNum,
  104. $sentNum,
  105. mb_strlen($data,"UTF-8"),
  106. $newSent,
  107. $lang,
  108. $_COOKIE["userid"],
  109. 10,
  110. mTime(),
  111. mTime()
  112. ));
  113. $newSent="";
  114. }
  115. #新的段落 不插入数据库
  116. $para++;
  117. $sentNum = 1;
  118. $newText .="\n";
  119. $isTable = false;
  120. $isList = false;
  121. continue;
  122. }else{
  123. $sentNum=$sentNum+10;
  124. }
  125. if(mb_substr($trimData,0,2,"UTF-8")=="{{"){
  126. #已经有的句子链接不处理
  127. $newText .=$trimData."\n";
  128. }else{
  129. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  130. $sth->execute(
  131. array(
  132. $book,
  133. $para,
  134. $sentNum,
  135. $sentNum,
  136. mb_strlen($data,"UTF-8"),
  137. $newSent,
  138. $lang,
  139. $_COOKIE["userid"],
  140. 10,
  141. mTime(),
  142. mTime()
  143. ));
  144. $newSent="";
  145. }
  146. }
  147. $this->dbh->commit();
  148. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  149. # 识别错误且回滚更改
  150. $this->dbh->rollBack();
  151. $error = $this->dbh->errorInfo();
  152. $respond['status']=1;
  153. $respond['message']=$error[2];
  154. $respond['content']="";
  155. }
  156. else{
  157. $respond['status']=0;
  158. $respond['message']="成功";
  159. $respond['content']=$newText;
  160. }
  161. return $respond;
  162. }
  163. public function getText($book,$para,$start,$end){
  164. $query="SELECT text FROM custom_book_sentence WHERE book = ? AND paragraph = ? AND begin=? AND end = ?";
  165. $result = $this->fetch($query,array($book,$para,$start,$end));
  166. if($result){
  167. return $result["text"];
  168. }
  169. else{
  170. return "unkow";
  171. }
  172. }
  173. }
  174. ?>