function.php 13 KB

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