-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
128 lines (126 loc) · 5.56 KB
/
index.php
File metadata and controls
128 lines (126 loc) · 5.56 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
<?php
namespace {
// Disable this extension if `page` extension is disabled or removed ;)
if (!isset($state->x->page)) {
return;
}
// Initialize layout variable(s)
\lot('archive', new \Time);
}
namespace x\archive {
function route__archive($content, $path, $query, $hash) {
if (null !== $content) {
return $content;
}
\extract(\lot(), \EXTR_SKIP);
$route = \trim($state->x->archive->route ?? 'archive', '/');
if ($part = \x\page\part($path = \trim($path ?? "", '/'))) {
$path = \substr($path, 0, -\strlen('/' . $part));
}
$at = ($part ?? 0) - 1;
// For `/…/archive/:name/:part`
if ($at >= 0 && $path) {
if ($file = \exist(\LOT . \D . 'page' . \D . \rawurldecode($path) . '.{' . ($x = \x\page\x()) . '}', 1)) {
if ($name = $state->q('archive.name')) {
\lot('page', $page = new \Page($file));
$chunk = $state->x->archive->lot->chunk ?? $page->chunk ?? 5;
$sort = \array_replace([-1, 'time'], (array) ($page->sort ?? []), (array) ($state->x->archive->lot->sort ?? []));
if ($pages = $page->children($x, 0)) {
$pages = $pages->is(function ($v) use ($name) {
return 0 === \strpos(\strtr($v->time . '-', [
' ' => '-',
':' => '-'
]), $name . '-');
})->sort($sort);
} else {
$pages = new \Pages;
}
\lot('t')[] = $page->title;
\lot('t')[] = \i('Archive');
$a = \explode('-', $name);
if (isset($a[1])) {
\lot('t')[] = (new \Time($a[0] . '-' . $a[1] . '-01 00:00:00'))('%B %Y');
} else {
\lot('t')[] = $a[0];
}
$pager = \Pager::from($pages);
$pager->path = $path . '/' . $route . '/' . $name;
\lot('pager', $pager = $pager->chunk($chunk, $at));
\lot('pages', $pages = $pages->chunk($chunk, $at));
if (0 === ($count = \q($pages))) {
\lot('t')[] = \i('Error');
}
\State::set([
'has' => [
'next' => !!$pager->next,
'parent' => !!$page->parent,
'prev' => !!$pager->prev
],
'is' => ['error' => 0 === $count ? 404 : false],
'with' => ['pages' => $count > 0]
]);
return [
'lot' => [],
'status' => 0 === $count ? 404 : 200,
'y' => 'pages/archive/' . $name
];
}
}
}
}
function route__page($content, $path, $query, $hash) {
if (null !== $content) {
return $content;
}
\extract(\lot(), \EXTR_SKIP);
$route = \trim($state->x->archive->route ?? 'archive', '/');
if ($part = \x\page\part($path = \trim($path ?? "", '/'))) {
$path = \substr($path, 0, -\strlen('/' . $part));
$path = \dirname($path); // Remove the name (time) part
if ($route !== \basename($path)) {
return $content;
}
return \Hook::fire('route.archive', [$content, \dirname($path) . '/' . $part, $query, $hash]);
}
return $content;
}
if ($part = \x\page\part($path = \trim($link->path ?? "", '/'))) {
$path = \substr($path, 0, -\strlen('/' . $part));
}
$at = ($part ?? 0) - 1;
$route = \trim($state->x->archive->route ?? 'archive', '/');
// For `/…/archive/:name/:part`
if ($at >= 0 && $route === \basename(\dirname($path = \rawurldecode($path)))) {
$a = \explode('-', $name = \basename($path));
// Year
if (\count($a) < 7 && ($n = (int) $a[0]) > 1969) {
// Month
if (!isset($a[1]) || ($n = (int) $a[1]) > 0 && $n < 13) {
// Day
if (!isset($a[2]) || ($n = (int) $a[2]) > 0 && $n < 32) {
// Hour
if (!isset($a[3]) || ($n = (int) $a[3]) > 0 && $n < 25) {
// Minute
if (!isset($a[4]) || ($n = (int) $a[4]) > 0 && $n < 61) {
// Second
if (!isset($a[5]) || ($n = (int) $a[5]) > 0 && $n < 61) {
\lot('archive', new \Time(\substr_replace('1970-01-01-00-00-00', $name, 0, \strlen($name))));
\Hook::set('route.archive', __NAMESPACE__ . "\\route__archive", 100);
\Hook::set('route.page', __NAMESPACE__ . "\\route__page", 90);
\State::set([
'is' => ['archives' => true],
'q' => [
'archive' => [
'name' => $name,
'part' => $part
]
]
]);
}
}
}
}
}
}
}
}