source: extensions/rv_db_integrity/check_db.php @ 7690

Last change on this file since 7690 was 7342, checked in by ddtddt, 14 years ago

[extensions] - rv_db_integrity - add localisation

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 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 = mysql_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 mysql_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(WAITING_TABLE,          'storage_category_id'),
49    array(USER_CACHE_CATEGORIES_TABLE, 'cat_id'),
50  );
51
52$reference_tests[TAGS_TABLE] = array(
53    array(IMAGE_TAG_TABLE,   'tag_id'),
54  );
55
56$reference_tests[GROUPS_TABLE] = array(
57    array(GROUP_ACCESS_TABLE,     'group_id'),
58    array(USER_GROUP_TABLE,       'group_id'),
59  );
60
61foreach ($reference_tests as $table=>$ref_test)
62{
63        $field_name = 'id';
64        $tpl_var =
65                array(
66                        'ID' => 'test-'.$table,
67                        'LABEL' => '#'.$table.'.'.$field_name,
68                        'CHECKED' => isset($_POST['test-'.$table]) ? 'checked="checked"' : $default_checked,
69                        'COUNT' => count($ref_test),
70                );
71
72        if ( isset($_POST['test-'.$table]) )
73        {
74                $failed = 0;
75                foreach ($ref_test as $test)
76                {
77                        $err = check_table_ref($table, $field_name, $test[0], $test[1] );
78                        if ($err)
79                        {
80                                $failed++;
81                                $tpl_var['errors'][]= $err.' error references; #'.$test[0].'.'.$test[1].' referring to #'.$table.'.'.$field_name;
82                        }
83                }
84                $tpl_var['result'] = $failed;
85        }
86        $template->append('reference_tests', $tpl_var);
87}
88
89// Permalinks test ------------------------------------------------------------
90$tpl_var = array(
91                'ID' => 'permalinks',
92                'LABEL' => l10n('Pemalinks'),
93                'CHECKED' => isset($_POST['permalinks']) ? 'checked="checked"' : $default_checked,
94                'COUNT' => 1,
95        );
96if (isset($_POST['permalinks']))
97{
98        $query = '
99SELECT c.permalink, c.id, op.cat_id
100  FROM '.CATEGORIES_TABLE.' c INNER JOIN '.OLD_PERMALINKS_TABLE.' op ON c.permalink=op.permalink';
101        $result = pwg_query($query);
102        $tpl_var['result'] = mysql_num_rows($result);
103        while ($row=mysql_fetch_assoc($result))
104        {
105                $tpl_var['errors'][] = $row['permalink'].' matches categories '.$row['id'].' and '.$row['cat_id'];
106        }
107}
108$template->append('reference_tests', $tpl_var);
109
110
111// #images(id,storage_category_id) vs. #image_category(image_id,category_id) ---
112$tpl_var = array(
113                'ID' => 'id_storage_category_id',
114                'LABEL' => l10n('#images(id,storage_category_id) in #image_category'),
115                'CHECKED' => isset($_POST['id_storage_category_id']) ? 'checked="checked"' : $default_checked,
116                'COUNT' => 1,
117        );
118if (isset($_POST['id_storage_category_id']))
119{
120  $query = '
121SELECT i.id, i.storage_category_id, i.path
122  FROM '.IMAGES_TABLE.' i LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.image_id=i.id AND ic.category_id=i.storage_category_id
123  WHERE (ic.category_id IS NULL OR ic.image_id IS NULL) AND i.storage_category_id IS NOT NULL';
124        $result = pwg_query($query);
125        $tpl_var['result'] = mysql_num_rows($result);
126        $i=0;
127        while ($row=mysql_fetch_assoc($result) and $i<=50 )
128        {
129                $tpl_var['errors'][] = $row['path'].' missing entry ('.$row['id'].','.$row['storage_category_id']. ') in #'.IMAGE_CATEGORY_TABLE;
130                $i++;
131        }
132}
133$template->append('reference_tests', $tpl_var);
134
135// uploadable virtual cats
136$tpl_var = array(
137                'ID' => 'uploadable_virtual_cats',
138                'LABEL' => l10n('Uploadable virtual cats'),
139                'CHECKED' => isset($_POST['uploadable_virtual_cats']) ? 'checked="checked"' : $default_checked,
140                'COUNT' => 1,
141        );
142if (isset($_POST['uploadable_virtual_cats']))
143{
144  $query = '
145SELECT c.id, c.name
146  FROM '.CATEGORIES_TABLE.' c
147  WHERE c.dir IS NULL AND uploadable="true"';
148        $result = pwg_query($query);
149        $tpl_var['result'] = mysql_num_rows($result);
150        $i=0;
151        while ($row=mysql_fetch_assoc($result) and $i<=50 )
152        {
153                $tpl_var['errors'][] = $row['id'].' ('.$row['name'].') is uploadable';
154                $i++;
155        }
156}
157$template->append('reference_tests', $tpl_var);
158
159$template->set_filename('check', dirname(__FILE__).'/check_db.tpl');
160$template->assign_var_from_handle('ADMIN_CONTENT', 'check');
161
162?>
Note: See TracBrowser for help on using the repository browser.