function.php 13 KB

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