tag_cleanOrphan()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 1
b 0
f 0
nc 32
nop 0
dl 0
loc 35
rs 8.9457
1
<?php declare(strict_types=1);
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * XOOPS tag management module
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         https://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @since           1.00
18
 * @author          Taiwen Jiang <[email protected]>
19
 */
20
21
use XoopsModules\Tag;
22
use XoopsModules\Tag\Helper;
23
24
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
25
26
//defined('TAG_FUNCTIONS_INI') || require __DIR__ . '/functions.ini.php';
27
define('TAG_FUNCTIONS_RECON_LOADED', true);
28
29
if (!defined('TAG_FUNCTIONS_RECON')) :
30
    define('TAG_FUNCTIONS_RECON', 1);
31
32
    function tag_synchronization(): bool
33
    {
34
        /** @var \XoopsModuleHandler $moduleHandler */
35
        $moduleHandler = xoops_getHandler('module');
36
        $criteria      = new \CriteriaCompo(new \Criteria('isactive', '1'));
37
        $criteria->add(new \Criteria('dirname', "('system', 'tag')", 'NOT IN'));
38
        $modules_obj = $moduleHandler->getObjects($criteria, true);
39
40
        /** @var Tag\LinkHandler $linkHandler */
41
        $linkHandler = Helper::getInstance()->getHandler('Link'); //@var \XoopsModules\Tag\Handler $tagHandler
42
        $linkHandler->deleteAll(new \Criteria('tag_modid', '(' . implode(', ', array_keys($modules_obj)) . ')', 'NOT IN'), true);
43
44
        foreach (array_keys($modules_obj) as $mid) {
45
            $dirname = $modules_obj[$mid]->getVar('dirname');
46
            if (!@include $GLOBALS['xoops']->path("/modules/{$dirname}/class/plugins/plugin.tag.php")) {
47
                if (!@include $GLOBALS['xoops']->path("/modules/{$dirname}/include/plugin.tag.php")) {
48
                    if (!@include $GLOBALS['xoops']->path("/modules/tag/plugin/{$dirname}.php")) {
49
                        continue;
50
                    }
51
                }
52
            }
53
            $func_tag = "{$dirname}_tag_synchronization";
54
            if (!function_exists($func_tag)) {
55
                continue;
56
            }
57
            $res = $func_tag($mid);
58
        }
59
60
        return tag_cleanOrphan();
61
        //    return true;
62
    }
63
64
    /**
65
     * Cleans orphans from dB table
66
     *
67
     * @return bool true successfully deleted all orphans, false otherwise
68
     */
69
    function tag_cleanOrphan(): bool
70
    {
71
        /** @var \XoopsModules\Tag\TagHandler $tagHandler */
72
        $tagHandler = Helper::getInstance()->getHandler('Tag'); // xoops_getModuleHandler('tag', 'tag');
73
74
        $success = true;
75
        /* clear item-tag links */
76
        $sql     = "DELETE FROM {$tagHandler->table_link}" . " WHERE ({$tagHandler->keyName} NOT IN ( SELECT DISTINCT {$tagHandler->keyName} FROM {$tagHandler->table}) )";
77
        $s1      = $tagHandler->db->queryF($sql);
78
        $success = $success && $s1;
79
80
        /* remove empty stats-tag links */
81
        $sql     = "DELETE FROM {$tagHandler->table_stats} WHERE tag_count = 0";
82
        $s1      = $tagHandler->db->queryF($sql);
83
        $success = $success && $s1;
84
85
        /* clear stats-tag links */
86
        $sql     = "DELETE FROM {$tagHandler->table_stats}" . " WHERE ({$tagHandler->keyName} NOT IN ( SELECT DISTINCT {$tagHandler->keyName} FROM {$tagHandler->table}) )";
87
        $s1      = $tagHandler->db->queryF($sql);
88
        $success = $success && $s1;
89
90
        $sql     = "    DELETE FROM {$tagHandler->table_stats}"
91
                   . "    WHERE NOT EXISTS ( SELECT * FROM {$tagHandler->table_link} "
92
                   . "                       WHERE  {$tagHandler->table_link}.tag_modid={$tagHandler->table_stats}.tag_modid"
93
                   . "                       AND  {$tagHandler->table_link}.tag_catid={$tagHandler->table_stats}.tag_catid"
94
                   . '                     )';
95
        $s1      = $tagHandler->db->queryF($sql);
96
        $success = $success && $s1;
97
98
        /* clear empty tags */
99
        $sql     = "DELETE FROM {$tagHandler->table}" . " WHERE ({$tagHandler->keyName} NOT IN ( SELECT DISTINCT {$tagHandler->keyName} FROM {$tagHandler->table_link}) )";
100
        $s1      = $tagHandler->db->queryF($sql);
101
        $success = $success && $s1;
102
103
        return $success;
104
    }
105
106
endif;
107