Tags.php 21.5 KB
Newer Older
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
3
4
5
6
7
8
 * @author Bernhard Reiter <ockham@raz.or.at>
 * @author derkostka <sebastian.kostka@gmail.com>
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Jörn Friedrich Dreyer <jfd@butonic.de>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <icewind@owncloud.com>
Thomas Müller's avatar
Thomas Müller committed
9
 * @author Robin McCorkell <robin@mccorkell.me.uk>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
10
11
12
13
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 * @author Thomas Tanghus <thomas@tanghus.net>
 * @author Vincent Petry <pvince81@owncloud.com>
 *
Thomas Müller's avatar
Thomas Müller committed
14
 * @copyright Copyright (c) 2016, ownCloud, Inc.
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */
30
31
32

/**
 * Class for easily tagging objects by their id
33
 *
34
35
36
37
38
39
 * A tag can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
 * anything else that is either parsed from a vobject or that the user chooses
 * to add.
 * Tag names are not case-sensitive, but will be saved with the case they
 * are entered in. If a user already has a tag 'family' for a type, and
 * tries to add a tag named 'Family' it will be silently ignored.
40
 */
41

42
43
namespace OC;

Joas Schilling's avatar
Joas Schilling committed
44
45
46
use OC\Tagging\Tag;
use OC\Tagging\TagMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
47

48
49
50
51
class Tags implements \OCP\ITags {

	/**
	 * Tags
52
53
	 *
	 * @var array
54
55
56
57
58
	 */
	private $tags = array();

	/**
	 * Used for storing objectid/categoryname pairs while rescanning.
59
60
	 *
	 * @var array
61
62
63
	 */
	private static $relations = array();

64
65
66
67
68
	/**
	 * Type
	 *
	 * @var string
	 */
69
	private $type;
70
71
72
73
74
75

	/**
	 * User
	 *
	 * @var string
	 */
76
	private $user;
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	/**
	 * Are we including tags for shared items?
	 *
	 * @var bool
	 */
	private $includeShared = false;

	/**
	 * The current user, plus any owners of the items shared with the current
	 * user, if $this->includeShared === true.
	 *
	 * @var array
	 */
	private $owners = array();

93
94
95
96
97
98
99
	/**
	 * The Mapper we're using to communicate our Tag objects to the database.
	 *
	 * @var TagMapper
	 */
	private $mapper;

100
101
102
103
104
105
106
107
	/**
	 * The sharing backend for objects of $this->type. Required if
	 * $this->includeShared === true to determine ownership of items.
	 *
	 * @var \OCP\Share_Backend
	 */
	private $backend;

108
109
110
111
112
113
114
115
	const TAG_TABLE = '*PREFIX*vcategory';
	const RELATION_TABLE = '*PREFIX*vcategory_to_object';

	const TAG_FAVORITE = '_$!<Favorite>!$_';

	/**
	* Constructor.
	*
116
117
118
119
	* @param TagMapper $mapper Instance of the TagMapper abstraction layer.
	* @param string $user The user whose data the object will operate on.
	* @param string $type The type of items for which tags will be loaded.
	* @param array $defaultTags Tags that should be created at construction.
120
	* @param boolean $includeShared Whether to include tags for items shared with this user by others.
121
	*/
122
	public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) {
123
		$this->mapper = $mapper;
124
		$this->user = $user;
125
		$this->type = $type;
126
127
128
129
130
131
132
		$this->includeShared = $includeShared;
		$this->owners = array($this->user);
		if ($this->includeShared) {
			$this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true));
			$this->backend = \OC\Share\Share::getBackend($this->type);
		}
		$this->tags = $this->mapper->loadTags($this->owners, $this->type);
133
134

		if(count($defaultTags) > 0 && count($this->tags) === 0) {
135
			$this->addMultiple($defaultTags, true);
136
137
138
139
140
141
142
143
144
		}
	}

	/**
	* Check if any tags are saved for this type and user.
	*
	* @return boolean.
	*/
	public function isEmpty() {
Thomas Tanghus's avatar
Thomas Tanghus committed
145
		return count($this->tags) === 0;
146
147
	}

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
	/**
	* Returns an array mapping a given tag's properties to its values:
	* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
	*
	* @param string $id The ID of the tag that is going to be mapped
	* @return array|false
	*/
	public function getTag($id) {
		$key = $this->getTagById($id);
		if ($key !== false) {
			return $this->tagMap($this->tags[$key]);
		}
		return false;
	}

