-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-grav.php
More file actions
171 lines (151 loc) · 6.4 KB
/
migrate-grav.php
File metadata and controls
171 lines (151 loc) · 6.4 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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Plugin\MigrateGrav\Kickoff;
use RocketTheme\Toolbox\Event\Event;
use RuntimeException;
class MigrateGravPlugin extends Plugin
{
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
public function onPluginsInitialized(): void
{
if (!$this->isAdmin()) {
return;
}
$this->enable([
'onAdminMenu' => ['onAdminMenu', 0],
'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', 0],
'onAdminTaskExecute' => ['onAdminTaskExecute', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
}
public function onAdminMenu(): void
{
$this->grav['twig']->plugins_hooked_nav['Migrate to Grav 2.0'] = [
'route' => 'migrate-grav',
'icon' => 'fa-rocket',
];
}
public function onAdminTwigTemplatePaths(Event $event): void
{
$paths = $event['paths'];
$paths[] = __DIR__ . '/admin/templates';
$event['paths'] = $paths;
}
public function onAdminTaskExecute(Event $event): void
{
$task = $event['method'] ?? null;
$controller = $event['controller'] ?? null;
$authorized = !$controller
|| !method_exists($controller, 'isAuthorizedFunction')
|| $controller->isAuthorizedFunction('admin.super');
if ($task === 'taskMigrateGravInit') {
if (!$authorized) {
$this->grav['admin']->setMessage('Super admin required to start migration.', 'error');
return;
}
try {
$payload = $this->runKickoff('admin');
} catch (RuntimeException $e) {
$this->grav['admin']->setMessage('Migration kickoff failed: ' . $e->getMessage(), 'error');
return;
}
$this->grav->redirect($payload['wizard_url'], 302);
return;
}
if ($task === 'taskMigrateGravReset' || $task === 'taskMigrateGravRestart') {
$isRestart = $task === 'taskMigrateGravRestart';
$verb = $isRestart ? 'restart wizard' : 'reset migration';
if (!$authorized) {
$this->grav['admin']->setMessage("Super admin required to {$verb}.", 'error');
return;
}
$result = $this->runReset($isRestart ? 'restart' : 'full');
if ($result['errors']) {
$label = $isRestart ? 'Restart' : 'Reset';
$this->grav['admin']->setMessage("{$label} incomplete: " . implode('; ', $result['errors']), 'error');
} else {
if (!$result['removed']) {
$msg = $isRestart ? 'Nothing to restart — no migration is staged.' : 'Nothing to reset.';
} else {
$msg = ($isRestart ? 'Wizard restarted. ' : 'Migration reset. ')
. 'Removed: ' . implode(', ', $result['removed']);
}
$this->grav['admin']->setMessage($msg, 'info');
}
// Restart preserves .migrating, so send the user back into the
// wizard at the staged step. Full reset has nothing to resume —
// return to the migrate-grav admin page.
if ($isRestart && !$result['errors']) {
$state = $this->newKickoff()->readFlag();
if (!empty($state['wizard_url'])) {
$this->grav->redirect($state['wizard_url'], 302);
return;
}
}
// Pass the Route object directly — Grav::redirect() handles
// Route instances via toString(true), which already includes the
// install base and admin route (no doubling, no manual stitching).
$this->grav->redirect($this->grav['admin']->getAdminRoute('/migrate-grav'), 302);
return;
}
}
/**
* Shared kickoff entry point used by both admin and CLI.
*/
public function runKickoff(string $trigger, ?string $adminUser = null): array
{
return $this->newKickoff()->run([
'grav_version' => GRAV_VERSION,
'admin_user' => $adminUser,
'trigger' => $trigger,
]);
}
/**
* Shared reset entry point used by admin and CLI.
*
* @param string $mode 'full' nukes everything; 'restart' keeps the staged
* zip + flag and rewinds the wizard to step='staged'.
*/
public function runReset(string $mode = 'full'): array
{
return $this->newKickoff()->reset($mode);
}
/**
* Expose the current .migrating state to admin twig templates, so the
* migrate-grav page can switch between "Start" and "Continue/Reset" UI
* without round-tripping through an AJAX call.
*/
public function onTwigSiteVariables(): void
{
// Only attach the flag state on the migrate-grav admin page. Checking
// the request URI is more reliable than poking admin internals.
$path = (string) $this->grav['uri']->path();
if (!str_ends_with(rtrim($path, '/'), '/migrate-grav')) {
return;
}
$state = $this->newKickoff()->readFlag();
$this->grav['twig']->twig_vars['migrate_grav_state'] = $state;
}
private function newKickoff(): Kickoff
{
require_once __DIR__ . '/classes/Kickoff.php';
$config = (array) $this->config->get('plugins.migrate-grav', []);
$webroot = defined('GRAV_WEBROOT') ? GRAV_WEBROOT : GRAV_ROOT;
// Forward the site's GPM channel so Kickoff can match the release
// channel the rest of the admin uses (?testing vs stable).
$config['gpm_channel'] = (string) $this->grav['config']->get('system.gpm.releases', 'stable');
// Forward Grav's proxy config so both the Kickoff's own zip download
// AND the standalone wizard (via the .migrating flag) honor it.
// Sites behind a corporate proxy were silently breaking on every
// outbound call (GPM catalog, GitHub release lookups, the 2.0 zip).
$config['proxy_url'] = (string) $this->grav['config']->get('system.http.proxy_url', '');
$config['proxy_cert_path'] = (string) $this->grav['config']->get('system.http.proxy_cert_path', '');
return new Kickoff($webroot, $config);
}
}