]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #887 from ArthurHoaro/hotfix/dash-tag-rename
authorArthurHoaro <arthur@hoa.ro>
Sat, 5 Aug 2017 07:59:03 +0000 (09:59 +0200)
committerGitHub <noreply@github.com>
Sat, 5 Aug 2017 07:59:03 +0000 (09:59 +0200)
Make sure that the tag exists before altering/removing it

1  2 
application/LinkDB.php
index.php

diff --combined application/LinkDB.php
index 9e3efd6b4229a691dbd9028deb73d4428cbeeea1,5e38e8484837002f5964b12c7d6898e5fd783222..9308164af685bd8830b6c1a4a68621fddaa9f548
@@@ -417,22 -417,21 +417,22 @@@ You use the community supported versio
       *                                - searchterm: term search
       * @param bool   $casesensitive Optional: Perform case sensitive filter
       * @param string $visibility    return only all/private/public links
 +     * @param string $untaggedonly  return only untagged links
       *
       * @return array filtered links, all links if no suitable filter was provided.
       */
 -    public function filterSearch($filterRequest = array(), $casesensitive = false, $visibility = 'all')
 +    public function filterSearch($filterRequest = array(), $casesensitive = false, $visibility = 'all', $untaggedonly = false)
      {
          // Filter link database according to parameters.
          $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
          $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
  
          // Search tags + fullsearch - blank string parameter will return all links.
 -        $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT;
 +        $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; // == "vuotext"
          $request = [$searchtags, $searchterm];
  
          $linkFilter = new LinkFilter($this);
 -        return $linkFilter->filter($type, $request, $casesensitive, $visibility);
 +        return $linkFilter->filter($type, $request, $casesensitive, $visibility, $untaggedonly);
      }
  
      /**
          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).
diff --combined index.php
index 5c292b04489cf55da21fb2b2071462c960a4ae99,891a2ece77b08fb435764ae022da569dc88ee03f..9025df58f03202aaad35c32c58160ebe902e30d9
+++ b/index.php
@@@ -287,7 -287,6 +287,7 @@@ function logout() 
          unset($_SESSION['ip']);
          unset($_SESSION['username']);
          unset($_SESSION['privateonly']);
 +        unset($_SESSION['untaggedonly']);
      }
      setcookie('shaarli_staySignedIn', FALSE, 0, WEB_PATH);
  }
@@@ -686,6 -685,7 +686,7 @@@ function showLinkList($PAGE, $LINKSDB, 
   * @param ConfigManager $conf          Configuration Manager instance.
   * @param PluginManager $pluginManager Plugin Manager instance,
   * @param LinkDB        $LINKSDB
+  * @param History       $history       instance
   */
  function renderPage($conf, $pluginManager, $LINKSDB, $history)
  {
  
          $tagList = array();
          foreach($tags as $key => $value) {
 +            if (in_array($key, $filteringTags)) {
 +                continue;
 +            }
              // Tag font size scaling:
              //   default 15 and 30 logarithm bases affect scaling,
              //   22 and 6 are arbitrary font sizes for max and min sizes.
          exit;
      }
  
 -    // -------- Tag cloud
 +    // -------- Tag list
      if ($targetPage == Router::$PAGE_TAGLIST)
      {
          $visibility = ! empty($_SESSION['privateonly']) ? 'private' : 'all';
          $filteringTags = isset($_GET['searchtags']) ? explode(' ', $_GET['searchtags']) : [];
          $tags = $LINKSDB->linksCountPerTag($filteringTags, $visibility);
 +        foreach ($filteringTags as $tag) {
 +            if (array_key_exists($tag, $tags)) {
 +                unset($tags[$tag]);
 +            }
 +        }
  
          if (! empty($_GET['sort']) && $_GET['sort'] === 'alpha') {
              alphabetical_sort($tags, false, true);
          exit;
      }
  
 +    // -------- User wants to see only untagged links (toggle)
 +    if (isset($_GET['untaggedonly'])) {
 +        $_SESSION['untaggedonly'] = !$_SESSION['untaggedonly'];
 +
 +        if (! empty($_SERVER['HTTP_REFERER'])) {
 +            $location = generateLocation($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'], array('untaggedonly'));
 +        } else {
 +            $location = '?';
 +        }
 +        header('Location: '. $location);
 +        exit;
 +    }
 +
      // -------- Handle other actions allowed for non-logged in users:
      if (!isLoggedIn())
      {
              die('Wrong token.');
          }
  
-         // Delete a tag:
-         if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
-             $needle = trim($_POST['fromtag']);
-             // True for case-sensitive tag search.
-             $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
-             foreach($linksToAlter as $key=>$value)
-             {
-                 $tags = explode(' ',trim($value['tags']));
-                 unset($tags[array_search($needle,$tags)]); // Remove tag.
-                 $value['tags']=trim(implode(' ',$tags));
-                 $LINKSDB[$key]=$value;
-                 $history->updateLink($LINKSDB[$key]);
-             }
-             $LINKSDB->save($conf->get('resource.page_cache'));
-             echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?do=changetag\';</script>';
-             exit;
-         }
-         // Rename a tag:
-         if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
-             $needle = trim($_POST['fromtag']);
-             // True for case-sensitive tag search.
-             $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
-             foreach($linksToAlter as $key=>$value) {
-                 $tags = preg_split('/\s+/', trim($value['tags']));
-                 // Replace tags value.
-                 $tags[array_search($needle, $tags)] = trim($_POST['totag']);
-                 $value['tags'] = implode(' ', array_unique($tags));
-                 $LINKSDB[$key] = $value;
-                 $history->updateLink($LINKSDB[$key]);
-             }
-             $LINKSDB->save($conf->get('resource.page_cache')); // Save to disk.
-             echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode(escape($_POST['totag'])).'\';</script>';
-             exit;
+         $alteredLinks = $LINKSDB->renameTag(escape($_POST['fromtag']), escape($_POST['totag']));
+         $LINKSDB->save($conf->get('resource.page_cache'));
+         foreach ($alteredLinks as $link) {
+             $history->updateLink($link);
          }
+         $delete = empty($_POST['totag']);
+         $redirect = $delete ? 'do=changetag' : 'searchtags='. urlencode(escape($_POST['totag']));
+         $alert = $delete
+             ? sprintf(t('The tag was removed from %d links.'), count($alteredLinks))
+             : sprintf(t('The tag was renamed in %d links.'), count($alteredLinks));
+         echo '<script>alert("'. $alert .'");document.location=\'?'. $redirect .'\';</script>';
+         exit;
      }
  
      // -------- User wants to add a link without using the bookmarklet: Show form.
@@@ -1665,7 -1621,7 +1643,7 @@@ function buildLinkList($PAGE,$LINKSDB, 
              'searchtags' => $searchtags,
              'searchterm' => $searchterm,
          ];
 -        $linksToDisplay = $LINKSDB->filterSearch($request, false, $visibility);
 +        $linksToDisplay = $LINKSDB->filterSearch($request, false, $visibility, !empty($_SESSION['untaggedonly']));
      }
  
      // ---- Handle paging.
@@@ -2259,6 -2215,12 +2237,12 @@@ if (!isset($_SESSION['LINKS_PER_PAGE'])
      $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20);
  }
  
+ try {
+     $history = new History($conf->get('resource.history'));
+ } catch(Exception $e) {
+     die($e->getMessage());
+ }
  $linkDb = new LinkDB(
      $conf->get('resource.datastore'),
      isLoggedIn(),
      $conf->get('redirector.encode_url')
  );
  
- try {
-     $history = new History($conf->get('resource.history'));
- } catch(Exception $e) {
-     die($e->getMessage());
- }
  $container = new \Slim\Container();
  $container['conf'] = $conf;
  $container['plugins'] = $pluginManager;