163
164
165
	/**
	* Get the tags for a specific user.
	*
166
	* This returns an array with maps containing each tag's properties:
167
	* [
168
169
	* 	['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'],
	* 	['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'],
170
171
172
173
	* ]
	*
	* @return array
	*/
174
	public function getTags() {
175
176
177
178
		if(!count($this->tags)) {
			return array();
		}

179
180
181
		usort($this->tags, function($a, $b) {
			return strnatcasecmp($a->getName(), $b->getName());
		});
182
183
		$tagMap = array();

184
185
		foreach($this->tags as $tag) {
			if($tag->getName() !== self::TAG_FAVORITE) {
186
				$tagMap[] = $this->tagMap($tag);
187
188
189
190
191
192
			}
		}
		return $tagMap;

	}

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	/**
	* Return only the tags owned by the given user, omitting any tags shared
	* by other users.
	*
	* @param string $user The user whose tags are to be checked.
	* @return array An array of Tag objects.
	*/
	public function getTagsForUser($user) {
		return array_filter($this->tags,
			function($tag) use($user) {
				return $tag->getOwner() === $user;
			}
		);
	}

Vincent Petry's avatar
Vincent Petry committed
208
209
210
211
212
213
214
215
216
217
218
	/**
	 * Get the list of tags for the given ids.
	 *
	 * @param array $objIds array of object ids
	 * @return array|boolean of tags id as key to array of tag names
	 * or false if an error occurred
	 */
	public function getTagsForObjects(array $objIds) {
		$entries = array();

		try {
Morris Jobke's avatar
Morris Jobke committed
219
			$conn = \OC::$server->getDatabaseConnection();
220
			$chunks = array_chunk($objIds, 900, false);
Vincent Petry's avatar
Vincent Petry committed
221
222
223
224
225
226
			foreach ($chunks as $chunk) {
				$result = $conn->executeQuery(
					'SELECT `category`, `categoryid`, `objid` ' .
					'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
					'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
					array($this->user, $this->type, $chunk),
Joas Schilling's avatar
Joas Schilling committed
227
					array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
Vincent Petry's avatar
Vincent Petry committed
228
229
230
231
				);
				while ($row = $result->fetch()) {
					$objId = (int)$row['objid'];
					if (!isset($entries[$objId])) {
Morris Jobke's avatar
Morris Jobke committed
232
						$entries[$objId] = array();
Vincent Petry's avatar
Vincent Petry committed
233
					}
Morris Jobke's avatar
Morris Jobke committed
234
					$entries[$objId][] = $row['category'];
Vincent Petry's avatar
Vincent Petry committed
235
				}
236
				if (\OCP\DB::isError($result)) {
237
					\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
238
239
					return false;
				}
Vincent Petry's avatar
Vincent Petry committed
240
241
242
243
244
245
246
247
248
249
			}
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}

		return $entries;
	}

250
251
252
253
254
	/**
	* Get the a list if items tagged with $tag.
	*
	* Throws an exception if the tag could not be found.
	*
255
	* @param string $tag Tag id or name.
256
	* @return array|false An array of object ids or false on error.
257
	* @throws \Exception
258
	*/
