-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtOutput.php
317 lines (285 loc) · 10.1 KB
/
rtOutput.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
<?php
class rtOutput
{
private function outputSummaryTable()
{
?>
<div class="panel panel-default" id="summary">
<div class="panel-heading">
<h3 class="panel-title">Summary</h3>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>URLs checked</th>
<th><?php print HTTP_STATUS_OK; ?></th>
<th><?php print HTTP_STATUS_MOVED_PERMANENTLY; ?></th>
<th><?php print HTTP_STATUS_NOT_FOUND; ?></th>
<?php if ($this->processor->successCount) : ?>
<th>Successes</th>
<?php endif; ?>
<?php if ($this->processor->failureCount): ?>
<th>Errors</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<tr>
<td><?php print $this->processor->resultsCount; ?></td>
<td><?php print $this->processor->count200; ?></td>
<td><?php print $this->processor->count301; ?></td>
<td><?php print $this->processor->count404; ?></td>
<?php if ($this->processor->successCount) : ?>
<td>
<a href="#success">
<?php print $this->processor->successCount; ?>
</a>
</td>
<?php endif; ?>
<?php if ($this->processor->failureCount): ?>
<td>
<a href="#failure">
<?php print $this->processor->failureCount; ?>
</a>
</td>
<?php endif; ?>
</tr>
</tbody>
</table>
</div>
<?php
}
private function outputFindAndReplace()
{
?>
<div class="panel panel-default" id="find-replace">
<div class="panel-heading">
<h3 class="panel-title">Find and replace</h3>
</div>
<table class="table table-striped table-bordered">
<thead>
<th>Find</th>
<th>Replace</th>
</thead>
<tbody>
<tr>
<td><?php print $this->processor->find; ?></td>
<td><?php print $this->processor->replace; ?></td>
</tr>
</tbody>
</table>
</div>
<?php
}
private function outputButtons()
{
?>
<p>
<a href="index.php" class="btn btn-success">Start again</a>
</p>
<form method="post">
<input type="hidden" name="csv_output" value="true"/>
<input type="hidden" name="results"
value="<?php print htmlentities(serialize($this->processor->results)); ?>"/>
<input type="submit" class="btn" value="Output as CSV"/>
</form>
<?php print $this->navigationMarkup; ?>
<?php
}
private function outputDuplicateOriginals()
{
?>
<div class="alert alert-warning" role="alert" id="error-duplicates">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
The input contains the following duplicate original URLs:
<ul>
<?php foreach ($this->duplicateOriginals as $original => $count): ?>
<li>
<?php print $original . ' : ' . $count . ' instances'; ?>
<ul>
<?php
$instances = array_keys($this->processor->getOriginals(), $original);
foreach ($instances as $instance) {
print '<li>Row ' . $this->processor->results[$instance]['row'] . '</li>';
}
?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php print $this->navigationMarkup; ?>
<?php
}
private function outputInvalidOriginals()
{
?>
<div class="alert alert-danger" role="alert" id="error-invalid-original">
<span class="glyphicon glyphicon-exclamation-sign"
aria-hidden="true"></span>
<span class="sr-only">Error:</span>
The input contains the following invalid original URLs:
<ul>
<?php foreach ($this->invalidOriginals as $url): ?>
<?php
$instances = array_keys($this->invalidOriginals, $url);
foreach ($instances as $instance): ?>
<li>
Row <?php print $this->processor->results[$instance]['row']; ?>: <?php print $url; ?>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</div>
<?php print $this->navigationMarkup; ?>
<?php
}
private function outputInvalidExpected()
{
?>
<div class="alert alert-danger" role="alert" id="error-invalid-expected">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
The input contains the following invalid expected URLs:
<ul>
<?php foreach ($this->invalidExpecteds as $url): ?>
<?php
$instances = array_keys($this->invalidExpecteds, $url);
foreach ($instances as $instance): ?>
<li>
Row <?php print $this->processor->results[$instance]['row']; ?>: <?php print $url; ?>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</div>
<?php print $this->navigationMarkup; ?>
<?php
}
private function outputFailures()
{
?>
<div class="panel panel-default" id="errors">
<div class="panel-heading">
<h3 class="panel-title">Errors</h3>
</div>
<table id="failure" class="table table-striped table-bordered">
<thead>
<tr>
<th>Original URL</th>
<th>Expected URL</th>
<th>HTTP response</th>
<th>Actual URL</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->processor->getFailures() as $failure): ?>
<tr>
<td><?php print $failure['original']; ?></td>
<td><?php print $failure['expected']; ?></td>
<td><?php print $failure['http_code']; ?></td>
<td><?php print $failure['actual']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php print $this->navigationMarkup; ?>
<?php
}
private function outputSuccesses()
{
?>
<div class="panel panel-default" id="successes">
<div class="panel-heading">
<h3 class="panel-title">Successes</h3>
</div>
<table id="success" class="table table-striped table-bordered">
<thead>
<tr>
<th>Original URL</th>
<th>Expected URL</th>
<th>HTTP response</th>
<th>Actual URL</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->processor->getSuccesses() as $success): ?>
<tr>
<td><?php print $success['original']; ?></td>
<td><?php print $success['expected']; ?></td>
<td><?php print $success['http_code']; ?></td>
<td><?php print $success['actual']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php print $this->navigationMarkup; ?>
<?php
}
private $processor = null;
private $duplicateOriginals = null;
private $invalidOriginal = null;
private $invalidExpecteds = null;
public function __construct($processor)
{
$this->processor = $processor;
$this->duplicateOriginals = array_filter(array_count_values($processor->getOriginals()), 'more_than_1'); //@todo: replace more_than_1 with inline function.
$this->invalidOriginals = array_filter($processor->getOriginals(), 'invalid_url');
$this->invalidExpecteds = array_filter($processor->getExpecteds(), 'invalid_url');
$this->navigationMarkup = $this->generateNavigation();
}
private function generateNavigation()
{
$back_to_top_link = '<a href="#">Back to top</a>';
$navigation = array($back_to_top_link);
if (!empty($this->duplicateOriginals)) {
$navigation[] = '<a href="#error-duplicates">Duplicate originals</a>';
}
if (!empty($this->invalidOriginals)) {
$navigation[] = '<a href="#error-invalid-original">Invalid original URLs</a>';
}
if (!empty($this->invalidExpecteds)) {
$navigation[] = '<a href="#error-invalid-expected">Invalid expected URLs</a>';
}
if ($this->processor->failureCount) {
$navigation[] = '<a href="#errors">Errors</a>';
}
if ($this->processor->successCount) {
$navigation[] = '<a href="#successes">Successes</a>';
}
$navigation_markup = '<ul>';
foreach ($navigation as $link) {
$navigation_markup .= '<li>' . $link . '</li>';
}
$navigation_markup .= '</ul>';
return $navigation_markup;
}
public function generate()
{
if ($this->processor->resultsCount) {
$this->outputSummaryTable();
if ($this->processor->find) {
$this->outputFindAndReplace();
}
}
$this->outputButtons();
if (!empty($this->duplicateOriginals)) {
$this->outputDuplicateOriginals();
}
if (!empty($this->invalidOriginals)) {
$this->outputInvalidOriginals();
}
if (!empty($this->invalidExpecteds)) {
$this->outputInvalidExpected();
}
if ($this->processor->failureCount) {
$this->outputFailures();
}
if ($this->processor->successCount) {
$this->outputSuccesses();
}
}
}