aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-08-04 19:10:00 +0200
committerArthurHoaro <arthur@hoa.ro>2017-08-05 09:55:20 +0200
commit3b67b22225c54b28c5ea8b118710ef450016fb86 (patch)
treedd58089728e46969c1eb5efb40d13b8580a4fe4e /index.php
parentd99aef535fa209c27c46a97dee4187ac21c84d4d (diff)
downloadShaarli-3b67b22225c54b28c5ea8b118710ef450016fb86.tar.gz
Shaarli-3b67b22225c54b28c5ea8b118710ef450016fb86.tar.zst
Shaarli-3b67b22225c54b28c5ea8b118710ef450016fb86.zip
Move tag renaming code to LinkDB and unit test it
Diffstat (limited to 'index.php')
-rw-r--r--index.php50
1 files changed, 14 insertions, 36 deletions
diff --git a/index.php b/index.php
index eb6b17d9..891a2ece 100644
--- a/index.php
+++ b/index.php
@@ -685,6 +685,7 @@ function showLinkList($PAGE, $LINKSDB, $conf, $pluginManager) {
685 * @param ConfigManager $conf Configuration Manager instance. 685 * @param ConfigManager $conf Configuration Manager instance.
686 * @param PluginManager $pluginManager Plugin Manager instance, 686 * @param PluginManager $pluginManager Plugin Manager instance,
687 * @param LinkDB $LINKSDB 687 * @param LinkDB $LINKSDB
688 * @param History $history instance
688 */ 689 */
689function renderPage($conf, $pluginManager, $LINKSDB, $history) 690function renderPage($conf, $pluginManager, $LINKSDB, $history)
690{ 691{
@@ -1176,39 +1177,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
1176 die('Wrong token.'); 1177 die('Wrong token.');
1177 } 1178 }
1178 1179
1179 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { 1180 $alteredLinks = $LINKSDB->renameTag(escape($_POST['fromtag']), escape($_POST['totag']));
1180 $delete = true;
1181 } else if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
1182 $delete = false;
1183 } else {
1184 $PAGE->renderPage('changetag');
1185 exit;
1186 }
1187
1188 $count = 0;
1189 $needle = trim($_POST['fromtag']);
1190 // True for case-sensitive tag search.
1191 $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
1192 foreach($linksToAlter as $key => $value)
1193 {
1194 $tags = explode(' ',trim($value['tags']));
1195 if (($pos = array_search($needle,$tags)) !== false) {
1196 if ($delete) {
1197 unset($tags[$pos]); // Remove tag.
1198 } else {
1199 $tags[$pos] = trim($_POST['totag']);
1200 }
1201 $value['tags'] = trim(implode(' ', array_unique($tags)));
1202 $LINKSDB[$key]=$value;
1203 $history->updateLink($LINKSDB[$key]);
1204 ++$count;
1205 }
1206 }
1207 $LINKSDB->save($conf->get('resource.page_cache')); 1181 $LINKSDB->save($conf->get('resource.page_cache'));
1182 foreach ($alteredLinks as $link) {
1183 $history->updateLink($link);
1184 }
1185 $delete = empty($_POST['totag']);
1208 $redirect = $delete ? 'do=changetag' : 'searchtags='. urlencode(escape($_POST['totag'])); 1186 $redirect = $delete ? 'do=changetag' : 'searchtags='. urlencode(escape($_POST['totag']));
1209 $alert = $delete 1187 $alert = $delete
1210 ? sprintf(t('The tag was removed from %d links.'), $count) 1188 ? sprintf(t('The tag was removed from %d links.'), count($alteredLinks))
1211 : sprintf(t('The tag was renamed in %d links.'), $count); 1189 : sprintf(t('The tag was renamed in %d links.'), count($alteredLinks));
1212 echo '<script>alert("'. $alert .'");document.location=\'?'. $redirect .'\';</script>'; 1190 echo '<script>alert("'. $alert .'");document.location=\'?'. $redirect .'\';</script>';
1213 exit; 1191 exit;
1214 } 1192 }
@@ -2237,6 +2215,12 @@ if (!isset($_SESSION['LINKS_PER_PAGE'])) {
2237 $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20); 2215 $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20);
2238} 2216}
2239 2217
2218try {
2219 $history = new History($conf->get('resource.history'));
2220} catch(Exception $e) {
2221 die($e->getMessage());
2222}
2223
2240$linkDb = new LinkDB( 2224$linkDb = new LinkDB(
2241 $conf->get('resource.datastore'), 2225 $conf->get('resource.datastore'),
2242 isLoggedIn(), 2226 isLoggedIn(),
@@ -2245,12 +2229,6 @@ $linkDb = new LinkDB(
2245 $conf->get('redirector.encode_url') 2229 $conf->get('redirector.encode_url')
2246); 2230);
2247 2231
2248try {
2249 $history = new History($conf->get('resource.history'));
2250} catch(Exception $e) {
2251 die($e->getMessage());
2252}
2253
2254$container = new \Slim\Container(); 2232$container = new \Slim\Container();
2255$container['conf'] = $conf; 2233$container['conf'] = $conf;
2256$container['plugins'] = $pluginManager; 2234$container['plugins'] = $pluginManager;