9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/grauphel.htm
14 namespace OCA\Grauphel\Lib;
17 * Flat file storage for notes
21 * @author Christian Weiske <cweiske@cweiske.de>
22 * @copyright 2014 Christian Weiske
23 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24 * @version Release: @package_version@
25 * @link http://cweiske.de/grauphel.htm
30 * @var \OCP\IDBConnection
37 public function __construct($urlGen)
39 $this->urlGen = $urlGen;
40 $this->db = \OC::$server->getDatabaseConnection();
43 public function setUsername($username)
45 $this->username = $username;
49 * Create a new sync data object for fresh users.
50 * Used by loadSyncData()
52 * @return SyncData New synchronization statistics
54 protected function getNewSyncData()
56 $syncdata = new SyncData();
57 $syncdata->initNew($this->username);
61 public function getTags()
63 $result = $this->db->executeQuery(
64 'SELECT `note_tags` FROM `*PREFIX*grauphel_notes`'
65 . ' WHERE note_user = ?',
66 array($this->username)
70 while ($row = $result->fetch()) {
71 $tags = array_merge($tags, json_decode($row['note_tags']));
73 return array_unique($tags);
77 * Updates the given $note object with data from $noteUpdate.
78 * Sets the last-sync-revision to $syncRevision
80 * @param object $note Original note object
81 * @param object $noteUpdate Update note object from a PUT to the API
82 * @param integer $syncRevision Current sync revision number
86 public function update($note, $noteUpdate, $syncRevision)
88 static $updateFields = array(
91 'last-metadata-change-date',
93 'note-content-version',
101 foreach ($updateFields as $field) {
102 $changed[$field] = false;
103 if (isset($noteUpdate->$field)) {
104 if ($note->$field != $noteUpdate->$field) {
105 $note->$field = $noteUpdate->$field;
106 $changed[$field] = true;
111 if (!isset($noteUpdate->{'last-change-date'})
112 && ($changed['title'] || $changed['note-content'])
114 //no idea how to get the microseconds in there
115 $note->{'last-change-date'} = date('c');
118 if (!isset($noteUpdate->{'last-metadata-change-date'})) {
119 //no idea how to get the microseconds in there
120 $note->{'last-metadata-change-date'} = date('c');
123 if (isset($noteUpdate->{'node-content'})
124 && $note->{'note-content-version'} == 0
126 $note->{'note-content-version'} = 0.3;
129 $note->{'last-sync-revision'} = $syncRevision;
133 * Loads synchronization data for the given user.
134 * Creates fresh sync data if there are none for the user.
136 * @return SyncData Synchronization statistics (revision, sync guid)
138 public function loadSyncData()
140 $row = $this->db->executeQuery(
141 'SELECT * FROM `*PREFIX*grauphel_syncdata`'
142 . ' WHERE `syncdata_user` = ?',
143 array($this->username)
146 if ($row === false) {
147 $syncdata = $this->getNewSyncData();
148 $this->saveSyncData($syncdata);
150 $syncdata = new SyncData();
151 $syncdata->latestSyncRevision = (int) $row['syncdata_latest_sync_revision'];
152 $syncdata->currentSyncGuid = $row['syncdata_current_sync_guid'];
159 * Save synchronization data for the given user.
161 * @param SyncData $syncdata Synchronization data object
165 public function saveSyncData(SyncData $syncdata)
167 $row = $this->db->executeQuery(
168 'SELECT * FROM `*PREFIX*grauphel_syncdata`'
169 . ' WHERE `syncdata_user` = ?',
170 array($this->username)
173 if ($row === false) {
175 $sql = 'INSERT INTO `*PREFIX*grauphel_syncdata`'
176 . '(`syncdata_user`, `syncdata_latest_sync_revision`, `syncdata_current_sync_guid`)'
177 . ' VALUES(?, ?, ?)';
180 $syncdata->latestSyncRevision,
181 $syncdata->currentSyncGuid
186 'syncdata_latest_sync_revision' => $syncdata->latestSyncRevision,
187 'syncdata_current_sync_guid' => $syncdata->currentSyncGuid,
189 $sql = 'UPDATE `*PREFIX*grauphel_syncdata` SET'
190 . ' `' . implode('` = ?, `', array_keys($data)) . '` = ?'
191 . ' WHERE `syncdata_user` = ?';
192 $params = array_values($data);
193 $params[] = $this->username;
195 $this->db->executeQuery($sql, $params);
199 * Delete synchronization data for the given user.
201 * @param SyncData $syncdata Synchronization data object
205 public function deleteSyncData()
207 $this->db->executeQuery(
208 'DELETE FROM `*PREFIX*grauphel_syncdata`'
209 . ' WHERE `syncdata_user` = ?',
210 array($this->username)
215 * Load a note from the storage.
217 * @param string $guid Note identifier
218 * @param boolean $createNew Create a new note if it does not exist
220 * @return object Note object, NULL if !$createNew and note does not exist
222 public function load($guid, $createNew = true)
224 $row = $this->db->executeQuery(
225 'SELECT * FROM `*PREFIX*grauphel_notes`'
226 . ' WHERE `note_user` = ? AND `note_guid` = ?',
227 array($this->username, $guid)
230 if ($row === false) {
234 return (object) array(
237 'create-date' => null,
238 'last-change-date' => null,
239 'last-metadata-change-date' => null,
242 'note-content' => null,
243 'note-content-version' => 0.3,
245 'open-on-startup' => false,
251 return $this->noteFromRow($row);
255 * Load a GUID of a note by the note title.
257 * The note title is stored html-escaped in the database because we
258 * get it that way from tomboy. Thus we have to escape the search
261 * @param string $title Note title.
263 * @return string GUID, NULL if note could not be found
265 public function loadGuidByTitle($title)
267 $row = $this->db->executeQuery(
268 'SELECT note_guid FROM `*PREFIX*grauphel_notes`'
269 . ' WHERE `note_user` = ? AND `note_title` = ?',
270 array($this->username, htmlspecialchars($title))
273 if ($row === false) {
277 return $row['note_guid'];
283 * @param array $keywords arrays of query strings within keys AND and NOT
285 * @return array Database rows with note_guid and note_title
287 public function search($keywordGroups)
289 if (!isset($keywordGroups['AND'])) {
290 $keywordGroups['AND'] = array();
292 if (!isset($keywordGroups['NOT'])) {
293 $keywordGroups['NOT'] = array();
296 $sqlTplAnd = ' AND (note_title ILIKE ? OR note_tags ILIKE ? OR note_content ILIKE ?)';
297 $sqlTplNot = ' AND NOT (note_title ILIKE ? OR note_tags ILIKE ? OR note_content ILIKE ?)';
301 foreach (array('AND', 'NOT') as $group) {
302 $keywords = $keywordGroups[$group];
303 foreach ($keywords as $keyword) {
304 $arData[] = '%' . $keyword . '%';//title
305 $arData[] = '%' . $keyword . '%';//tags
306 $arData[] = '%' . $keyword . '%';//content
310 $result = $this->db->executeQuery(
311 'SELECT `note_guid`, `note_title`'
312 . ' FROM `*PREFIX*grauphel_notes`'
313 . ' WHERE note_user = ?'
314 . str_repeat($sqlTplAnd, count($keywordGroups['AND']))
315 . str_repeat($sqlTplNot, count($keywordGroups['NOT'])),
320 while ($row = $result->fetch()) {
327 * Save a note into storage.
329 * @param object $note Note to save
333 public function save($note)
335 $row = $this->db->executeQuery(
336 'SELECT * FROM `*PREFIX*grauphel_notes`'
337 . ' WHERE `note_user` = ? AND `note_guid` = ?',
338 array($this->username, $note->guid)
341 $data = $this->rowFromNote($note);
342 if ($row === false) {
344 $data['note_user'] = $this->username;
345 $sql = 'INSERT INTO `*PREFIX*grauphel_notes`'
346 . ' (`' . implode('`, `', array_keys($data)) . '`)'
347 . ' VALUES(' . implode(', ', array_fill(0, count($data), '?')) . ')';
348 $params = array_values($data);
351 $sql = 'UPDATE `*PREFIX*grauphel_notes` SET '
352 . '`' . implode('` = ?, `', array_keys($data)) . '` = ?'
353 . ' WHERE `note_user` = ? AND `note_guid` = ?';
354 $params = array_values($data);
355 $params[] = $this->username;
356 $params[] = $note->guid;
358 $this->db->executeQuery($sql, $params);
362 * Delete a note from storage.
364 * @param object $guid ID of the note
368 public function delete($guid)
370 $this->db->executeQuery(
371 'DELETE FROM `*PREFIX*grauphel_notes`'
372 . ' WHERE `note_user` = ? AND `note_guid` = ?',
373 array($this->username, $guid)
378 * Delete all notes from storage.
382 public function deleteAll()
384 $this->db->executeQuery(
385 'DELETE FROM `*PREFIX*grauphel_notes`'
386 . ' WHERE `note_user` = ?',
387 array($this->username)
392 * Load notes for the given user in short form.
393 * Optionally only those changed after $since revision
395 * @param integer $since Revision number after which the notes changed
396 * @param string $rawtag Filter by tag. Special tags:
397 * - grauphel:special:all
398 * - grauphel:special:untagged
399 * @param boolean $includeDate Load the last modification date or not
401 * @return array Array of short note objects
403 public function loadNotesOverview(
404 $since = null, $rawtag = null, $includeDate = false
406 $sql = 'SELECT `note_guid`, `note_title`'
407 . ', `note_last_sync_revision`, `note_tags`'
408 . ', `note_last_change_date`'
409 . ' FROM `*PREFIX*grauphel_notes`'
410 . ' WHERE note_user = ?';
411 $sqlData = array($this->username);
413 if ($since !== null) {
415 $sql .= ' AND note_last_sync_revision > ?';
418 if ($rawtag == 'grauphel:special:all') {
420 } else if ($rawtag == 'grauphel:special:untagged') {
421 $jsRawtag = json_encode(array());
423 $jsRawtag = json_encode($rawtag);
425 if ($rawtag !== null) {
426 $sqlData[] = '%' . $jsRawtag . '%';
427 $sql .= ' AND note_tags LIKE ?';
430 $result = $this->db->executeQuery($sql, $sqlData);
432 while ($row = $result->fetch()) {
434 'guid' => $row['note_guid'],
436 'api-ref' => $this->urlGen->getAbsoluteURL(
437 $this->urlGen->linkToRoute(
440 'username' => $this->username,
441 'guid' => $row['note_guid']
445 'href' => $this->urlGen->getAbsoluteURL(
446 $this->urlGen->linkToRoute(
449 'guid' => $row['note_guid']
454 'title' => $row['note_title'],
457 $note['last-change-date'] = $row['note_last_change_date'];
466 * Load notes for the given user in full form.
467 * Optionally only those changed after $since revision
469 * @param integer $since Revision number after which the notes changed
471 * @return array Array of full note objects
473 public function loadNotesFull($since = null)
475 $result = $this->db->executeQuery(
476 'SELECT * FROM `*PREFIX*grauphel_notes`'
477 . ' WHERE note_user = ?',
478 array($this->username)
482 while ($row = $result->fetch()) {
483 if ($since !== null && $row['note_last_sync_revision'] <= $since) {
486 $notes[] = $this->noteFromRow($row);
492 protected function fixDate($date)
494 if (strlen($date) == 32) {
495 //Bug in grauphel 0.1.1; date fields in DB had only 32 instead of 33
496 // characters. The last digit of the time zone was missing
502 protected function noteFromRow($row)
504 return (object) array(
505 'guid' => $row['note_guid'],
507 'create-date' => $this->fixDate($row['note_create_date']),
508 'last-change-date' => $this->fixDate($row['note_last_change_date']),
509 'last-metadata-change-date' => $this->fixDate($row['note_last_metadata_change_date']),
511 'title' => $row['note_title'],
512 'note-content' => $row['note_content'],
513 'note-content-version' => $row['note_content_version'],
515 'open-on-startup' => (bool) $row['note_open_on_startup'],
516 'pinned' => (bool) $row['note_pinned'],
517 'tags' => json_decode($row['note_tags']),
519 'last-sync-revision' => (int) $row['note_last_sync_revision'],
523 protected function rowFromNote($note)
526 'note_guid' => $note->guid,
527 'note_title' => (string) $note->title,
529 'note_content' => (string) $note->{'note-content'},
530 'note_content_version' => (string) $note->{'note-content-version'},
532 'note_create_date' => $note->{'create-date'},
533 'note_last_change_date' => $note->{'last-change-date'},
534 'note_last_metadata_change_date' => $note->{'last-metadata-change-date'},
536 'note_open_on_startup' => (int) $note->{'open-on-startup'},
537 'note_pinned' => (int) $note->pinned,
538 'note_tags' => json_encode($note->tags),
540 'note_last_sync_revision' => $note->{'last-sync-revision'},