/
Tag.php
1111 lines (958 loc) · 35.7 KB
/
Tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2022 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
declare(strict_types=0);
namespace Ampache\Repository\Model;
use Ampache\Config\AmpConfig;
use Ampache\Module\System\Core;
use Ampache\Module\System\Dba;
use Ampache\Module\Util\InterfaceImplementationChecker;
use PDOStatement;
/**
* Tag Class
*
* This class handles all of the genre related operations
*
*/
class Tag extends database_object implements library_item, GarbageCollectibleInterface
{
protected const DB_TABLENAME = 'tag';
public $id;
public $name;
public $f_name;
public $is_hidden;
/**
* constructor
* This takes a tag id and returns all of the relevant information
* @param $tag_id
*/
public function __construct($tag_id)
{
if (!$tag_id) {
return false;
}
$info = $this->get_info($tag_id);
if (empty($info)) {
return false;
}
foreach ($info as $key => $value) {
$this->$key = $value;
} // end foreach
// the ui is sometimes looking for a formatted name...
$this->f_name = scrub_out($this->name);
return true;
} // constructor
public function getId(): int
{
return (int)$this->id;
}
/**
* construct_from_name
* This attempts to construct the tag from a name, rather then the ID
* @param string $name
* @return Tag
*/
public static function construct_from_name($name)
{
$tag_id = self::tag_exists($name);
return new Tag($tag_id);
} // construct_from_name
/**
* build_cache
* This takes an array of object ids and caches all of their information
* in a single query, cuts down on the connections
* @param array $ids
* @return boolean
*/
public static function build_cache($ids)
{
if (empty($ids)) {
return false;
}
$idlist = '(' . implode(',', $ids) . ')';
$sql = "SELECT * FROM `tag` WHERE `id` IN $idlist";
$db_results = Dba::read($sql);
while ($row = Dba::fetch_assoc($db_results)) {
parent::add_to_cache('tag', $row['id'], $row);
}
return true;
} // build_cache
/**
* build_map_cache
* This builds a cache of the mappings for the specified object, no limit is given
* @param string $type
* @param $ids
* @return boolean
* @params array $ids
*/
public static function build_map_cache($type, $ids)
{
if (!is_array($ids) || !count($ids)) {
return false;
}
if (!InterfaceImplementationChecker::is_library_item($type)) {
return false;
}
$idlist = '(' . implode(',', $ids) . ')';
$sql = "SELECT `tag_map`.`id`, `tag_map`.`tag_id`, `tag`.`name`, `tag_map`.`object_id`, `tag_map`.`user` FROM `tag` LEFT JOIN `tag_map` ON `tag_map`.`tag_id`=`tag`.`id` WHERE `tag`.`is_hidden` = false AND `tag_map`.`object_type`='$type' AND `tag_map`.`object_id` IN $idlist";
$db_results = Dba::read($sql);
$tags = array();
$tag_map = array();
while ($row = Dba::fetch_assoc($db_results)) {
$tags[$row['object_id']][$row['tag_id']] = array(
'user' => $row['user'],
'id' => $row['tag_id'],
'name' => $row['name']
);
$tag_map[$row['object_id']] = array(
'id' => $row['id'],
'tag_id' => $row['tag_id'],
'user' => $row['user'],
'object_type' => $type,
'object_id' => $row['object_id']
);
}
// Run through our original ids as we also want to cache NULL
// results
foreach ($ids as $tagid) {
if (!isset($tags[$tagid])) {
$tags[$tagid] = null;
$tag_map[$tagid] = null;
}
parent::add_to_cache('tag_top_' . $type, $tagid, array($tags[$tagid]));
parent::add_to_cache('tag_map_' . $type, $tagid, array($tag_map[$tagid]));
}
return true;
} // build_map_cache
/**
* add
* This is a wrapper function, it figures out what we need to add, be it a tag
* and map, or just the mapping
* @param string $type
* @param integer $object_id
* @param string $value
* @param boolean $user
* @return bool|int
*/
public static function add($type, $object_id, $value, $user = true)
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
return false;
}
if (!is_numeric($object_id)) {
return false;
}
$cleaned_value = str_replace('Folk, World, & Country', 'Folk World & Country', $value);
if (!strlen((string)$cleaned_value)) {
return false;
}
if ($user === true) {
$uid = (int)(Core::get_global('user')->id);
} else {
$uid = (int)($user);
}
// Check and see if the tag exists, if not create it, we need the tag id from this
if (!$tag_id = self::tag_exists($cleaned_value)) {
debug_event(self::class, 'Adding new tag {' . $cleaned_value . '}', 5);
$tag_id = self::add_tag($cleaned_value);
}
if (!$tag_id) {
debug_event(self::class, 'Error unable to create tag value:' . $cleaned_value . ' unknown error', 1);
return false;
}
// We've got the tag id, let's see if it's already got a map, if not then create the map and return the value
if (!$map_id = self::tag_map_exists($type, $object_id, (int)$tag_id, $uid)) {
$map_id = self::add_tag_map($type, $object_id, (int)$tag_id, $user);
}
return (int)$map_id;
} // add
/**
* add_tag
* This function adds a new tag, for now we're going to limit the tagging a bit
* @param string $value
* @return int|null
*/
public static function add_tag($value)
{
if (!strlen((string)$value)) {
return null;
}
$sql = "REPLACE INTO `tag` SET `name` = ?";
Dba::write($sql, array($value));
$insert_id = (int)Dba::insert_id();
parent::add_to_cache('tag_name', $value, array($insert_id));
return $insert_id;
} // add_tag
/**
* update
* Update the name of the tag
* @param array $data
* @return boolean
*/
public function update(array $data)
{
if (!strlen((string)$data['name'])) {
return false;
}
$name = $data['name'] ?? $this->name;
$is_hidden = (array_key_exists('is_hidden', $data))
? (int)$data['is_hidden']
: 0;
if ($name != $this->name) {
debug_event(self::class, 'Updating tag {' . $this->id . '} with name {' . $data['name'] . '}...', 5);
$sql = 'UPDATE `tag` SET `name` = ? WHERE `id` = ?';
Dba::write($sql, array($name, $this->id));
}
if ($is_hidden != (int)$this->is_hidden) {
debug_event(self::class, 'Hidden tag {' . $this->id . '} with status {' . $is_hidden . '}...', 5);
$sql = 'UPDATE `tag` SET `is_hidden` = ? WHERE `id` = ?';
Dba::write($sql, array($is_hidden, $this->id));
// if you had previously hidden this tag then remove the merges too
if ($is_hidden == 0 && (int)$this->is_hidden == 1) {
debug_event(self::class, 'Unhiding tag {' . $this->id . '} removing all previous merges', 5);
$this->remove_merges();
}
$this->is_hidden = $is_hidden;
}
if (array_key_exists('edit_tags', $data) && $data['edit_tags']) {
$filterfolk = str_replace('Folk, World, & Country', 'Folk World & Country', $data['edit_tags']);
$filterunder = str_replace('_', ', ', $filterfolk);
$filter = str_replace(';', ', ', $filterunder);
$filter_list = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $filter);
$tag_names = (is_array($filter_list)) ? array_unique($filter_list) : array();
foreach ($tag_names as $tag) {
$merge_to = self::construct_from_name($tag);
if ($merge_to->id == 0) {
self::add_tag($tag);
$merge_to = self::construct_from_name($tag);
}
$this->merge($merge_to->id, array_key_exists('merge_persist', $data));
}
if (!array_key_exists('keep_existing', $data)) {
$sql = "DELETE FROM `tag_map` WHERE `tag_map`.`tag_id` = ? ";
Dba::write($sql, array($this->id));
if (!array_key_exists('merge_persist', $data)) {
$this->delete();
} else {
$sql = "UPDATE `tag` SET `is_hidden` = 1 WHERE `tag`.`id` = ? ";
Dba::write($sql, array($this->id));
}
}
}
return $this->id;
} // add_tag
/**
* merge
* merges this tag to another one.
* @param integer $merge_to
* @param boolean $is_persistent
*/
public function merge($merge_to, $is_persistent)
{
if ($this->id != $merge_to) {
debug_event(self::class, 'Merging tag ' . $this->id . ' into ' . $merge_to . ')...', 5);
$sql = "REPLACE INTO `tag_map` (`tag_id`, `user`, `object_type`, `object_id`) SELECT " . $merge_to . ",`user`, `object_type`, `object_id` FROM `tag_map` AS `tm` WHERE `tm`.`tag_id` = " . $this->id . " AND NOT EXISTS (SELECT 1 FROM `tag_map` WHERE `tag_map`.`tag_id` = " . $merge_to . " AND `tag_map`.`object_id` = `tm`.`object_id` AND `tag_map`.`object_type` = `tm`.`object_type` AND `tag_map`.`user` = `tm`.`user`)";
Dba::write($sql);
if ($is_persistent) {
$sql = "REPLACE INTO `tag_merge` (`tag_id`, `merged_to`) VALUES (?, ?)";
Dba::write($sql, array($this->id, $merge_to));
}
}
}
/**
* get_merged_tags
* Get merged tags to this tag.
* @return array
*/
public function get_merged_tags()
{
$sql = "SELECT `tag`.`id`, `tag`.`name`FROM `tag_merge` INNER JOIN `tag` ON `tag`.`id` = `tag_merge`.`merged_to` WHERE `tag_merge`.`tag_id` = ? ORDER BY `tag`.`name` ";
$db_results = Dba::read($sql, array($this->id));
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[$row['id']] = array('id' => $row['id'], 'name' => $row['name']);
}
return $results;
}
/**
* get_merged_count
* @return int
*/
public static function get_merged_count()
{
$results = 0;
$sql = "SELECT COUNT(DISTINCT `tag_id`) AS `tag_count` FROM `tag_merge`;";
$db_results = Dba::read($sql);
if ($row = Dba::fetch_assoc($db_results)) {
$results = (int)$row['tag_count'];
}
return $results;
}
/**
* has_merge
* Get merged tags to this tag.
* @param string $name
* @return bool
*/
public function has_merge($name)
{
$sql = "SELECT `tag`.`name` FROM `tag_merge` INNER JOIN `tag` ON `tag`.`id` = `tag_merge`.`merged_to` WHERE `tag_merge`.`tag_id` = ? ORDER BY `tag`.`name` ";
$db_results = Dba::read($sql, array($this->id));
while ($row = Dba::fetch_assoc($db_results)) {
if ($name == $row['name']) {
return true;
}
}
return false;
}
/**
* remove_merges
* Remove merged tags from this tag.
*/
public function remove_merges()
{
$sql = "DELETE FROM `tag_merge` WHERE `tag_merge`.`tag_id` = ?;";
Dba::write($sql, array($this->id));
}
/**
* add_tag_map
* This adds a specific tag to the map for specified object
* @param string $type
* @param integer|string $object_id
* @param integer|string $tag_id
* @param boolean $user
* @return boolean|int
*/
public static function add_tag_map($type, $object_id, $tag_id, $user = true)
{
if ($user === true) {
$uid = (int)(Core::get_global('user')->id);
} else {
$uid = (int)($user);
}
if (!InterfaceImplementationChecker::is_library_item($type)) {
debug_event(__CLASS__, $type . " is not a library item.", 3);
return false;
}
$tag_id = (int)($tag_id);
$item_id = (int)($object_id);
if (!$tag_id || !$item_id) {
return false;
}
// If tag merged to another one, add reference to the merge destination
$parent = new Tag($tag_id);
$merges = $parent->get_merged_tags();
if (!$parent->is_hidden) {
$merges[] = array('id' => $parent->id, 'name' => $parent->name);
}
foreach ($merges as $tag) {
$sql = "INSERT IGNORE INTO `tag_map` (`tag_id`, `user`, `object_type`, `object_id`) VALUES (?, ?, ?, ?)";
Dba::write($sql, array($tag['id'], $uid, $type, $item_id));
}
$insert_id = (int)Dba::insert_id();
parent::add_to_cache('tag_map_' . $type, $insert_id, array('tag_id' => $tag_id, 'user' => $uid, 'object_type' => $type, 'object_id' => $item_id));
return $insert_id;
} // add_tag_map
/**
* garbage_collection
*
* This cleans out tag_maps that are obsolete and then removes tags that
* have no maps.
*/
public static function garbage_collection()
{
$sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `song` ON `song`.`id`=`tag_map`.`object_id` WHERE `tag_map`.`object_type`='song' AND `song`.`id` IS NULL";
Dba::write($sql);
$sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `album` ON `album`.`id`=`tag_map`.`object_id` WHERE `tag_map`.`object_type`='album' AND `album`.`id` IS NULL";
Dba::write($sql);
$sql = "DELETE FROM `tag_map` USING `tag_map` LEFT JOIN `artist` ON `artist`.`id`=`tag_map`.`object_id` WHERE `tag_map`.`object_type`='artist' AND `artist`.`id` IS NULL";
Dba::write($sql);
// Now nuke the tags themselves
$sql = "DELETE FROM `tag` USING `tag` LEFT JOIN `tag_map` ON `tag`.`id`=`tag_map`.`tag_id` WHERE `tag_map`.`id` IS NULL AND NOT EXISTS (SELECT 1 FROM `tag_merge` WHERE `tag_merge`.`tag_id` = `tag`.`id`)";
Dba::write($sql);
// delete duplicates
$sql = "DELETE `b` FROM `tag_map` AS `a`, `tag_map` AS `b` WHERE `a`.`id` < `b`.`id` AND `a`.`tag_id` <=> `b`.`tag_id` AND `a`.`object_id` <=> `b`.`object_id` AND `a`.`object_type` <=> `b`.`object_type`";
Dba::write($sql);
}
/**
* delete
*
* Delete the tag and all maps
*/
public function delete()
{
$sql = "DELETE FROM `tag_map` WHERE `tag_map`.`tag_id` = ?";
Dba::write($sql, array($this->id));
$sql = "DELETE FROM `tag_merge` WHERE `tag_merge`.`tag_id` = ?";
Dba::write($sql, array($this->id));
$sql = "DELETE FROM `tag` WHERE `tag`.`id` = ? ";
Dba::write($sql, array($this->id));
// Call the garbage collector to clean everything
self::garbage_collection();
parent::clear_cache();
}
/**
* tag_exists
* This checks to see if a tag exists, this has nothing to do with objects or maps
* @param string $value
* @return integer
*/
public static function tag_exists($value)
{
if (parent::is_cached('tag_name', $value)) {
return (int)(parent::get_from_cache('tag_name', $value))[0];
}
$sql = "SELECT `id` FROM `tag` WHERE `name` = ?";
$db_results = Dba::read($sql, array($value));
$results = Dba::fetch_assoc($db_results);
if (array_key_exists('id', $results)) {
parent::add_to_cache('tag_name', $value, array($results['id']));
return (int)$results['id'];
}
return 0;
} // tag_exists
/**
* tag_map_exists
* This looks to see if the current mapping of the current object of the current tag of the current
* user exists, lots of currents... taste good in scones.
* @param string $type
* @param integer $object_id
* @param integer $tag_id
* @param integer $user
* @return bool|int
*/
public static function tag_map_exists($type, $object_id, $tag_id, $user)
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
debug_event(__CLASS__, 'Requested type is not a library item.', 3);
return false;
}
$sql = "SELECT * FROM `tag_map` LEFT JOIN `tag` ON `tag`.`id` = `tag_map`.`tag_id` LEFT JOIN `tag_merge` ON `tag`.`id`=`tag_merge`.`tag_id` WHERE (`tag_map`.`tag_id` = ? OR `tag_map`.`tag_id` = `tag_merge`.`merged_to`) AND `tag_map`.`user` = ? AND `tag_map`.`object_id` = ? AND `tag_map`.`object_type` = ?";
$db_results = Dba::read($sql, array($tag_id, $user, $object_id, $type));
$results = Dba::fetch_assoc($db_results);
if (array_key_exists('id', $results)) {
return (int)$results['id'];
}
return false;
} // tag_map_exists
/**
* get_top_tags
* This gets the top tags for the specified object using limit
* @param string $type
* @param integer $object_id
* @param integer $limit
* @return array
*/
public static function get_top_tags($type, $object_id, $limit = 10)
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
return array();
}
$object_id = (int)($object_id);
$limit = (int)($limit);
$sql = "SELECT `tag_map`.`id`, `tag_map`.`tag_id`, `tag`.`name`, `tag_map`.`user` FROM `tag` LEFT JOIN `tag_map` ON `tag_map`.`tag_id`=`tag`.`id` WHERE `tag`.`is_hidden` = false AND `tag_map`.`object_type` = ? AND `tag_map`.`object_id` = ? LIMIT $limit";
$db_results = Dba::read($sql, array($type, $object_id));
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[$row['id']] = array('user' => $row['user'], 'id' => $row['tag_id'], 'name' => $row['name']);
}
return $results;
} // get_top_tags
/**
* get_object_tags
* Display all tags that apply to matching target type of the specified id
* @param string $type
* @param integer $object_id
* @return array|boolean
*/
public static function get_object_tags($type, $object_id = null)
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
return false;
}
$params = array($type);
$sql = "SELECT `tag_map`.`id`, `tag`.`name`, `tag_map`.`user` FROM `tag` LEFT JOIN `tag_map` ON `tag_map`.`tag_id`=`tag`.`id` WHERE `tag`.`is_hidden` = false AND `tag_map`.`object_type` = ?";
if ($object_id !== null) {
$sql .= " AND `tag_map`.`object_id` = ?";
$params[] = $object_id;
}
$results = array();
$db_results = Dba::read($sql, $params);
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = $row;
}
return $results;
} // get_object_tags
/**
* get_tag_objects
* This gets the objects from a specified tag and returns an array of object ids, nothing more
* @param string $type
* @param $tag_id
* @param string $count
* @param string $offset
* @return integer[]
*/
public static function get_tag_objects($type, $tag_id, $count = '', $offset = '')
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
return array();
}
$tag_sql = ((int) $tag_id == 0) ? "" : "`tag_map`.`tag_id` = ? AND";
$sql_param = ($tag_sql == "") ? array($type) : array($tag_id, $type);
$limit_sql = "";
if ($count) {
$limit_sql = " LIMIT ";
if ($offset) {
$limit_sql .= (string)($offset) . ', ';
}
$limit_sql .= (string)($count);
}
$sql = ($type == 'album')
? "SELECT DISTINCT MIN(`tag_map`.`object_id`) AS `object_id` FROM `tag_map` LEFT JOIN `album` ON `tag_map`.`object_id` = `album`.`id` "
: "SELECT DISTINCT `tag_map`.`object_id` FROM `tag_map` ";
$sql .= "WHERE $tag_sql `tag_map`.`object_type` = ?";
if (AmpConfig::get('catalog_disable') && in_array($type, array('song', 'artist', 'album'))) {
$sql .= "AND " . Catalog::get_enable_filter($type, '`tag_map`.`object_id`');
}
if ($type == 'album') {
if (AmpConfig::get('album_group')) {
$sql .= " GROUP BY `album`.`prefix`, `album`.`name`, `album`.`album_artist`, `album`.`release_type`, `album`.`release_status`, `album`.`mbid`, `album`.`year`, `album`.`original_year`, `album`.`mbid_group`";
} else {
$sql .= " GROUP BY `album`.`id`, `album`.`disk`";
}
}
$sql .= $limit_sql;
$db_results = Dba::read($sql, $sql_param);
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = (int)$row['object_id'];
}
return $results;
} // get_tag_objects
/**
* get_tag_ids
* This gets the objects from a specified tag and returns an array of object ids, nothing more
* @param string $type
* @param string $count
* @param string $offset
* @return integer[]
*/
public static function get_tag_ids($type, $count = '', $offset = '')
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
return array();
}
$limit_sql = "";
if ($count) {
$limit_sql = " LIMIT ";
if ($offset) {
$limit_sql .= (string)($offset) . ', ';
}
$limit_sql .= (string)($count);
}
$sql = "SELECT DISTINCT `tag_map`.`tag_id` FROM `tag_map` WHERE `tag_map`.`object_type` = ? ";
if (AmpConfig::get('catalog_disable') && in_array($type, array('song', 'artist', 'album'))) {
$sql .= "AND " . Catalog::get_enable_filter($type, '`tag_map`.`object_id`');
}
$sql .= $limit_sql;
$db_results = Dba::read($sql, array($type));
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = (int)$row['tag_id'];
}
return $results;
} // get_tag_ids
/**
* get_tags
* This is a non-object non type dependent function that just returns tags
* we've got, it can take filters (this is used by the tag cloud)
* @param string $type
* @param integer $limit
* @param string $order
* @return array
*/
public static function get_tags($type = '', $limit = 0, $order = 'count')
{
if (parent::is_cached('tags_list', 'no_name')) {
//debug_event(self::class, 'Tags list found into cache memory!', 5);
return parent::get_from_cache('tags_list', 'no_name');
}
$results = array();
if ($type == 'tag_hidden') {
$sql = "SELECT `tag`.`id` AS `tag_id`, `tag`.`name`, `tag`.`is_hidden` FROM `tag` WHERE `tag`.`is_hidden` = true ";
} else {
$type_sql = (!empty($type))
? "AND `tag_map`.`object_type` = '" . (string)scrub_in($type) . "'"
: "";
$sql = (AmpConfig::get('catalog_filter') && !empty(Core::get_global('user')))
? "SELECT `tag_map`.`tag_id`, `tag`.`name`, `tag`.`is_hidden`, COUNT(`tag_map`.`object_id`) AS `count` FROM `tag_map` LEFT JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` $type_sql AND `tag`.`is_hidden` = false WHERE" . Catalog::get_user_filter('tag', Core::get_global('user')->id) . " AND `name` IS NOT NULL "
: "SELECT `tag_map`.`tag_id`, `tag`.`name`, `tag`.`is_hidden`, COUNT(`tag_map`.`object_id`) AS `count` FROM `tag_map` LEFT JOIN `tag` ON `tag`.`id`=`tag_map`.`tag_id` $type_sql AND `tag`.`is_hidden` = false WHERE `name` IS NOT NULL ";
$sql .= "GROUP BY `tag_map`.`tag_id`, `tag`.`name`, `tag`.`is_hidden` ";
}
$order = "`" . $order . "`";
if ($order == 'count') {
$order .= " DESC";
}
$sql .= "ORDER BY " . $order;
if ($limit > 0) {
$sql .= " LIMIT $limit";
}
//debug_event(self::class, 'get_tags ' . $sql, 5);
$db_results = Dba::read($sql);
while ($row = Dba::fetch_assoc($db_results)) {
$results[$row['tag_id']] = array(
'id' => $row['tag_id'],
'name' => $row['name'],
'is_hidden' => $row['is_hidden'],
'count' => $row['count'] ?? 0
);
}
parent::add_to_cache('tags_list', 'no_name', $results);
return $results;
} // get_tags
/**
* get_display
* This returns a csv formatted version of the tags that we are given
* it also takes a type so that it knows how to return it, this is used
* by the formatting functions of the different objects
* @param array $tags
* @param boolean $link
* @param string $filter_type
* @return string
*/
public static function get_display($tags, $link = false, $filter_type = '')
{
//debug_event(self::class, 'Get display tags called...', 5);
if (!is_array($tags)) {
return '';
}
$web_path = AmpConfig::get('web_path');
$results = '';
// Iterate through the tags, format them according to type and element id
foreach ($tags as $value) {
if ($link) {
$results .= '<a href="' . $web_path . '/browse.php?action=tag&show_tag=' . $value['id'] . (!empty($filter_type) ? '&type=' . $filter_type : '') . '" title="' . scrub_out($value['name']) . '">';
}
$results .= $value['name'];
if ($link) {
$results .= '</a>';
}
$results .= ', ';
}
$results = rtrim((string)$results, ', ');
return $results;
} // get_display
/**
* update_tag_list
* Update the tags list based on a comma-separated list
* (ex. tag1,tag2,tag3,..)
* @param string $tags_comma
* @param string $object_type
* @param integer $object_id
* @param boolean $overwrite
* @return boolean
*/
public static function update_tag_list($tags_comma, $object_type, $object_id, $overwrite)
{
if (!strlen((string) $tags_comma) > 0) {
return self::remove_all_map($object_type, $object_id);
}
debug_event(self::class, "update_tag_list $object_type: {{$object_id}}", 5);
// tags from your file can be in a terrible format
$filterfolk = str_replace('Folk, World, & Country', 'Folk World & Country', $tags_comma);
$filterunder = str_replace('_', ', ', $filterfolk);
$filter = str_replace(';', ', ', $filterunder);
$filter_list = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $filter);
$editedTags = (is_array($filter_list)) ? array_unique($filter_list) : array();
$ctags = self::get_top_tags($object_type, $object_id, 50);
foreach ($ctags as $ctid => $ctv) {
//debug_event(self::class, 'ctag {' . $ctid . '} = ' . print_r($ctv, true), 5);
$found = false;
if ($ctv['id'] != '') {
$ctag = new Tag($ctv['id']);
foreach ($editedTags as $tk => $tv) {
//debug_event(self::class, 'from_tags {' . $tk . '} = ' . $tv, 5);
if (strtolower($ctag->name) == strtolower($tv)) {
$found = true;
break;
}
// check if this thing has been renamed into something else
$merged = self::construct_from_name($tv);
if ($merged && $merged->is_hidden && $merged->has_merge($ctag->name)) {
$found = true;
break;
}
}
if ($found) {
unset($editedTags[$ctag->name]);
}
if (!$found && $overwrite && $ctv['user'] == 0) {
debug_event(self::class, 'update_tag_list {' . $ctag->name . '} not found. Delete it.', 5);
$ctag->remove_map($object_type, $object_id, false);
}
}
}
// Look if we need to add some new tags
foreach ($editedTags as $tk => $tv) {
if ($tv != '') {
self::add($object_type, $object_id, $tv, false);
}
}
return true;
} // update_tag_list
/**
* clean_to_existing
* Clean tag list to existing tag list only
* @param array|string $tags
* @return array|string
*/
public static function clean_to_existing($tags)
{
if (is_array($tags)) {
$taglist = $tags;
} else {
$filterfolk = str_replace('Folk, World, & Country', 'Folk World & Country', $tags);
$filterunder = str_replace('_', ', ', $filterfolk);
$filter = str_replace(';', ', ', $filterunder);
$filter_list = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $filter);
$taglist = (is_array($filter_list)) ? array_unique($filter_list) : array();
}
$ret = array();
foreach ($taglist as $tag) {
$tag = trim((string)$tag);
if (!empty($tag)) {
if (self::tag_exists($tag)) {
$ret[] = $tag;
}
}
}
return (is_array($tags) ? $ret : implode(",", $ret));
}
/**
* count
* This returns the count for the all objects associated with this tag
* If a type is specific only counts for said type are returned
* @param string $type
* @param integer $user_id
* @return array
*/
public function count($type = '', $user_id = 0)
{
$params = array($this->id);
$filter_sql = "";
if ($user_id > 0) {
$filter_sql = " AND `user` = ?";
$params[] = $user_id;
}
if ($type) {
$filter_sql = " AND `object_type` = ?";
$params[] = $type;
}
$results = array();
$sql = "SELECT DISTINCT(`object_type`), COUNT(`object_id`) AS `count` FROM `tag_map` WHERE `tag_id` = ?" . $filter_sql . " GROUP BY `object_type`";
$db_results = Dba::read($sql, $params);
while ($row = Dba::fetch_assoc($db_results)) {
$results[$row['object_type']] = (int)$row['count'];
}
return $results;
} // count
/**
* remove_map
* This will only remove tag maps for the current user
* @param string $type
* @param integer $object_id
* @param boolean $user
* @return boolean
*/
public function remove_map($type, $object_id, $user = true)
{
if (!InterfaceImplementationChecker::is_library_item($type)) {
return false;
}
$uid = 0;
if ($user === true) {
$uid = (int)(Core::get_global('user')->id);
}
$sql = "DELETE FROM `tag_map` WHERE `tag_id` = ? AND `object_type` = ? AND `object_id` = ? AND `user` = ?";
Dba::write($sql, array($this->id, $type, $object_id, $uid));
return true;
} // remove_map
/**
* remove_all_map
* Clear all the tags from an object when there isn't anything there
* @param string $object_type
* @param integer $object_id
* @return boolean
*/
public static function remove_all_map($object_type, $object_id)
{
if (!InterfaceImplementationChecker::is_library_item($object_type)) {
return false;
}
$sql = "DELETE FROM `tag_map` WHERE `object_type` = ? AND `object_id` = ?";
Dba::write($sql, array($object_type, $object_id));
return true;
} // remove_all_map
/**
* @param boolean $details
*/
public function format($details = true)
{
unset($details); //dead code but called from other format calls
}
/**
* Get item keywords for metadata searches.
* @return array
*/
public function get_keywords()
{
$keywords = [];
$keywords['tag'] = [
'important' => true,
'label' => T_('Genre'),
'value' => $this->name
];
return $keywords;
}
/**
* get_fullname
* @return string
*/
public function get_fullname()
{
return $this->name;
}
/**
* Get item link.
* @return string
*/
public function get_link()
{
return '';
}
/**
* Get item f_link.
* @return string
*/
public function get_f_link()
{
return '';
}
/**