From 3b67b22225c54b28c5ea8b118710ef450016fb86 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 4 Aug 2017 19:10:00 +0200 Subject: Move tag renaming code to LinkDB and unit test it --- application/LinkDB.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'application/LinkDB.php') diff --git a/application/LinkDB.php b/application/LinkDB.php index 8ca0fab3..5e38e848 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -463,6 +463,39 @@ You use the community supported version of the original Shaarli project, by Seba return $tags; } + /** + * Rename or delete a tag across all links. + * + * @param string $from Tag to rename + * @param string $to New tag. If none is provided, the from tag will be deleted + * + * @return array|bool List of altered links or false on error + */ + public function renameTag($from, $to) + { + if (empty($from)) { + return false; + } + $delete = empty($to); + // True for case-sensitive tag search. + $linksToAlter = $this->filterSearch(['searchtags' => $from], true); + foreach($linksToAlter as $key => &$value) + { + $tags = preg_split('/\s+/', trim($value['tags'])); + if (($pos = array_search($from, $tags)) !== false) { + if ($delete) { + unset($tags[$pos]); // Remove tag. + } else { + $tags[$pos] = trim($to); + } + $value['tags'] = trim(implode(' ', array_unique($tags))); + $this[$value['id']] = $value; + } + } + + return $linksToAlter; + } + /** * Returns the list of days containing articles (oldest first) * Output: An array containing days (in format YYYYMMDD). -- cgit v1.2.3