Passed
Push — master ( cd8ed8...ef1fc1 )
by Andreas
10:21
created

net_nemein_wiki_handler_latest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 49.06%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 100
ccs 26
cts 53
cp 0.4906
rs 10
c 1
b 0
f 0
wmc 17

4 Methods

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