source: extensions/rv_db_integrity/check_db.php @ 32020

Last change on this file since 32020 was 27563, checked in by rvelices, 10 years ago
  • check parent/child album status/visibility/permissions
  • show offending ids for missing foreign keys
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 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        return query2array($query,null,$reff);
23}
24
25$default_checked = count($_POST)==0 ? 'checked="checked"' : '';
26
27// Standard reference tests ---------------------------------------------------
28$reference_tests = array();
29
30$reference_tests[IMAGES_TABLE] = array(
31    array(CADDIE_TABLE,           'element_id'),
32    array(CATEGORIES_TABLE,       'representative_picture_id'),
33    array(COMMENTS_TABLE,         'image_id'),
34    array(FAVORITES_TABLE,        'image_id'),
35    array(IMAGE_CATEGORY_TABLE,   'image_id'),
36    array(IMAGE_TAG_TABLE,        'image_id'),
37    array(RATE_TABLE,             'element_id'),
38  );
39
40$reference_tests[CATEGORIES_TABLE] = array(
41    array(CATEGORIES_TABLE,       'id_uppercat'),
42    array(GROUP_ACCESS_TABLE,     'cat_id'),
43    array(IMAGE_CATEGORY_TABLE,   'category_id'),
44    array(IMAGES_TABLE,           'storage_category_id'),
45    array(OLD_PERMALINKS_TABLE,   'cat_id'),
46    array(USER_ACCESS_TABLE,      'cat_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 (count($err))
77                        {
78                                $failed++;
79                                $tpl_var['errors'][]= count($err).' error references; #'.$test[0].'.'.$test[1].' referring to #'.$table.'.'.$field_name;
80                                $tpl_var['errors'][]= 'Offending '.$test[1].' '.implode(',',$err);
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// Status/visible tests
110$tpl_var = array(
111                'ID' => 'status',
112                'LABEL' => 'Album status/visibility',
113                'CHECKED' => isset($_POST['status']) ? 'checked="checked"' : $default_checked,
114                'COUNT' => 1,
115        );
116if (isset($_POST['status']))
117{
118        $failed = 0;
119        $query = '
120SELECT id,name,id_uppercat,status,visible
121  FROM '.CATEGORIES_TABLE;
122        $cats = query2array($query,'id');
123        foreach($cats as $cat)
124        {
125                if (!isset($cat['id_uppercat'])) continue;
126                if ('public'==$cat['status'] && 'public'!=$cats[$cat['id_uppercat']]['status'])
127                {
128                        $failed++;
129                        $tpl_var['errors'][] = 'Public album '.$cat['id'].' '.$cat['name'].' parent '.$cat['id_uppercat'].' is '.$cats[$cat['id_uppercat']]['status'];
130                }
131                if ('true'==$cat['visible'] && 'true'!=$cats[$cat['id_uppercat']]['visible'])
132                {
133                        $failed++;
134                        $tpl_var['errors'][] = 'Visible album '.$cat['id'].' '.$cat['name'].' parent '.$cat['id_uppercat'].' is '.$cats[$cat['id_uppercat']]['visible'];
135                }
136        }
137        $tpl_var['result'] = $failed;
138}
139$template->append('reference_tests', $tpl_var);
140
141// Permissions
142$tpl_var = array(
143                'ID' => 'permissions',
144                'LABEL' => 'Permissions',
145                'CHECKED' => isset($_POST['permissions']) ? 'checked="checked"' : $default_checked,
146                'COUNT' => 1,
147        );
148if (isset($_POST['permissions']))
149{
150        $query = '
151SELECT id,name,id_uppercat
152        FROM '.CATEGORIES_TABLE.'
153        WHERE status="private"';
154        $cats = query2array($query,'id');
155
156        $groups = array_fill_keys( array_keys($cats), array());
157        $query = 'SELECT cat_id,group_id FROM '.GROUP_ACCESS_TABLE;
158        $result = pwg_query($query);
159        while ($row=pwg_db_fetch_assoc($result))
160                $groups[$row['cat_id']][] = $row['group_id'];
161
162        $users = array_fill_keys( array_keys($cats), array());
163        $query = 'SELECT cat_id,user_id FROM '.USER_ACCESS_TABLE;
164        $result = pwg_query($query);
165        while ($row=pwg_db_fetch_assoc($result))
166                $users[$row['cat_id']][] = $row['user_id'];
167
168        $failed = 0;
169        foreach($cats as $cat)
170        {
171                if (!isset($cats[$cat['id_uppercat']])) continue;
172                foreach( array('users','groups') as $type)
173                {
174                        $arr = $$type;
175                        $me = $arr[$cat['id']];
176                        $dad = $arr[$cat['id_uppercat']];
177                        $delta = array_diff($me, $dad);
178                        if (count($delta))
179                        {
180                                $tpl_var['errors'][] = 'Album '.$cat['id'].' '.$cat['name'].' too many '.$type.' permissions '.implode(',',$delta);
181                                $failed++;
182                        }
183                }
184        }
185        $tpl_var['result'] = $failed;
186}
187$template->append('reference_tests', $tpl_var);
188
189
190// #images(id,storage_category_id) vs. #image_category(image_id,category_id) ---
191$tpl_var = array(
192                'ID' => 'id_storage_category_id',
193                'LABEL' => l10n('#images(id,storage_category_id) in #image_category'),
194                'CHECKED' => isset($_POST['id_storage_category_id']) ? 'checked="checked"' : $default_checked,
195                'COUNT' => 1,
196        );
197if (isset($_POST['id_storage_category_id']))
198{
199        $query = '
200SELECT i.id, i.storage_category_id, i.path
201  FROM '.IMAGES_TABLE.' i LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.image_id=i.id AND ic.category_id=i.storage_category_id
202  WHERE (ic.category_id IS NULL OR ic.image_id IS NULL) AND i.storage_category_id IS NOT NULL';
203        $result = pwg_query($query);
204        $tpl_var['result'] = pwg_db_num_rows($result);
205        $i=0;
206        while ($row=pwg_db_fetch_assoc($result) and $i<=50 )
207        {
208                $tpl_var['errors'][] = $row['path'].' missing entry ('.$row['id'].','.$row['storage_category_id']. ') in #'.IMAGE_CATEGORY_TABLE;
209                $i++;
210        }
211}
212$template->append('reference_tests', $tpl_var);
213
214
215$template->set_filename('check', dirname(__FILE__).'/check_db.tpl');
216$template->assign_var_from_handle('ADMIN_CONTENT', 'check');
217
218?>
Note: See TracBrowser for help on using the repository browser.