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
32 public function __construct($urlGen)
34 $this->urlGen = $urlGen;
37 public function setUsername($username)
39 $this->username = $username;
43 * Create a new sync data object for fresh users.
44 * Used by loadSyncData()
46 * @return SyncData New synchronization statistics
48 protected function getNewSyncData()
50 $syncdata = new SyncData();
51 $syncdata->initNew($this->username);
55 public function getTags()
60 * Updates the given $note object with data from $noteUpdate.
61 * Sets the last-sync-revision to $syncRevision
63 * @param object $note Original note object
64 * @param object $noteUpdate Update note object from a PUT to the API
65 * @param integer $syncRevision Current sync revision number
69 public function update($note, $noteUpdate, $syncRevision)
71 static $updateFields = array(
74 'last-metadata-change-date',
76 'note-content-version',
84 foreach ($updateFields as $field) {
85 $changed[$field] = false;
86 if (isset($noteUpdate->$field)) {
87 if ($note->$field != $noteUpdate->$field) {
88 $note->$field = $noteUpdate->$field;
89 $changed[$field] = true;
94 if (!isset($noteUpdate->{'last-change-date'})
95 && ($changed['title'] || $changed['note-content'])
97 //no idea how to get the microseconds in there
98 $note->{'last-change-date'} = date('c');
101 if (!isset($noteUpdate->{'last-metadata-change-date'})) {
102 //no idea how to get the microseconds in there
103 $note->{'last-metadata-change-date'} = date('c');
106 if (isset($noteUpdate->{'node-content'})
107 && $note->{'note-content-version'} == 0
109 $note->{'note-content-version'} = 0.3;
112 $note->{'last-sync-revision'} = $syncRevision;
116 * Loads synchronization data for the given user.
117 * Creates fresh sync data if there are none for the user.
119 * @return SyncData Synchronization statistics (revision, sync guid)
121 public function loadSyncData()
123 $row = \OC_DB::executeAudited(
124 'SELECT * FROM `*PREFIX*grauphel_syncdata`'
125 . ' WHERE `syncdata_user` = ?',
126 array($this->username)
129 if ($row === false) {
130 $syncdata = $this->getNewSyncData($this->username);
131 $this->saveSyncData($this->username, $syncdata);
133 $syncdata = new SyncData();
134 $syncdata->latestSyncRevision = (int) $row['syncdata_latest_sync_revision'];
135 $syncdata->currentSyncGuid = $row['syncdata_current_sync_guid'];
142 * Save synchronization data for the given user.
144 * @param SyncData $syncdata Synchronization data object
148 public function saveSyncData(SyncData $syncdata)
150 $row = \OC_DB::executeAudited(
151 'SELECT * FROM `*PREFIX*grauphel_syncdata`'
152 . ' WHERE `syncdata_user` = ?',
153 array($this->username)
156 if ($row === false) {
158 $sql = 'INSERT INTO `*PREFIX*grauphel_syncdata`'
159 . '(`syncdata_user`, `syncdata_latest_sync_revision`, `syncdata_current_sync_guid`)'
160 . ' VALUES(?, ?, ?)';
163 $syncdata->latestSyncRevision,
164 $syncdata->currentSyncGuid
169 'syncdata_latest_sync_revision' => $syncdata->latestSyncRevision,
170 'syncdata_current_sync_guid' => $syncdata->currentSyncGuid,
172 $sql = 'UPDATE `*PREFIX*grauphel_syncdata` SET'
173 . ' `' . implode('` = ?, `', array_keys($data)) . '` = ?'
174 . ' WHERE `syncdata_user` = ?';
175 $params = array_values($data);
176 $params[] = $this->username;
178 \OC_DB::executeAudited($sql, $params);
182 * Load a note from the storage.
184 * @param string $guid Note identifier
185 * @param boolean $createNew Create a new note if it does not exist
187 * @return object Note object, NULL if !$createNew and note does not exist
189 public function load($guid, $createNew = true)
191 $row = \OC_DB::executeAudited(
192 'SELECT * FROM `*PREFIX*grauphel_notes`'
193 . ' WHERE `note_user` = ? AND `note_guid` = ?',
194 array($this->username, $guid)
197 if ($row === false) {
201 return (object) array(
204 'create-date' => null,
205 'last-change-date' => null,
206 'last-metadata-change-date' => null,
209 'note-content' => null,
210 'note-content-version' => 0.3,
212 'open-on-startup' => false,
218 return $this->noteFromRow($row);
222 * Save a note into storage.
224 * @param object $note Note to save
228 public function save($note)
230 $row = \OC_DB::executeAudited(
231 'SELECT * FROM `*PREFIX*grauphel_notes`'
232 . ' WHERE `note_user` = ? AND `note_guid` = ?',
233 array($this->username, $note->guid)
236 $data = $this->rowFromNote($note);
237 if ($row === false) {
239 $data['note_user'] = $this->username;
240 $sql = 'INSERT INTO `*PREFIX*grauphel_notes`'
241 . ' (`' . implode('`, `', array_keys($data)) . '`)'
242 . ' VALUES(' . implode(', ', array_fill(0, count($data), '?')) . ')';
243 $params = array_values($data);
246 $sql = 'UPDATE `*PREFIX*grauphel_notes` SET '
247 . '`' . implode('` = ?, `', array_keys($data)) . '` = ?'
248 . ' WHERE `note_user` = ? AND `note_guid` = ?';
249 $params = array_values($data);
250 $params[] = $this->username;
251 $params[] = $note->guid;
253 \OC_DB::executeAudited($sql, $params);
257 * Delete a note from storage.
259 * @param object $guid ID of the note
263 public function delete($guid)
265 \OC_DB::executeAudited(
266 'DELETE FROM `*PREFIX*grauphel_notes`'
267 . ' WHERE `note_user` = ? AND `note_guid` = ?',
268 array($this->username, $guid)
273 * Load notes for the given user in short form.
274 * Optionally only those changed after $since revision
276 * @param integer $since Revision number after which the notes changed
278 * @return array Array of short note objects
280 public function loadNotesOverview($since = null)
282 $result = \OC_DB::executeAudited(
283 'SELECT `note_guid`, `note_title`, `note_last_sync_revision`'
284 . ' FROM `*PREFIX*grauphel_notes`'
285 . ' WHERE note_user = ?',
286 array($this->username)
290 while ($row = $result->fetchRow()) {
291 if ($since !== null && $row['note_last_sync_revision'] <= $since) {
295 'guid' => $row['note_guid'],
297 'api-ref' => $this->urlGen->getAbsoluteURL(
298 $this->urlGen->linkToRoute(
301 'username' => $this->username,
302 'guid' => $row['note_guid']
306 'href' => null,//FIXME
308 'title' => $row['note_title'],
316 * Load notes for the given user in full form.
317 * Optionally only those changed after $since revision
319 * @param integer $since Revision number after which the notes changed
321 * @return array Array of full note objects
323 public function loadNotesFull($since = null)
325 $result = \OC_DB::executeAudited(
326 'SELECT * FROM `*PREFIX*grauphel_notes`'
327 . ' WHERE note_user = ?',
328 array($this->username)
332 while ($row = $result->fetchRow()) {
333 if ($since !== null && $row['note_last_sync_revision'] <= $since) {
336 $notes[] = $this->noteFromRow($row);
342 protected function noteFromRow($row)
344 return (object) array(
345 'guid' => $row['note_guid'],
347 'create-date' => $row['note_create_date'],
348 'last-change-date' => $row['note_last_change_date'],
349 'last-metadata-change-date' => $row['note_last_metadata_change_date'],
351 'title' => $row['note_title'],
352 'note-content' => $row['note_content'],
353 'note-content-version' => $row['note_content_version'],
355 'open-on-startup' => (bool) $row['note_open_on_startup'],
356 'pinned' => (bool) $row['note_pinned'],
357 'tags' => json_decode($row['note_tags']),
359 'last-sync-revision' => (int) $row['note_last_sync_revision'],
363 protected function rowFromNote($note)
366 'note_guid' => $note->guid,
367 'note_title' => $note->title,
369 'note_content' => $note->{'note-content'},
370 'note_content_version' => $note->{'note-content-version'},
372 'note_create_date' => $note->{'create-date'},
373 'note_last_change_date' => $note->{'last-change-date'},
374 'note_last_metadata_change_date' => $note->{'last-metadata-change-date'},
376 'note_open_on_startup' => $note->{'open-on-startup'},
377 'note_pinned' => $note->pinned,
378 'note_tags' => json_encode($note->tags),
380 'note_last_sync_revision' => $note->{'last-sync-revision'},