function.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. require_once "../config.php";
  3. require_once "../share/function.php";
  4. require_once "../channal/function.php";
  5. require_once "../db/table.php";
  6. function update_historay($sent_id, $user_id, $text, $landmark)
  7. {
  8. # 更新historay
  9. PDO_Connect("" . _FILE_DB_USER_SENTENCE_HISTORAY_);
  10. $query = "INSERT INTO "._TABLE_SENTENCE_HISTORAY_." (sent_uid, user_uid, content, date, landmark) VALUES (? , ? , ? , ? , ? )";
  11. $stmt = PDO_Execute($query,
  12. array($sent_id,
  13. $user_id,
  14. $text,
  15. mTime(),
  16. $landmark,
  17. ));
  18. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  19. /* 识别错误 */
  20. $error = PDO_ErrorInfo();
  21. return $error[2];
  22. } else {
  23. #没错误
  24. return "";
  25. }
  26. }
  27. class SentPr{
  28. private $dbh_sent;
  29. private $redis;
  30. public function __construct($redis=false) {
  31. $this->dbh_sent = new PDO(_FILE_DB_SENTENCE_, _DB_USERNAME_, _DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  32. $this->dbh_sent->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  33. $this->redis=$redis;
  34. }
  35. public function getNewPrNumber($book,$para,$begin,$end,$channel){
  36. if ($this->dbh_sent) {
  37. $query = "SELECT count(*) as ct FROM "._TABLE_SENTENCE_PR_." WHERE book_id = ? and paragraph= ? and word_start=? and word_end=? and channel_uid=? and status=1 ";
  38. $stmt = $this->dbh_sent->prepare($query);
  39. $stmt->execute(array($book,$para,$begin,$end,$channel));
  40. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  41. if($result){
  42. return $result["ct"];
  43. }
  44. else{
  45. return false;
  46. }
  47. }
  48. else{
  49. return false;
  50. }
  51. }
  52. public function getAllPrNumber($book,$para,$begin,$end,$channel){
  53. if ($this->dbh_sent) {
  54. $query = "SELECT count(*) as ct FROM "._TABLE_SENTENCE_PR_." WHERE book_id = ? and paragraph= ? and word_start=? and word_end=? and channel_uid=? ";
  55. $stmt = $this->dbh_sent->prepare($query);
  56. $stmt->execute(array($book,$para,$begin,$end,$channel));
  57. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  58. if($result){
  59. return $result["ct"];
  60. }
  61. else{
  62. return false;
  63. }
  64. }
  65. else{
  66. return false;
  67. }
  68. }
  69. public function getPrData($book,$para,$begin,$end,$channel){
  70. if ($this->dbh_sent) {
  71. $query = "SELECT id,book_id as book,paragraph,word_start as begin,word_end as end,channel_uid as channel,content as text,editor_uid as editor,modify_time FROM "._TABLE_SENTENCE_PR_." WHERE book_id = ? and paragraph= ? and word_start=? and word_end=? and channel_uid=? and status=1 limit 100";
  72. $stmt = $this->dbh_sent->prepare($query);
  73. $stmt->execute(array($book,$para,$begin,$end,$channel));
  74. $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  75. if($result){
  76. return $result;
  77. }
  78. else{
  79. return false;
  80. }
  81. }
  82. else{
  83. return false;
  84. }
  85. }
  86. public function getPrDataById($id){
  87. if ($this->dbh_sent) {
  88. $query = "SELECT id,book_id as book,paragraph,word_start as begin,word_end as end,channel_uid as channel,content as text,editor_uid as editor,modify_time FROM "._TABLE_SENTENCE_PR_." WHERE id = ? ";
  89. $stmt = $this->dbh_sent->prepare($query);
  90. $stmt->execute(array($id));
  91. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  92. if($result){
  93. return $result;
  94. }
  95. else{
  96. return false;
  97. }
  98. }
  99. else{
  100. return false;
  101. }
  102. }
  103. public function setPrData($id,$text){
  104. if ($this->dbh_sent) {
  105. $query = "UPDATE "._TABLE_SENTENCE_PR_." set content=? ,modify_time=? , updated_at = now() WHERE id = ? and editor_uid= ? ";
  106. $stmt = $this->dbh_sent->prepare($query);
  107. $stmt->execute(array($text,mTime(),$id,$_COOKIE["userid"]));
  108. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  109. /* 识别错误 */
  110. $error = $this->dbh_sent->errorInfo();
  111. //$respond['message'] = $error[2];
  112. return false;
  113. } else {
  114. #没错误 更新历史记录
  115. return true;
  116. }
  117. }
  118. else{
  119. return false;
  120. }
  121. }
  122. }
  123. class Sent_his
  124. {
  125. private $dbh_his;
  126. private $errorMsg="";
  127. }
  128. class Sent_DB
  129. {
  130. private $dbh_sent;
  131. private $dbh_his;
  132. private $errorMsg="";
  133. private $selectCol="uid,parent_uid,block_uid,channel_uid,book_id,paragraph,word_start,word_end,author,editor_uid,content,language,version,status,strlen,modify_time,create_time";
  134. private $updateCol="";
  135. public function __construct($redis=false) {
  136. $this->dbh_sent = new PDO(_FILE_DB_SENTENCE_, _DB_USERNAME_, _DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  137. $this->dbh_sent->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  138. $this->dbh_his = new PDO(_FILE_DB_USER_SENTENCE_HISTORAY_, _DB_USERNAME_, _DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  139. $this->dbh_his->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  140. }
  141. public function getError(){
  142. return $errorMsg;
  143. }
  144. #获取单个句子数据
  145. public function getSent($book,$para,$begin,$end,$channel){
  146. $query = "SELECT {$this->selectCol} FROM "._TABLE_SENTENCE_." WHERE book_id= ? AND paragraph= ? AND word_start= ? AND word_end= ? AND channel_uid = ? ";
  147. $stmt = $this->dbh_sent->prepare($query);
  148. if($stmt){
  149. $stmt->execute(array($book,$para,$begin,$end,$channel));
  150. $fetchDest = $stmt->fetch(PDO::FETCH_ASSOC);
  151. return $fetchDest;
  152. }
  153. else{
  154. return false;
  155. }
  156. }
  157. public function getSentDefaultByLan($book,$para,$begin,$end,$lang){
  158. $query = "SELECT {$this->selectCol} FROM "._TABLE_SENTENCE_." WHERE book_id= ? AND paragraph= ? AND word_start= ? AND word_end= ? AND language = ? ";
  159. $stmt = $this->dbh_sent->prepare($query);
  160. if($stmt){
  161. $stmt->execute(array($book,$para,$begin,$end,$lang));
  162. $fetchDest = $stmt->fetch(PDO::FETCH_ASSOC);
  163. return $fetchDest;
  164. }
  165. else{
  166. return false;
  167. }
  168. }
  169. public function update($arrData){
  170. /* 修改现有数据 */
  171. if (count($arrData) > 0) {
  172. //add_edit_event(_SENT_EDIT_, "{$oldList[0]["book"]}-{$oldList[0]["paragraph"]}-{$oldList[0]["begin"]}-{$oldList[0]["end"]}@{$oldList[0]["channal"]}");
  173. $this->dbh_sent->beginTransaction();
  174. if(isset($arrData[0]["book"]) && isset($arrData[0]["paragraph"]) && isset($arrData[0]["begin"]) && isset($arrData[0]["end"]) && isset($arrData[0]["channal"])){
  175. $query = "UPDATE "._TABLE_SENTENCE_." SET content = ? , strlen = ? , editor_uid=?, modify_time= ? ,updated_at=now() where book_id = ? and paragraph=? and word_start=? and word_end=? and channel_uid=? ";
  176. $sth = $this->dbh_sent->prepare($query);
  177. foreach ($arrData as $data) {
  178. if(!isset($data["modify_time"])){
  179. $data["modify_time"] = mTime();
  180. }
  181. $sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],mTime(),$data["book"],$data["paragraph"],$data["begin"],$data["end"],$data["channal"]));
  182. }
  183. }
  184. else if(isset($arrData[0]["id"])){
  185. $query = "UPDATE "._TABLE_SENTENCE_." SET content = ? , strlen = ? , editor_uid=?, modify_time= ? ,updated_at = now() where uid= ? ";
  186. $sth = $this->dbh_sent->prepare($query);
  187. foreach ($arrData as $data) {
  188. if(!isset($data["modify_time"])){
  189. $data["modify_time"]=mTime();
  190. }
  191. $sth->execute(array($data["text"],mb_strlen($data["text"],"UTF-8"),$data["editor"],mTime(),$data["id"]));
  192. }
  193. }
  194. $this->dbh_sent->commit();
  195. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  196. /* 识别错误且回滚更改 */
  197. $this->dbh_sent->rollBack();
  198. $error = $this->dbh_sent->errorInfo();
  199. $this->errorMsg = $error[2];
  200. return false;
  201. } else {
  202. #没错误 添加log
  203. $this->errorMsg = "";
  204. return true;
  205. }
  206. }
  207. else{
  208. $this->errorMsg = "";
  209. return true;
  210. }
  211. }
  212. public function insert($arrData){
  213. # 插入新数据
  214. if (count($arrData) > 0) {
  215. //add_edit_event(_SENT_NEW_, "{$newList[0]["book"]}-{$newList[0]["paragraph"]}-{$newList[0]["begin"]}-{$newList[0]["end"]}@{$newList[0]["channal"]}");
  216. $this->dbh_sent->beginTransaction();
  217. $query = "INSERT INTO "._TABLE_SENTENCE_." (uid,
  218. parent_uid,
  219. book_id,
  220. paragraph,
  221. word_start,
  222. word_end,
  223. channel_uid,
  224. author,
  225. editor_uid,
  226. content,
  227. language,
  228. version,
  229. status,
  230. strlen,
  231. modify_time,
  232. create_time
  233. )
  234. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
  235. $sth = $this->dbh_sent->prepare($query);
  236. #查询channel语言
  237. $channel_info = new Channal();
  238. foreach ($arrData as $data) {
  239. if($data["id"]==""){
  240. $data["id"] = UUID::v4();
  241. }
  242. if(!isset($data["create_time"])){
  243. $data["create_time"]=mTime();
  244. }
  245. if(!isset($data["modify_time"])){
  246. $data["modify_time"]=mTime();
  247. }
  248. $data["receive_time"]=mTime();
  249. $queryChannel = $channel_info->getChannal($data["channal"]);
  250. if ($queryChannel == false) {
  251. $lang = $data["language"];
  252. $status = 10;
  253. } else {
  254. $lang = $queryChannel["lang"];
  255. $status = $queryChannel["status"];
  256. }
  257. $sth->execute(array($data["id"],
  258. isset($data["parent"]) ? $data["parent"] : "",
  259. $data["book"],
  260. $data["paragraph"],
  261. $data["begin"],
  262. $data["end"],
  263. $data["channal"],
  264. $data["author"],
  265. $data["editor"],
  266. $data["text"],
  267. $lang,
  268. 1,
  269. $status,
  270. mb_strlen($data["text"], "UTF-8"),
  271. $data["modify_time"],
  272. $data["create_time"]
  273. ));
  274. }
  275. $this->dbh_sent->commit();
  276. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  277. /* 识别错误且回滚更改 */
  278. $this->dbh_sent->rollBack();
  279. $error = $this->dbh_sent->errorInfo();
  280. $this->errorMsg = $error[2];
  281. return false;
  282. } else {
  283. $this->errorMsg = "";
  284. return true;
  285. }
  286. }
  287. else{
  288. $this->errorMsg = "";
  289. return true;
  290. }
  291. }
  292. public function send_pr($arrData){
  293. if (count($arrData) ==0) {
  294. $this->errorMsg = "";
  295. return true;
  296. }
  297. $this->dbh_sent->beginTransaction();
  298. $query = "INSERT INTO "._TABLE_SENTENCE_PR_." (
  299. book_id,
  300. paragraph,
  301. word_start,
  302. word_end,
  303. channel_uid,
  304. author,
  305. editor_uid,
  306. content,
  307. language,
  308. status,
  309. strlen,
  310. modify_time,
  311. create_time
  312. )
  313. VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
  314. $stmt = $this->dbh_sent->prepare($query);
  315. foreach ($arrData as $data) {
  316. # 初始状态 1 未处理
  317. $stmt->execute(array(
  318. $data["book"],
  319. $data["para"],
  320. $data["begin"],
  321. $data["end"],
  322. $data["channal"],
  323. "",
  324. $data["editor"],
  325. $data["text"],
  326. $data["language"],
  327. 1,
  328. mb_strlen($data["text"], "UTF-8"),
  329. mTime(),
  330. mTime(),
  331. ));
  332. }
  333. $this->dbh_sent->commit();
  334. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  335. # 识别错误
  336. $this->dbh_sent->rollBack();
  337. $error = $this->dbh_sent->errorInfo();
  338. $this->errorMsg = $error[2];
  339. return false;
  340. } else {
  341. # 没错误
  342. $this->errorMsg = "";
  343. return true;
  344. }
  345. }
  346. public function historay($arrData){
  347. if (count($arrData) ==0) {
  348. $this->errorMsg = "";
  349. return true;
  350. }
  351. $this->dbh_his->beginTransaction();
  352. # 更新historay
  353. $query = "INSERT INTO "._TABLE_SENTENCE_HISTORAY_." (sent_uid, user_uid, content, date, landmark) VALUES (? , ? , ? , ? , ? )";
  354. $stmt = $this->dbh_his->prepare($query);
  355. foreach ($arrData as $data) {
  356. $stmt->execute(array($data["id"],
  357. $data["editor"],
  358. $data["text"],
  359. mTime(),
  360. $data["landmark"]));
  361. }
  362. $this->dbh_his->commit();
  363. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  364. /* 识别错误 */
  365. $this->dbh_his->rollBack();
  366. $error = $this->dbh_his->errorInfo();
  367. $this->errorMsg = $error[2];
  368. return false;
  369. } else {
  370. #没错误
  371. $this->errorMsg = "";
  372. return true;
  373. }
  374. }
  375. }