2 require_once 'persistance.php';
4 class Mark
implements sql
, persistance
12 var $startNodePath, $startOffset;
13 var $endNodePath, $endOffset;
16 function __construct($id = NULL)
18 $this->creationDate
= time();
24 static function get_table_name()
29 static function sql_select($wildcards = NULL)
31 $tableName = self
::get_table_name();
33 $order = ' order by creationDate desc, pageUrl ';
34 if (array_key_exists('order', $wildcards)) {
35 $order = " order by ". $wildcards['order'];
36 unset($wildcards['order']);
40 if (array_key_exists('limit', $wildcards)) {
41 $limit = " limit ". $wildcards['limit'];
42 unset($wildcards['limit']);
45 if (empty($wildcards)) {
46 return "SELECT id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from $tableName $order $limit;";
50 if (array_key_exists('pageUrl', $wildcards)) {
51 $tuples []= " pageUrl LIKE '". $wildcards['pageUrl'] ."%'";
52 unset($wildcards['pageUrl']);
55 foreach ($wildcards as $key => $value) {
56 $tuples []= "$key='$value'";
59 $conditions = implode(' and ', $tuples);
61 $query = "SELECT id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from $tableName where $conditions $order $limit;";
66 function toHTMLanchor()
68 $anchor = "<a href='". $this->pageUrl
."' >". $this->text
."</a>";
72 function toSQLinsert()
75 $creationDate = date('c', $this->creationDate
);
76 $pageUrl = $this->pageUrl
;
78 $owner = $this->owner
;
79 $startNodePath = $this->startNodePath
;
80 $startOffset = $this->startOffset
;
81 $endNodePath = $this->endNodePath
;
82 $endOffset = $this->endOffset
;
84 $tableName = Mark
::get_table_name();
85 return "INSERT into $tableName (id, creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset) values ('$id', '$creationDate', '$pageUrl', '$text', '$owner', '$startNodePath', '$startOffset', '$endNodePath', '$endOffset');";
88 function toSQLselect()
90 return "SELECT creationDate, pageUrl, text, owner, startNodePath, startOffset, endNodePath, endOffset from marks where id='". $this->id
."';";
93 static function set_db($db, $user = "", $password = "")
95 if ($db instanceof PDO
) {
99 self
::$db =& new PDO($db);
101 self
::$db =& new PDO($db, $user, $password);
108 static function count($wildcards = NULL, $db = NULL)
113 if (! $db instanceof PDO
) {
114 throw new Exception("Not a Data Base");
117 $tableName = Mark
::get_table_name();
118 $query = "select count(*) from $tableName";
119 if ($result = $db->query($query)) {
120 $result = $result->fetchAll();
123 print_r($db->errorInfo());
124 throw new Exception("Query error");
127 return $result[0][0];
130 static function select($wildcards = NULL, $db = NULL)
135 if (! $db instanceof PDO
) {
136 throw new Exception("Not a Data Base");
139 $query = self
::sql_select($wildcards);
140 if ($result = $db->query($query)) {
141 $result = $result->fetchAll();
144 print_r($db->errorInfo());
145 throw new Exception("Query error");
147 foreach ($result as $r) {
148 $mark = new Mark($r['id']);
149 $mark->creationDate
= strtotime($r['creationDate']);
150 $mark->pageUrl
= strip_http_get_params($r['pageUrl']);
151 $mark->text
= $r['text'];
152 $mark->owner
= $r['owner'];
153 $mark->startNodePath
= json_decode($r['startNodePath']);
154 $mark->startOffset
= $r['startOffset'];
155 $mark->endNodePath
= json_decode($r['endNodePath']);
156 $mark->endOffset
= $r['endOffset'];
157 array_push( $marks, $mark);
163 function save($db = NULL)
168 $query = $this->toSQLinsert();
170 $db->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
171 $statement = $db->query($query);
176 function load($db = NULL)
181 if (is_string($db)) {
182 $db = new PDO($db, $user, $pass);
184 $query = $this->toSQLselect();
185 if ($result = $db->query($query)) {
186 $result = $result->fetch();
188 throw new Exception("Query error");
191 $this->creationDate
= strtotime($result['creationDate']);
192 $this->pageUrl
= $result['pageUrl'];
193 $this->text
= $result['text'];
194 $this->owner
= $result['owner'];
195 $this->startNodePath
= $result['startNodePath'];
196 $this->startOffset
= $result['startOffset'];
197 $this->endNodePath
= $result['endNodePath'];
198 $this->endOffset
= $result['endOffset'];
203 function __toString()
205 return json_encode($this);
209 function strip_http_get_params($url)
211 if (!$urlBaseLength = strpos($url, '?')) return $url;
212 return substr($url, 0, $urlBaseLength);