custom_book.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. $listHead= "";
  104. $isList = false;
  105. $trimData = trim($data);
  106. # 判断是否为list
  107. $listLeft =strstr($data,"- ",true);
  108. if($listLeft !== FALSE){
  109. if(ctype_space($listLeft) || empty($listLeft)){
  110. # - 左侧是空,判定为list
  111. $isList=true;
  112. $iListPos = mb_strpos($data,'- ',0,"UTF-8");
  113. $listHead = mb_substr($data,0,$iListPos+2,"UTF-8");
  114. $listBody = mb_substr($data,$iListPos+2,mb_strlen($data,"UTF-8")-$iListPos+2,"UTF-8");
  115. }
  116. }
  117. # TODO 判断是否为标题
  118. # [#]+\s
  119. if(mb_substr($trimData,0,1,"UTF-8")=="|"){
  120. $isTable=true;
  121. }
  122. if($trimData!="" && $isTable == true){
  123. $newSent .= "\n";
  124. continue;
  125. }
  126. if($isList == true){
  127. $newSent .= $listBody;
  128. }else{
  129. $newSent .= $trimData;
  130. }
  131. #生成句子编号
  132. if($trimData==""){
  133. #空行
  134. if(strlen($newSent)>0){
  135. //之前有内容
  136. {
  137. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  138. $sth->execute(
  139. array(
  140. $this->SnowFlake->id(),
  141. $book,
  142. $para,
  143. $sentNum,
  144. $sentNum,
  145. mb_strlen($data,"UTF-8"),
  146. $newSent,
  147. $lang,
  148. $_COOKIE["userid"],
  149. 10,
  150. mTime(),
  151. mTime()
  152. ));
  153. }
  154. $newSent="";
  155. }
  156. #新的段落 不插入数据库
  157. $para++;
  158. $sentNum = 1;
  159. $newText .="\n";
  160. $isTable = false;
  161. $isList = false;
  162. continue;
  163. }else{
  164. $sentNum=$sentNum+10;
  165. }
  166. if(mb_substr($trimData,0,2,"UTF-8")=="{{"){
  167. #已经有的句子链接不处理
  168. $newText .=$trimData."\n";
  169. }else{
  170. $newText .= $listHead;
  171. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  172. $sth->execute(
  173. array(
  174. $this->SnowFlake->id(),
  175. $book,
  176. $para,
  177. $sentNum,
  178. $sentNum,
  179. mb_strlen($data,"UTF-8"),
  180. $newSent,
  181. $lang,
  182. $_COOKIE["userid"],
  183. 10,
  184. mTime(),
  185. mTime()
  186. ));
  187. $newSent="";
  188. }
  189. }
  190. if(strlen($newSent)>0){
  191. //最后一行是表格结束
  192. {
  193. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  194. $sth->execute(
  195. array(
  196. $this->SnowFlake->id(),
  197. $book,
  198. $para,
  199. $sentNum,
  200. $sentNum,
  201. mb_strlen($data,"UTF-8"),
  202. $newSent,
  203. $lang,
  204. $_COOKIE["userid"],
  205. 10,
  206. mTime(),
  207. mTime()
  208. ));
  209. }
  210. $newSent="";
  211. }
  212. $this->dbh->commit();
  213. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  214. # 识别错误且回滚更改
  215. $this->dbh->rollBack();
  216. $error = $this->dbh->errorInfo();
  217. $respond['status']=1;
  218. $respond['message']=$error[2];
  219. $respond['content']="";
  220. }
  221. else{
  222. $respond['status']=0;
  223. $respond['message']="成功";
  224. $respond['content']=$newText;
  225. }
  226. return $respond;
  227. }
  228. public function getText($book,$para,$start,$end){
  229. $query="SELECT content FROM {$this->table} WHERE book = ? AND paragraph = ? AND word_start=? AND word_end = ?";
  230. $result = $this->fetch($query,array($book,$para,$start,$end));
  231. if($result){
  232. return $result["content"];
  233. }
  234. else{
  235. return "unkow";
  236. }
  237. }
  238. }
  239. ?>