Total Complexity | 73 |
Total Lines | 425 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DataexportController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataexportController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class DataexportController extends BaseController |
||
15 | { |
||
16 | public $extensions = [ |
||
17 | 'sql' => 'sql', |
||
18 | 'copy' => 'sql', |
||
19 | 'csv' => 'csv', |
||
20 | 'tab' => 'txt', |
||
21 | 'html' => 'html', |
||
22 | 'xml' => 'xml', |
||
23 | ]; |
||
24 | public $controller_title = 'strexport'; |
||
25 | |||
26 | /** |
||
27 | * Default method to render the controller according to the action parameter. |
||
28 | */ |
||
29 | public function render() |
||
30 | { |
||
31 | set_time_limit(0); |
||
32 | |||
33 | // if (!isset($_REQUEST['table']) && !isset($_REQUEST['query'])) |
||
34 | // What must we do in this case? Maybe redirect to the homepage? |
||
35 | |||
36 | $format = 'N/A'; |
||
37 | // If format is set, then perform the export |
||
38 | if (!isset($_REQUEST['what'])) { |
||
39 | return $this->doDefault(); |
||
40 | } |
||
41 | |||
42 | $this->prtrace("REQUEST['what']", $_REQUEST['what']); |
||
43 | |||
44 | // Include application functions |
||
45 | $this->setNoOutput(true); |
||
46 | $clean = false; |
||
47 | $oids = false; |
||
48 | switch ($_REQUEST['what']) { |
||
49 | case 'dataonly': |
||
50 | // Check to see if they have pg_dump set up and if they do, use that |
||
51 | // instead of custom dump code |
||
52 | if ($this->misc->isDumpEnabled() && ('copy' == $_REQUEST['d_format'] || 'sql' == $_REQUEST['d_format'])) { |
||
53 | $this->prtrace('DUMP ENABLED, d_format is', $_REQUEST['d_format']); |
||
54 | $dbexport_controller = new \PHPPgAdmin\Controller\DbexportController($this->getContainer()); |
||
55 | |||
56 | return $dbexport_controller->render(); |
||
57 | } |
||
58 | $this->prtrace('d_format is', $_REQUEST['d_format'], 'd_oids is', isset($_REQUEST['d_oids'])); |
||
59 | $format = $_REQUEST['d_format']; |
||
60 | $oids = isset($_REQUEST['d_oids']); |
||
61 | |||
62 | break; |
||
63 | case 'structureonly': |
||
64 | // Check to see if they have pg_dump set up and if they do, use that |
||
65 | // instead of custom dump code |
||
66 | if ($this->misc->isDumpEnabled()) { |
||
67 | $dbexport_controller = new \PHPPgAdmin\Controller\DbexportController($this->getContainer()); |
||
68 | |||
69 | return $dbexport_controller->render(); |
||
70 | } |
||
71 | $clean = isset($_REQUEST['s_clean']); |
||
72 | |||
73 | break; |
||
74 | case 'structureanddata': |
||
75 | // Check to see if they have pg_dump set up and if they do, use that |
||
76 | // instead of custom dump code |
||
77 | if ($this->misc->isDumpEnabled()) { |
||
78 | $dbexport_controller = new \PHPPgAdmin\Controller\DbexportController($this->getContainer()); |
||
79 | |||
80 | return $dbexport_controller->render(); |
||
81 | } |
||
82 | $format = $_REQUEST['sd_format']; |
||
83 | $clean = isset($_REQUEST['sd_clean']); |
||
84 | $oids = isset($_REQUEST['sd_oids']); |
||
85 | |||
86 | break; |
||
87 | } |
||
88 | |||
89 | return $this->mimicDumpFeature($format, $clean, $oids); |
||
90 | } |
||
91 | |||
92 | protected function mimicDumpFeature($format, $clean, $oids) |
||
93 | { |
||
94 | $data = $this->misc->getDatabaseAccessor(); |
||
95 | |||
96 | set_time_limit(0); |
||
97 | |||
98 | // if (!isset($_REQUEST['table']) && !isset($_REQUEST['query'])) |
||
99 | // What must we do in this case? Maybe redirect to the homepage? |
||
100 | |||
101 | $format = 'N/A'; |
||
102 | // If format is set, then perform the export |
||
103 | if (!isset($_REQUEST['what'])) { |
||
104 | return $this->doDefault(); |
||
105 | } |
||
106 | |||
107 | $this->prtrace("REQUEST['what']", $_REQUEST['what']); |
||
108 | |||
109 | // Include application functions |
||
110 | $this->setNoOutput(true); |
||
111 | $clean = false; |
||
112 | $response = $this |
||
113 | ->container |
||
114 | ->responseobj; |
||
115 | |||
116 | // Make it do a download, if necessary |
||
117 | if ('download' == $_REQUEST['output']) { |
||
118 | // Set headers. MSIE is totally broken for SSL downloading, so |
||
119 | // we need to have it download in-place as plain text |
||
120 | if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) { |
||
121 | $response = $response |
||
122 | ->withHeader('Content-type', 'text/plain'); |
||
123 | } else { |
||
124 | $response = $response |
||
125 | ->withHeader('Content-type', 'application/download'); |
||
126 | |||
127 | if (isset($this->extensions[$format])) { |
||
128 | $ext = $this->extensions[$format]; |
||
129 | } else { |
||
130 | $ext = 'txt'; |
||
131 | } |
||
132 | $response = $response |
||
133 | ->withHeader('Content-Disposition', 'attachment; filename=dump.'.$ext); |
||
134 | } |
||
135 | } else { |
||
136 | $response = $response |
||
137 | ->withHeader('Content-type', 'text/plain'); |
||
138 | } |
||
139 | |||
140 | if (isset($_REQUEST['query'])) { |
||
141 | $_REQUEST['query'] = trim(urldecode($_REQUEST['query'])); |
||
142 | } |
||
143 | |||
144 | // Set the schema search path |
||
145 | if (isset($_REQUEST['search_path'])) { |
||
146 | $data->setSearchPath(array_map('trim', explode(',', $_REQUEST['search_path']))); |
||
147 | } |
||
148 | |||
149 | $subject = $this->coalesceArr($_REQUEST, 'subject', 'table')['subject']; |
||
150 | |||
151 | $object = $this->coalesceArr($_REQUEST, $subject)[$subject]; |
||
152 | |||
153 | // Set up the dump transaction |
||
154 | $status = $data->beginDump(); |
||
155 | $this->prtrace('subject', $subject); |
||
156 | $this->prtrace('object', $object); |
||
157 | |||
158 | // If the dump is not dataonly then dump the structure prefix |
||
159 | if ('dataonly' != $_REQUEST['what']) { |
||
160 | $tabledefprefix = $data->getTableDefPrefix($object, $clean); |
||
161 | $this->prtrace('tabledefprefix', $tabledefprefix); |
||
162 | echo $tabledefprefix; |
||
163 | } |
||
164 | |||
165 | // If the dump is not structureonly then dump the actual data |
||
166 | if ('structureonly' != $_REQUEST['what']) { |
||
167 | // Get database encoding |
||
168 | $dbEncoding = $data->getDatabaseEncoding(); |
||
1 ignored issue
–
show
|
|||
169 | |||
170 | // Set fetch mode to NUM so that duplicate field names are properly returned |
||
171 | $data->conn->setFetchMode(ADODB_FETCH_NUM); |
||
172 | |||
173 | // Execute the query, if set, otherwise grab all rows from the table |
||
174 | if ($object) { |
||
175 | $rs = $data->dumpRelation($object, $oids); |
||
176 | } else { |
||
177 | $rs = $data->conn->Execute($_REQUEST['query']); |
||
178 | } |
||
179 | $this->prtrace('$_REQUEST[query]', $_REQUEST['query']); |
||
180 | |||
181 | if ('copy' == $format) { |
||
182 | $data->fieldClean($object); |
||
183 | echo "COPY \"{$_REQUEST['table']}\""; |
||
184 | if ($oids) { |
||
185 | echo ' WITH OIDS'; |
||
186 | } |
||
187 | |||
188 | echo " FROM stdin;\n"; |
||
189 | while (!$rs->EOF) { |
||
190 | $first = true; |
||
191 | //while (list($k, $v) = each($rs->fields)) { |
||
192 | foreach ($rs->fields as $k => $v) { |
||
193 | // Escape value |
||
194 | $v = $data->escapeBytea($v); |
||
195 | |||
196 | // We add an extra escaping slash onto octal encoded characters |
||
197 | $v = preg_replace('/\\\\([0-7]{3})/', '\\\\\1', $v); |
||
198 | if ($first) { |
||
199 | echo (is_null($v)) ? '\\N' : $v; |
||
200 | $first = false; |
||
201 | } else { |
||
202 | echo "\t", (is_null($v)) ? '\\N' : $v; |
||
203 | } |
||
204 | } |
||
205 | echo "\n"; |
||
206 | $rs->moveNext(); |
||
207 | } |
||
208 | echo "\\.\n"; |
||
209 | } elseif ('html' == $format) { |
||
210 | echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n"; |
||
211 | echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"; |
||
212 | echo "<head>\r\n"; |
||
213 | echo "\t<title></title>\r\n"; |
||
214 | echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n"; |
||
215 | echo "</head>\r\n"; |
||
216 | echo "<body>\r\n"; |
||
217 | echo "<table class=\"phppgadmin\">\r\n"; |
||
218 | echo "\t<tr>\r\n"; |
||
219 | if (!$rs->EOF) { |
||
220 | // Output header row |
||
221 | $j = 0; |
||
222 | foreach ($rs->fields as $k => $v) { |
||
223 | $finfo = $rs->fetchField($j++); |
||
224 | if ($finfo->name == $data->id && !$oids) { |
||
225 | continue; |
||
226 | } |
||
227 | |||
228 | echo "\t\t<th>", $this->misc->printVal($finfo->name, true), "</th>\r\n"; |
||
229 | } |
||
230 | } |
||
231 | echo "\t</tr>\r\n"; |
||
232 | while (!$rs->EOF) { |
||
233 | echo "\t<tr>\r\n"; |
||
234 | $j = 0; |
||
235 | foreach ($rs->fields as $k => $v) { |
||
236 | $finfo = $rs->fetchField($j++); |
||
237 | if ($finfo->name == $data->id && !$oids) { |
||
238 | continue; |
||
239 | } |
||
240 | |||
241 | echo "\t\t<td>", $this->misc->printVal($v, true, $finfo->type), "</td>\r\n"; |
||
242 | } |
||
243 | echo "\t</tr>\r\n"; |
||
244 | $rs->moveNext(); |
||
245 | } |
||
246 | echo "</table>\r\n"; |
||
247 | echo "</body>\r\n"; |
||
248 | echo "</html>\r\n"; |
||
249 | } elseif ('xml' == $format) { |
||
250 | echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; |
||
251 | echo "<data>\n"; |
||
252 | if (!$rs->EOF) { |
||
253 | // Output header row |
||
254 | $j = 0; |
||
255 | echo "\t<header>\n"; |
||
256 | foreach ($rs->fields as $k => $v) { |
||
257 | $finfo = $rs->fetchField($j++); |
||
258 | $name = htmlspecialchars($finfo->name); |
||
259 | $type = htmlspecialchars($finfo->type); |
||
260 | echo "\t\t<column name=\"{$name}\" type=\"{$type}\" />\n"; |
||
261 | } |
||
262 | echo "\t</header>\n"; |
||
263 | } |
||
264 | echo "\t<records>\n"; |
||
265 | while (!$rs->EOF) { |
||
266 | $j = 0; |
||
267 | echo "\t\t<row>\n"; |
||
268 | foreach ($rs->fields as $k => $v) { |
||
269 | $finfo = $rs->fetchField($j++); |
||
270 | $name = htmlspecialchars($finfo->name); |
||
271 | if (!is_null($v)) { |
||
272 | $v = htmlspecialchars($v); |
||
273 | } |
||
274 | |||
275 | echo "\t\t\t<column name=\"{$name}\"", (is_null($v) ? ' null="null"' : ''), ">{$v}</column>\n"; |
||
276 | } |
||
277 | echo "\t\t</row>\n"; |
||
278 | $rs->moveNext(); |
||
279 | } |
||
280 | echo "\t</records>\n"; |
||
281 | echo "</data>\n"; |
||
282 | } elseif ('sql' == $format) { |
||
283 | $data->fieldClean($object); |
||
284 | while (!$rs->EOF) { |
||
285 | echo "INSERT INTO \"{$object}\" ("; |
||
286 | $first = true; |
||
287 | $j = 0; |
||
288 | foreach ($rs->fields as $k => $v) { |
||
289 | $finfo = $rs->fetchField($j++); |
||
290 | $k = $finfo->name; |
||
291 | // SQL (INSERT) format cannot handle oids |
||
292 | // if ($k == $data->id) continue; |
||
293 | // Output field |
||
294 | $data->fieldClean($k); |
||
295 | if ($first) { |
||
296 | echo "\"{$k}\""; |
||
297 | } else { |
||
298 | echo ", \"{$k}\""; |
||
299 | } |
||
300 | |||
301 | if (!is_null($v)) { |
||
302 | // Output value |
||
303 | // addCSlashes converts all weird ASCII characters to octal representation, |
||
304 | // EXCEPT the 'special' ones like \r \n \t, etc. |
||
305 | $v = addcslashes($v, "\0..\37\177..\377"); |
||
306 | // We add an extra escaping slash onto octal encoded characters |
||
307 | $v = preg_replace('/\\\\([0-7]{3})/', '\\\1', $v); |
||
308 | // Finally, escape all apostrophes |
||
309 | $v = str_replace("'", "''", $v); |
||
310 | } |
||
311 | if ($first) { |
||
312 | $values = (is_null($v) ? 'NULL' : "'{$v}'"); |
||
313 | $first = false; |
||
314 | } else { |
||
315 | $values .= ', '.((is_null($v) ? 'NULL' : "'{$v}'")); |
||
316 | } |
||
317 | } |
||
318 | echo ") VALUES ({$values});\n"; |
||
319 | $rs->moveNext(); |
||
320 | } |
||
321 | } else { |
||
322 | switch ($format) { |
||
323 | case 'tab': |
||
324 | $sep = "\t"; |
||
325 | |||
326 | break; |
||
327 | case 'csv': |
||
328 | default: |
||
329 | $sep = ','; |
||
330 | |||
331 | break; |
||
332 | } |
||
333 | if (!$rs->EOF) { |
||
334 | // Output header row |
||
335 | $first = true; |
||
336 | foreach ($rs->fields as $k => $v) { |
||
337 | $finfo = $rs->fetchField($k); |
||
338 | $v = $finfo->name; |
||
339 | if (!is_null($v)) { |
||
340 | $v = str_replace('"', '""', $v); |
||
341 | } |
||
342 | |||
343 | if ($first) { |
||
344 | echo "\"{$v}\""; |
||
345 | $first = false; |
||
346 | } else { |
||
347 | echo "{$sep}\"{$v}\""; |
||
348 | } |
||
349 | } |
||
350 | echo "\r\n"; |
||
351 | } |
||
352 | while (!$rs->EOF) { |
||
353 | $first = true; |
||
354 | foreach ($rs->fields as $k => $v) { |
||
355 | if (!is_null($v)) { |
||
356 | $v = str_replace('"', '""', $v); |
||
357 | } |
||
358 | |||
359 | if ($first) { |
||
360 | echo (is_null($v)) ? '"\\N"' : "\"{$v}\""; |
||
361 | $first = false; |
||
362 | } else { |
||
363 | echo is_null($v) ? "{$sep}\"\\N\"" : "{$sep}\"{$v}\""; |
||
364 | } |
||
365 | } |
||
366 | echo "\r\n"; |
||
367 | $rs->moveNext(); |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | |||
372 | // If the dump is not dataonly then dump the structure suffix |
||
373 | if ('dataonly' != $_REQUEST['what']) { |
||
374 | // Set fetch mode back to ASSOC for the table suffix to work |
||
375 | $data->conn->setFetchMode(ADODB_FETCH_ASSOC); |
||
376 | $tabledefsuffix = $data->getTableDefSuffix($object); |
||
377 | $this->prtrace('tabledefsuffix', $tabledefsuffix); |
||
378 | echo $tabledefsuffix; |
||
379 | } |
||
380 | |||
381 | // Finish the dump transaction |
||
382 | $status = $data->endDump(); |
||
383 | |||
384 | return $response; |
||
385 | } |
||
386 | |||
387 | public function doDefault($msg = '') |
||
439 | } |
||
440 | } |
||
441 |