Completed
Push — development ( f93eb8...ffa1a0 )
by Thomas
20s
created

util2/cron/modules/cache_waypoint_pool.class.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 *
5
 *
6
 *  This cronjob fills the table cache_waypoint_pool with waypoints that
7
 *  can be assigned to new caches. The code is cpu intensive on database
8
 *  server.
9
 ***************************************************************************/
10
11
checkJob(new CacheWayPointPool());
12
13
class CacheWayPointPool
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    public $name = 'cache_waypoint_pool';
16
    public $interval = 604800; // once a week
17
18
    public function run()
19
    {
20
        global $opt;
21
        $nLastInsertsCount = 1;
22
23
        // check if the pool needs to be filled up and repeat until the
24
        $nPoolSize = $this->getCurrentPoolSize();
25
        if ($nPoolSize < $opt['logic']['waypoint_pool']['min_count']) {
26
            while (($nPoolSize < $opt['logic']['waypoint_pool']['max_count']) && ($nLastInsertsCount > 0)) {
27
                $nLastInsertsCount = $this->fill($opt['logic']['waypoint_pool']['max_count'] - $nPoolSize);
28
                $nPoolSize = $this->getCurrentPoolSize();
29
            }
30
        }
31
    }
32
33
    public function getCurrentPoolSize()
34
    {
35
        return sql_value('SELECT COUNT(*) FROM `cache_waypoint_pool`', 0);
36
    }
37
38
    public function fill($max_inserts_count)
39
    {
40
        global $opt;
41
42
        $rowCount = 0;
43
        $nInsertCount = 0;
44
45
        if ($opt['logic']['waypoint_pool']['fill_gaps'] === true) {
46
            // query the first unused waypoint (between other waypoints)
47
            $rsStartWp = sql(
48
                "SELECT SQL_BUFFER_RESULT DECTOWP(WPTODEC(`c`.`wp_oc`, '&1')+1, '&1') AS `free_wp`
49
                               FROM `caches` AS `c`
50
                          LEFT JOIN `caches` AS `cNext` ON DECTOWP(WPTODEC(`c`.`wp_oc`, '&1')+1, '&1')=`cNext`.`wp_oc`
51
                          LEFT JOIN `cache_waypoint_pool` ON DECTOWP(WPTODEC(`c`.`wp_oc` ,'&1')+1, '&1')=`cache_waypoint_pool`.`wp_oc`
52
                              WHERE `c`.`wp_oc` REGEXP '&2'
53
                                AND ISNULL(`cNext`.`wp_oc`)
54
                                AND ISNULL(`cache_waypoint_pool`.`wp_oc`)
55
                           ORDER BY `free_wp` ASC
56
                              LIMIT 250",
57
                $opt['logic']['waypoint_pool']['prefix'],
58
                '^' . $opt['logic']['waypoint_pool']['prefix'] . '[' . $opt['logic']['waypoint_pool']['valid_chars'] . ']{1,}$'
59
            );
60
        } else {
61
            // query the last used waypoint
62
            $rsStartWp = sql(
63
                "SELECT SQL_BUFFER_RESULT DECTOWP(MAX(dec_wp)+1, '&2') AS `free_wp`
64
                           FROM (
65
                                   SELECT MAX(WPTODEC(`wp_oc`, '&2')) AS dec_wp
66
                                     FROM `caches`
67
                                    WHERE `wp_oc` REGEXP '&1'
68
                              UNION
69
                                   SELECT MAX(WPTODEC(`wp_oc`, '&2')) AS dec_wp
70
                                     FROM `cache_waypoint_pool`
71
                                 ) AS tbl",
72
                '^' . $opt['logic']['waypoint_pool']['prefix'] . '[' . $opt['logic']['waypoint_pool']['valid_chars'] . ']{1,}$',
73
                $opt['logic']['waypoint_pool']['prefix']
74
            );
75
        }
76
77
        while (($rStartWp = sql_fetch_assoc($rsStartWp)) && ($nInsertCount < $max_inserts_count)) {
78
            $free_wp = $rStartWp['free_wp'];
79
            if ($free_wp == '') {
80
                $free_wp = $opt['logic']['waypoint_pool']['prefix'] . '0001';
81
            }
82
            $nInsertCount += $this->fillTurn($free_wp, $max_inserts_count - $nInsertCount);
83
            $rowCount++;
84
        }
85
        sql_free_result($rsStartWp);
86
87
        if ($rowCount == 0) {
88
            // new database ...
89
            $nInsertCount += $this->fillTurn($opt['logic']['waypoint_pool']['prefix'] . '0001', $max_inserts_count);
90
        }
91
92
        return $nInsertCount;
93
    }
