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