custom_book.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_, _TABLE_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}
  34. (
  35. id,
  36. book_id,
  37. title,
  38. owner,
  39. editor_id,
  40. lang,
  41. status
  42. ) VALUES (?,?, ?, ?, ?, ? , ?)";
  43. $stmt = $this->execute($query,array($this->SnowFlake->id(),$currBook,$title,$_COOKIE["user_uid"],$_COOKIE["user_id"],$lang,10));
  44. if($stmt){
  45. $CSent = new CustomBookSentence($this->redis);
  46. $respond = $CSent->insert($currBook,$sent,$lang);
  47. }
  48. else{
  49. $respond["status"]=1;
  50. $respond["message"]="插入新书失败";
  51. }
  52. }
  53. return $respond;
  54. }
  55. }
  56. class CustomBookSentence extends Table
  57. {
  58. function __construct($redis=false) {
  59. parent::__construct(_FILE_DB_USER_CUSTOM_BOOK_, _TABLE_CUSTOM_BOOK_SENT_, "", "",$redis);
  60. }
  61. public function getAll($book,$para,$start,$end){
  62. $query="SELECT content as text,length,lang,modify_time,create_time,owner FROM {$this->table} WHERE book = ? AND paragraph = ? AND word_start = ? AND word_end = ?";
  63. $result = $this->fetch($query,array($book,$para,$start,$end));
  64. if($result){
  65. return $result;
  66. }
  67. else{
  68. return array("text"=>"","length"=>"","lang"=>"","modify_time"=>0,"create_time"=>0,"owner"=>"");
  69. }
  70. }
  71. #将句子插入数据库
  72. #包含句子识别算法,表格算一个句子,list算一个句子
  73. public function insert($book,$content,$lang)
  74. {
  75. $respond['status']=0;
  76. $respond['message']="";
  77. $respond['content']="";
  78. # 开始一个事务,关闭自动提交
  79. $this->dbh->beginTransaction();
  80. $query="INSERT INTO {$this->table}
  81. (
  82. id,
  83. book,
  84. paragraph,
  85. word_start,
  86. word_end,
  87. length,
  88. content,
  89. lang,
  90. owner,
  91. status,
  92. create_time,
  93. modify_time
  94. ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  95. $sth = $this->dbh->prepare($query);
  96. $para = 1;
  97. $sentNum = 1;
  98. $newText = "";
  99. $isTable=false;
  100. $isList=false;
  101. $newSent="";
  102. foreach ($content as $data) {
  103. $trimData = trim($data);
  104. $newSent .= $trimData;
  105. /*
  106. $left2 = mb_substr($trimData,0,2,"UTF-8");
  107. if($left2 == "- " || $left2 == "* " || $left2 == "+ "){
  108. $isList=true;
  109. }
  110. */
  111. if(mb_substr($trimData,0,1,"UTF-8")=="|"){
  112. $isTable=true;
  113. }
  114. if($trimData!="" && ($isTable == true || $isList == true)){
  115. $newSent .= "\n";
  116. continue;
  117. }
  118. #生成句子编号
  119. if($trimData==""){
  120. #空行
  121. if(strlen($newSent)>0){
  122. //之前有内容
  123. {
  124. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  125. $sth->execute(
  126. array(
  127. $this->SnowFlake->id(),
  128. $book,
  129. $para,
  130. $sentNum,
  131. $sentNum,
  132. mb_strlen($data,"UTF-8"),
  133. $newSent,
  134. $lang,
  135. $_COOKIE["userid"],
  136. 10,
  137. mTime(),
  138. mTime()
  139. ));
  140. }
  141. $newSent="";
  142. }
  143. #新的段落 不插入数据库
  144. $para++;
  145. $sentNum = 1;
  146. $newText .="\n";
  147. $isTable = false;
  148. $isList = false;
  149. continue;
  150. }else{
  151. $sentNum=$sentNum+10;
  152. }
  153. if(mb_substr($trimData,0,2,"UTF-8")=="{{"){
  154. #已经有的句子链接不处理
  155. $newText .=$trimData."\n";
  156. }else{
  157. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  158. $sth->execute(
  159. array(
  160. $this->SnowFlake->id(),
  161. $book,
  162. $para,
  163. $sentNum,
  164. $sentNum,
  165. mb_strlen($data,"UTF-8"),
  166. $newSent,
  167. $lang,
  168. $_COOKIE["userid"],
  169. 10,
  170. mTime(),
  171. mTime()
  172. ));
  173. $newSent="";
  174. }
  175. }
  176. if(strlen($newSent)>0){
  177. //最后一行是表格结束
  178. {
  179. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  180. $sth->execute(
  181. array(
  182. $this->SnowFlake->id(),
  183. $book,
  184. $para,
  185. $sentNum,
  186. $sentNum,
  187. mb_strlen($data,"UTF-8"),
  188. $newSent,
  189. $lang,
  190. $_COOKIE["userid"],
  191. 10,
  192. mTime(),
  193. mTime()
  194. ));
  195. }
  196. $newSent="";
  197. }
  198. $this->dbh->commit();
  199. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  200. # 识别错误且回滚更改
  201. $this->dbh->rollBack();
  202. $error = $this->dbh->errorInfo();
  203. $respond['status']=1;
  204. $respond['message']=$error[2];
  205. $respond['content']="";
  206. }
  207. else{
  208. $respond['status']=0;
  209. $respond['message']="成功";
  210. $respond['content']=$newText;
  211. }
  212. return $respond;
  213. }
  214. public function getText($book,$para,$start,$end){
  215. $query="SELECT content FROM {$this->table} WHERE book = ? AND paragraph = ? AND word_start=? AND word_end = ?";
  216. $result = $this->fetch($query,array($book,$para,$start,$end));
  217. if($result){
  218. return $result["content"];
  219. }
  220. else{
  221. return "unkow";
  222. }
  223. }
  224. }
  225. ?>