source: extensions/rv_db_integrity/check_db.php @ 25964

Last change on this file since 25964 was 20461, checked in by rvelices, 11 years ago

rv* plugins use pwg_db_* instead of mysql_* functions

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1<?php
2defined('PHPWG_ROOT_PATH') or trigger_error('Hacking attempt!', E_USER_ERROR);
3
4load_language('plugin.lang', RVDI_PATH);
5
6function check_table_ref($t, $f, $reft, $reff)
7{
8  $query = '
9DESCRIBE '.$reft.' '.$reff;
10  $row = pwg_db_fetch_assoc( pwg_query($query) );
11  $ref_nullable = ( $row['Null'] != '');
12
13  $query = '
14SELECT ref.'.$reff.'
15  FROM '.$reft.' ref LEFT JOIN '.$t.' main ON ref.'.$reff.' = main.'.$f.'
16  WHERE main.'.$f.' IS NULL';
17  if ($ref_nullable)
18    $query.=' AND ref.'.$reff.' IS NOT NULL';
19  $query.='
20  GROUP BY ref.'.$reff;
21
22  $result = pwg_query($query);
23  return pwg_db_num_rows($result);
24}
25
26$default_checked = count($_POST)==0 ? 'checked="checked"' : '';
27
28// Standard reference tests ---------------------------------------------------
29$reference_tests = array();
30
31$reference_tests[IMAGES_TABLE] = array(
32    array(CADDIE_TABLE,           'element_id'),
33    array(CATEGORIES_TABLE,       'representative_picture_id'),
34    array(COMMENTS_TABLE,         'image_id'),
35    array(FAVORITES_TABLE,        'image_id'),
36    array(IMAGE_CATEGORY_TABLE,   'image_id'),
37    array(IMAGE_TAG_TABLE,        'image_id'),
38    array(RATE_TABLE,             'element_id'),
39  );
40
41$reference_tests[CATEGORIES_TABLE] = array(
42    array(CATEGORIES_TABLE,       'id_uppercat'),
43    array(GROUP_ACCESS_TABLE,     'cat_id'),
44    array(IMAGE_CATEGORY_TABLE,   'category_id'),
45    array(IMAGES_TABLE,           'storage_category_id'),
46    array(OLD_PERMALINKS_TABLE,   'cat_id'),
47    array(USER_ACCESS_TABLE,      'cat_id'),
48    array(USER_CACHE_CATEGORIES_TABLE, 'cat_id'),
49  );
50
51$reference_tests[TAGS_TABLE] = array(
52    array(IMAGE_TAG_TABLE,   'tag_id'),
53  );
54
55$reference_tests[GROUPS_TABLE] = array(
56    array(GROUP_ACCESS_TABLE,     'group_id'),
57    array(USER_GROUP_TABLE,       'group_id'),
58  );
59
60foreach ($reference_tests as $table=>$ref_test)
61{
62        $field_name = 'id';
63        $tpl_var =
64                array(
65                        'ID' => 'test-'.$table,
66                        'LABEL' => '#'.$table.'.'.$field_name,
67                        'CHECKED' => isset($_POST['test-'.$table]) ? 'checked="checked"' : $default_checked,
68                        'COUNT' => count($ref_test),
69                );
70
71        if ( isset($_POST['test-'.$table]) )
72        {
73                $failed = 0;
74                foreach ($ref_test as $test)
75                {
76                        $err = check_table_ref($table, $field_name, $test[0], $test[1] );
77                        if ($err)
78                        {
79                                $failed++;
80                                $tpl_var['errors'][]= $err.' error references; #'.$test[0].'.'.$test[1].' referring to #'.$table.'.'.$field_name;
81                        }
82                }
83                $tpl_var['result'] = $failed;
84        }
85        $template->append('reference_tests', $tpl_var);
86}
87
88// Permalinks test ------------------------------------------------------------
89$tpl_var = array(
90                'ID' => 'permalinks',
91                'LABEL' => l10n('Pemalinks'),
92                'CHECKED' => isset($_POST['permalinks']) ? 'checked="checked"' : $default_checked,
93                'COUNT' => 1,
94        );
95if (isset($_POST['permalinks']))
96{
97        $query = '
98SELECT c.permalink, c.id, op.cat_id
99  FROM '.CATEGORIES_TABLE.' c INNER JOIN '.OLD_PERMALINKS_TABLE.' op ON c.permalink=op.permalink';
100        $result = pwg_query($query);
101        $tpl_var['result'] = pwg_db_num_rows($result);
102        while ($row=pwg_db_fetch_assoc($result))
103        {
104                $tpl_var['errors'][] = $row['permalink'].' matches categories '.$row['id'].' and '.$row['cat_id'];
105        }
106}
107$template->append('reference_tests', $tpl_var);
108
109
110// #images(id,storage_category_id) vs. #image_category(image_id,category_id) ---
111$tpl_var = array(
112                'ID' => 'id_storage_category_id',
113                'LABEL' => l10n('#images(id,storage_category_id) in #image_category'),
114                'CHECKED' => isset($_POST['id_storage_category_id']) ? 'checked="checked"' : $default_checked,
115                'COUNT' => 1,
116        );
117if (isset($_POST['id_storage_category_id']))
118{
119  $query = '
120SELECT i.id, i.storage_category_id, i.path
121  FROM '.IMAGES_TABLE.' i LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.image_id=i.id AND ic.category_id=i.storage_category_id
122  WHERE (ic.category_id IS NULL OR ic.image_id IS NULL) AND i.storage_category_id IS NOT NULL';
123        $result = pwg_query($query);
124        $tpl_var['result'] = pwg_db_num_rows($result);
125        $i=0;
126        while ($row=pwg_db_fetch_assoc($result) and $i<=50 )
127        {
128                $tpl_var['errors'][] = $row['path'].' missing entry ('.$row['id'].','.$row['storage_category_id']. ') in #'.IMAGE_CATEGORY_TABLE;
129                $i++;
130        }
131}
132$template->append('reference_tests', $tpl_var);
133
134
135$template->set_filename('check', dirname(__FILE__).'/check_db.tpl');
136$template->assign_var_from_handle('ADMIN_CONTENT', 'check');
137
138?>
Note: See TracBrowser for help on using the repository browser.