94
95
    // search for the next free range and use that waypoints to fill the waypoint pool
96
    public function fillTurn($start_wp, $max_inserts_count)
97
    {
98
        global $opt;
99
100
        // query the end of this waypoint range
101
        $end_wp = sql_value(
102
            "SELECT DECTOWP(MIN(dec_wp), '&3')
103
                           FROM (
104
                                   SELECT MIN(WPTODEC(`wp_oc`, '&3')) AS dec_wp
105
                                     FROM `caches`
106
                                    WHERE WPTODEC(`wp_oc`, '&3')>WPTODEC('&1', '&3')
107
                                      AND `wp_oc` REGEXP '&2'
108
                              UNION
109
                                   SELECT MIN(WPTODEC(`wp_oc`, '&3')) AS dec_wp
110
                                     FROM `cache_waypoint_pool`
111
                                    WHERE WPTODEC(`wp_oc`, '&3')>WPTODEC('&1', '&3')
112
                                 ) AS tbl",
113
            $opt['logic']['waypoint_pool']['prefix'] . '100000',
114
            $start_wp,
115
            '^' . $opt['logic']['waypoint_pool']['prefix'] . '[' . $opt['logic']['waypoint_pool']['valid_chars'] . ']{1,}$',
116
            $opt['logic']['waypoint_pool']['prefix']
117
        );
118
119
        // now, we have start and end waypoints ...
120
        $nWaypointsGenerated = 0;
121
        while (($nWaypointsGenerated < $max_inserts_count) && ($start_wp != $end_wp)) {
122
            sql("INSERT INTO `cache_waypoint_pool` (`wp_oc`) VALUES ('&1')", $start_wp);
123
            $nWaypointsGenerated++;
124
            $start_wp = $this->incrementWayPoint($start_wp, $opt['logic']['waypoint_pool']['prefix']);
125
        }
126
127
        return $nWaypointsGenerated;
128
    }
129
130
    // see mysql functions in doc/sql/stored-proc/maintain.php for explanation
131
    public function incrementWayPoint($wp, $prefix)
132
    {
133
        global $opt;
134
135
        $wp_chars = $opt['logic']['waypoint_pool']['valid_chars'];
136
        $b36_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
137
138
        if (substr($wp, 0, 2) != $prefix) {
139
            return '';
140
        }
141
142
        $wp_value = substr($wp, 2);
143
        $b36_value = '';
144
        $wpValueLen = strlen($wp_value);
145 View Code Duplication
        for ($i = 0; $i < $wpValueLen; $i++) {
146
            $b36_value .= substr($b36_chars, strpos($wp_chars, substr($wp_value, $i, 1)), 1);
147
        }
148
149
        $dec_value = base_convert($b36_value, strlen($wp_chars), 10) + 1;
150
        $b36_value = strtoupper(base_convert($dec_value, 10, strlen($wp_chars)));
151
152
        $wp_value = '';
153
        $b36ValueLen = strlen($b36_value);
154 View Code Duplication
        for ($i = 0; $i < $b36ValueLen; $i++) {
155
            $wp_value .= substr($wp_chars, strpos($b36_chars, substr($b36_value, $i, 1)), 1);
156
        }
157
158
        if (strlen($wp_value) < 4) {
159
            return $prefix . substr('0000' . $wp_value, - 4);
160
        }
161
162
        return $prefix . $wp_value;
163
    }
164
}
165