259
	public function getIdsForTag($tag) {
260
		$result = null;
Joas Schilling's avatar
Joas Schilling committed
261
		$tagId = false;
262
263
264
265
		if(is_numeric($tag)) {
			$tagId = $tag;
		} elseif(is_string($tag)) {
			$tag = trim($tag);
Thomas Tanghus's avatar
Thomas Tanghus committed
266
267
268
269
			if($tag === '') {
				\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
				return false;
			}
270
			$tagId = $this->getTagId($tag);
271
272
273
		}

		if($tagId === false) {
274
			$l10n = \OC::$server->getL10N('core');
275
276
277
278
279
280
281
282
283
284
285
286
287
			throw new \Exception(
				$l10n->t('Could not find category "%s"', $tag)
			);
		}

		$ids = array();
		$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
			. '` WHERE `categoryid` = ?';

		try {
			$stmt = \OCP\DB::prepare($sql);
			$result = $stmt->execute(array($tagId));
			if (\OCP\DB::isError($result)) {
288
				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
289
290
291
292
293
294
295
296
297
298
				return false;
			}
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}

		if(!is_null($result)) {
			while( $row = $result->fetchRow()) {
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
				$id = (int)$row['objid'];

				if ($this->includeShared) {
					// We have to check if we are really allowed to access the
					// items that are tagged with $tag. To that end, we ask the
					// corresponding sharing backend if the item identified by $id
					// is owned by any of $this->owners.
					foreach ($this->owners as $owner) {
						if ($this->backend->isValidSource($id, $owner)) {
							$ids[] = $id;
							break;
						}
					}
				} else {
					$ids[] = $id;
				}
315
316
317
318
319
320
321
			}
		}

		return $ids;
	}

	/**
322
323
	* Checks whether a tag is saved for the given user,
	* disregarding the ones shared with him or her.
324
	*
325
326
327
328
329
330
331
332
333
334
335
336
337
	* @param string $name The tag name to check for.
	* @param string $user The user whose tags are to be checked.
	* @return bool
	*/
	public function userHasTag($name, $user) {
		$key = $this->array_searchi($name, $this->getTagsForUser($user));
		return ($key !== false) ? $this->tags[$key]->getId() : false;
	}

	/**
	* Checks whether a tag is saved for or shared with the current user.
	*
	* @param string $name The tag name to check for.
338
339
340
	* @return bool
	*/
	public function hasTag($name) {
341
		return $this->getTagId($name) !== false;
342
343
344
345
346
347
	}

	/**
	* Add a new tag.
	*
	* @param string $name A string with a name of the tag
348
	* @return false|int the id of the added tag or false on error.
349
350
351
352
	*/
	public function add($name) {
		$name = trim($name);

Thomas Tanghus's avatar
Thomas Tanghus committed
353
354
355
356
		if($name === '') {
			\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
			return false;
		}
357
		if($this->userHasTag($name, $this->user)) {
358
359
360
361
			\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
			return false;
		}
		try {
362
363
364
			$tag = new Tag($this->user, $this->type, $name);
			$tag = $this->mapper->insert($tag);
			$this->tags[] = $tag;
365
366
367
368
369
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}
370
371
		\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
		return $tag->getId();
372
373
374
375
376
	}

	/**
	* Rename tag.
	*
377
	* @param string|integer $from The name or ID of the existing tag
378
379
380
381
382
383
	* @param string $to The new name of the tag.
	* @return bool
	*/
	public function rename($from, $to) {
		$from = trim($from);
		$to = trim($to);
Thomas Tanghus's avatar
Thomas Tanghus committed
384
385
386
387
388
389

		if($to === '' || $from === '') {
			\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
			return false;
		}

390
391
392
393
394
		if (is_numeric($from)) {
			$key = $this->getTagById($from);
		} else {
			$key = $this->getTagByName($from);
		}
395
		if($key === false) {
396
397
398
			\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
			return false;
		}
399
400
401
402
403
404
		$tag = $this->tags[$key];

		if($this->userHasTag($to, $tag->getOwner())) {
			\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
			return false;
		}
405
406

		try {
407
408
			$tag->setName($to);
			$this->tags[$key] = $this->mapper->update($tag);
409
410
411
412
413
414
415
416
417
418
419
420
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}
		return true;
	}

	/**
	* Add a list of new tags.
	*
	* @param string[] $names A string with a name or an array of strings containing
421
	* the name(s) of the tag(s) to add.
422
423
424
425
	* @param bool $sync When true, save the tags
	* @param int|null $id int Optional object id to add to this|these tag(s)
	* @return bool Returns false on error.
	*/
426
	public function addMultiple($names, $sync=false, $id = null) {
427
428
429
430
		if(!is_array($names)) {
			$names = array($names);
		}
		$names = array_map('trim', $names);
Thomas Tanghus's avatar
Thomas Tanghus committed
431
432
		array_filter($names);

433
434
		$newones = array();
		foreach($names as $name) {
435
436
			if(!$this->hasTag($name) && $name !== '') {
				$newones[] = new Tag($this->user, $this->type, $name);
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
			}
			if(!is_null($id) ) {
				// Insert $objectid, $categoryid  pairs if not exist.
				self::$relations[] = array('objid' => $id, 'tag' => $name);
			}
		}
		$this->tags = array_merge($this->tags, $newones);
		if($sync === true) {
			$this->save();
		}

		return true;
	}

	/**
	 * Save the list of tags and their object relations
	 */
	protected function save() {
		if(is_array($this->tags)) {
			foreach($this->tags as $tag) {
				try {
458
459
460
					if (!$this->mapper->tagExists($tag)) {
						$this->mapper->insert($tag);
					}
461
462
463
464
465
				} catch(\Exception $e) {
					\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
						\OCP\Util::ERROR);
				}
			}
466

467
			// reload tags to get the proper ids.
468
469
470
			$this->tags = $this->mapper->loadTags($this->owners, $this->type);
			\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
				\OCP\Util::DEBUG);
