Conditions | 27 |
Paths | 3282 |
Total Lines | 189 |
Code Lines | 104 |
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 |
||
269 | public function apply_block() |
||
270 | { |
||
271 | global $xoopsDB; |
||
272 | $xoopsDB->queryF('UPDATE ' . $xoopsDB->prefix('block_module_link') . ' SET module_id = -1, pageid = 0 WHERE module_id < 2 AND pageid = 1'); |
||
273 | |||
274 | //Change block module link to remove pages |
||
275 | //Remove page links for module subpages |
||
276 | $xoopsDB->queryF('DELETE FROM ' . $xoopsDB->prefix('block_module_link') . ' WHERE pageid > 0'); |
||
277 | |||
278 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP PRIMARY KEY'; |
||
279 | $xoopsDB->queryF($sql); |
||
280 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP pageid'; |
||
281 | $xoopsDB->queryF($sql); |
||
282 | $sql = 'ALTER IGNORE TABLE `' . $xoopsDB->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id` , `module_id`)'; |
||
283 | $xoopsDB->queryF($sql); |
||
284 | |||
285 | $xoopsDB->queryF('RENAME TABLE `' . $xoopsDB->prefix('newblocks') . '` TO `' . $xoopsDB->prefix('newblocks_bak') . '`'); |
||
286 | |||
287 | // Create new block table |
||
288 | $sql = 'CREATE TABLE ' . $xoopsDB->prefix('newblocks') . " ( |
||
289 | bid mediumint(8) unsigned NOT NULL auto_increment, |
||
290 | mid smallint(5) unsigned NOT NULL default '0', |
||
291 | func_num tinyint(3) unsigned NOT NULL default '0', |
||
292 | options varchar(255) NOT NULL default '', |
||
293 | name varchar(150) NOT NULL default '', |
||
294 | title varchar(255) NOT NULL default '', |
||
295 | content text, |
||
296 | side tinyint(1) unsigned NOT NULL default '0', |
||
297 | weight smallint(5) unsigned NOT NULL default '0', |
||
298 | visible tinyint(1) unsigned NOT NULL default '0', |
||
299 | block_type char(1) NOT NULL default '', |
||
300 | c_type char(1) NOT NULL default '', |
||
301 | isactive tinyint(1) unsigned NOT NULL default '0', |
||
302 | dirname varchar(50) NOT NULL default '', |
||
303 | func_file varchar(50) NOT NULL default '', |
||
304 | show_func varchar(50) NOT NULL default '', |
||
305 | edit_func varchar(50) NOT NULL default '', |
||
306 | template varchar(50) NOT NULL default '', |
||
307 | bcachetime int(10) unsigned NOT NULL default '0', |
||
308 | last_modified int(10) unsigned NOT NULL default '0', |
||
309 | PRIMARY KEY (bid), |
||
310 | KEY `mid` (mid), |
||
311 | KEY visible (visible), |
||
312 | KEY isactive_visible_mid (isactive,visible,mid), |
||
313 | KEY mid_funcnum (mid,func_num) |
||
314 | ) TYPE=MyISAM; |
||
315 | "; |
||
316 | $xoopsDB->queryF($sql); |
||
317 | |||
318 | $sql = ' SELECT MAX(instanceid) FROM ' . $xoopsDB->prefix('block_instance'); |
||
319 | $result = $xoopsDB->query($sql); |
||
320 | if (!$xoopsDB->isResultSet($result)) { |
||
321 | throw new \RuntimeException( |
||
322 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
323 | ); |
||
324 | } |
||
325 | |||
326 | list($MaxInstanceId) = $xoopsDB->fetchRow($result); |
||
327 | |||
328 | // Change custom block mid from 1 to 0 |
||
329 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks_bak') . "` SET mid = 0 WHERE show_func = 'b_system_custom_show'"; |
||
330 | $result = $xoopsDB->queryF($sql); |
||
331 | |||
332 | $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'; |
||
333 | $result = $xoopsDB->query($sql); |
||
334 | if (!$xoopsDB->isResultSet($result)) { |
||
335 | throw new \RuntimeException( |
||
336 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
337 | ); |
||
338 | } |
||
339 | $dirname = ''; |
||
340 | $bid = 0; |
||
341 | $block_key = null; |
||
342 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
343 | if ($row['dirname'] != $dirname) { |
||
344 | $dirname = $row['dirname']; |
||
345 | $modversion = array(); |
||
346 | if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') { |
||
347 | continue; |
||
348 | } |
||
349 | } |
||
350 | if (empty($modversion['blocks']) && $dirname !== 'system') { |
||
351 | continue; |
||
352 | } |
||
353 | |||
354 | $isClone = true; |
||
355 | if ($row['bid'] != $bid) { |
||
356 | $bid = $row['bid']; |
||
357 | $isClone = false; |
||
358 | $block_key = null; |
||
359 | $block_key = @$this->_block_lookup($row, $modversion['blocks']); |
||
360 | } |
||
361 | if ($block_key === null) { |
||
362 | continue; |
||
363 | } |
||
364 | |||
365 | // Copy data from block instance table and blocks table |
||
366 | $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']; |
||
367 | $xoopsDB->queryF($sql); |
||
368 | } |
||
369 | |||
370 | $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'; |
||
371 | ' GROUP BY b.dirname, b.bid'; |
||
372 | $result = $xoopsDB->query($sql); |
||
373 | if (!$xoopsDB->isResultSet($result)) { |
||
374 | throw new \RuntimeException( |
||
375 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
376 | ); |
||
377 | } |
||
378 | $dirname = ''; |
||
379 | $bid = 0; |
||
380 | $block_key = null; |
||
381 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
382 | if ($row['dirname'] != $dirname) { |
||
383 | $dirname = $row['dirname']; |
||
384 | $modversion = array(); |
||
385 | if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') { |
||
386 | continue; |
||
387 | } |
||
388 | } |
||
389 | if (empty($modversion['blocks']) && $dirname !== 'system') { |
||
390 | continue; |
||
391 | } |
||
392 | |||
393 | if ($row['bid'] != $bid) { |
||
394 | $bid = $row['bid']; |
||
395 | $block_key = null; |
||
396 | $block_key = @$this->_block_lookup($row, $modversion['blocks']); |
||
397 | } |
||
398 | if ($block_key === null) { |
||
399 | continue; |
||
400 | } |
||
401 | |||
402 | // Copy data from blocks table |
||
403 | $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']; |
||
404 | $xoopsDB->queryF($sql); |
||
405 | |||
406 | // Build block-module link |
||
407 | $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']; |
||
408 | $xoopsDB->queryF($sql); |
||
409 | } |
||
410 | |||
411 | // Dealing with tables |
||
412 | $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('block_instance') . '`;'); |
||
413 | $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('newblocks_bak') . '`;'); |
||
414 | |||
415 | // Deal with custom blocks, convert options to type and content |
||
416 | $sql = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func='b_system_custom_show'"; |
||
417 | $result = $xoopsDB->query($sql); |
||
418 | if (!$xoopsDB->isResultSet($result)) { |
||
419 | throw new \RuntimeException( |
||
420 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
421 | ); |
||
422 | } |
||
423 | while (false !== (list($bid, $options) = $xoopsDB->fetchRow($result))) { |
||
424 | $_options = unserialize($options); |
||
425 | $content = $_options[0]; |
||
426 | $type = $_options[1]; |
||
427 | $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET c_type = '{$type}', options = '', content = " . $xoopsDB->quote($content) . " WHERE bid = {$bid}"); |
||
428 | } |
||
429 | |||
430 | // Deal with block options, convert array values to "," and "|" delimited |
||
431 | $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:{}' )"; |
||
432 | $result = $xoopsDB->queryF($sql); |
||
433 | $sql = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func <> 'b_system_custom_show' AND options <> ''"; |
||
434 | $result = $xoopsDB->query($sql); |
||
435 | if (!$xoopsDB->isResultSet($result)) { |
||
436 | throw new \RuntimeException( |
||
437 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
438 | ); |
||
439 | } |
||
440 | while (false !== (list($bid, $_options) = $xoopsDB->fetchRow($result))) { |
||
441 | $options = unserialize($_options); |
||
442 | if (empty($options) || !is_array($options)) { |
||
443 | $options = array(); |
||
444 | } |
||
445 | $count = count($options); |
||
446 | //Convert array values to comma-separated |
||
447 | for ($i = 0; $i < $count; ++$i) { |
||
448 | if (is_array($options[$i])) { |
||
449 | $options[$i] = implode(',', $options[$i]); |
||
450 | } |
||
451 | } |
||
452 | $options = implode('|', $options); |
||
453 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks') . '` SET options = ' . $xoopsDB->quote($options) . " WHERE bid = {$bid}"; |
||
454 | $xoopsDB->queryF($sql); |
||
455 | } |
||
456 | |||
457 | return true; |
||
458 | } |
||
463 |