custom_book.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. public function insert($book,$content,$lang)
  63. {
  64. $respond['status']=0;
  65. $respond['message']="";
  66. $respond['content']="";
  67. # 开始一个事务,关闭自动提交
  68. $this->dbh->beginTransaction();
  69. $query="INSERT INTO custom_book_sentence ('book','paragraph','begin','end','length','text','lang','owner','status','create_time','modify_time') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  70. $sth = $this->dbh->prepare($query);
  71. $para = 1;
  72. $sentNum = 0;
  73. $newText = "";
  74. foreach ($content as $data) {
  75. $data = trim($data);
  76. if($data==""){
  77. $para++;
  78. $sentNum = 0;
  79. $newText .="\n";
  80. continue;
  81. }
  82. else{
  83. $sentNum=$sentNum+10;
  84. }
  85. if(mb_substr($data,0,2,"UTF-8")=="{{"){
  86. $newText .=$data."\n";
  87. }
  88. else{
  89. $newText .='{{'."{$book}-{$para}-{$sentNum}-{$sentNum}"."}}\n";
  90. $sth->execute(
  91. array(
  92. $book,
  93. $para,
  94. $sentNum,
  95. $sentNum,
  96. mb_strlen($data,"UTF-8"),
  97. $data,
  98. $lang,
  99. $_COOKIE["userid"],
  100. 10,
  101. mTime(),
  102. mTime()
  103. ));
  104. }
  105. }
  106. $this->dbh->commit();
  107. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  108. # 识别错误且回滚更改
  109. $this->dbh->rollBack();
  110. $error = $this->dbh->errorInfo();
  111. $respond['status']=1;
  112. $respond['message']=$error[2];
  113. $respond['content']="";
  114. }
  115. else{
  116. $respond['status']=0;
  117. $respond['message']="成功";
  118. $respond['content']=$newText;
  119. }
  120. return $respond;
  121. }
  122. public function getText($book,$para,$start,$end){
  123. $query="SELECT text FROM custom_book_sentence WHERE book = ? AND paragraph = ? AND begin=? AND end = ?";
  124. $result = $this->fetch($query,array($book,$para,$start,$end));
  125. if($result){
  126. return $result["text"];
  127. }
  128. else{
  129. return "unkow";
  130. }
  131. }
  132. }
  133. ?>