diff options
author | ArthurHoaro <arthur@hoa.ro> | 2019-07-27 12:34:30 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2019-07-27 12:34:30 +0200 |
commit | 38672ba0d1c722e5d6d33a58255ceb55e9410e46 (patch) | |
tree | dae4c7c47532380eac3ae641db99122fc77c93dc /application/LinkDB.php | |
parent | 83faedadff76c5bdca036f39f13943f63b27e164 (diff) | |
parent | 1e77e0448bbd25675d8c0fe4a73206ad9048904b (diff) | |
download | Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.tar.gz Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.tar.zst Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.zip |
Merge tag 'v0.10.4' into stable
Release v0.10.4
Diffstat (limited to 'application/LinkDB.php')
-rw-r--r-- | application/LinkDB.php | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php index c1661d52..803757ca 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php | |||
@@ -107,8 +107,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
107 | $hidePublicLinks, | 107 | $hidePublicLinks, |
108 | $redirector = '', | 108 | $redirector = '', |
109 | $redirectorEncode = true | 109 | $redirectorEncode = true |
110 | ) | 110 | ) { |
111 | { | ||
112 | $this->datastore = $datastore; | 111 | $this->datastore = $datastore; |
113 | $this->loggedIn = $isLoggedIn; | 112 | $this->loggedIn = $isLoggedIn; |
114 | $this->hidePublicLinks = $hidePublicLinks; | 113 | $this->hidePublicLinks = $hidePublicLinks; |
@@ -250,14 +249,18 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
250 | 'id' => 1, | 249 | 'id' => 1, |
251 | 'title'=> t('The personal, minimalist, super-fast, database free, bookmarking service'), | 250 | 'title'=> t('The personal, minimalist, super-fast, database free, bookmarking service'), |
252 | 'url'=>'https://shaarli.readthedocs.io', | 251 | 'url'=>'https://shaarli.readthedocs.io', |
253 | 'description'=>t('Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login. | 252 | 'description'=>t( |
253 | 'Welcome to Shaarli! This is your first public bookmark. ' | ||
254 | .'To edit or delete me, you must first login. | ||
254 | 255 | ||
255 | To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page. | 256 | To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page. |
256 | 257 | ||
257 | You use the community supported version of the original Shaarli project, by Sebastien Sauvage.'), | 258 | You use the community supported version of the original Shaarli project, by Sebastien Sauvage.' |
259 | ), | ||
258 | 'private'=>0, | 260 | 'private'=>0, |
259 | 'created'=> new DateTime(), | 261 | 'created'=> new DateTime(), |
260 | 'tags'=>'opensource software' | 262 | 'tags'=>'opensource software', |
263 | 'sticky' => false, | ||
261 | ); | 264 | ); |
262 | $link['shorturl'] = link_small_hash($link['created'], $link['id']); | 265 | $link['shorturl'] = link_small_hash($link['created'], $link['id']); |
263 | $this->links[1] = $link; | 266 | $this->links[1] = $link; |
@@ -270,6 +273,7 @@ You use the community supported version of the original Shaarli project, by Seba | |||
270 | 'private'=>1, | 273 | 'private'=>1, |
271 | 'created'=> new DateTime('1 minute ago'), | 274 | 'created'=> new DateTime('1 minute ago'), |
272 | 'tags'=>'secretstuff', | 275 | 'tags'=>'secretstuff', |
276 | 'sticky' => false, | ||
273 | ); | 277 | ); |
274 | $link['shorturl'] = link_small_hash($link['created'], $link['id']); | 278 | $link['shorturl'] = link_small_hash($link['created'], $link['id']); |
275 | $this->links[0] = $link; | 279 | $this->links[0] = $link; |
@@ -317,11 +321,12 @@ You use the community supported version of the original Shaarli project, by Seba | |||
317 | } else { | 321 | } else { |
318 | $link['real_url'] .= $link['url']; | 322 | $link['real_url'] .= $link['url']; |
319 | } | 323 | } |
320 | } | 324 | } else { |
321 | else { | ||
322 | $link['real_url'] = $link['url']; | 325 | $link['real_url'] = $link['url']; |
323 | } | 326 | } |
324 | 327 | ||
328 | $link['sticky'] = isset($link['sticky']) ? $link['sticky'] : false; | ||
329 | |||
325 | // To be able to load links before running the update, and prepare the update | 330 | // To be able to load links before running the update, and prepare the update |
326 | if (! isset($link['created'])) { | 331 | if (! isset($link['created'])) { |
327 | $link['id'] = $link['linkdate']; | 332 | $link['id'] = $link['linkdate']; |
@@ -403,7 +408,8 @@ You use the community supported version of the original Shaarli project, by Seba | |||
403 | * | 408 | * |
404 | * @return array list of shaare found. | 409 | * @return array list of shaare found. |
405 | */ | 410 | */ |
406 | public function filterDay($request) { | 411 | public function filterDay($request) |
412 | { | ||
407 | $linkFilter = new LinkFilter($this->links); | 413 | $linkFilter = new LinkFilter($this->links); |
408 | return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request); | 414 | return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request); |
409 | } | 415 | } |
@@ -420,8 +426,12 @@ You use the community supported version of the original Shaarli project, by Seba | |||
420 | * | 426 | * |
421 | * @return array filtered links, all links if no suitable filter was provided. | 427 | * @return array filtered links, all links if no suitable filter was provided. |
422 | */ | 428 | */ |
423 | public function filterSearch($filterRequest = array(), $casesensitive = false, $visibility = 'all', $untaggedonly = false) | 429 | public function filterSearch( |
424 | { | 430 | $filterRequest = array(), |
431 | $casesensitive = false, | ||
432 | $visibility = 'all', | ||
433 | $untaggedonly = false | ||
434 | ) { | ||
425 | // Filter link database according to parameters. | 435 | // Filter link database according to parameters. |
426 | $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; | 436 | $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; |
427 | $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; | 437 | $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; |
@@ -436,15 +446,17 @@ You use the community supported version of the original Shaarli project, by Seba | |||
436 | 446 | ||
437 | /** | 447 | /** |
438 | * Returns the list tags appearing in the links with the given tags | 448 | * Returns the list tags appearing in the links with the given tags |
439 | * @param $filteringTags: tags selecting the links to consider | 449 | * |
440 | * @param $visibility: process only all/private/public links | 450 | * @param array $filteringTags tags selecting the links to consider |
441 | * @return: a tag=>linksCount array | 451 | * @param string $visibility process only all/private/public links |
452 | * | ||
453 | * @return array tag => linksCount | ||
442 | */ | 454 | */ |
443 | public function linksCountPerTag($filteringTags = [], $visibility = 'all') | 455 | public function linksCountPerTag($filteringTags = [], $visibility = 'all') |
444 | { | 456 | { |
445 | $links = empty($filteringTags) ? $this->links : $this->filterSearch(['searchtags' => $filteringTags], false, $visibility); | 457 | $links = $this->filterSearch(['searchtags' => $filteringTags], false, $visibility); |
446 | $tags = array(); | 458 | $tags = []; |
447 | $caseMapping = array(); | 459 | $caseMapping = []; |
448 | foreach ($links as $link) { | 460 | foreach ($links as $link) { |
449 | foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) { | 461 | foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) { |
450 | if (empty($tag)) { | 462 | if (empty($tag)) { |
@@ -458,8 +470,19 @@ You use the community supported version of the original Shaarli project, by Seba | |||
458 | $tags[$caseMapping[strtolower($tag)]]++; | 470 | $tags[$caseMapping[strtolower($tag)]]++; |
459 | } | 471 | } |
460 | } | 472 | } |
461 | // Sort tags by usage (most used tag first) | 473 | |
462 | arsort($tags); | 474 | /* |
475 | * Formerly used arsort(), which doesn't define the sort behaviour for equal values. | ||
476 | * Also, this function doesn't produce the same result between PHP 5.6 and 7. | ||
477 | * | ||
478 | * So we now use array_multisort() to sort tags by DESC occurrences, | ||
479 | * then ASC alphabetically for equal values. | ||
480 | * | ||
481 | * @see https://github.com/shaarli/Shaarli/issues/1142 | ||
482 | */ | ||
483 | $keys = array_keys($tags); | ||
484 | $tmpTags = array_combine($keys, $keys); | ||
485 | array_multisort($tags, SORT_DESC, $tmpTags, SORT_ASC, $tags); | ||
463 | return $tags; | 486 | return $tags; |
464 | } | 487 | } |
465 | 488 | ||
@@ -479,8 +502,7 @@ You use the community supported version of the original Shaarli project, by Seba | |||
479 | $delete = empty($to); | 502 | $delete = empty($to); |
480 | // True for case-sensitive tag search. | 503 | // True for case-sensitive tag search. |
481 | $linksToAlter = $this->filterSearch(['searchtags' => $from], true); | 504 | $linksToAlter = $this->filterSearch(['searchtags' => $from], true); |
482 | foreach($linksToAlter as $key => &$value) | 505 | foreach ($linksToAlter as $key => &$value) { |
483 | { | ||
484 | $tags = preg_split('/\s+/', trim($value['tags'])); | 506 | $tags = preg_split('/\s+/', trim($value['tags'])); |
485 | if (($pos = array_search($from, $tags)) !== false) { | 507 | if (($pos = array_search($from, $tags)) !== false) { |
486 | if ($delete) { | 508 | if ($delete) { |
@@ -523,7 +545,10 @@ You use the community supported version of the original Shaarli project, by Seba | |||
523 | { | 545 | { |
524 | $order = $order === 'ASC' ? -1 : 1; | 546 | $order = $order === 'ASC' ? -1 : 1; |
525 | // Reorder array by dates. | 547 | // Reorder array by dates. |
526 | usort($this->links, function($a, $b) use ($order) { | 548 | usort($this->links, function ($a, $b) use ($order) { |
549 | if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) { | ||
550 | return $a['sticky'] ? -1 : 1; | ||
551 | } | ||
527 | return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; | 552 | return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; |
528 | }); | 553 | }); |
529 | 554 | ||