Passed
Push — master ( 409c3f...e5371a )
by Andreas
09:22
created

net_nemein_wiki_handler_latest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 96
ccs 26
cts 52
cp 0.5
rs 10
c 1
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _seek_updated() 0 30 6
A _handler_latest() 0 22 3
A _show_latest() 0 20 5
A _add_history_entry() 0 7 1
1
<?php
2
/**
3
 * @package net.nemein.wiki
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Wikipage latest handler
11
 *
12
 * @package net.nemein.wiki
13
 */
14
class net_nemein_wiki_handler_latest extends midcom_baseclasses_components_handler
15
{
16
    private int $_updated_pages = 0;
17
    private int $_max_pages = 0;
18
    private array $latest_pages = [];
19
20
    /**
21
     * List all items updated with then given timeframe
22
     */
23 1
    private function _seek_updated(int $from, int $to = null)
24
    {
25 1
        $to ??= time();
26
27 1
        $qb = net_nemein_wiki_wikipage::new_query_builder();
28 1
        $qb->add_constraint('topic.component', '=', 'net.nemein.wiki');
29 1
        $qb->add_constraint('topic', 'INTREE', $this->_topic->id);
30 1
        $qb->add_constraint('metadata.revised', '<=', date('Y-m-d H:i:s', $to));
31 1
        $qb->add_constraint('metadata.revised', '>=', date('Y-m-d H:i:s', $from));
32 1
        $qb->add_order('metadata.revised', 'DESC');
33
34 1
        $rcs = midcom::get()->rcs;
35
36 1
        foreach ($qb->execute() as $page) {
37
            $rcs_handler = $rcs->load_backend($page);
38
39
            // Get object history
40
            foreach ($rcs_handler->get_history()->all() as $history) {
41
                if ($this->_updated_pages >= $this->_max_pages) {
42
                    // End here
43
                    return;
44
                }
45
46
                if (   $history['date'] < $from
47
                    || $history['date'] > $to) {
48
                    // We can ignore revisions outside the timeframe
49
                    continue;
50
                }
51
                $history['object'] = $page;
52
                $this->_add_history_entry($history);
53
            }
54
        }
55
    }
56
57
    private function _add_history_entry(array $entry)
58
    {
59
        $history_date = date('Y-m-d', $entry['date']);
60
61
        $this->latest_pages[$history_date] ??= [];
62
        $this->latest_pages[$history_date][] = $entry;
63
        $this->_updated_pages++;
64
    }
65
66 1
    public function _handler_latest(array &$data)
67
    {
68 1
        $this->_max_pages = $this->_config->get('latest_count');
69
70
        // Start by looking for items within last two weeks
71 1
        $from = strtotime('14 days ago');
72 1
        $this->_seek_updated($from);
73
74 1
        $i = 0;
75 1
        while (   $this->_updated_pages < $this->_max_pages
76 1
               && $i < 20) {
77
            // Expand seek by another two weeks
78 1
            $to = $from;
79 1
            $from = strtotime('14 days ago', $to);
80 1
            $this->_seek_updated($from, $to);
81 1
            $i++;
82
        }
83
84 1
        $data['view_title'] = sprintf($this->_l10n->get('latest updates in %s'), $this->_topic->extra);
85 1
        midcom::get()->head->set_pagetitle($data['view_title']);
86
87 1
        $this->add_breadcrumb('latest/', $data['view_title']);
88
    }
89
90 1
    public function _show_latest(string $handler_id, array &$data)
91
    {
92 1
        if (!empty($this->latest_pages)) {
93
            krsort($this->latest_pages);
94
            $dates_shown = [];
95
            midcom_show_style('view-latest-header');
96
            foreach ($this->latest_pages as $date => $versions) {
97
                if (!isset($dates_shown[$date])) {
98
                    $data['date'] = $date;
99
                    midcom_show_style('view-latest-date');
100
                    $dates_shown[$date] = true;
101
                }
102
103
                foreach ($versions as $history) {
104
                    $data['history'] = $history;
105
                    midcom_show_style('view-latest-item');
106
                }
107
            }
108
109
            midcom_show_style('view-latest-footer');
110
        }
111
    }
112
}
113