Conditions | 22 |
Paths | 2688 |
Total Lines | 163 |
Code Lines | 89 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
268 | public function apply_block() |
||
269 | { |
||
270 | global $xoopsDB; |
||
271 | $xoopsDB->queryF('UPDATE ' . $xoopsDB->prefix('block_module_link') . ' SET module_id = -1, pageid = 0 WHERE module_id < 2 AND pageid = 1'); |
||
272 | |||
273 | //Change block module link to remove pages |
||
274 | //Remove page links for module subpages |
||
275 | $xoopsDB->queryF('DELETE FROM ' . $xoopsDB->prefix('block_module_link') . ' WHERE pageid > 0'); |
||
276 | |||
277 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP PRIMARY KEY'; |
||
278 | $xoopsDB->queryF($sql); |
||
279 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP pageid'; |
||
280 | $xoopsDB->queryF($sql); |
||
281 | $sql = 'ALTER IGNORE TABLE `' . $xoopsDB->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id` , `module_id`)'; |
||
282 | $xoopsDB->queryF($sql); |
||
283 | |||
284 | $xoopsDB->queryF('RENAME TABLE `' . $xoopsDB->prefix('newblocks') . '` TO `' . $xoopsDB->prefix('newblocks_bak') . '`'); |
||
285 | |||
286 | // Create new block table |
||
287 | $sql = 'CREATE TABLE ' . $xoopsDB->prefix('newblocks') . " ( |
||
288 | bid mediumint(8) unsigned NOT NULL auto_increment, |
||
289 | mid smallint(5) unsigned NOT NULL default '0', |
||
290 | func_num tinyint(3) unsigned NOT NULL default '0', |
||
291 | options varchar(255) NOT NULL default '', |
||
292 | name varchar(150) NOT NULL default '', |
||
293 | title varchar(255) NOT NULL default '', |
||
294 | content text, |
||
295 | side tinyint(1) unsigned NOT NULL default '0', |
||
296 | weight smallint(5) unsigned NOT NULL default '0', |
||
297 | visible tinyint(1) unsigned NOT NULL default '0', |
||
298 | block_type char(1) NOT NULL default '', |
||
299 | c_type char(1) NOT NULL default '', |
||
300 | isactive tinyint(1) unsigned NOT NULL default '0', |
||
301 | dirname varchar(50) NOT NULL default '', |
||
302 | func_file varchar(50) NOT NULL default '', |
||
303 | show_func varchar(50) NOT NULL default '', |
||
304 | edit_func varchar(50) NOT NULL default '', |
||
305 | template varchar(50) NOT NULL default '', |
||
306 | bcachetime int(10) unsigned NOT NULL default '0', |
||
307 | last_modified int(10) unsigned NOT NULL default '0', |
||
308 | PRIMARY KEY (bid), |
||
309 | KEY mid (mid), |
||
310 | KEY visible (visible), |
||
311 | KEY isactive_visible_mid (isactive,visible,mid), |
||
312 | KEY mid_funcnum (mid,func_num) |
||
313 | ) TYPE=MyISAM; |
||
314 | "; |
||
315 | $xoopsDB->queryF($sql); |
||
316 | |||
317 | $sql = ' SELECT MAX(instanceid) FROM ' . $xoopsDB->prefix('block_instance'); |
||
318 | $result = $xoopsDB->query($sql); |
||
319 | list($MaxInstanceId) = $xoopsDB->fetchRow($result); |
||
320 | |||
321 | // Change custom block mid from 1 to 0 |
||
322 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks_bak') . "` SET mid = 0 WHERE show_func = 'b_system_custom_show'"; |
||
323 | $result = $xoopsDB->queryF($sql); |
||
324 | |||
325 | $sql = ' SELECT b.*, i.instanceid ' . ' FROM ' . $xoopsDB->prefix('block_instance') . ' AS i LEFT JOIN ' . $xoopsDB->prefix('newblocks_bak') . ' AS b ON b.bid = i.bid ' . ' GROUP BY b.dirname, b.bid, i.instanceid'; |
||
326 | $result = $xoopsDB->query($sql); |
||
327 | $dirname = ''; |
||
328 | $bid = 0; |
||
329 | $block_key = null; |
||
330 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
331 | if ($row['dirname'] != $dirname) { |
||
332 | $dirname = $row['dirname']; |
||
333 | $modversion = array(); |
||
334 | if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') { |
||
335 | continue; |
||
336 | } |
||
337 | } |
||
338 | if (empty($modversion['blocks']) && $dirname !== 'system') { |
||
339 | continue; |
||
340 | } |
||
341 | |||
342 | $isClone = true; |
||
343 | if ($row['bid'] != $bid) { |
||
344 | $bid = $row['bid']; |
||
345 | $isClone = false; |
||
346 | $block_key = null; |
||
347 | $block_key = @$this->_block_lookup($row, $modversion['blocks']); |
||
348 | } |
||
349 | if ($block_key === null) { |
||
350 | continue; |
||
351 | } |
||
352 | |||
353 | // Copy data from block instance table and blocks table |
||
354 | $sql = ' INSERT INTO ' . $xoopsDB->prefix('newblocks') . ' (bid, mid, options, name, title, side, weight, visible, ' . ' func_num, ' . ' block_type, ' . ' c_type, ' . ' isactive, dirname, func_file,' . ' show_func, edit_func, template, bcachetime, last_modified)' . ' SELECT ' . ' i.instanceid, c.mid, i.options, c.name, i.title, i.side, i.weight, i.visible, ' . " {$block_key}, " . ($isClone ? " CASE WHEN c.show_func='b_system_custom_show' THEN 'C' ELSE 'D' END," : " CASE WHEN c.show_func='b_system_custom_show' THEN 'C' WHEN c.mid = 1 THEN 'S' ELSE 'M' END,") . " CASE WHEN c.c_type='' THEN 'H' ELSE c.c_type END," . ' c.isactive, c.dirname, c.func_file,' . ' c.show_func, c.edit_func, c.template, i.bcachetime, c.last_modified' . ' FROM ' . $xoopsDB->prefix('block_instance') . ' AS i,' . ' ' . $xoopsDB->prefix('newblocks_bak') . ' AS c' . ' WHERE i.bid = c.bid' . ' AND i.instanceid = ' . $row['instanceid']; |
||
355 | $xoopsDB->queryF($sql); |
||
356 | } |
||
357 | |||
358 | $sql = ' SELECT b.* ' . ' FROM ' . $xoopsDB->prefix('newblocks_bak') . ' AS b LEFT JOIN ' . $xoopsDB->prefix('block_instance') . ' AS i ON b.bid = i.bid ' . ' WHERE i.instanceid IS NULL'; |
||
359 | ' GROUP BY b.dirname, b.bid'; |
||
360 | $result = $xoopsDB->query($sql); |
||
361 | $dirname = ''; |
||
362 | $bid = 0; |
||
363 | $block_key = null; |
||
364 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
365 | if ($row['dirname'] != $dirname) { |
||
366 | $dirname = $row['dirname']; |
||
367 | $modversion = array(); |
||
368 | if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') { |
||
369 | continue; |
||
370 | } |
||
371 | } |
||
372 | if (empty($modversion['blocks']) && $dirname !== 'system') { |
||
373 | continue; |
||
374 | } |
||
375 | |||
376 | if ($row['bid'] != $bid) { |
||
377 | $bid = $row['bid']; |
||
378 | $block_key = null; |
||
379 | $block_key = @$this->_block_lookup($row, $modversion['blocks']); |
||
380 | } |
||
381 | if ($block_key === null) { |
||
382 | continue; |
||
383 | } |
||
384 | |||
385 | // Copy data from blocks table |
||
386 | $sql = ' INSERT INTO ' . $xoopsDB->prefix('newblocks') . ' (bid, mid, options, name, title, side, weight, visible, ' . ' func_num, ' . ' block_type, ' . ' c_type, ' . ' isactive, dirname, func_file,' . ' show_func, edit_func, template, bcachetime, last_modified)' . ' SELECT ' . " bid + {$MaxInstanceId}, mid, options, name, name, 0, 0, 0, " . " {$block_key}, " . " CASE WHEN show_func='b_system_custom_show' THEN 'C' WHEN mid = 1 THEN 'S' ELSE 'M' END," . " CASE WHEN c_type='' THEN 'H' ELSE c_type END," . ' isactive, dirname, func_file,' . ' show_func, edit_func, template, 0, last_modified' . ' FROM ' . $xoopsDB->prefix('newblocks_bak') . ' WHERE bid = ' . $row['bid']; |
||
387 | $xoopsDB->queryF($sql); |
||
388 | |||
389 | // Build block-module link |
||
390 | $sql = ' INSERT INTO ' . $xoopsDB->prefix('block_module_link') . ' (block_id, module_id)' . ' SELECT ' . " bid + {$MaxInstanceId}, -1" . ' FROM ' . $xoopsDB->prefix('newblocks_bak') . ' WHERE bid = ' . $row['bid']; |
||
391 | $xoopsDB->queryF($sql); |
||
392 | } |
||
393 | |||
394 | // Dealing with tables |
||
395 | $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('block_instance') . '`;'); |
||
396 | $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('newblocks_bak') . '`;'); |
||
397 | |||
398 | // Deal with custom blocks, convert options to type and content |
||
399 | $sql = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func='b_system_custom_show'"; |
||
400 | $result = $xoopsDB->query($sql); |
||
401 | while (false !== (list($bid, $options) = $xoopsDB->fetchRow($result))) { |
||
402 | $_options = unserialize($options); |
||
403 | $content = $_options[0]; |
||
404 | $type = $_options[1]; |
||
405 | $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET c_type = '{$type}', options = '', content = " . $xoopsDB->quote($content) . " WHERE bid = {$bid}"); |
||
406 | } |
||
407 | |||
408 | // Deal with block options, convert array values to "," and "|" delimited |
||
409 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET options = '' WHERE show_func <> 'b_system_custom_show' AND ( options = 'a:1:{i:0;s:0:\"\";}' OR options = 'a:0:{}' )"; |
||
410 | $result = $xoopsDB->queryF($sql); |
||
411 | $sql = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func <> 'b_system_custom_show' AND options <> ''"; |
||
412 | $result = $xoopsDB->query($sql); |
||
413 | while (false !== (list($bid, $_options) = $xoopsDB->fetchRow($result))) { |
||
414 | $options = unserialize($_options); |
||
415 | if (empty($options) || !is_array($options)) { |
||
416 | $options = array(); |
||
417 | } |
||
418 | $count = count($options); |
||
419 | //Convert array values to comma-separated |
||
420 | for ($i = 0; $i < $count; ++$i) { |
||
421 | if (is_array($options[$i])) { |
||
422 | $options[$i] = implode(',', $options[$i]); |
||
423 | } |
||
424 | } |
||
425 | $options = implode('|', $options); |
||
426 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks') . '` SET options = ' . $xoopsDB->quote($options) . " WHERE bid = {$bid}"; |
||
427 | $xoopsDB->queryF($sql); |
||
428 | } |
||
429 | |||
430 | return true; |
||
431 | } |
||
436 |