-
Notifications
You must be signed in to change notification settings - Fork 18
/
report.php
361 lines (319 loc) · 14 KB
/
report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Admin report GUI
*
* @package tool_crawler
* @copyright 2016 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once('locallib.php');
require_login(null, false);
$report = optional_param('report', '', PARAM_ALPHANUMEXT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 50, PARAM_INT);
$courseid = optional_param('course', 0, PARAM_INT);
$retryid = optional_param('retryid', 0, PARAM_INT);
$start = $page * $perpage;
$sqlfilter = '';
$navurl = new moodle_url('/admin/tool/crawler/report.php', array(
'report' => $report,
'course' => $courseid
));
$baseurl = new moodle_url('/admin/tool/crawler/report.php', array(
'perpage' => $perpage,
'report' => $report,
'course' => $courseid
));
if ($courseid) {
// If course then this is an a course editor report.
$course = get_course($courseid);
require_login($course);
$coursecontext = context_course::instance($courseid);
require_capability('moodle/course:update', $coursecontext);
$coursename = format_string($course->fullname, true, array('context' => $coursecontext));
$PAGE->set_context($coursecontext);
$PAGE->set_url($navurl);
$PAGE->set_title( get_string($report, 'tool_crawler') );
$PAGE->set_heading($coursename);
$PAGE->set_pagelayout('incourse');
$PAGE->add_body_class('limitedwidth');
$sqlfilter = ' AND c.id = '.$courseid;
} else {
// If no course then this is an admin only report.
require_capability('moodle/site:config', context_system::instance());
admin_externalpage_setup('tool_crawler_'.$report);
}
echo $OUTPUT->header();
require('tabs.php');
echo $tabs;
if ($retryid) {
$persistent = new \tool_crawler\local\url();
$persistent->reset_for_recrawl($retryid);
}
$datetimeformat = get_string('strftimerecentsecondshtml', 'tool_crawler');
if ($report == 'broken') {
$sql = " FROM {tool_crawler_url} b
LEFT JOIN {tool_crawler_edge} l ON l.b = b.id
LEFT JOIN {tool_crawler_url} a ON l.a = a.id
LEFT JOIN {course} c ON c.id = a.courseid
WHERE b.httpcode != ? $sqlfilter";
$opts = array('200');
$data = $DB->get_records_sql("SELECT concat(b.id, '-', l.id, '-', a.id) AS id,
b.url target,
b.httpcode,
b.httpmsg,
b.errormsg,
b.lastcrawled,
b.priority,
b.id AS toid,
l.id linkid,
l.text,
a.url,
a.title,
a.redirect,
a.courseid,
c.shortname $sql
ORDER BY httpcode DESC,
c.shortname ASC",
$opts,
$start,
$perpage);
$count = $DB->get_field_sql ("SELECT count(*) AS count" . $sql, $opts);
$table = new html_table();
$table->head = array(
'',
get_string('lastcrawledtime', 'tool_crawler'),
get_string('priority', 'tool_crawler'),
get_string('response', 'tool_crawler'),
get_string('broken', 'tool_crawler'),
get_string('frompage', 'tool_crawler')
);
if (!$courseid) {
array_push($table->head, get_string('course', 'tool_crawler'));
}
$table->data = array();
foreach ($data as $row) {
$text = trim($row->text);
if ($text == "") {
$text = get_string('missing', 'tool_crawler');
$text = htmlspecialchars($text, ENT_NOQUOTES | ENT_HTML401);
// May add a bit of markup here so that the user can differentiate the "missing" message from an equal link text.
} else {
$text = htmlspecialchars($text, ENT_NOQUOTES | ENT_HTML401);
}
$data = array(
html_writer::link(new moodle_url($baseurl, array('retryid' => $row->toid )),
get_string('retry', 'tool_crawler')),
userdate($row->lastcrawled, $datetimeformat),
tool_crawler_priority_level($row->priority),
tool_crawler_http_code($row),
tool_crawler_link($row->target, $text, $row->redirect, true),
tool_crawler_link($row->url, $row->title, $row->redirect)
);
if (!$courseid) {
$escapedshortname = htmlspecialchars($row->shortname, ENT_NOQUOTES | ENT_HTML401);
array_push($data, html_writer::link('/course/view.php?id='.$row->courseid, $escapedshortname) );
}
$table->data[] = $data;
}
} else if ($report == 'queued') {
$sql = " FROM {tool_crawler_url} a
LEFT JOIN {course} c ON c.id = a.courseid
WHERE (a.lastcrawled IS NULL OR a.lastcrawled < needscrawl)
$sqlfilter";
$opts = array();
$data = $DB->get_records_sql("SELECT a.id,
a.url target,
a.title,
a.redirect,
a.lastcrawled,
a.needscrawl,
a.courseid,
a.priority,
c.shortname $sql
ORDER BY a.priority DESC,
a.needscrawl ASC,
a.id ASC",
$opts,
$start,
$perpage);
$count = $DB->get_field_sql ("SELECT count(*) AS count" . $sql, $opts);
$table = new html_table();
$table->head = array(
get_string('whenqueued', 'tool_crawler'),
get_string('url', 'tool_crawler'),
get_string('priority', 'tool_crawler'),
);
if (!$courseid) {
array_push($table->head, get_string('incourse', 'tool_crawler'));
}
$table->data = array();
foreach ($data as $row) {
$title = trim($row->title);
if ($title == "") {
$title = get_string('notyetknown', 'tool_crawler');
}
$data = array(
userdate($row->needscrawl, $datetimeformat),
tool_crawler_link($row->target, $title, $row->redirect),
tool_crawler_priority_level($row->priority),
);
if (!$courseid) {
$escapedshortname = htmlspecialchars($row->shortname, ENT_NOQUOTES | ENT_HTML401);
array_push($data, html_writer::link('/course/view.php?id='.$row->courseid, $escapedshortname) );
}
$table->data[] = $data;
}
} else if ($report == 'recent') {
$sql = " FROM {tool_crawler_url} b
LEFT JOIN {course} c ON c.id = b.courseid
WHERE b.lastcrawled IS NOT NULL
$sqlfilter";
$opts = array();
$data = $DB->get_records_sql("SELECT b.id,
b.url target,
b.lastcrawled,
b.filesize,
b.filesizestatus,
b.httpcode,
b.httpmsg,
b.errormsg,
b.title,
b.redirect,
b.mimetype,
b.courseid,
b.priority,
c.shortname
$sql
ORDER BY b.lastcrawled DESC",
$opts,
$start,
$perpage);
$count = $DB->get_field_sql ("SELECT count(*) AS count" . $sql, $opts);
$table = new html_table();
$table->head = array(
get_string('lastcrawledtime', 'tool_crawler'),
get_string('response', 'tool_crawler'),
get_string('size', 'tool_crawler'),
get_string('url', 'tool_crawler'),
get_string('priority', 'tool_crawler'),
get_string('mimetype', 'tool_crawler'),
);
if (!$courseid) {
array_push($table->head, get_string('incourse', 'tool_crawler'));
}
$table->data = array();
foreach ($data as $row) {
$title = trim($row->title);
if ($title == "") {
$title = get_string('unknown', 'tool_crawler');
}
$code = tool_crawler_http_code($row);
$data = array(
userdate($row->lastcrawled, $datetimeformat),
$code,
htmlspecialchars(tool_crawler_displaysize($row), ENT_NOQUOTES | ENT_HTML401),
tool_crawler_link($row->target, $title, $row->redirect),
tool_crawler_priority_level($row->priority),
htmlspecialchars($row->mimetype, ENT_NOQUOTES | ENT_HTML401),
);
if (!$courseid) {
$escapedshortname = htmlspecialchars($row->shortname, ENT_NOQUOTES | ENT_HTML401);
array_push($data, html_writer::link('/course/view.php?id='.$row->courseid, $escapedshortname) );
}
$table->data[] = $data;
}
} else if ($report == 'oversize') {
$oversizesqlfilter = tool_crawler_sql_oversize_filter('b');
$sql = " FROM {tool_crawler_url} b
LEFT JOIN {tool_crawler_edge} l ON l.b = b.id
LEFT JOIN {tool_crawler_url} a ON l.a = a.id
LEFT JOIN {course} c ON c.id = a.courseid
WHERE {$oversizesqlfilter['wherecondition']}
$sqlfilter";
$opts = $oversizesqlfilter['params'];
$data = $DB->get_records_sql("SELECT concat(b.id, '-', a.id, '-', l.id) id,
b.url target,
b.filesize,
b.filesizestatus,
b.lastcrawled,
b.mimetype,
b.priority,
l.text,
a.title,
a.url,
a.redirect,
a.courseid,
c.shortname
$sql
ORDER BY b.filesize DESC,
b.filesizestatus DESC,
l.text,
a.id",
$opts,
$start,
$perpage);
$count = $DB->get_field_sql ("SELECT count(*) AS count" . $sql, $opts);
$table = new html_table();
$table->head = array(
get_string('lastcrawledtime', 'tool_crawler'),
get_string('size', 'tool_crawler'),
get_string('slowurl', 'tool_crawler'),
get_string('priority', 'tool_crawler'),
get_string('mimetype', 'tool_crawler'),
get_string('frompage', 'tool_crawler'),
);
if (!$courseid) {
array_push($table->head, get_string('course', 'tool_crawler'));
}
$table->data = array();
foreach ($data as $row) {
$text = trim($row->text);
if ($text == "") {
$text = get_string('missing', 'tool_crawler');
$text = htmlspecialchars($text, ENT_NOQUOTES | ENT_HTML401);
// May add a bit of markup here so that the user can differentiate the "missing" message from an equal link text.
} else {
$text = htmlspecialchars($text, ENT_NOQUOTES | ENT_HTML401);
}
$data = array(
userdate($row->lastcrawled, $datetimeformat),
htmlspecialchars(tool_crawler_displaysize($row), ENT_NOQUOTES | ENT_HTML401),
tool_crawler_link($row->target, $text, $row->redirect, true),
tool_crawler_priority_level($row->priority),
htmlspecialchars($row->mimetype, ENT_NOQUOTES | ENT_HTML401),
tool_crawler_link($row->url, $row->title, $row->redirect)
);
if (!$courseid) {
$escapedshortname = htmlspecialchars($row->shortname, ENT_NOQUOTES | ENT_HTML401);
array_push($data, html_writer::link('/course/view.php?id='.$row->courseid, $escapedshortname) );
}
$table->data[] = $data;
}
}
echo $OUTPUT->heading(get_string('numberurlsfound', 'tool_crawler',
array(
'reports_number' => $count,
'report_type' => $report
)
));
echo get_string($report . '_header', 'tool_crawler');
echo html_writer::table($table);
echo $OUTPUT->paging_bar($count, $page, $perpage, $baseurl);
echo $OUTPUT->footer();