source: extensions/rv_db_integrity/check_db.php @ 4194

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