471
472
473
474
475
476
			// Loop through temporarily cached objectid/tagname pairs
			// and save relations.
			$tags = $this->tags;
			// For some reason this is needed or array_search(i) will return 0..?
			ksort($tags);
			foreach(self::$relations as $relation) {
477
				$tagId = $this->getTagId($relation['tag']);
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
				\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
				if($tagId) {
					try {
						\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
							array(
								'objid' => $relation['objid'],
								'categoryid' => $tagId,
								'type' => $this->type,
								));
					} catch(\Exception $e) {
						\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
							\OCP\Util::ERROR);
					}
				}
			}
			self::$relations = array(); // reset
		} else {
			\OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! '
				. print_r($this->tags, true), \OCP\Util::ERROR);
		}
	}

	/**
	* Delete tags and tag/object relations for a user.
	*
	* For hooking up on post_deleteUser
	*
505
	* @param array $arguments
506
507
508
509
510
511
512
513
514
	*/
	public static function post_deleteUser($arguments) {
		// Find all objectid/tagId pairs.
		$result = null;
		try {
			$stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
				. 'WHERE `uid` = ?');
			$result = $stmt->execute(array($arguments['uid']));
			if (\OCP\DB::isError($result)) {
515
				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
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
			}
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
		}

		if(!is_null($result)) {
			try {
				$stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
					. 'WHERE `categoryid` = ?');
				while( $row = $result->fetchRow()) {
					try {
						$stmt->execute(array($row['id']));
					} catch(\Exception $e) {
						\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
							\OCP\Util::ERROR);
					}
				}
			} catch(\Exception $e) {
				\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
					\OCP\Util::ERROR);
			}
		}
		try {
			$stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
				. 'WHERE `uid` = ?');
			$result = $stmt->execute(array($arguments['uid']));
543
			if (\OCP\DB::isError($result)) {
544
				\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
545
546
			}
		} catch(\Exception $e) {
547
548
			\OCP\Util::writeLog('core', __METHOD__ . ', exception: '
				. $e->getMessage(), \OCP\Util::ERROR);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
		}
	}

	/**
	* Delete tag/object relations from the db
	*
	* @param array $ids The ids of the objects
	* @return boolean Returns false on error.
	*/
	public function purgeObjects(array $ids) {
		if(count($ids) === 0) {
			// job done ;)
			return true;
		}
		$updates = $ids;
		try {
			$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
			$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
			$query .= 'AND `type`= ?';
			$updates[] = $this->type;
569
			$stmt = \OCP\DB::prepare($query);
570
571
			$result = $stmt->execute($updates);
			if (\OCP\DB::isError($result)) {
572
				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
573
574
575
576
577
578
579
580
581
582
583
584
585
				return false;
			}
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}
		return true;
	}

	/**
	* Get favorites for an object type
	*
586
	* @return array|false An array of object ids.
587
588
589
	*/
	public function getFavorites() {
		try {
590
			return $this->getIdsForTag(self::TAG_FAVORITE);
591
592
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
593
				\OCP\Util::DEBUG);
594
595
596
597
598
599
600
601
602
603
604
			return array();
		}
	}

	/**
	* Add an object to favorites
	*
	* @param int $objid The id of the object
	* @return boolean
	*/
	public function addToFavorites($objid) {
605
		if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
Thomas Tanghus's avatar
Thomas Tanghus committed
606
			$this->add(self::TAG_FAVORITE);
607
		}
Thomas Tanghus's avatar
Thomas Tanghus committed
608
		return $this->tagAs($objid, self::TAG_FAVORITE);
