custom_book.php 5.7 KB

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