1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\qa\Taxonomy; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\PrivateKey; |
6
|
|
|
use Drupal\qa\Pass; |
7
|
|
|
use Drupal\qa\Plugin\Qa\Control\BaseControl; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Find views containing PHP code |
11
|
|
|
*/ |
12
|
|
|
class Freetagging extends BaseControl { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc] |
16
|
|
|
*/ |
17
|
|
|
public function __construct(PrivateKey $pk) { |
18
|
|
|
parent::__construct($pk); |
19
|
|
|
$this->package_name = __NAMESPACE__; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function getDependencies(): array { |
23
|
|
|
$ret = BaseControl::getDependencies(); |
24
|
|
|
$ret = array_merge($ret, ['taxonomy']); |
25
|
|
|
return $ret; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc] |
30
|
|
|
*/ |
31
|
|
|
public function init() { |
32
|
|
|
$this->package_name = __NAMESPACE__; |
33
|
|
|
$this->title = t('Unused freetagging terms'); |
34
|
|
|
$this->description = t('Unused freetagging terms mean useless volume. Removing them helps making term autocompletion more relevant.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* List unused tags. |
39
|
|
|
* |
40
|
|
|
* @param object $vocabulary |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function checkTags($vocabulary) { |
45
|
|
|
$sq = <<<sql |
46
|
|
|
SELECT td.tid |
47
|
|
|
FROM {taxonomy_term_data} td |
48
|
|
|
LEFT JOIN {taxonomy_index} tn ON td.tid = tn.tid |
49
|
|
|
WHERE |
50
|
|
|
td.vid = :vid AND tn.nid IS NULL |
51
|
|
|
sql; |
52
|
|
|
// no db_rewrite_sql(): we are checking the whole database |
53
|
|
|
$q = db_query($sq, [':vid' => $vocabulary->vid]); |
|
|
|
|
54
|
|
|
$result = [ |
55
|
|
|
'vocabulary' => $vocabulary, |
56
|
|
|
'terms' => [], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
foreach ($q->fetchAll() as $o) { |
60
|
|
|
$term = taxonomy_term_load($o->tid); // has an internal cache, so we may loop |
|
|
|
|
61
|
|
|
$result['terms'][$term->tid] = l($term->name, 'admin/content/taxonomy/edit/term/'. $term->tid, [ |
|
|
|
|
62
|
|
|
'query' => ['destination' => 'admin/reports/qa/result'], |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
$ret = [ |
66
|
|
|
'name' => $vocabulary->name, |
67
|
|
|
'status' => empty($result['terms']) ? 1 : 0, |
68
|
|
|
'result' => $result, |
69
|
|
|
]; |
70
|
|
|
return $ret; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function run(): Pass { |
74
|
|
|
$pass = parent::run(); |
75
|
|
|
$vocabularies = taxonomy_get_vocabularies(); |
|
|
|
|
76
|
|
|
foreach ($vocabularies as $vocabulary) { |
77
|
|
|
if (!empty($vocabulary->tags)) { |
78
|
|
|
$pass->record($this->checkTags($vocabulary)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
$pass->life->end(); |
82
|
|
|
|
83
|
|
|
// Prepare for theming |
84
|
|
|
$result = ''; |
85
|
|
|
uksort($pass->result, 'strcasecmp'); // @XXX May be inconsistent with non-BMP strings ? |
86
|
|
|
foreach ($pass->result as $vocabulary_name => $info) { |
87
|
|
|
$vocabulary_link = l($vocabulary_name, 'admin/content/taxonomy/'. $info['vocabulary']->vid); |
|
|
|
|
88
|
|
|
$result[] = t('!link: !terms', [ |
89
|
|
|
'!link' => $vocabulary_link, |
90
|
|
|
'!terms' => implode(', ', $info['terms']), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
$result = empty($result) |
|
|
|
|
94
|
|
|
? t('All tags are in use') |
95
|
|
|
: theme('item_list', $result); |
|
|
|
|
96
|
|
|
$pass->result = $result; |
|
|
|
|
97
|
|
|
return $pass; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|