-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeed_up.php
202 lines (169 loc) · 7.5 KB
/
speed_up.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
<?php
class speed_up
{
private $urls = [];
public function __construct($profile = 'auto')
{
$urls = [];
$article_current = rex_article::getCurrent();
$article_current_prio = $article_current->getPriority();
$category_current = $article_current->getCategory();
if ($category_current) {
$category_children = $category_current->getChildren(true);
$category_parent = $category_current->getParent();
$category_articles = $category_current->getArticles(true);
$category_current_prio = $category_current->getPriority();
} else {
$category_children = rex_category::getRootCategories(true);
$category_parent = [];
$category_articles = rex_article::getRootArticles(true);
$category_current_prio = 0;
}
$mount_id = rex_yrewrite::getCurrentDomain()->getMountId();
$category_mount_children = [];
if (rex_category::get($mount_id)) {
$category_mount_children = rex_category::get($mount_id)->getChildren(true);
}
$start_id = rex_yrewrite::getCurrentDomain()->getStartId();
$current_id = $article_current->getId();
$category_neighbours = [];
if (null != $category_parent && $category_current->getId() != $mount_id) {
// Nur wenn wir uns nicht in Root befinden oder überhalb eines Mount-Points - andere YRewrite-Domains möchten wir nicht prefetchen.
$category_neighbours = $category_parent->getChildren(true);
}
// Manuelle Einstellungen
$article_prefetch_config = explode(',', self::getConfig('prefetch_articles') ?? '');
if ('auto' === self::getConfig('profile')) {
// Mount-Point = oberste Navigationsebene (Startseite könnte auch in einer Unterkategorie sein)
foreach ($category_mount_children as $category) {
$urls[$category->getId()] = $category->getUrl();
}
// Nur das erste Kind-Element
foreach ($category_children as $category) {
$urls[$category->getId()] = $category->getUrl();
continue;
}
$neighbours = 0;
// Nur die Nachbar-Kategorien
foreach ($category_neighbours as $category) {
if ($category->getPriority() != $category_current_prio - 1 && $category->getPriority() != $category_current_prio + 1) {
continue;
}
$urls[$category->getId()] = $category->getUrl();
// Nach 2 gefundenen Nachbarn aussteigen
if (2 == ++$neighbours) {
break;
}
}
$neighbours = 0;
// Nur die Nachbar-Artikel
foreach ($category_articles as $article) {
if ($article->getPriority() != $article_current_prio - 1 && $article->getPriority() != $article_current_prio + 1) {
continue;
}
$urls[$article->getId()] = $article->getUrl();
if (2 == ++$neighbours) {
break;
}
}
if ($category_current && $category_current->getId() != $start_id) {
// Startseite hinzufügen
$urls[$start_id] = rex_article::get($start_id)->getUrl();
}
} elseif ('aggressive' === self::getConfig('profile')) {
// Mount-Point = oberste Navigationsebene (Startseite könnte auch in einer Unterkategorie sein)
foreach ($category_mount_children as $category) {
$urls[$category->getId()] = $category->getUrl();
}
foreach ($category_children as $category) {
$urls[$category->getId()] = $category->getUrl();
}
foreach ($category_articles as $article) {
$urls[$article->getId()] = $article->getUrl();
}
foreach ($category_neighbours as $category) {
$urls[$category->getId()] = $category->getUrl();
}
if ($category_current && $category_current->getId() != $start_id) {
// Startseite hinzufügen
$urls[$start_id] = rex_article::get($start_id)->getUrl();
}
}
foreach ($article_prefetch_config as $article_id) {
$article = rex_article::get($article_id);
if ($article) {
$urls[$article_id] = $article->getUrl();
}
}
unset($urls[$current_id]);
$urls = rex_extension::registerPoint(new rex_extension_point(
'PREFETCH_URLS',
$urls,
));
/*
if(rex_addon::get('url')->isAvailable()) {
// gut zu wissen, ob URL installiert ist - damit könnte man noch etwas anfangen.
$is_url_addon_url = Url\Url::resolveCurrent();
}
*/
if (rex_addon::get('ycom')->isAvailable()) {
// YCom-spezifische Seiten wie Logout sollten keinesfalls eigenständig aufgerufen werden. */
unset($urls[rex_config::get('ycom/auth', 'article_id_jump_denied')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_jump_logout')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_jump_not_ok')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_jump_ok')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_jump_password')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_jump_termsofuse')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_login')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_logout')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_register')]);
unset($urls[rex_config::get('ycom/auth', 'article_id_password')]);
unset($urls[rex_config::get('ycom', 'otp_article_id')]);
}
unset($urls[rex_yrewrite::getCurrentDomain()->getNotfoundId()]);
$this->urls = $urls;
return $this;
}
public function showOutput()
{
echo $this->getOutput();
}
public function show() /* Alias, BC */
{
return $this->showOutput();
}
public function getOutput()
{
if ('disabled' === self::getConfig('profile')) {
return;
}
$output = PHP_EOL;
$output .= self::getConfig('preload') . PHP_EOL;
$output .= self::getConfig('prefetch') . PHP_EOL;
$preload_media_config = explode(',', self::getConfig('preload_media') ?? '');
foreach ($preload_media_config as $file) {
if ($media = rex_media::get($file)) {
$output .= '<link rel="preload" href="' . $media->getUrl() . '" as="image" type="' . $media->getType() . '">' . PHP_EOL;
}
}
if ('custom' === self::getConfig('profile')) {
return;
}
foreach ($this->urls as $url) {
$parsedUrl = parse_url($url);
if (isset($parsedUrl['scheme']) && !in_array($parsedUrl['scheme'], ['http', 'https'])) {
continue;
}
$output .= '<link rel="prefetch" href="' . $url . '">' . PHP_EOL;
}
return $output;
}
public static function install()
{
rex_metainfo_add_field('translate:art_speed_up_label', 'art_speed_up', '100', '', 5, '|1|', '1:translate:art_speed_up_label');
}
public static function getConfig($key)
{
return rex_config::get('speed_up', $key);
}
}