Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
624 lines (526 loc) · 25.7 KB
/
Copy pathindex.php
File metadata and controls
624 lines (526 loc) · 25.7 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
@include_once __DIR__ . '/vendor/autoload.php';
use E9li\Fire\Commands;
use E9li\Fire\Jobs;
use E9li\Fire\Pages;
use E9li\Fire\PagesCache;
use E9li\Fire\Progress;
use E9li\Fire\Renderer;
use E9li\Fire\RunState;
use E9li\Fire\Urls;
use E9li\Fire\Warmer;
use Kirby\Cms\App;
load([
'e9li\\fire\\commands' => __DIR__ . '/src/Commands.php',
'e9li\\fire\\jobs' => __DIR__ . '/src/Jobs.php',
'e9li\\fire\\pages' => __DIR__ . '/src/Pages.php',
'e9li\\fire\\pagescache' => __DIR__ . '/src/PagesCache.php',
'e9li\\fire\\progress' => __DIR__ . '/src/Progress.php',
'e9li\\fire\\renderer' => __DIR__ . '/src/Renderer.php',
'e9li\\fire\\runstate' => __DIR__ . '/src/RunState.php',
'e9li\\fire\\urls' => __DIR__ . '/src/Urls.php',
'e9li\\fire\\warmer' => __DIR__ . '/src/Warmer.php',
]);
App::plugin('e9li/kirby-fire', [
'options' => [
'domain' => '',
'ignore' => [
'page' => [],
'template' => [],
'language' => [],
],
// TLS verification is on by default; set to true for local dev
// certificates (or pass --insecure to fire:up)
'insecure' => false,
'timeout' => 60,
'concurrency' => 5,
// CLI memory limit for the commands (image decoding is memory
// hungry); never lowers the environment's limit, false disables
'memory' => '512M',
],
'translations' => [
'en' => require __DIR__ . '/translations/en.php',
'de' => require __DIR__ . '/translations/de.php',
'de-ch' => require __DIR__ . '/translations/de-ch.php',
'fr' => require __DIR__ . '/translations/fr.php',
'it' => require __DIR__ . '/translations/it.php',
'ru' => require __DIR__ . '/translations/ru.php',
'sr' => require __DIR__ . '/translations/sr.php',
],
'commands' => [
'fire:up' => [
'description' => 'generate page cache',
'args' => [
'domain' => [
'description' => 'Domain to fire up the cache (defaults to the site URL)',
],
'no-media' => [
'description' => 'Skip fetching thumbs over HTTP — pair with fire:thumbs',
// domain is positional, so this has to be a flag or it would
// swallow the domain argument
'longPrefix' => 'no-media',
'noValue' => true,
],
'concurrency' => [
'description' => 'Requests in flight at once (default 5)',
'longPrefix' => 'concurrency',
'castTo' => 'int',
],
'insecure' => [
'description' => 'Skip TLS certificate verification (local dev certificates)',
'longPrefix' => 'insecure',
'noValue' => true,
],
'fresh' => [
'description' => 'Flush the pages cache before warming',
'longPrefix' => 'fresh',
'noValue' => true,
],
],
'command' => function ($cli): void {
$cli->br();
$cli->bold('Fire up the cache...');
Commands::raiseMemory();
Commands::cacheWarning($cli);
$skipMedia = $cli->arg('no-media') === true;
// without the url option Kirby's CLI base is "/" — page URLs
// are root-relative then and a domain is the only way to
// build requestable URLs
$hasBase = rtrim(kirby()->url(), '/') !== '';
// explicit CLI argument wins over the config option; prompt
// only as a last resort and only on a terminal, so cron jobs
// fall through to the site URL instead of hanging
$domain = $cli->arg('domain')
?: kirby()->option('e9li.kirby-fire.domain')
?: (stream_isatty(STDIN)
? $cli->prompt(
$hasBase
? 'Enter the domain to fire up (leave empty for ' . kirby()->url() . '):'
: 'Enter the domain to fire up (required — the url option is not set):',
false
)
: '');
if (empty($domain) === false && filter_var($domain, FILTER_VALIDATE_URL) === false) {
$cli->br();
$cli->error(' Invalid domain! Use a full URL like https://domain.com ');
$cli->br();
exit(1);
}
if (empty($domain) === true && $hasBase === false) {
$cli->br();
$cli->error(' No usable site URL — the url option is not set. ');
$cli->out('Pass a domain ("kirby fire:up https://example.com") or set the url');
$cli->out('option in site/config/config.php (fire:render needs it as well).');
$cli->br();
exit(1);
}
if ($cli->arg('fresh') === true) {
Commands::freshFlush($cli);
}
$concurrency = max(1, (int)($cli->arg('concurrency')
?: kirby()->option('e9li.kirby-fire.concurrency', 5)));
$client = Warmer::client($cli->arg('insecure') === true ? true : null);
// incremental by default: pages that are provably cached on
// disk are skipped, so a run after adding 20 pages warms
// exactly those 20 — except when a domain override points at
// another host, whose cache state the local disk cannot know
$incremental = empty($domain) === true
|| rtrim($domain, '/') === rtrim(kirby()->url(), '/');
// resolve every target URL first — the crawl runs with
// $concurrency requests in flight, so results arrive in
// completion order and need their per-URL context up front
$targets = [];
$skipped = 0;
$alreadyCached = 0;
foreach (Pages::urls() as $item) {
if ($incremental === true && $item['cached'] === true) {
$alreadyCached++;
continue;
}
$url = $item['url'];
if (empty($domain) === false) {
if (($url = Urls::rewrite($url, $domain)) === null) {
// e.g. a language living on its own domain — the
// URL cannot be mapped onto the target domain
$skipped++;
continue;
}
}
$targets[$url] = [
'isErrorPage' => $item['isErrorPage'],
'id' => $item['id'],
'language' => $item['language'],
'key' => RunState::key($item['id'], $item['language']),
];
}
if ($alreadyCached > 0) {
$cli->out($alreadyCached . ' page(s) already cached — skipped. Use --fresh for a full rebuild.');
}
$pagesOn = 0;
$mediaOn = 0;
$failed = 0;
$uncacheable = 0;
$mediaQueue = [];
$stateChanges = [];
$progress = new Progress(count($targets), $cli->arg('quiet') !== true);
Warmer::warmAll(
$client,
array_keys($targets),
$concurrency,
true,
function (string $url, array $result) use ($cli, $targets, $progress, $incremental, &$pagesOn, &$failed, &$uncacheable, &$mediaQueue, &$stateChanges): void {
$meta = $targets[$url];
// the error page renders (and caches) with HTTP 404 by
// design — that is a warmed page, not a failure
$expected404 = $meta['isErrorPage'] === true && $result['status'] === 404;
if ($result['status'] === 0 || ($result['status'] >= 400 && $expected404 === false)) {
$failed++;
$error = $result['error'] ?? 'HTTP ' . $result['status'];
$stateChanges[$meta['key']] = ['state' => 'extinguished', 'error' => $error, 'time' => time()];
$progress->error($cli, $url . ' → ' . $error);
} else {
$pagesOn++;
// a 200 whose response says no-store: served, but
// Kirby refused to cache it (session/cookies)
$noStore = $result['cacheable'] === false;
// for local warms the disk is authoritative — it
// also catches pages that disable caching without
// the no-store header (response()->cache(false))
if ($noStore === false && $incremental === true) {
$page = kirby()->page($meta['id']);
$noStore = $page !== null
&& PagesCache::has($page, $meta['language']) === false;
}
$uncacheable += $noStore ? 1 : 0;
$stateChanges[$meta['key']] = $noStore
? ['state' => 'no-store', 'error' => null, 'time' => time()]
: null;
$progress->advance(
'fire up ' . $url
. ($expected404 ? ' (error page, 404 expected)' : '')
. ($noStore ? ' (not cacheable)' : '')
);
foreach ($result['media'] as $mediaUrl) {
$mediaQueue[$mediaUrl] = true;
}
}
}
);
$progress->finish();
// persist the outcomes, so the Panel presents the same state
RunState::update($stateChanges);
if ($skipMedia === false && $mediaQueue !== []) {
// status-only requests: the thumb is generated server-side
// before the first body byte, so no image is downloaded
$mediaProgress = new Progress(count($mediaQueue), $cli->arg('quiet') !== true);
Warmer::warmAll(
$client,
array_keys($mediaQueue),
$concurrency,
false,
function (string $url, array $result) use ($cli, $mediaProgress, &$mediaOn, &$failed): void {
if ($result['status'] === 0 || $result['status'] >= 400) {
$failed++;
$mediaProgress->error($cli, 'media: ' . $url . ' → ' . ($result['error'] ?? 'HTTP ' . $result['status']));
} else {
$mediaOn++;
$mediaProgress->advance('media ' . $url);
}
}
);
$mediaProgress->finish();
}
$cli->br();
if ($skipped > 0) {
$cli->out($skipped . ' URL(s) skipped — they do not live below ' . kirby()->url() . ' and cannot be rewritten to ' . $domain . '.');
}
if ($failed > 0) {
$cli->error(' ' . $failed . ' URL(s) failed — see above. ');
}
if ($uncacheable > 0) {
$cli->error(' ' . $uncacheable . ' page(s) answered with no-store — Kirby did not cache them! ');
$cli->out('Their responses start a session or set cookies. Typical causes: csrf()');
$cli->out('or $kirby->session() in an always-rendered snippet (header, footer, popup).');
}
$cli->success(' Cache is on (' . ($pagesOn - $uncacheable + $alreadyCached) . ' pages cached, ' . $mediaOn . ' media files), site is ready! ');
if ($skipMedia === true) {
$cli->out('Thumbs were skipped — run "kirby fire:thumbs" to generate them.');
}
$cli->br();
if ($failed > 0) {
// cron and CI monitors need the failure to be visible
exit(1);
}
},
],
'fire:render' => [
'description' => 'render pages in-process into the cache — no HTTP',
'args' => [
'fresh' => [
'description' => 'Flush the pages cache before rendering',
'longPrefix' => 'fresh',
'noValue' => true,
],
],
'command' => function ($cli): void {
$cli->br();
$cli->bold('Render pages into the cache...');
Commands::raiseMemory();
Commands::cacheWarning($cli);
// rendered URLs come from the configured url, not from a
// request — without the option the cached HTML would link to
// whatever the CLI environment guesses
if (empty(kirby()->option('url')) === true) {
$cli->out('Note: the url option is not set — URLs in the cached HTML are based on ' . kirby()->url());
}
if ($cli->arg('fresh') === true) {
Commands::freshFlush($cli);
}
$rendered = 0;
$failed = 0;
$uncached = 0;
$alreadyCached = 0;
$cacheActive = (kirby()->cache('pages')->options()['active'] ?? false) === true;
$targets = Pages::targets();
// incremental by default: skip pages that are provably
// cached on disk — --fresh flushes first for a full rebuild
if ($cacheActive === true) {
$targets = array_values(array_filter($targets, function (array $target) use (&$alreadyCached): bool {
if (PagesCache::has($target['page'], $target['language']) === true) {
$alreadyCached++;
return false;
}
return true;
}));
}
if ($alreadyCached > 0) {
$cli->out($alreadyCached . ' page(s) already cached — skipped. Use --fresh for a full rebuild.');
}
$progress = new Progress(count($targets), $cli->arg('quiet') !== true);
$stateChanges = [];
Renderer::renderAll(function (array $result) use ($cli, $progress, $cacheActive, &$rendered, &$failed, &$uncached, &$stateChanges): void {
$key = RunState::key($result['id'], $result['language']);
if ($result['ok'] === false) {
$failed++;
$stateChanges[$key] = ['state' => 'extinguished', 'error' => $result['error'], 'time' => time()];
$progress->error($cli, $result['url'] . ' → ' . $result['error']);
} else {
$rendered++;
$noStore = $cacheActive === true && $result['cached'] === false;
$uncached += $noStore ? 1 : 0;
$stateChanges[$key] = $noStore
? ['state' => 'no-store', 'error' => null, 'time' => time()]
: null;
$progress->advance('render ' . $result['url'] . ($noStore ? ' (not cacheable)' : ''));
}
}, $targets);
$progress->finish();
// persist the outcomes, so the Panel presents the same state
RunState::update($stateChanges);
$cli->br();
if ($failed > 0) {
$cli->error(' ' . $failed . ' page(s) failed — see above. ');
}
if ($uncached > 0) {
$cli->error(' ' . $uncached . ' page(s) rendered but were not cached! ');
$cli->out('Their render starts a session or sets cookies. Typical causes: csrf()');
$cli->out('or $kirby->session() in an always-rendered snippet (header, footer, popup).');
}
$cli->success(' ' . ($rendered - $uncached) . ' page(s) rendered into the cache. ');
$cli->out('Run "kirby fire:thumbs" to generate the queued thumbs.');
$cli->br();
if ($failed > 0) {
exit(1);
}
},
],
'fire:thumbs' => [
'description' => 'generate pending thumbnails',
'args' => [
'limit' => [
'description' => 'Render at most this many thumbs, then stop',
'longPrefix' => 'limit',
'castTo' => 'int',
],
],
'command' => function ($cli): void {
$cli->br();
$cli->bold('Render pending thumbs...');
Commands::raiseMemory();
$jobs = Jobs::all();
if ($jobs === []) {
$cli->br();
$cli->success(' Nothing to render — every thumb is already on. ');
$cli->out('If images are missing, the pages have to be rendered first: flush the');
$cli->out('page cache, then run "kirby fire:render && kirby fire:thumbs".');
$cli->br();
return;
}
// batching for very constrained environments: finished jobs
// disappear from the queue, so repeated limited runs converge
$total = count($jobs);
$limit = (int)$cli->arg('limit');
if ($limit > 0 && $limit < $total) {
$jobs = array_slice($jobs, 0, $limit);
}
$rendered = 0;
$failed = 0;
$i = 0;
$progress = new Progress(count($jobs), $cli->arg('quiet') !== true);
foreach ($jobs as $job) {
$result = Jobs::thumb($job);
if ($result['ok'] === false) {
$failed++;
$progress->error($cli, $job['path'] . ' → ' . $result['error']);
} else {
$rendered++;
$progress->advance($job['filename']);
}
// page objects reference each other in cycles; collect
// them periodically so long runs stay flat in memory
if (++$i % 250 === 0) {
gc_collect_cycles();
}
}
$progress->finish();
$cli->br();
if ($failed > 0) {
$cli->error(' ' . $failed . ' thumb(s) failed — see above. ');
}
$cli->success(' ' . $rendered . ' thumb(s) rendered ');
if (($left = $total - count($jobs)) > 0) {
$cli->out($left . ' job(s) left — run "kirby fire:thumbs" again.');
}
$cli->br();
if ($failed > 0) {
exit(1);
}
},
],
],
'icons' => [
'fire' => '<path d="M12 23C16.1421 23 19.5 19.6421 19.5 15.5C19.5 14.6345 19.2697 13.8032 19 13.0296C17.3333 14.6765 16.0667 15.5 15.2 15.5C19.1954 8.5 17 5.5 11 1.5C11.5 6.49951 8.20403 8.77375 6.86179 10.0366C5.40786 11.4045 4.5 13.3462 4.5 15.5C4.5 19.6421 7.85786 23 12 23ZM12.7094 5.23498C15.9511 7.98528 15.9666 10.1223 13.463 14.5086C12.702 15.8419 13.6648 17.5 15.2 17.5C15.8884 17.5 16.5841 17.2992 17.3189 16.9051C16.6979 19.262 14.5519 21 12 21C8.96243 21 6.5 18.5376 6.5 15.5C6.5 13.9608 7.13279 12.5276 8.23225 11.4932C8.35826 11.3747 8.99749 10.8081 9.02477 10.7836C9.44862 10.4021 9.7978 10.0663 10.1429 9.69677C11.3733 8.37932 12.2571 6.91631 12.7094 5.23498Z"></path>',
],
'areas' => [
'fireView' => function (): array {
return [
'label' => t('e9li.kirby-fire.title', 'Fire'),
'icon' => 'fire',
'menu' => true,
'link' => 'fire',
'views' => [
[
'pattern' => 'fire',
'action' => function () {
return [
'component' => 'fireView',
'title' => t('e9li.kirby-fire.title', 'Fire'),
];
},
],
],
];
},
],
'api' => [
'routes' => [
[
'pattern' => 'fire/pages',
'method' => 'GET',
'action' => function (): array {
// rows carry the shared state: cached pages from the
// disk cache, problem pages (no-store, failed) with
// their reasons from the last CLI or Panel run
return RunState::apply(Pages::urls());
},
],
[
'pattern' => 'fire/state',
'method' => 'POST',
'action' => function (): array {
// the Panel crawls in the browser — it reports its
// outcomes here so the CLI and later Panel loads
// present the same state
$changes = $this->requestBody('results');
$clean = [];
foreach (is_array($changes) ? $changes : [] as $key => $entry) {
if ($entry === null) {
$clean[(string)$key] = null;
continue;
}
if (
is_array($entry) === true &&
in_array($entry['state'] ?? null, ['no-store', 'extinguished'], true)
) {
$clean[(string)$key] = [
'state' => $entry['state'],
'error' => isset($entry['error']) ? (string)$entry['error'] : null,
'time' => time(),
];
}
}
RunState::update($clean);
return ['ok' => true];
},
],
[
'pattern' => 'fire/up',
'method' => 'POST',
'action' => function (): array {
$url = (string)$this->requestBody('url');
$language = $this->requestBody('language');
// only same-site URLs may be fetched (SSRF guard)
if ($url === '' || Urls::isAllowed($url) === false) {
return [
'url' => $url,
'language' => $language,
'state' => 'extinguished',
'error' => 'URL not allowed',
];
}
$result = Warmer::warm(Warmer::client(), $url);
// the error page renders (and caches) with HTTP 404 by
// design — that is a warmed page, not a failure
$expected404 = $result['status'] === 404 && Pages::isErrorPageUrl($url);
if ($result['status'] === 0 || ($result['status'] >= 400 && $expected404 === false)) {
return [
'url' => $url,
'language' => $language,
'state' => 'extinguished',
'error' => $result['error'] ?? 'HTTP ' . $result['status'],
];
}
return [
'url' => $url,
'language' => $language,
'state' => $result['cacheable'] === false ? 'no-store' : 'fire-on',
'media' => $result['media'],
];
},
],
[
'pattern' => 'fire/status',
'method' => 'GET',
'action' => function (): array {
return PagesCache::status();
},
],
[
'pattern' => 'fire/clear',
'method' => 'POST',
'action' => function (): array {
try {
kirby()->cache('pages')->flush();
$flushed = ['pages'];
} catch (Throwable) {
// pages cache not available — nothing to flush
$flushed = [];
}
return [
'flushed' => $flushed,
];
},
],
],
],
]);