Completed
Push — newinternal ( 65a0f5...5b021c )
by Simon
08:29
created

PageLog::setupSearchHelper()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 6
dl 0
loc 24
ccs 0
cts 2
cp 0
crap 30
rs 9.2248
c 0
b 0
f 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Waca\DataObjects\Log;
12
use Waca\DataObjects\User;
13
use Waca\Helpers\LogHelper;
14
use Waca\Helpers\SearchHelpers\LogSearchHelper;
15
use Waca\Helpers\SearchHelpers\UserSearchHelper;
16
use Waca\Tasks\InternalPageBase;
17
use Waca\WebRequest;
18
19
class PageLog extends InternalPageBase
20
{
21
    /**
22
     * Main function for this page, when no specific actions are called.
23
     */
24
    protected function main()
25
    {
26
        $this->setHtmlTitle('Logs');
27
28
        $filterUser = WebRequest::getString('filterUser');
29
        $filterAction = WebRequest::getString('filterAction');
30
        $filterObjectType = WebRequest::getString('filterObjectType');
31
        $filterObjectId = WebRequest::getInt('filterObjectId');
32
33
        $database = $this->getDatabase();
34
35
        if (!array_key_exists($filterObjectType, LogHelper::getObjectTypes())) {
36
            $filterObjectType = null;
37
        }
38
39
        $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) {
40
            return UserSearchHelper::get($database)->fetchColumn('username');
41
        });
42
43
        $limit = WebRequest::getInt('limit');
44
        if ($limit === null) {
45
            $limit = 100;
46
        }
47
48
        $page = WebRequest::getInt('page');
49
        if ($page === null) {
50
            $page = 1;
51
        }
52
53
        $offset = ($page - 1) * $limit;
54
55
        $logSearch = LogSearchHelper::get($database)->limit($limit, $offset);
56
        $this->setupSearchHelper($logSearch, $database, $filterUser, $filterAction, $filterObjectType, $filterObjectId);
57
58
        /** @var Log[] $logs */
59
        $logs = $logSearch->getRecordCount($count)->fetch();
60
61
        if ($count === 0) {
62
            $this->assign('logs', array());
63
            $this->setTemplate('logs/main.tpl');
64
65
            return;
66
        }
67
68
        list($users, $logData) = LogHelper::prepareLogsForTemplate($logs, $database, $this->getSiteConfiguration());
69
70
        $this->setupPageData($page, $limit, $count);
71
72
        $this->assign("logs", $logData);
73
        $this->assign("users", $users);
74
75
        $this->assign("filterUser", $filterUser);
76
        $this->assign("filterAction", $filterAction);
77
        $this->assign("filterObjectType", $filterObjectType);
78
        $this->assign("filterObjectId", $filterObjectId);
79
80
        $this->assign('allLogActions', LogHelper::getLogActions($this->getDatabase()));
81
        $this->assign('allObjectTypes', LogHelper::getObjectTypes());
82
83
        $this->setTemplate("logs/main.tpl");
84
    }
85
86
    /**
87
     * @param int $page
88
     * @param int $limit
89
     * @param int $count
90
     */
91
    protected function setupPageData($page, $limit, $count)
92
    {
93
        // The number of pages on the pager to show. Must be odd
94
        $pageLimit = 9;
95
96
        $pageData = array(
97
            // Can the user go to the previous page?
98
            'canprev'   => $page != 1,
99
            // Can the user go to the next page?
100
            'cannext'   => ($page * $limit) < $count,
101
            // Maximum page number
102
            'maxpage'   => ceil($count / $limit),
103
            // Limit to the number of pages to display
104
            'pagelimit' => $pageLimit,
105
        );
106
107
        // number of pages either side of the current to show
108
        $pageMargin = (($pageLimit - 1) / 2);
109
110
        // Calculate the number of pages either side to show - this is for situations like:
111
        //  [1]  [2] [[3]] [4]  [5]  [6]  [7]  [8]  [9] - where you can't just use the page margin calculated
112
        $pageData['lowpage'] = max(1, $page - $pageMargin);
113
        $pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin);
114
        $pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1;
115
116
        if ($pageCount < $pageLimit) {
117
            if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) {
118
                $pageData['hipage'] = min($pageLimit, $pageData['maxpage']);
119
            }
120
            elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) {
121
                $pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1);
122
            }
123
        }
124
125
        // Put the range of pages into the page data
126
        $pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']);
127
128
        $this->assign("pagedata", $pageData);
129
130
        $this->assign("limit", $limit);
131
        $this->assign("page", $page);
132
    }
133
134
    /**
135
     * @param $logSearch
136
     * @param $database
137
     * @param $filterUser
138
     * @param $filterAction
139
     * @param $filterObjectType
140
     * @param $filterObjectId
141
     */
142
    private function setupSearchHelper(
143
        $logSearch,
144
        $database,
145
        $filterUser,
146
        $filterAction,
147
        $filterObjectType,
148
        $filterObjectId
149
    ) {
150
        if ($filterUser !== null) {
151
            $logSearch->byUser(User::getByUsername($filterUser, $database)->getId());
152
        }
153
154
        if ($filterAction !== null) {
155
            $logSearch->byAction($filterAction);
156
        }
157
158
        if ($filterObjectType !== null) {
159
            $logSearch->byObjectType($filterObjectType);
160
        }
161
162
        if ($filterObjectId !== null) {
163
            $logSearch->byObjectId($filterObjectId);
164
        }
165
    }
166
}
167