function.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. require_once "../config.php";
  3. require_once "../db/table.php";
  4. class PaliText extends Table
  5. {
  6. function __construct($redis=false) {
  7. parent::__construct(_FILE_DB_PALITEXT_, _TABLE_PALI_TEXT_, "", "",$redis);
  8. }
  9. public function getPath($book,$para){
  10. if($this->redis!==false){
  11. $path = $this->redis->hGet("pali_text://path",$book."-".$para);
  12. if($path!==FALSE){
  13. return json_decode($path,true);
  14. }
  15. }
  16. $path = array();
  17. $parent = $para;
  18. $deep = 0;
  19. $sFirstParentTitle = "";
  20. //循环查找父标题 得到整条路径
  21. while ($parent > -1) {
  22. $query = "SELECT * from "._TABLE_PALI_TEXT_." where \"book\" = ? and \"paragraph\" = ? ";
  23. $stmt = $this->dbh->prepare($query);
  24. $stmt->execute(array($book, $parent));
  25. $FetParent = $stmt->fetch(PDO::FETCH_ASSOC);
  26. if($FetParent){
  27. $toc =array("book"=>$book,"para"=>$parent,"level"=>$FetParent["level"],"title"=>$FetParent["toc"]);
  28. $path[] = $toc;
  29. $parent = $FetParent["parent"];
  30. $deep++;
  31. if ($deep > 5) {
  32. break;
  33. }
  34. }else{
  35. break;
  36. }
  37. }
  38. if($this->redis){
  39. $this->redis->hSet("pali_text://path",$book."-".$para,json_encode($path,JSON_UNESCAPED_UNICODE));
  40. }
  41. return ($path);
  42. }
  43. public function getPathHtml($arrPath){
  44. $path="";
  45. foreach ($arrPath as $key => $value) {
  46. # code...
  47. $toc = "<chapter book='{$value["book"]}' para='{$value["para"]}' title='{$value["title"]}'>{$value["title"]}</chapter>";
  48. if ($path == "") {
  49. if ($value["level"] < 100) {
  50. $path = $toc;
  51. } else {
  52. $path = "<para book='{$value["book"]}' para='{$value["para"]}' title='{$value["title"]}'>{$value["para"]}</para>";
  53. }
  54. } else {
  55. $path = $toc . $path;
  56. }
  57. }
  58. return $path;
  59. }
  60. }
  61. class PaliBook extends Table
  62. {
  63. function __construct($redis=false) {
  64. parent::__construct(_FILE_DB_PALITEXT_, _TABLE_PALI_BOOK_NAME_, "", "",$redis);
  65. }
  66. public function getBookTitle($book,$para){
  67. /*
  68. if($this->redis!==false){
  69. $result = $this->redis->hGet("pali_text://book",$book."-".$para);
  70. if($result!==FALSE){
  71. return $result;
  72. }
  73. }
  74. */
  75. $query = "SELECT title from "._TABLE_PALI_BOOK_NAME_." where book = ? and paragraph = ? limit 1";
  76. $stmt = $this->dbh->prepare($query);
  77. $stmt->execute(array($book, $para));
  78. $book = $stmt->fetch(PDO::FETCH_ASSOC);
  79. if($book){
  80. if($this->redis){
  81. //$this->redis->hSet("pali_text://book",$book."-".$para,$book["title"]);
  82. }
  83. return $book["title"];
  84. }
  85. else{
  86. return false;
  87. }
  88. }
  89. }
  90. ?>