609
610
611
612
613
614
615
616
617
	}

	/**
	* Remove an object from favorites
	*
	* @param int $objid The id of the object
	* @return boolean
	*/
	public function removeFromFavorites($objid) {
Thomas Tanghus's avatar
Thomas Tanghus committed
618
		return $this->unTag($objid, self::TAG_FAVORITE);
619
620
621
622
623
624
	}

	/**
	* Creates a tag/object relation.
	*
	* @param int $objid The id of the object
625
	* @param string $tag The id or name of the tag
Thomas Tanghus's avatar
Thomas Tanghus committed
626
	* @return boolean Returns false on error.
627
628
629
630
	*/
	public function tagAs($objid, $tag) {
		if(is_string($tag) && !is_numeric($tag)) {
			$tag = trim($tag);
Thomas Tanghus's avatar
Thomas Tanghus committed
631
632
633
634
			if($tag === '') {
				\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
				return false;
			}
635
			if(!$this->hasTag($tag)) {
Thomas Tanghus's avatar
Thomas Tanghus committed
636
				$this->add($tag);
637
			}
638
			$tagId =  $this->getTagId($tag);
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
		} else {
			$tagId = $tag;
		}
		try {
			\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
				array(
					'objid' => $objid,
					'categoryid' => $tagId,
					'type' => $this->type,
				));
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}
		return true;
	}

	/**
	* Delete single tag/object relation from the db
	*
	* @param int $objid The id of the object
661
	* @param string $tag The id or name of the tag
662
663
664
665
666
	* @return boolean
	*/
	public function unTag($objid, $tag) {
		if(is_string($tag) && !is_numeric($tag)) {
			$tag = trim($tag);
Thomas Tanghus's avatar
Thomas Tanghus committed
667
668
669
670
			if($tag === '') {
				\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
				return false;
			}
671
			$tagId =  $this->getTagId($tag);
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
		} else {
			$tagId = $tag;
		}

		try {
			$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
					. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
			$stmt = \OCP\DB::prepare($sql);
			$stmt->execute(array($objid, $tagId, $this->type));
		} catch(\Exception $e) {
			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
				\OCP\Util::ERROR);
			return false;
		}
		return true;
	}

	/**
690
	* Delete tags from the database.
691
	*
692
	* @param string[]|integer[] $names An array of tags (names or IDs) to delete
693
694
695
696
697
698
699
700
	* @return bool Returns false on error
	*/
	public function delete($names) {
		if(!is_array($names)) {
			$names = array($names);
		}

		$names = array_map('trim', $names);
Thomas Tanghus's avatar
Thomas Tanghus committed
701
		array_filter($names);
702
703
704
705
706
707

		\OCP\Util::writeLog('core', __METHOD__ . ', before: '
			. print_r($this->tags, true), \OCP\Util::DEBUG);
		foreach($names as $name) {
			$id = null;

708
709
710
711
712
713
			if (is_numeric($name)) {
				$key = $this->getTagById($name);
			} else {
				$key = $this->getTagByName($name);
			}
			if ($key !== false) {
714
715
716
				$tag = $this->tags[$key];
				$id = $tag->getId();
				unset($this->tags[$key]);
717
718
719
720
				$this->mapper->delete($tag);
			} else {
				\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
					. ': not found.', \OCP\Util::ERROR);
721
722
723
724
725
726
727
728
729
			}
			if(!is_null($id) && $id !== false) {
				try {
					$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
							. 'WHERE `categoryid` = ?';
					$stmt = \OCP\DB::prepare($sql);
					$result = $stmt->execute(array($id));
					if (\OCP\DB::isError($result)) {
						\OCP\Util::writeLog('core',
730
							__METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
731
732
733
734
735
736
737
738
739
740
741
742
743
							\OCP\Util::ERROR);
						return false;
					}
				} catch(\Exception $e) {
					\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
						\OCP\Util::ERROR);
					return false;
				}
			}
		}
		return true;
	}

744
745
	// case-insensitive array_search
	protected function array_searchi($needle, $haystack, $mem='getName') {
746
747
748
		if(!is_array($haystack)) {
			return false;
		}
749
750
751
752
753
		return array_search(strtolower($needle), array_map(
			function($tag) use($mem) {
				return strtolower(call_user_func(array($tag, $mem)));
			}, $haystack)
		);
754
755
	}

756
757
758
759
	/**
	* Get a tag's ID.
	*
	* @param string $name The tag name to look for.
760
	* @return string|bool The tag's id or false if no matching tag is found.
761
762
	*/
	private function getTagId($name) {
763
764
765
		$key = $this->array_searchi($name, $this->tags);
		if ($key !== false) {
			return $this->tags[$key]->getId();
766
		}
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
		return false;
	}

	/**
	* Get a tag by its name.
	*
	* @param string $name The tag name.
	* @return integer|bool The tag object's offset within the $this->tags
	*                      array or false if it doesn't exist.
	*/
	private function getTagByName($name) {
		return $this->array_searchi($name, $this->tags, 'getName');
	}

	/**
	* Get a tag by its ID.
	*
	* @param string $id The tag ID to look for.
	* @return integer|bool The tag object's offset within the $this->tags
	*                      array or false if it doesn't exist.
	*/
	private function getTagById($id) {
		return $this->array_searchi($id, $this->tags, 'getId');
790
	}
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806

	/**
	* Returns an array mapping a given tag's properties to its values:
	* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
	*
	* @param Tag $tag The tag that is going to be mapped
	* @return array
	*/
	private function tagMap(Tag $tag) {
		return array(
			'id'    => $tag->getId(),
			'name'  => $tag->getName(),
			'owner' => $tag->getOwner(),
			'type'  => $tag->getType()
		);
	}
807
}