aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2014-02-19 13:47:11 +0100
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2014-02-19 13:47:11 +0100
commit60ca369cd328f4eae31005c547b9419a5ab33324 (patch)
treef9ab84e607cb54bdb0dc1d0027435af213a722f3 /inc
parent5e98c2183ae824ab6a1dd2dc22f8619a0d718977 (diff)
parentb89d5a2bf48c2c1eb796963b3401aca498618ec4 (diff)
downloadwallabag-60ca369cd328f4eae31005c547b9419a5ab33324.tar.gz
wallabag-60ca369cd328f4eae31005c547b9419a5ab33324.tar.zst
wallabag-60ca369cd328f4eae31005c547b9419a5ab33324.zip
Merge pull request #468 from wallabag/dev1.5.1
1.5.1
Diffstat (limited to 'inc')
-rw-r--r--inc/poche/Database.class.php40
-rw-r--r--inc/poche/Poche.class.php61
2 files changed, 88 insertions, 13 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php
index 9f553fa1..3332b5a3 100644
--- a/inc/poche/Database.class.php
+++ b/inc/poche/Database.class.php
@@ -241,6 +241,22 @@ class Database {
241 return isset($entry[0]) ? $entry[0] : null; 241 return isset($entry[0]) ? $entry[0] : null;
242 } 242 }
243 243
244 public function retrieveOneByURL($url, $user_id) {
245 $entry = NULL;
246 $sql = "SELECT * FROM entries WHERE url=? AND user_id=?";
247 $params = array($url, $user_id);
248 $query = $this->executeQuery($sql, $params);
249 $entry = $query->fetchAll();
250
251 return isset($entry[0]) ? $entry[0] : null;
252 }
253
254 public function reassignTags($old_entry_id, $new_entry_id) {
255 $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?";
256 $params = array($new_entry_id, $old_entry_id);
257 $query = $this->executeQuery($sql, $params);
258 }
259
244 public function getEntriesByView($view, $user_id, $limit = '') { 260 public function getEntriesByView($view, $user_id, $limit = '') {
245 switch ($_SESSION['sort']) 261 switch ($_SESSION['sort'])
246 { 262 {
@@ -328,30 +344,36 @@ class Database {
328 return $this->getHandle()->lastInsertId($column); 344 return $this->getHandle()->lastInsertId($column);
329 } 345 }
330 346
331 public function retrieveAllTags() { 347 public function retrieveAllTags($user_id) {
332 $sql = "SELECT * FROM tags"; 348 $sql = "SELECT tags.* FROM tags
333 $query = $this->executeQuery($sql, array()); 349 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
350 LEFT JOIN entries ON tags_entries.entry_id=entries.id
351 WHERE entries.user_id=?";
352 $query = $this->executeQuery($sql, array($user_id));
334 $tags = $query->fetchAll(); 353 $tags = $query->fetchAll();
335 354
336 return $tags; 355 return $tags;
337 } 356 }
338 357
339 public function retrieveTag($id) { 358 public function retrieveTag($id, $user_id) {
340 $tag = NULL; 359 $tag = NULL;
341 $sql = "SELECT * FROM tags WHERE id=?"; 360 $sql = "SELECT tags.* FROM tags
342 $params = array(intval($id)); 361 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
362 LEFT JOIN entries ON tags_entries.entry_id=entries.id
363 WHERE tags.id=? AND entries.user_id=?";
364 $params = array(intval($id), $user_id);
343 $query = $this->executeQuery($sql, $params); 365 $query = $this->executeQuery($sql, $params);
344 $tag = $query->fetchAll(); 366 $tag = $query->fetchAll();
345 367
346 return isset($tag[0]) ? $tag[0] : null; 368 return isset($tag[0]) ? $tag[0] : null;
347 } 369 }
348 370
349 public function retrieveEntriesByTag($tag_id) { 371 public function retrieveEntriesByTag($tag_id, $user_id) {
350 $sql = 372 $sql =
351 "SELECT entries.* FROM entries 373 "SELECT entries.* FROM entries
352 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id 374 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
353 WHERE tags_entries.tag_id = ?"; 375 WHERE tags_entries.tag_id = ? AND entries.user_id=?";
354 $query = $this->executeQuery($sql, array($tag_id)); 376 $query = $this->executeQuery($sql, array($tag_id, $user_id));
355 $entries = $query->fetchAll(); 377 $entries = $query->fetchAll();
356 378
357 return $entries; 379 return $entries;
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 5eba3564..753bd7f0 100644
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -374,6 +374,11 @@ class Poche
374 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); 374 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
375 $body = $content['rss']['channel']['item']['description']; 375 $body = $content['rss']['channel']['item']['description'];
376 376
377 //search for possible duplicate if not in import mode
378 if (!$import) {
379 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId());
380 }
381
377 if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { 382 if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) {
378 Tools::logm('add link ' . $url->getUrl()); 383 Tools::logm('add link ' . $url->getUrl());
379 $sequence = ''; 384 $sequence = '';
@@ -386,6 +391,20 @@ class Poche
386 Tools::logm('updating content article'); 391 Tools::logm('updating content article');
387 $this->store->updateContent($last_id, $content, $this->user->getId()); 392 $this->store->updateContent($last_id, $content, $this->user->getId());
388 } 393 }
394
395 if ($duplicate != NULL) {
396 // duplicate exists, so, older entry needs to be deleted (as new entry should go to the top of list), BUT favorite mark and tags should be preserved
397 Tools::logm('link ' . $url->getUrl() . ' is a duplicate');
398 // 1) - preserve tags and favorite, then drop old entry
399 $this->store->reassignTags($duplicate['id'], $last_id);
400 if ($duplicate['is_fav']) {
401 $this->store->favoriteById($last_id, $this->user->getId());
402 }
403 if ($this->store->deleteById($duplicate['id'], $this->user->getId())) {
404 Tools::logm('previous link ' . $url->getUrl() .' entry deleted');
405 }
406 }
407
389 if (!$import) { 408 if (!$import) {
390 $this->messages->add('s', _('the link has been added successfully')); 409 $this->messages->add('s', _('the link has been added successfully'));
391 } 410 }
@@ -444,6 +463,12 @@ class Poche
444 case 'add_tag' : 463 case 'add_tag' :
445 $tags = explode(',', $_POST['value']); 464 $tags = explode(',', $_POST['value']);
446 $entry_id = $_POST['entry_id']; 465 $entry_id = $_POST['entry_id'];
466 $entry = $this->store->retrieveOneById($entry_id, $this->user->getId());
467 if (!$entry) {
468 $this->messages->add('e', _('Article not found!'));
469 Tools::logm('error : article not found');
470 Tools::redirect();
471 }
447 foreach($tags as $key => $tag_value) { 472 foreach($tags as $key => $tag_value) {
448 $value = trim($tag_value); 473 $value = trim($tag_value);
449 $tag = $this->store->retrieveTagByValue($value); 474 $tag = $this->store->retrieveTagByValue($value);
@@ -468,6 +493,12 @@ class Poche
468 break; 493 break;
469 case 'remove_tag' : 494 case 'remove_tag' :
470 $tag_id = $_GET['tag_id']; 495 $tag_id = $_GET['tag_id'];
496 $entry = $this->store->retrieveOneById($id, $this->user->getId());
497 if (!$entry) {
498 $this->messages->add('e', _('Article not found!'));
499 Tools::logm('error : article not found');
500 Tools::redirect();
501 }
471 $this->store->removeTagForEntry($id, $tag_id); 502 $this->store->removeTagForEntry($id, $tag_id);
472 Tools::redirect(); 503 Tools::redirect();
473 break; 504 break;
@@ -506,6 +537,12 @@ class Poche
506 break; 537 break;
507 case 'edit-tags': 538 case 'edit-tags':
508 # tags 539 # tags
540 $entry = $this->store->retrieveOneById($id, $this->user->getId());
541 if (!$entry) {
542 $this->messages->add('e', _('Article not found!'));
543 Tools::logm('error : article not found');
544 Tools::redirect();
545 }
509 $tags = $this->store->retrieveTagsByEntry($id); 546 $tags = $this->store->retrieveTagsByEntry($id);
510 $tpl_vars = array( 547 $tpl_vars = array(
511 'entry_id' => $id, 548 'entry_id' => $id,
@@ -513,8 +550,8 @@ class Poche
513 ); 550 );
514 break; 551 break;
515 case 'tag': 552 case 'tag':
516 $entries = $this->store->retrieveEntriesByTag($id); 553 $entries = $this->store->retrieveEntriesByTag($id, $this->user->getId());
517 $tag = $this->store->retrieveTag($id); 554 $tag = $this->store->retrieveTag($id, $this->user->getId());
518 $tpl_vars = array( 555 $tpl_vars = array(
519 'tag' => $tag, 556 'tag' => $tag,
520 'entries' => $entries, 557 'entries' => $entries,
@@ -522,7 +559,7 @@ class Poche
522 break; 559 break;
523 case 'tags': 560 case 'tags':
524 $token = $this->user->getConfigValue('token'); 561 $token = $this->user->getConfigValue('token');
525 $tags = $this->store->retrieveAllTags(); 562 $tags = $this->store->retrieveAllTags($this->user->getId());
526 $tpl_vars = array( 563 $tpl_vars = array(
527 'token' => $token, 564 'token' => $token,
528 'user_id' => $this->user->getId(), 565 'user_id' => $this->user->getId(),
@@ -1037,7 +1074,7 @@ class Poche
1037 $feed->setChannelElement('author', 'wallabag'); 1074 $feed->setChannelElement('author', 'wallabag');
1038 1075
1039 if ($type == 'tag') { 1076 if ($type == 'tag') {
1040 $entries = $this->store->retrieveEntriesByTag($tag_id); 1077 $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id);
1041 } 1078 }
1042 else { 1079 else {
1043 $entries = $this->store->getEntriesByView($type, $user_id); 1080 $entries = $this->store->getEntriesByView($type, $user_id);
@@ -1057,4 +1094,20 @@ class Poche
1057 $feed->genarateFeed(); 1094 $feed->genarateFeed();
1058 exit; 1095 exit;
1059 } 1096 }
1097
1098 public function emptyCache() {
1099 $files = new RecursiveIteratorIterator(
1100 new RecursiveDirectoryIterator(CACHE, RecursiveDirectoryIterator::SKIP_DOTS),
1101 RecursiveIteratorIterator::CHILD_FIRST
1102 );
1103
1104 foreach ($files as $fileinfo) {
1105 $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
1106 $todo($fileinfo->getRealPath());
1107 }
1108
1109 Tools::logm('empty cache');
1110 $this->messages->add('s', _('Cache deleted.'));
1111 Tools::redirect();
1112 }
1060} 1113}