function.php 13 KB

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