Completed
Push — master ( cde53c...fe9df2 )
by Andreas
27:57 queued 42s
created

net_nemein_wiki_handler_latest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 52.54%

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 106
ccs 31
cts 59
cp 0.5254
rs 10
c 0
b 0
f 0
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
B _seek_updated() 0 32 7
A _handler_latest() 0 23 3
A _show_latest() 0 21 5
A _add_history_entry() 0 9 2
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
19
    /**
20
     * List all items updated with then given timeframe
21
     */
22 1
    private function _seek_updated(int $from, $to = null)
23
    {
24 1
        if ($to === null) {
25 1
            $to = time();
26
        }
27
28 1
        $qb = net_nemein_wiki_wikipage::new_query_builder();
29 1
        $qb->add_constraint('topic.component', '=', 'net.nemein.wiki');
30 1
        $qb->add_constraint('topic', 'INTREE', $this->_topic->id);
31 1
        $qb->add_constraint('metadata.revised', '<=', date('Y-m-d H:i:s', $to));
32 1
        $qb->add_constraint('metadata.revised', '>=', date('Y-m-d H:i:s', $from));
33 1
        $qb->add_order('metadata.revised', 'DESC');
34
35 1
        $rcs = midcom::get()->rcs;
36
37 1
        foreach ($qb->execute() as $page) {
38
            $rcs_handler = $rcs->load_backend($page);
39
40
            // Get object history
41
            foreach ($rcs_handler->get_history()->all() as $version => $history) {
42
                if ($this->_updated_pages >= $this->_max_pages) {
43
                    // End here
44
                    return;
45
                }
46
47
                if (   $history['date'] < $from
48
                    || $history['date'] > $to) {
49
                    // We can ignore revisions outside the timeframe
50
                    continue;
51
                }
52
                $history['object'] = $page;
53
                $this->_add_history_entry($version, $history);
54
            }
55
        }
56 1
    }
57
58
    private function _add_history_entry($version, array $entry)
59
    {
60
        $history_date = date('Y-m-d', $entry['date']);
61
62
        if (!isset($this->_request_data['latest_pages'][$history_date])) {
63
            $this->_request_data['latest_pages'][$history_date] = [];
64
        }
65
        $this->_request_data['latest_pages'][$history_date][] = $entry;
66
        $this->_updated_pages++;
67
    }
68
69 1
    public function _handler_latest(array &$data)
70
    {
71 1
        $data['latest_pages'] = [];
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 1
               && $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 1
    }
93
94
    /**
95
     *
96
     * @param mixed $handler_id The ID of the handler.
97
     * @param array $data The local request data.
98
     */
99 1
    public function _show_latest($handler_id, array &$data)
100
    {
101 1
        $data['wikiname'] = $this->_topic->extra;
102 1
        if (!empty($data['latest_pages'])) {
103
            krsort($data['latest_pages']);
104
            $dates_shown = [];
105
            midcom_show_style('view-latest-header');
106
            foreach ($data['latest_pages'] as $date => $versions) {
107
                if (!isset($dates_shown[$date])) {
108
                    $data['date'] = $date;
109
                    midcom_show_style('view-latest-date');
110
                    $dates_shown[$date] = true;
111
                }
112
113
                foreach ($versions as $history) {
114
                    $data['history'] = $history;
115
                    midcom_show_style('view-latest-item');
116
                }
117
            }
118
119
            midcom_show_style('view-latest-footer');
120
        }
121 1
    }
122
}
123