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