page_cache.cls.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. /**
  9. * Caches individual rendered PDF pages
  10. *
  11. * Not totally implemented yet. Use at your own risk ;)
  12. *
  13. * @access private
  14. * @package dompdf
  15. * @static
  16. */
  17. class Page_Cache {
  18. const DB_USER = "dompdf_page_cache";
  19. const DB_PASS = "some meaningful password";
  20. const DB_NAME = "dompdf_page_cache";
  21. static private $__connection = null;
  22. static function init() {
  23. if ( is_null(self::$__connection) ) {
  24. $con_str = "host=" . DB_HOST .
  25. " dbname=" . self::DB_NAME .
  26. " user=" . self::DB_USER .
  27. " password=" . self::DB_PASS;
  28. if ( !self::$__connection = pg_connect($con_str) )
  29. throw new Exception("Database connection failed.");
  30. }
  31. }
  32. function __construct() { throw new Exception("Can not create instance of Page_Class. Class is static."); }
  33. private static function __query($sql) {
  34. if ( !($res = pg_query(self::$__connection, $sql)) )
  35. throw new Exception(pg_last_error(self::$__connection));
  36. return $res;
  37. }
  38. static function store_page($id, $page_num, $data) {
  39. $where = "WHERE id='" . pg_escape_string($id) . "' AND ".
  40. "page_num=". pg_escape_string($page_num);
  41. $res = self::__query("SELECT timestamp FROM page_cache ". $where);
  42. $row = pg_fetch_assoc($res);
  43. if ( $row )
  44. self::__query("UPDATE page_cache SET data='" . pg_escape_string($data) . "' " . $where);
  45. else
  46. self::__query("INSERT INTO page_cache (id, page_num, data) VALUES ('" . pg_escape_string($id) . "', ".
  47. pg_escape_string($page_num) . ", ".
  48. "'". pg_escape_string($data) . "')");
  49. }
  50. static function store_fonts($id, $fonts) {
  51. self::__query("BEGIN");
  52. // Update the font information
  53. self::__query("DELETE FROM page_fonts WHERE id='" . pg_escape_string($id) . "'");
  54. foreach (array_keys($fonts) as $font)
  55. self::__query("INSERT INTO page_fonts (id, font_name) VALUES ('" .
  56. pg_escape_string($id) . "', '" . pg_escape_string($font) . "')");
  57. self::__query("COMMIT");
  58. }
  59. // static function retrieve_page($id, $page_num) {
  60. // $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ".
  61. // "page_num=". pg_escape_string($page_num));
  62. // $row = pg_fetch_assoc($res);
  63. // return pg_unescape_bytea($row["data"]);
  64. // }
  65. static function get_page_timestamp($id, $page_num) {
  66. $res = self::__query("SELECT timestamp FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ".
  67. "page_num=". pg_escape_string($page_num));
  68. $row = pg_fetch_assoc($res);
  69. return $row["timestamp"];
  70. }
  71. // Adds the cached document referenced by $id to the provided pdf
  72. static function insert_cached_document(CPDF_Adapter $pdf, $id, $new_page = true) {
  73. $res = self::__query("SELECT font_name FROM page_fonts WHERE id='" . pg_escape_string($id) . "'");
  74. // Ensure that the fonts needed by the cached document are loaded into
  75. // the pdf
  76. while ($row = pg_fetch_assoc($res))
  77. $pdf->get_cpdf()->selectFont($row["font_name"]);
  78. $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "'");
  79. if ( $new_page )
  80. $pdf->new_page();
  81. $first = true;
  82. while ($row = pg_fetch_assoc($res)) {
  83. if ( !$first )
  84. $pdf->new_page();
  85. else
  86. $first = false;
  87. $page = $pdf->reopen_serialized_object($row["data"]);
  88. //$pdf->close_object();
  89. $pdf->add_object($page, "add");
  90. }
  91. }
  92. }
  93. Page_Cache::init();