/[drupal]/contributions/modules/vardump/vardump.module
ViewVC logotype

Contents of /contributions/modules/vardump/vardump.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Sat Mar 22 07:25:19 2008 UTC (20 months ago) by puregin
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.5: +71 -56 lines
File MIME type: text/x-php
First checkin for Drupal 6.x. Nicer treatment of output to screen.
1 <?php
2 // $Id: vardump.module,v 1.5 2008/03/22 05:42:24 puregin Exp $
3 /**
4 * @file vardump.module
5 *
6 * Allows privileged users to dump the variable table in a variety
7 * of formats, and to set variables from a file imported.
8 *
9 */
10
11 define('VARDUMP_FORMAT_XML', 0);
12 define('VARDUMP_FORMAT_PHP', 1);
13 define('VARDUMP_FORMAT_NAMEVALUE', 2);
14 define('VARDUMP_OUTPUT_SCREEN', 'vardump_destination_screen');
15 define('VARDUMP_OUTPUT_FILE', 'vardump_destination_file');
16
17 /** Implementation of hook_help(). */
18 function vardump_help($section) {
19 switch ($section) {
20 case 'admin/help#vardump':
21 $output = "<p>".
22 t('The vardump module allows a sufficiently privileged user to dump the Drupal\'s variable table in XML format. This allows for easy comparison of configuration settings determined by variables.').
23 "</p>";
24 return $output;
25 }
26 }
27
28 /** Implementation of hook_menu(). */
29 function vardump_menu() {
30 $items = array();
31
32 $items['admin/settings/vardump'] = array(
33 'title' => 'Variable dump',
34 'page callback' => 'vardump_admin',
35 'access arguments' => array('dump variable table'),
36 'type' => MENU_NORMAL_ITEM);
37 return $items;
38 }
39
40 /** Implementation of hook_perm(). */
41 function vardump_perm() {
42 return array('dump variable table');
43 }
44
45 /** Admin form */
46 function vardump_admin_form(&$form_state) {
47 $form_values = isset($form_state['values']) ? $form_state['values'] : array();
48 $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] + 1 : 1;
49
50 switch ($step) {
51 case 1: // select options
52 $output = t('Select the output format you want for the variable table dump. ');
53 $form['output_format'] =
54 array('#type' => 'radios',
55 '#title' => 'output format',
56 '#description' => $output,
57 '#default_value' =>
58 isset($edit['output_format']) ?
59 $edit['output_format'] : VARDUMP_FORMAT_XML,
60 '#options' =>
61 array(VARDUMP_FORMAT_XML => t('XML'),
62 VARDUMP_FORMAT_PHP => t('PHP'),
63 VARDUMP_FORMAT_NAMEVALUE => t('Name-Value'),
64 )
65 );
66 $form['output_destination'] =
67 array('#type' => 'radios',
68 '#title' => 'output destination',
69 '#description' => t('Select whether you want the output to be sent to the screen or to a file. Files are saved as timestamped text files in the system-configured files directory.'),
70 '#default_value' => isset($edit['output_destination']) ?
71 $edit['output_destination'] : VARDUMP_OUTPUT_SCREEN,
72 '#options' =>
73 array(VARDUMP_OUTPUT_SCREEN => t('screen'),
74 VARDUMP_OUTPUT_FILE => t('file'),
75 )
76 );
77 break;
78 case 2: // generate output
79 $format = $form_values['output_format'];
80 $destination = $form_values['output_destination'];
81 $form['output'] =
82 array('#title' => 'vardump ouput',
83 '#type' => 'textarea',
84 '#description' => t('Copy the variable definitions and paste into another file as required.'),
85 '#cols' => 60,
86 '#rows' => 40,
87 '#value' => vardump_dump_variables($format, $destination),
88 );
89 break;
90 }
91 if ($step < 2) { // Only display submit button on settings screen.
92 $form['submit'] = array(
93 '#type' => 'submit',
94 '#value' => t('Dump variable table'),
95 );
96 }
97 $form['step'] = array(
98 '#type' => 'value',
99 '#value' => $step,
100 );
101 return $form;
102 }
103
104 /** Callback for variable dump menu item. */
105 function vardump_admin() {
106 return drupal_get_form('vardump_admin_form');
107 }
108
109 /** Validation function for admin_form. */
110 function vardump_admin_form_validate($form, &$form_state) {
111 return NULL;
112 }
113
114 /** Dumps a given variable as php. */
115 function vardump_format_php($name, $rawvalue) {
116 $value = unserialize($rawvalue);
117 $output = "vardump_update_or_insert('$name', '$rawvalue');\n";
118 return $output;
119 }
120
121 /** Dumps a given variable as a key-value pair. */
122 function vardump_format_namevalue($name, $value) {
123 $output = "$name:$value\n";
124 return $output;
125 }
126
127 /** Dumps a given variable as an xml element. */
128 function vardump_format_xml($name, $rawvalue) {
129 $value = unserialize($rawvalue);
130 $type = substr($rawvalue, 0, 1);
131 switch ($type) {
132 case 'i': $typestr = 'integer'; break;
133 case 's': $typestr = 'string'; break;
134 case 'a':
135 $typestr = 'array';
136 //$value = var_dump($value);
137 break;
138 case 'b': $typestr = 'boolean'; break;
139 case 'd': $typestr = 'decimal'; break;
140 default: $typestr = 'undefined';
141 }
142 $output = "<variable>\n";
143 $output .= " <name>$name</name>\n";
144 $output .= " <type>$typestr</type>\n";
145 $output .= " <value>$value</value>\n";
146 $output .= "</variable>\n";
147 return $output;
148 }
149
150 /**
151 * @todo db name in export
152 */
153 function vardump_dump_init($format=VARDUMP_FORMAT_NAMEVALUE, $destination) {
154 $date = date('Y-m-d@G:i');
155 switch ($format) {
156 case VARDUMP_FORMAT_XML:
157 $init = '<variables timestamp=\'$date\'>';
158 $output .= $init;
159 return $output;
160 break;
161 case VARDUMP_FORMAT_PHP: // heredoc notation
162 $output.=<<<END_OF_INIT_FUNCTION
163 /* Variable table dump generated at $date */
164 function vardump_update_or_insert(\$name, \$value) {
165 \$result = db_query("SELECT name FROM {variable} WHERE name='%s'", \$name);
166 if (db_num_results(\$result) == 0) {
167 db_query("INSERT INTO {variable} (name, value) VALUES (%s, %s);", \$name, \$value);
168 }
169 else {
170 // variable \$name exists; do an update;
171 db_query("UPDATE {variable} SET value='%s' WHERE name='%s'", \$name, \$value);
172 }
173 }
174
175 END_OF_INIT_FUNCTION;
176 break;
177 case VARDUMP_FORMAT_NAMEVALUE:
178 $output .= "# Variable table dump generated at $date\n";
179 default:
180 }
181 return $output;
182 }
183
184 /** Close out a variable dump. */
185 function vardump_dump_finish($format=VARDUMP_FORMAT_NAMEVALUE,
186 $destination=VARDUMP_OUTPUT_SCREEN) {
187 switch ($format) {
188 case VARDUMP_FORMAT_XML:
189 $finish = '</variables>';
190 $output .= $finish;
191 break;
192 case VARDUMP_FORMAT_PHP:
193 break;
194 case VARDUMP_FORMAT_NAMEVALUE:
195 default:
196 }
197 return $output;
198 }
199
200 /**
201 * @todo refactor export code
202 */
203 function vardump_dump_rows($format=VARDUMP_FORMAT_NAMEVALUE, $destination=VARDUMP_OUTPUT_SCREEN) {
204 $result = db_query('SELECT name, value FROM {variable}');
205 while ($row = db_fetch_object($result)) {
206 switch ($format) {
207 case VARDUMP_FORMAT_XML:
208 $output .= vardump_format_xml($row->name, $row->value);
209 break;
210 case VARDUMP_FORMAT_PHP:
211 $output .= vardump_format_php($row->name, $row->value);
212 break;
213 case VARDUMP_FORMAT_NAMEVALUE:
214 default:
215 $output .= vardump_format_namevalue($row->name, $row->value);
216 break;
217 }
218 }
219 return $output;
220 }
221
222 /** Submit-action for admin_form. */
223 function vardump_admin_form_submit($form, &$form_state) {
224 $format = $form_state['values']['output_format'];
225 $destination = $form_state['values']['output_destination'];
226 if ($destination == VARDUMP_OUTPUT_SCREEN) {
227 $form_state['rebuild'] = TRUE;
228 $form_state['storage']['step'] = $form_state['values']['step'];
229 return;
230 }
231 else {
232 vardump_dump_variables($format, $destination);
233 drupal_goto('admin');
234 }
235 }
236
237 /**
238 * @todo If destination is file, then create an entry for the file in
239 * the files table
240 */
241 function vardump_dump_variables($format, $destination=VARDUMP_OUTPUT_SCREEN) {
242 $output = vardump_dump_init($format, $destination);
243 $output .= vardump_dump_rows($format, $destination);
244 $output .= vardump_dump_finish($format, $destination);
245 if ($destination != VARDUMP_OUTPUT_SCREEN) {
246 $date = date('Y-m-d@H\hi');
247 $filepath = file_directory_path() . "/vardump$date";
248 watchdog('vardump', t('Dumping variables to file %filepath',
249 array('%filepath' => $filepath)));
250 file_save_data($output, $filepath, FILE_EXISTS_RENAME);
251 drupal_set_message(t('Variables dumped to file %filepath',
252 array('%filepath' => $filepath)));
253 }
254 else {
255 return $output;
256 }
257 }
258
259
260 /**
261 * @todo write import code (import from php settings)
262 *
263 * @arg mode is the merge mode:
264 * overwrite
265 * merge (interactive)
266 * only_new
267 */
268 function vardump_import_variables($mode, $filename) {
269 }

  ViewVC Help
Powered by ViewVC 1.1.2