Completed
Push — 7.x-1.x ( 2f9e3c...1070be )
by Frédéric G.
01:36
created

Orphans::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\qa\Plugin\Qa\Control\Taxonomy;
4
5
class Orphans extends Taxonomy {
6
7
  /**
8
   * {@inheritdoc]
9
   */
10
  public function init() {
11
    $this->package_name = __NAMESPACE__;
12
    $this->title = t('Inconsistent node tagging');
13
    $this->description = t('Check for taxonomy_index entries pointing to a missing node or term. These should never happen, and should be removed when they do.');
14
  }
15
16
  /**
17
   * Locate {taxonomy_index} entries linking to a non-existent term or node revision.
18
   *
19
   * @return array
20
   */
21
  function checkOrphans() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    $sq = <<<sql
23
SELECT ti.tid AS titid,ti.nid AS tinid,
24
  td.tid AS tdtid, td.name AS tdname,
25
  n.nid
26
FROM {taxonomy_index} ti
27
  LEFT JOIN {taxonomy_term_data} td ON ti.tid = td.tid
28
  LEFT JOIN {node} n ON ti.nid = n.nid
29
WHERE td.tid IS NULL
30
  OR n.nid IS NULL
31
sql;
32
    // No node access: we are scanning the whole database for a fully privileged user.
33
    $q = db_query($sq);
34
    $orphans = array(
35
      'terms' => array(),
36
      'nodes' => array(),
37
    );
38
    foreach ($q->fetchAll() as $o) {
39
      if (is_null($o->tdtid)) {
40
        $orphans['terms'][] = $o->titid;
41
      }
42
      if (is_null($o->nid)) {
43
        $orphans['nodes'][] = $o->tinid;
44
      }
45
    }
46
    $orphans['terms']     = array_unique($orphans['terms']);
47
    sort($orphans['terms']);
48
    $orphans['nodes'] = array_unique($orphans['nodes']);
49
    sort($orphans['nodes']);
50
    return array(
51
      'status' => (empty($orphans['terms']) && empty($orphans['nodes'])) ? 1 : 0,
52
      'result' => $orphans,
53
    );
54
  }
55
56
  function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
    $pass = parent::run();
58
    $pass->record($this->checkOrphans());
59
    $pass->life->end();
60
61
    // Prepare for theming. Only one pass for this check.
62
    $result = isset($pass->result[0]) ? $pass->result[0] : NULL;
63
    $result = array(
64
      empty($result['terms'])
65
        ? t('All terms found')
66
        : t('Missing term IDs: @tids', array('@tids' => implode(', ', $result['terms']))),
67
      empty($result['nodes'])
68
        ? t('All nodes found')
69
        : t('Missing nodes: @nids', array('@nids' => implode(', ', $result['nodes']))),
70
    );
71
    $result = theme('item_list', $result);
72
    $pass->result = $result;
73
    return $pass;
74
  }
75
}
76