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
31 public function __construct($urlGen)
33 $this->urlGen = $urlGen;
36 * Create a new sync data object for fresh users.
37 * Used by loadSyncData()
39 * @param string $username User name
41 * @return SyncData New synchronization statistics
43 protected function getNewSyncData($username)
45 $syncdata = new SyncData();
46 $syncdata->initNew($username);
51 * Updates the given $note object with data from $noteUpdate.
52 * Sets the last-sync-revision to $syncRevision
54 * @param object $note Original note object
55 * @param object $noteUpdate Update note object from a PUT to the API
56 * @param integer $syncRevision Current sync revision number
60 public function update($note, $noteUpdate, $syncRevision)
62 static $updateFields = array(
65 'last-metadata-change-date',
67 'note-content-version',
75 foreach ($updateFields as $field) {
76 $changed[$field] = false;
77 if (isset($noteUpdate->$field)) {
78 if ($note->$field != $noteUpdate->$field) {
79 $note->$field = $noteUpdate->$field;
80 $changed[$field] = true;
85 if (!isset($noteUpdate->{'last-change-date'})
86 && ($changed['title'] || $changed['note-content'])
88 //no idea how to get the microseconds in there
89 $note->{'last-change-date'} = date('c');
92 if (!isset($noteUpdate->{'last-metadata-change-date'})) {
93 //no idea how to get the microseconds in there
94 $note->{'last-metadata-change-date'} = date('c');
97 if (isset($noteUpdate->{'node-content'})
98 && $note->{'note-content-version'} == 0
100 $note->{'note-content-version'} = 0.3;
103 $note->{'last-sync-revision'} = $syncRevision;
107 * Loads synchronization data for the given user.
108 * Creates fresh sync data if there are none for the user.
110 * @param string $username User name
112 * @return SyncData Synchronization statistics (revision, sync guid)
114 public function loadSyncData($username)
116 $row = \OC_DB::executeAudited(
117 'SELECT * FROM `*PREFIX*grauphel_syncdata`'
118 . ' WHERE `syncdata_user` = ?',
122 if ($row === false) {
123 $syncdata = $this->getNewSyncData($username);
124 $this->saveSyncData($username, $syncdata);
126 $syncdata = new SyncData();
127 $syncdata->latestSyncRevision = (int) $row['syncdata_latest_sync_revision'];
128 $syncdata->currentSyncGuid = $row['syncdata_current_sync_guid'];
135 * Save synchronization data for the given user.
137 * @param string $username User name
138 * @param SyncData $syncdata Synchronization data object
142 public function saveSyncData($username, SyncData $syncdata)
144 $row = \OC_DB::executeAudited(
145 'SELECT * FROM `*PREFIX*grauphel_syncdata`'
146 . ' WHERE `syncdata_user` = ?',
150 if ($row === false) {
152 $sql = 'INSERT INTO `*PREFIX*grauphel_syncdata`'
153 . '(`syncdata_user`, `syncdata_latest_sync_revision`, `syncdata_current_sync_guid`)'
154 . ' VALUES(?, ?, ?)';
157 $syncdata->latestSyncRevision,
158 $syncdata->currentSyncGuid
163 'syncdata_latest_sync_revision' => $syncdata->latestSyncRevision,
164 'syncdata_current_sync_guid' => $syncdata->currentSyncGuid,
166 $sql = 'UPDATE `*PREFIX*grauphel_syncdata` SET'
167 . ' `' . implode('` = ?, `', array_keys($data)) . '` = ?'
168 . ' WHERE `syncdata_user` = ?';
169 $params = array_values($data);
170 $params[] = $username;
172 \OC_DB::executeAudited($sql, $params);
176 * Load a note from the storage.
178 * @param string $username User name
179 * @param string $guid Note identifier
180 * @param boolean $createNew Create a new note if it does not exist
182 * @return object Note object, NULL if !$createNew and note does not exist
184 public function load($username, $guid, $createNew = true)
186 $row = \OC_DB::executeAudited(
187 'SELECT * FROM `*PREFIX*grauphel_notes`'
188 . ' WHERE `note_user` = ? AND `note_guid` = ?',
189 array($username, $guid)
192 if ($row === false) {
196 return (object) array(
199 'create-date' => null,
200 'last-change-date' => null,
201 'last-metadata-change-date' => null,
204 'note-content' => null,
205 'note-content-version' => 0.3,
207 'open-on-startup' => false,
213 return $this->noteFromRow($row);
217 * Save a note into storage.
219 * @param string $username User name
220 * @param object $note Note to save
224 public function save($username, $note)
226 $row = \OC_DB::executeAudited(
227 'SELECT * FROM `*PREFIX*grauphel_notes`'
228 . ' WHERE `note_user` = ? AND `note_guid` = ?',
229 array($username, $note->guid)
232 $data = $this->rowFromNote($note);
233 if ($row === false) {
235 $data['note_user'] = $username;
236 $sql = 'INSERT INTO `*PREFIX*grauphel_notes`'
237 . ' (`' . implode('`, `', array_keys($data)) . '`)'
238 . ' VALUES(' . implode(', ', array_fill(0, count($data), '?')) . ')';
239 $params = array_values($data);
242 $sql = 'UPDATE `*PREFIX*grauphel_notes` SET '
243 . '`' . implode('` = ?, `', array_keys($data)) . '` = ?'
244 . ' WHERE `note_user` = ? AND `note_guid` = ?';
245 $params = array_values($data);
246 $params[] = $username;
247 $params[] = $note->guid;
249 \OC_DB::executeAudited($sql, $params);
253 * Delete a note from storage.
255 * @param string $username User name
256 * @param object $guid ID of the note
260 public function delete($username, $guid)
262 \OC_DB::executeAudited(
263 'DELETE FROM `*PREFIX*grauphel_notes`'
264 . ' WHERE `note_user` = ? AND `note_guid` = ?',
265 array($username, $guid)
270 * Load notes for the given user in short form.
271 * Optionally only those changed after $since revision
273 * @param string $username User name
274 * @param integer $since Revision number after which the notes changed
276 * @return array Array of short note objects
278 public function loadNotesOverview($username, $since = null)
280 $result = \OC_DB::executeAudited(
281 'SELECT `note_guid`, `note_title`, `note_last_sync_revision`'
282 . ' FROM `*PREFIX*grauphel_notes`'
283 . ' WHERE note_user = ?',
288 while ($row = $result->fetchRow()) {
289 if ($since !== null && $row['note_last_sync_revision'] <= $since) {
293 'guid' => $row['note_guid'],
295 'api-ref' => $this->urlGen->getAbsoluteURL(
296 $this->urlGen->linkToRoute(
299 'username' => $username,
300 'guid' => $row['note_guid']
304 'href' => null,//FIXME
306 'title' => $row['note_title'],
314 * Load notes for the given user in full form.
315 * Optionally only those changed after $since revision
317 * @param string $username User name
318 * @param integer $since Revision number after which the notes changed
320 * @return array Array of full note objects
322 public function loadNotesFull($username, $since = null)
324 $result = \OC_DB::executeAudited(
325 'SELECT * FROM `*PREFIX*grauphel_notes`'
326 . ' WHERE note_user = ?',
331 while ($row = $result->fetchRow()) {
332 if ($since !== null && $row['note_last_sync_revision'] <= $since) {
335 $notes[] = $this->noteFromRow($row);
341 protected function noteFromRow($row)
343 return (object) array(
344 'guid' => $row['note_guid'],
346 'create-date' => $row['note_create_date'],
347 'last-change-date' => $row['note_last_change_date'],
348 'last-metadata-change-date' => $row['note_last_metadata_change_date'],
350 'title' => $row['note_title'],
351 'note-content' => $row['note_content'],
352 'note-content-version' => $row['note_content_version'],
354 'open-on-startup' => (bool) $row['note_open_on_startup'],
355 'pinned' => (bool) $row['note_pinned'],
356 'tags' => json_decode($row['note_tags']),
358 'last-sync-revision' => (int) $row['note_last_sync_revision'],
362 protected function rowFromNote($note)
365 'note_guid' => $note->guid,
366 'note_title' => $note->title,
368 'note_content' => $note->{'note-content'},
369 'note_content_version' => $note->{'note-content-version'},
371 'note_create_date' => $note->{'create-date'},
372 'note_last_change_date' => $note->{'last-change-date'},
373 'note_last_metadata_change_date' => $note->{'last-metadata-change-date'},
375 'note_open_on_startup' => $note->{'open-on-startup'},
376 'note_pinned' => $note->pinned,
377 'note_tags' => json_encode($note->tags),
379 'note_last_sync_revision' => $note->{'last-sync-revision'},