depage-xmldb v2.3.1
XML documents in a relational database
Loading...
Searching...
No Matches
XmlDbHistory.php
Go to the documentation of this file.
1<?php
2
12
13namespace Depage\XmlDb;
14
16{
17 protected $pdo;
18 protected $xmlDb;
19 protected $dbNs;
20 protected $tableHistory;
21
22 protected $docIds = [];
23
24 protected $tablePrefix = 'dp_';
25 protected $tableDocs;
26
27 protected $options;
28
29 public $timestamp = null;
30
39 public function __construct($tablePrefix, $pdo, $cache, $options = [])
40 {
41 $this->pdo = $pdo;
42 $this->pdo->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_NATURAL);
43
44 $this->xmlDb = new XmlDb($tablePrefix, $pdo, $cache, $options);
45 $this->dbNs = new XmlNs('db', 'http://cms.depagecms.net/ns/database');
46
47 $this->tablePrefix = $tablePrefix;
48 $this->tableDocs = $tablePrefix . '_xmldocs';
49 $this->tableHistory = $tablePrefix . '_history';
50 }
51
61 public function docExists($docIdOrName)
62 {
63 $result = false;
64
65 $id = $this->xmlDb->docExists($docIdOrName);
66
67 $query = $this->pdo->prepare("
68 SELECT doc_id
69 FROM {$this->tableHistory}
70 WHERE doc_id = :id
71 LIMIT 1
72 ");
73
74 $query->execute([
75 'id' => $id,
76 ]);
77
78 if ($query->fetch()) {
79 $result = $id;
80 }
81
82 return $result;
83 }
84
92 public function getDocInfo($docIdOrName)
93 {
94 return $this->xmlDb->getDocInfo($docIdOrName);
95 }
96
107 public function getDocXml($docIdOrName, $addIdAttribute = true)
108 {
109 $xml = false;
110
111 if ($docId = $this->docExists($docIdOrName)) {
112 $doc = new Document($this, $docId);
113 $history = $doc->getHistory();
114
115 if (!is_null($this->timestamp)) {
116 $xml = $history->getXml($this->timestamp, $addIdAttribute);
117 } else {
118 $xml = $history->getLastPublishedXml($addIdAttribute);
119 }
120 }
121
122 return $xml;
123 }
124
134 public function getDocXmlXpath($docIdOrName, $xpath, $addIdAttribute = true)
135 {
136 $xml = false;
137
138 if ($docId = $this->docExists($docIdOrName)) {
139 $xmlFull = $this->getDocXml($docIdOrName, $addIdAttribute);
140 if ($xmlFull === false) {
141 throw new \Exception("Document could not be read from history");
142 }
143
144 $domXpath = new \DomXpath($xmlFull);
145 $list = $domXpath->query($xpath);
146
147 if ($list->length > 0) {
148 $xml = new \Depage\Xml\Document();
149 $xml->appendChild($xml->importNode($list->item(0), true));
150
151 return $xml;
152 }
153 }
154
155 return $xml;
156 }
157
164 public function __get($property)
165 {
166 if (property_exists($this, $property)) {
167 return $this->$property;
168 }
169 }
170
173 public function __sleep()
174 {
175 return [
176 'pdo',
177 'xmlDb',
178 'dbNs',
179 'tablePrefix',
180 'tableDocs',
181 'tableHistory',
182 'options',
183 ];
184 }
185}
186
187/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
docExists($docIdOrName)
gets the doc-id of a xml-document by name or id and checks if the document has at least one version i...
__get($property)
Get properties (basically read-only).
getDocXml($docIdOrName, $addIdAttribute=true)
gets a published version of a document as a DOMDocument
getDocXmlXpath($docIdOrName, $xpath, $addIdAttribute=true)
gets the first node of a published version of a document that matches an xpath query
__construct($tablePrefix, $pdo, $cache, $options=[])
creates an instance of the read only view on the published versions
__sleep()
allows Depage\Db\Pdo-object to be serialized
getDocInfo($docIdOrName)
gets the info of a document
A namespace prefix and the URI it is bound to.
Definition XmlNs.php:9
Interface of a read only accessor for xml documents.
Definition XmlGetter.php:12