diff options
-rw-r--r-- | application/LinkDB.php | 33 | ||||
-rw-r--r-- | index.php | 58 | ||||
-rw-r--r-- | tests/LinkDBTest.php | 55 |
3 files changed, 106 insertions, 40 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php index 9e3efd6b..9308164a 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php | |||
@@ -465,6 +465,39 @@ You use the community supported version of the original Shaarli project, by Seba | |||
465 | } | 465 | } |
466 | 466 | ||
467 | /** | 467 | /** |
468 | * Rename or delete a tag across all links. | ||
469 | * | ||
470 | * @param string $from Tag to rename | ||
471 | * @param string $to New tag. If none is provided, the from tag will be deleted | ||
472 | * | ||
473 | * @return array|bool List of altered links or false on error | ||
474 | */ | ||
475 | public function renameTag($from, $to) | ||
476 | { | ||
477 | if (empty($from)) { | ||
478 | return false; | ||
479 | } | ||
480 | $delete = empty($to); | ||
481 | // True for case-sensitive tag search. | ||
482 | $linksToAlter = $this->filterSearch(['searchtags' => $from], true); | ||
483 | foreach($linksToAlter as $key => &$value) | ||
484 | { | ||
485 | $tags = preg_split('/\s+/', trim($value['tags'])); | ||
486 | if (($pos = array_search($from, $tags)) !== false) { | ||
487 | if ($delete) { | ||
488 | unset($tags[$pos]); // Remove tag. | ||
489 | } else { | ||
490 | $tags[$pos] = trim($to); | ||
491 | } | ||
492 | $value['tags'] = trim(implode(' ', array_unique($tags))); | ||
493 | $this[$value['id']] = $value; | ||
494 | } | ||
495 | } | ||
496 | |||
497 | return $linksToAlter; | ||
498 | } | ||
499 | |||
500 | /** | ||
468 | * Returns the list of days containing articles (oldest first) | 501 | * Returns the list of days containing articles (oldest first) |
469 | * Output: An array containing days (in format YYYYMMDD). | 502 | * Output: An array containing days (in format YYYYMMDD). |
470 | */ | 503 | */ |
@@ -686,6 +686,7 @@ function showLinkList($PAGE, $LINKSDB, $conf, $pluginManager) { | |||
686 | * @param ConfigManager $conf Configuration Manager instance. | 686 | * @param ConfigManager $conf Configuration Manager instance. |
687 | * @param PluginManager $pluginManager Plugin Manager instance, | 687 | * @param PluginManager $pluginManager Plugin Manager instance, |
688 | * @param LinkDB $LINKSDB | 688 | * @param LinkDB $LINKSDB |
689 | * @param History $history instance | ||
689 | */ | 690 | */ |
690 | function renderPage($conf, $pluginManager, $LINKSDB, $history) | 691 | function renderPage($conf, $pluginManager, $LINKSDB, $history) |
691 | { | 692 | { |
@@ -1198,41 +1199,18 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1198 | die('Wrong token.'); | 1199 | die('Wrong token.'); |
1199 | } | 1200 | } |
1200 | 1201 | ||
1201 | // Delete a tag: | 1202 | $alteredLinks = $LINKSDB->renameTag(escape($_POST['fromtag']), escape($_POST['totag'])); |
1202 | if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { | 1203 | $LINKSDB->save($conf->get('resource.page_cache')); |
1203 | $needle = trim($_POST['fromtag']); | 1204 | foreach ($alteredLinks as $link) { |
1204 | // True for case-sensitive tag search. | 1205 | $history->updateLink($link); |
1205 | $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); | ||
1206 | foreach($linksToAlter as $key=>$value) | ||
1207 | { | ||
1208 | $tags = explode(' ',trim($value['tags'])); | ||
1209 | unset($tags[array_search($needle,$tags)]); // Remove tag. | ||
1210 | $value['tags']=trim(implode(' ',$tags)); | ||
1211 | $LINKSDB[$key]=$value; | ||
1212 | $history->updateLink($LINKSDB[$key]); | ||
1213 | } | ||
1214 | $LINKSDB->save($conf->get('resource.page_cache')); | ||
1215 | echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?do=changetag\';</script>'; | ||
1216 | exit; | ||
1217 | } | ||
1218 | |||
1219 | // Rename a tag: | ||
1220 | if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { | ||
1221 | $needle = trim($_POST['fromtag']); | ||
1222 | // True for case-sensitive tag search. | ||
1223 | $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); | ||
1224 | foreach($linksToAlter as $key=>$value) { | ||
1225 | $tags = preg_split('/\s+/', trim($value['tags'])); | ||
1226 | // Replace tags value. | ||
1227 | $tags[array_search($needle, $tags)] = trim($_POST['totag']); | ||
1228 | $value['tags'] = implode(' ', array_unique($tags)); | ||
1229 | $LINKSDB[$key] = $value; | ||
1230 | $history->updateLink($LINKSDB[$key]); | ||
1231 | } | ||
1232 | $LINKSDB->save($conf->get('resource.page_cache')); // Save to disk. | ||
1233 | echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode(escape($_POST['totag'])).'\';</script>'; | ||
1234 | exit; | ||
1235 | } | 1206 | } |
1207 | $delete = empty($_POST['totag']); | ||
1208 | $redirect = $delete ? 'do=changetag' : 'searchtags='. urlencode(escape($_POST['totag'])); | ||
1209 | $alert = $delete | ||
1210 | ? sprintf(t('The tag was removed from %d links.'), count($alteredLinks)) | ||
1211 | : sprintf(t('The tag was renamed in %d links.'), count($alteredLinks)); | ||
1212 | echo '<script>alert("'. $alert .'");document.location=\'?'. $redirect .'\';</script>'; | ||
1213 | exit; | ||
1236 | } | 1214 | } |
1237 | 1215 | ||
1238 | // -------- User wants to add a link without using the bookmarklet: Show form. | 1216 | // -------- User wants to add a link without using the bookmarklet: Show form. |
@@ -2259,6 +2237,12 @@ if (!isset($_SESSION['LINKS_PER_PAGE'])) { | |||
2259 | $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20); | 2237 | $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20); |
2260 | } | 2238 | } |
2261 | 2239 | ||
2240 | try { | ||
2241 | $history = new History($conf->get('resource.history')); | ||
2242 | } catch(Exception $e) { | ||
2243 | die($e->getMessage()); | ||
2244 | } | ||
2245 | |||
2262 | $linkDb = new LinkDB( | 2246 | $linkDb = new LinkDB( |
2263 | $conf->get('resource.datastore'), | 2247 | $conf->get('resource.datastore'), |
2264 | isLoggedIn(), | 2248 | isLoggedIn(), |
@@ -2267,12 +2251,6 @@ $linkDb = new LinkDB( | |||
2267 | $conf->get('redirector.encode_url') | 2251 | $conf->get('redirector.encode_url') |
2268 | ); | 2252 | ); |
2269 | 2253 | ||
2270 | try { | ||
2271 | $history = new History($conf->get('resource.history')); | ||
2272 | } catch(Exception $e) { | ||
2273 | die($e->getMessage()); | ||
2274 | } | ||
2275 | |||
2276 | $container = new \Slim\Container(); | 2254 | $container = new \Slim\Container(); |
2277 | $container['conf'] = $conf; | 2255 | $container['conf'] = $conf; |
2278 | $container['plugins'] = $pluginManager; | 2256 | $container['plugins'] = $pluginManager; |
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 25438277..5b2f3667 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -487,4 +487,59 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
487 | $this->assertEquals($linkIds[$cpt++], $key); | 487 | $this->assertEquals($linkIds[$cpt++], $key); |
488 | } | 488 | } |
489 | } | 489 | } |
490 | |||
491 | /** | ||
492 | * Test rename tag with a valid value present in multiple links | ||
493 | */ | ||
494 | public function testRenameTagMultiple() | ||
495 | { | ||
496 | self::$refDB->write(self::$testDatastore); | ||
497 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
498 | |||
499 | $res = $linkDB->renameTag('cartoon', 'Taz'); | ||
500 | $this->assertEquals(3, count($res)); | ||
501 | $this->assertContains(' Taz ', $linkDB[4]['tags']); | ||
502 | $this->assertContains(' Taz ', $linkDB[1]['tags']); | ||
503 | $this->assertContains(' Taz ', $linkDB[0]['tags']); | ||
504 | } | ||
505 | |||
506 | /** | ||
507 | * Test rename tag with a valid value | ||
508 | */ | ||
509 | public function testRenameTagCaseSensitive() | ||
510 | { | ||
511 | self::$refDB->write(self::$testDatastore); | ||
512 | $linkDB = new LinkDB(self::$testDatastore, true, false, ''); | ||
513 | |||
514 | $res = $linkDB->renameTag('sTuff', 'Taz'); | ||
515 | $this->assertEquals(1, count($res)); | ||
516 | $this->assertEquals('Taz', $linkDB[41]['tags']); | ||
517 | } | ||
518 | |||
519 | /** | ||
520 | * Test rename tag with invalid values | ||
521 | */ | ||
522 | public function testRenameTagInvalid() | ||
523 | { | ||
524 | $linkDB = new LinkDB(self::$testDatastore, false, false); | ||
525 | |||
526 | $this->assertFalse($linkDB->renameTag('', 'test')); | ||
527 | $this->assertFalse($linkDB->renameTag('', '')); | ||
528 | // tag non existent | ||
529 | $this->assertEquals([], $linkDB->renameTag('test', '')); | ||
530 | $this->assertEquals([], $linkDB->renameTag('test', 'retest')); | ||
531 | } | ||
532 | |||
533 | /** | ||
534 | * Test delete tag with a valid value | ||
535 | */ | ||
536 | public function testDeleteTag() | ||
537 | { | ||
538 | self::$refDB->write(self::$testDatastore); | ||
539 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
540 | |||
541 | $res = $linkDB->renameTag('cartoon', null); | ||
542 | $this->assertEquals(3, count($res)); | ||
543 | $this->assertNotContains('cartoon', $linkDB[4]['tags']); | ||
544 | } | ||
490 | } | 545 | } |