function.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. if (count($arrData) > 0) {
  226. //add_edit_event(_SENT_NEW_, "{$newList[0]["book"]}-{$newList[0]["paragraph"]}-{$newList[0]["begin"]}-{$newList[0]["end"]}@{$newList[0]["channal"]}");
  227. $this->dbh_sent->beginTransaction();
  228. $query = "INSERT INTO "._TABLE_SENTENCE_." (uid,
  229. parent_uid,
  230. book_id,
  231. paragraph,
  232. word_start,
  233. word_end,
  234. channel_uid,
  235. author,
  236. editor_uid,
  237. content,
  238. language,
  239. version,
  240. status,
  241. strlen,
  242. modify_time,
  243. create_time
  244. )
  245. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
  246. $sth = $this->dbh_sent->prepare($query);
  247. #查询channel语言
  248. $channel_info = new Channal();
  249. foreach ($arrData as $data) {
  250. if($data["id"]==""){
  251. $data["id"] = UUID::v4();
  252. }
  253. if(!isset($data["create_time"])){
  254. $data["create_time"]=mTime();
  255. }
  256. if(!isset($data["modify_time"])){
  257. $data["modify_time"]=mTime();
  258. }
  259. $data["receive_time"]=mTime();
  260. $queryChannel = $channel_info->getChannal($data["channal"]);
  261. if ($queryChannel == false) {
  262. $lang = $data["language"];
  263. $status = 10;
  264. } else {
  265. $lang = $queryChannel["lang"];
  266. $status = $queryChannel["status"];
  267. }
  268. $sth->execute(array($data["id"],
  269. isset($data["parent"]) ? $data["parent"] : "",
  270. $data["book"],
  271. $data["paragraph"],
  272. $data["begin"],
  273. $data["end"],
  274. $data["channal"],
  275. $data["author"],
  276. $data["editor"],
  277. $data["text"],
  278. $lang,
  279. 1,
  280. $status,
  281. mb_strlen($data["text"], "UTF-8"),
  282. $data["modify_time"],
  283. $data["create_time"]
  284. ));
  285. }
  286. $this->dbh_sent->commit();
  287. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  288. /* 识别错误且回滚更改 */
  289. $this->dbh_sent->rollBack();
  290. $error = $this->dbh_sent->errorInfo();
  291. $this->errorMsg = $error[2];
  292. return false;
  293. } else {
  294. $this->errorMsg = "";
  295. return true;
  296. }
  297. }
  298. else{
  299. $this->errorMsg = "";
  300. return true;
  301. }
  302. }
  303. public function send_pr($arrData){
  304. if (count($arrData) ==0) {
  305. $this->errorMsg = "";
  306. return true;
  307. }
  308. $this->dbh_sent->beginTransaction();
  309. $query = "INSERT INTO "._TABLE_SENTENCE_PR_." (
  310. book_id,
  311. paragraph,
  312. word_start,
  313. word_end,
  314. channel_uid,
  315. author,
  316. editor_uid,
  317. content,
  318. language,
  319. status,
  320. strlen,
  321. modify_time,
  322. create_time
  323. )
  324. VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
  325. $stmt = $this->dbh_sent->prepare($query);
  326. foreach ($arrData as $data) {
  327. # 初始状态 1 未处理
  328. $stmt->execute(array(
  329. $data["book"],
  330. $data["para"],
  331. $data["begin"],
  332. $data["end"],
  333. $data["channal"],
  334. "",
  335. $data["editor"],
  336. $data["text"],
  337. $data["language"],
  338. 1,
  339. mb_strlen($data["text"], "UTF-8"),
  340. mTime(),
  341. mTime(),
  342. ));
  343. }
  344. $this->dbh_sent->commit();
  345. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  346. # 识别错误
  347. $this->dbh_sent->rollBack();
  348. $error = $this->dbh_sent->errorInfo();
  349. $this->errorMsg = $error[2];
  350. return false;
  351. } else {
  352. # 没错误
  353. $this->errorMsg = "";
  354. return true;
  355. }
  356. }
  357. public function historay($arrData){
  358. if (count($arrData) ==0) {
  359. $this->errorMsg = "";
  360. return true;
  361. }
  362. $snowflake = new SnowFlakeId();
  363. $this->dbh_his->beginTransaction();
  364. # 更新historay
  365. $query = "INSERT INTO "._TABLE_SENTENCE_HISTORAY_."
  366. (
  367. id,
  368. sent_uid,
  369. user_uid,
  370. content,
  371. create_time,
  372. landmark
  373. ) VALUES (? , ? , ? , ? , ? , ? )";
  374. $stmt = $this->dbh_his->prepare($query);
  375. foreach ($arrData as $data) {
  376. $stmt->execute(
  377. $snowflake->id(),
  378. array($data["id"],
  379. $data["editor"],
  380. $data["text"],
  381. mTime(),
  382. $data["landmark"]));
  383. }
  384. $this->dbh_his->commit();
  385. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  386. /* 识别错误 */
  387. $this->dbh_his->rollBack();
  388. $error = $this->dbh_his->errorInfo();
  389. $this->errorMsg = $error[2];
  390. return false;
  391. } else {
  392. #没错误
  393. $this->errorMsg = "";
  394. return true;
  395. }
  396. }
  397. }