custom_book.php 6.3 KB

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