-
Notifications
You must be signed in to change notification settings - Fork 6
/
ProcessWireUpgrade.module
995 lines (832 loc) · 32 KB
/
ProcessWireUpgrade.module
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
<?php namespace ProcessWire;
/**
* Tool to display available core and module upgrades for ProcessWire
*
* ProcessWire
* Copyright (C) 2021 by Ryan Cramer
* Licensed under MPL 2.0
*
* https://processwire.com
*
*/
class ProcessWireUpgrade extends Process {
/**
* Return information about this module (required)
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Upgrades',
'summary' => 'Tool that helps you identify and install core and module upgrades.',
'version' => 11,
'author' => 'Ryan Cramer',
'installs' => 'ProcessWireUpgradeCheck',
'requires' => 'ProcessWire>=3.0.0',
'icon' => 'coffee'
);
}
const debug = false;
const pageName = 'upgrades';
const minVersionPHP = '5.3.8';
/**
* Path to /wire/
*
*/
protected $wirePath = '';
/**
* Temporary path used by installer for download and ZIP extraction
*
*/
protected $tempPath = '';
/**
* Temporary path used by installer for file storage when file system isn't writable
*
*/
protected $cachePath = '';
/**
* Array of renames (oldPath => newPath) scheduled for __destruct
*
*/
protected $renames = array(); // scheduled renames to occur after page render
/**
* Instance of ProcessWireUpgradeCheck
*
* @var ProcessWireUpgradeCheck
*
*/
protected $checker = null;
/**
* Construct
*
*/
public function __construct() {
$this->initPaths();
parent::__construct();
}
/**
* API wired
*
*/
public function wired() {
$this->initPaths(); // duplication from construct intended
parent::wired();
}
/**
* Initialize and perform access checks
*
*/
public function init() {
if($this->config->demo) {
throw new WireException("This module cannot be used in demo mode");
}
if(!$this->user->isSuperuser()) {
throw new WireException("This module requires superuser");
}
set_time_limit(3600);
$this->checker = $this->modules->getInstall('ProcessWireUpgradeCheck');
if(!$this->checker) {
throw new WireException("Please go to Modules and click 'Check for new modules' - this will auto-update ProcessWireUpgrade.");
}
parent::init();
}
/**
* Initialize paths used by this module
*
*/
protected function initPaths() {
$config = $this->wire()->config;
$this->wirePath = $config->paths->root . 'wire/';
$this->tempPath = $config->paths->cache . $this->className() . '/';
$this->cachePath = $config->paths->cache . 'core-upgrade/';
}
/**
* Get info for either a specific core branch, or for the currently selected core branch
*
* @param string $name
* @return array
*
*/
protected function getBranch($name = '') {
$branches = $this->checker->getCoreBranches();
if(empty($name)) $name = $this->session->getFor($this, 'branch');
return isset($branches[$name]) ? $branches[$name] : array();
}
/**
* Set the current core branch
*
* @param string $name
*
*/
protected function setBranch($name) {
$this->session->setFor($this, 'branch', $name);
}
/**
* Preflight checks before listing modules
*
* @return string
*
*/
protected function preflight() {
$phpVersion = PHP_VERSION;
if(version_compare($phpVersion, self::minVersionPHP) >= 0) {
// good
} else {
$this->error("Please note that your current PHP version ($phpVersion) is not adequate to upgrade to the latest ProcessWire.");
}
if(!extension_loaded('pdo_mysql')) {
$this->error("Your PHP is not compiled with PDO support. PDO is required by ProcessWire.");
}
if(!class_exists('\ZipArchive')) {
$this->warning(
"Your PHP does not have ZipArchive support. This is required to install core or module upgrades with this tool. " .
"You can still use this tool to identify new versions and install them manually."
);
}
$upgradePaths = array($this->cachePath, $this->tempPath);
foreach($upgradePaths as $key => $path) {
if(!file_exists($path)) unset($upgradePaths[$key]);
}
if(count($upgradePaths)) {
$btn = $this->modules->get('InputfieldButton'); /** @var InputfieldButton $btn */
$btn->href = "./remove";
$btn->value = $this->_('Remove');
$btn->icon = 'trash-o';
return
$this->h('Upgrade files are already present. Please remove them before continuing.') . $this->ul($upgradePaths) .
$this->p($btn->render());
}
$lastRefresh = $this->session->getFor($this, 'lastRefresh');
if(!$lastRefresh || $lastRefresh < (time() - 86400)) {
$btn = $this->refreshButton();
$btn->value = $this->_('Continue');
$btn->icon = 'angle-right';
return
$this->h('We will load the latest core and module versions from the ProcessWire modules directory.') .
$this->p('Please be patient, this may take a few seconds to complete.') .
$this->p($btn->render()) .
$this->lastRefreshInfo();
}
return '';
}
/**
* Ask user to select branch or make them remove existing installation files
*
*/
public function execute() {
$sanitizer = $this->sanitizer;
$config = $this->config;
$modules = $this->modules;
$preflight = $this->preflight();
if($preflight) return $preflight;
/** @var MarkupAdminDataTable $table */
$table = $modules->get('MarkupAdminDataTable');
$table->setEncodeEntities(false);
$table->headerRow(array(
$this->_('Module title'),
$this->_('Module name'),
$this->_('Installed'),
$this->_('Latest'),
$this->_('Status'),
$this->_('Links'),
));
$items = $this->checker->getVersions();
$numPro = 0;
if(count($items)) {
foreach($items as $name => $item) {
if(empty($item['local'])) continue;
if(empty($item['remote'])) { /* not in directory */ }
$remote = $sanitizer->entities($item['remote']);
$installer = empty($item['installer']) ? '' : $sanitizer->entities($item['installer']);
$upgradeLabel = $this->_('Up-to-date');
$links = [];
if($item['new'] > 0) {
$upgradeLabel = $this->_('Upgrade available');
$remote = $this->b($remote);
} else if($item['new'] < 0) {
$upgradeLabel .= '+';
}
if(empty($remote)) {
$remote = "";
$upgradeLabel = "";
}
if(empty($item['branch'])) {
$upgradeURL = $config->urls->admin . "module/?update=" . ($installer ? $installer : $name);
} else {
$upgradeURL = "./check?branch=$item[branch]";
}
if($item['new'] > 0) {
$upgradeLabel = $this->icon('lightbulb-o') . $upgradeLabel;
$upgrade = $this->a($upgradeURL, $upgradeLabel);
} else {
$upgrade = $this->span($upgradeLabel, 'detail');
}
$urls = isset($item['urls']) ? $item['urls'] : array();
foreach($urls as $key => $url) $urls[$key] = $sanitizer->entities($url);
if(!empty($urls['support'])) {
$links[] = $this->iconLink('life-ring', $urls['support'], 'Support');
}
if(!empty($urls['repo']) && strpos($urls['repo'], 'github.com')) {
$links[] = $this->iconLink('github-alt', $urls['repo'], 'GitHub');
} else if(!empty($urls['repo'])) {
$links[] = $this->iconLink('info-circle', $urls['repo'], 'Details');
} else if(!empty($item['href'])) {
$links[] = $this->iconLink('info-circle', $item['href'], 'Info');
}
if(!empty($urls['dir'])) {
$links[] = $this->iconLink('share-alt', $urls['dir'], 'Directory');
}
if(empty($remote) && empty($links)) continue;
// else if(!$item['remote']) $upgrade = "<span class='detail'>" . $this->_('Not in directory') . "</span>";
$icon = empty($item['icon']) ? wireIconMarkup('plug', 'fw') : wireIconMarkup($this->sanitizer->entities($item['icon']), 'fw');
$title = $icon . ' ' . $sanitizer->entities($item['title']);
$proLabel = ' ' . $this->span('PRO', 'pro');
if($installer && empty($remote)) {
$title = $this->tooltip("Upgraded with $installer", $title);
if(!empty($item['pro'])) $title .= $proLabel;
} else if(!empty($item['pro'])) {
$title = $this->a($upgradeURL, $title) . $proLabel;
if($item['new'] > 0) {
if(!empty($urls['support'])) $upgradeURL = $urls['support'];
$protip = 'PRO module upgrade available in ProcessWire VIP support board (login required)';
$upgrade = $this->tooltip($protip, $this->aa($upgradeURL, $upgradeLabel));
// $title = $this->tooltip($protip, $this->aa($upgradeURL, $title) . $proLabel);
}
$numPro++;
} else {
$title = $this->a($upgradeURL, $title);
}
$table->row(array(
$title,
$sanitizer->entities($name),
$sanitizer->entities($item['local']),
$remote,
$upgrade,
$this->span(implode(' ', $links), 'links')
));
}
}
return
$table->render() .
$this->p($this->refreshButton(true)->render()) .
$this->lastRefreshInfo();
}
/**
* Refresh module versions data
*
*/
public function executeRefresh() {
$this->session->setFor($this, 'lastRefresh', time());
if(method_exists($this->modules, 'resetCache')) $this->modules->resetCache();
$this->checker->getVersions(true);
$this->message($this->_('Refreshed module versions data'));
$this->session->redirect('./');
}
/**
* Remove existing installation files
*
*/
public function executeRemove() {
$paths = array(
$this->cachePath,
$this->tempPath,
);
foreach($paths as $path) {
if(!file_exists($path)) continue;
if(wireRmdir($path, true)) {
$this->session->message(sprintf($this->_('Removed: %s'), $path));
} else {
$this->session->error(sprintf($this->_('Permission error removing path (please remove manually): %s'), $path));
}
}
$this->session->redirect('./');
}
/**
* Check the selected branch and compare to current version to see if user wants to continue
*
*/
public function executeCheck() {
$this->coreUpgrade();
$config = $this->config;
$input = $this->input;
$modules = $this->modules;
if(!$config->debug) {
$this->error(
"While optional, we recommend that you enable debug mode during the upgrade so that you will see detailed error messages, should they occur. " .
"Do this by editing /site/config.php and setting the debug option to true. Example: \$config->debug = true;"
);
}
$name = $input->get('branch');
if(empty($name)) throw new WireException("No branch selected");
$branch = $this->getBranch($name);
if(!count($branch)) throw new WireException("Unknown branch");
$this->headline("ProcessWire Core ($name)");
$this->setBranch($name);
$result = version_compare($config->version, $branch['version']);
if($result < 0) {
$msg = "The available version ($branch[version]) is newer than the one you currently have ($config->version).";
} else if($result > 0) {
$msg = "The available version ($branch[version]) is older than the one you currently have ($config->version).";
} else {
$msg = "The available version is the same as the one you currently have.";
}
$out = $this->h("Do you want to download and install ProcessWire $branch[name] version $branch[version]?") . $this->p($msg);
/** @var InputfieldButton $btn */
$btn = $modules->get('InputfieldButton');
$btn->href = "./download";
$btn->value = $this->_('Download Now');
$btn->icon = 'cloud-download';
$out .= $btn->render();
$btn = $modules->get('InputfieldButton');
$btn->href = "./";
$btn->value = $this->_('Abort');
$btn->icon = 'times-circle';
$btn->addClass('ui-priority-secondary');
$out .= $btn->render();
$out .= $this->p("After clicking the download button, be patient, as this may take a minute.", 'detail');
return $out;
}
/**
* Download the selected branch ZIP file
*
*/
public function executeDownload() {
$branch = $this->getBranch();
if(empty($branch)) throw new WireException("No branch selected");
wireMkdir($this->tempPath);
$http = new WireHttp();
$this->wire($http);
$zipfile = $http->download($branch['zipURL'], $this->tempPath . "$branch[name].zip");
if(!$zipfile || !file_exists($zipfile) || !filesize($zipfile)) {
throw new WireException("Unable to download $branch[zipURL]");
}
$this->message("Downloaded version $branch[version] (" . number_format(filesize($zipfile)) . " bytes)");
$this->session->redirect('./database');
}
/**
* Export database or instruct them to do it
*
*/
public function executeDatabase() {
$this->coreUpgrade();
$config = $this->config;
$input = $this->input;
$session = $this->session;
$modules = $this->modules;
$session->removeFor($this, 'database');
$branch = $this->getBranch();
$canBackup = class_exists('\\ProcessWire\\WireDatabaseBackup');
if($input->get('backup') && $canBackup) {
$options = array(
'filename' => $config->dbName . "-" . $config->version . "-" . date('Y-m-d_H-i-s') . ".sql",
'description' => "Automatic backup made by ProcessWireUpgrade before upgrading from {$config->version} to $branch[version]."
);
$backups = $this->database->backups();
$file = $backups->backup($options);
$errors = $backups->errors();
if(count($errors)) {
foreach($errors as $error) $this->error($error);
}
if($file) {
clearstatcache();
$bytes = filesize($file);
$file = str_replace($config->paths->root, '/', $file);
$this->message("Backup saved to $file ($bytes bytes) - Please note this location should you later need to restore it.");
$session->setFor($this, 'database', $file);
} else {
$this->error("Database backup failed");
}
$session->redirect('./prepare');
}
$out = $this->h('Database Backup');
if($canBackup) {
$out .= $this->p('Your version of ProcessWire supports automatic database backups.*');
/** @var InputfieldButton $btn */
$btn = $modules->get('InputfieldButton');
$btn->href = "./database?backup=1";
$btn->value = $this->_('Backup Database Now');
$btn->icon = 'database';
$out .= $btn->render();
$btn = $modules->get('InputfieldButton');
$btn->href = "./prepare";
$btn->icon = 'angle-right';
$btn->value = $this->_('Skip Database Backup');
$btn->addClass('ui-priority-secondary');
$out .= $btn->render();
$out .= $this->p('*We also recommend creating an independent database backup, for instance with PhpMyAdmin.', 'detail');
} else {
$out .= $this->p(
"Your current version of ProcessWire does not support automatic database backups. " .
"We recommend making a backup of database `$config->dbName` using a tool like PhpMyAdmin. " .
"Click the button below once you have saved a backup."
);
/** @var InputfieldButton $btn */
$btn = $modules->get('InputfieldButton');
$btn->href = "./prepare";
$btn->icon = 'angle-right';
$btn->value = $this->_('Confirm');
$out .= $btn->render();
}
return $this->out($out);
}
/**
* Unzip files and prepare them for installation
*
*/
public function executePrepare() {
$config = $this->config;
$this->coreUpgrade();
$error = '';
$branch = $this->getBranch();
if(empty($branch)) throw new WireException("No branch selected");
$zipfile = $this->tempPath . "$branch[name].zip"; // site/assets/cache/ProcessWireUpgrade/branch-dev.zip
if(!file_exists($zipfile)) throw new WireException("Unable to locate ZIP: $zipfile");
$files = wireUnzipFile($zipfile, $this->tempPath);
if(!count($files)) $error = "No files were found in $zipfile";
$oldVersion = $config->version;
$newVersion = $branch['version'];
$rootPath = dirname(rtrim($this->wirePath, '/')) . '/';
$rootTempPath = $this->tempPath; // site/assets/cache/ProcessWireUpgrade/
$wireTempPath = $this->tempPath . 'wire/'; // site/assets/cache/ProcessWireUpgrade/wire/
$wireNewName = "wire-$newVersion"; // wire-2.5.0
if(!$error && !is_dir($wireTempPath)) {
// adjust paths according to where they were unzipped, as needed
// need to drill down a level from extracted archive
// i.e. files[0] may be a dir like /ProcessWire-dev/
$rootTempPath = $this->tempPath . trim($files[0], '/') . '/';
$wireTempPath = $rootTempPath . "wire/";
if(!is_dir($wireTempPath)) $error = "Unable to find /wire/ directory in archive";
}
$indexNewName = "index-$newVersion.php"; // index-2.5.0.php
$htaccessNewName = "htaccess-$newVersion.txt"; // htaccess-2.5.0.txt
$newIndexData = file_get_contents($rootTempPath . 'index.php');
$newIndexVersion = preg_match('/define\("PROCESSWIRE", (\d+)\);/', $newIndexData, $matches) ? (int) $matches[1] : 0;
$oldIndexData = file_get_contents($rootPath . 'index.php');
$oldIndexVersion = preg_match('/define\("PROCESSWIRE", (\d+)\);/', $oldIndexData, $matches) ? (int) $matches[1] : 0;
$indexIsOk = $newIndexVersion === $oldIndexVersion;
$oldHtaccessData = file_get_contents($rootTempPath . 'htaccess.txt');
$oldHtaccessVersion = preg_match('/@htaccessVersion\s+(\d+)/', $oldHtaccessData, $matches) ? (int) $matches[1] : 0;
$newHtaccessData = file_get_contents($rootPath . '.htaccess');
$newHtaccessVersion = preg_match('/@htaccessVersion\s+(\d+)/', $newHtaccessData, $matches) ? (int) $matches[1] : 0;
$htaccessIsOk = $oldHtaccessVersion === $newHtaccessVersion;
$rootWritable = is_writable($rootPath) && is_writable($rootPath . "wire/");
// determine where we will be moving upgrade files to
if($rootWritable) {
// if root path is writable, we can place new dirs/files in the same
// location as what they are replacing, i.e. /wire/ and /wire-2.5.0/
$wireNewPath = $rootPath . $wireNewName . "/";
$htaccessNewFile = $rootPath . $htaccessNewName;
$indexNewFile = $rootPath . $indexNewName;
} else {
// if root is not writable, we will place dirs/files in /site/assets/cache/core-upgrade/ instead.
$cacheUpgradePath = $this->cachePath;
$cacheUpgradeURL = str_replace($config->paths->root, '/', $cacheUpgradePath);
$this->warning(
"Your file system is not writable, so we are installing files to $cacheUpgradeURL instead. " .
"You will have to copy them manually to your web root."
);
wireMkdir($cacheUpgradePath);
$wireNewPath = $cacheUpgradePath . 'wire/';
$htaccessNewFile = $cacheUpgradePath . 'htaccess.txt';
$indexNewFile = $cacheUpgradePath . 'index.php';
}
if(!$error) {
$this->renameNow($wireTempPath, $wireNewPath); // /temp/path/wire/ => /wire-2.5.0/
$this->renameNow($rootTempPath . "index.php", $indexNewFile); // /temp/path/index.php => /index-2.5.0.php
$this->renameNow($rootTempPath . "htaccess.txt", $htaccessNewFile); // /temp/path/htaccess.txt => /htaccess-2.5.0.txt
// remove /temp/path/ as no longer needed since we've taken everything we need out of it above
wireRmdir($this->tempPath, true);
}
if($error) throw new WireException($error);
$wireNewLabel = str_replace($config->paths->root, '/', $wireNewPath);
$indexNewLabel = $rootWritable ? basename($indexNewFile) : str_replace($config->paths->root, '/', $indexNewFile);
$htaccessNewLabel = $rootWritable ? basename($htaccessNewFile) : str_replace($config->paths->root, '/', $htaccessNewFile);
$out =
$this->h('Upgrade files copied') .
$this->p(
"We have prepared copies of upgrade files for installation. At this point, " .
"you may install them yourself by replacing the existing `/wire/` directory with `$wireNewLabel`, " .
"and optionally `index.php` with `$indexNewLabel`, and `.htaccess` with `$htaccessNewLabel`. " .
($rootWritable ? "Or, since your file system is writable, this tool can install them." : "Full instructions are below.")
);
$items = array();
if($indexIsOk) {
$items[] = "Your `index.php` file appears to be up-to-date, it should be okay to leave your existing one in place.";
} else {
$items[] = "Your `index.php` file appears to need an update, check that you haven’t made any site-specific customizations to it before replacing it.";
}
if($htaccessIsOk) {
$items[] = "Your `.htaccess` file appears to be up-to-date, it should be okay to leave your existing one in place.";
} else {
$items[] = "Your `.htaccess` file appears to need an update, check for any site-specific customizations before replacing it. You may want to apply updates to it manually.";
}
$out .= $this->ul($items);
if($rootWritable) {
$htaccessNote = '';
if(!$htaccessIsOk) {
$htaccessNote = 'Since the `.htaccess` file can often have site-specific customizations, it would be best to handle that one manually, unless you know it is unmodified.';
}
$out .=
$this->h('Want this tool to install the upgrade?') .
$this->p("Check the boxes below for what you’d like us to install. $htaccessNote");
/** @var InputfieldSubmit $btn */
$btn = $this->modules->get('InputfieldSubmit');
$btn->attr('name', 'submit_install');
$btn->value = $this->_('Install');
$btn->icon = 'angle-right';
$out .=
$this->form('./install/',
$this->p(
$this->checkbox('wire', 'Install new core `/wire/` directory', "(old will be renamed to /.wire-$oldVersion/)", true) .
$this->checkbox('index', 'Install new `index.php` file', "(old will be renamed to .index-$oldVersion.php)", !$indexIsOk) .
$this->checkbox('htaccess', 'Install new `.htaccess` file', "(old will be renamed to .htaccess-$oldVersion)", false)
) .
$this->p($btn->render())
);
} else {
// root not writable
$backupInfo = array(
"/wire" => "/.wire-$oldVersion",
"/index.php" => "/.index-$oldVersion.php",
"/.htaccess" => "/.htaccess-$oldVersion",
);
$renameInfo = array(
rtrim($wireNewPath, '/') => "/wire",
"$indexNewFile" => "/index.php",
"$htaccessNewFile" => "/.htaccess",
);
$out .= $this->p(
"Your file system is not writable so we can’t automatically install the upgrade files for you. " .
"However, the files are ready for you to move to their destinations."
);
$out .=
$this->h('Backup your existing files') .
$this->p(
"While optional, we strongly recommend making backups of everything replaced so that you can always revert back to it if needed. " .
"We recommend doing this by performing the following file rename operations:"
);
$items = array();
foreach($backupInfo as $old => $new) {
$items[] = "Rename `$old` to `$new`";
}
$out .= $this->ul($items);
$out .=
$this->h('Migrate the new files') .
$this->p('Now you can migrate the new files, renaming them from their temporary location to their destination.');
$items = array();
foreach($renameInfo as $old => $new) {
$old = str_replace($config->paths->root, '/', $old);
$items[] = "Rename `$old` to `$new`";
}
$out .=
$this->ul($items) .
$this->p('Once you’ve completed the above steps, your upgrade will be complete.') .
$this->completionNotes();
}
$out .= $this->p('*In many cases, it is not necessary to upgrade the index.php and .htaccess files since they don’t always change between versions.', 'detail');
return $this->out($out);
}
/**
* Install prepared files
*
*/
public function executeInstall() {
$this->coreUpgrade();
$input = $this->input;
$config = $this->config;
if(!$input->post('submit_install')) throw new WireException('No form received');
$branch = $this->getBranch();
if(empty($branch)) throw new WireException("No branch selected");
$oldVersion = $config->version;
$newVersion = $branch['version'];
$rootPath = dirname(rtrim($this->wirePath, '/')) . '/';
$renames = array();
if($input->post('wire')) {
$renames["wire"] = ".wire-$oldVersion";
$renames["wire-$newVersion"] = "wire";
}
if($input->post('index')) {
$renames["index.php"] = ".index-$oldVersion.php";
$renames["index-$newVersion.php"] = "index.php";
}
if($input->post('htaccess')) {
$renames[".htaccess"] = ".htaccess-$oldVersion";
$renames["htaccess-$newVersion.txt"] = ".htaccess";
}
foreach($renames as $old => $new) {
$this->renameLater($rootPath . $old, $rootPath . $new);
}
$out =
$this->h('Upgrade completed') .
$this->p($this->b('Double check that everything works before you leave this page')) .
$this->completionNotes();
return $this->out($out);
}
/**
* Completion notes
*
*/
protected function completionNotes() {
$config = $this->config;
$branch = $this->getBranch();
$newVersion = $branch['version'];
$oldVersion = $config->version;
$dbFile = $this->session->getFor($this, 'database');
$dbNote = '';
if($dbFile) $dbNote = "Your database was backed up to this file:<br />`$dbFile`";
$frontEndLink = $this->aa($config->urls->root, 'front-end');
$adminLink = $this->aa($config->urls->admin, 'admin');
$items = array();
$items[] = "Test out both the $frontEndLink and $adminLink of your site in full to make sure everything works.";
$items[] = "Installed files have permission `$config->chmodFile` and directories `$config->chmodDir`. Double check that these are safe with your web host, especially in a shared hosting environment.";
if($config->debug) {
$items[] = "For production sites, remember to turn off debug mode once your upgrade is complete.";
}
$out = $this->ul($items);
$out .=
$this->p($this->b('If your upgrade did not work…')) .
$this->p(
'If you encounter fatal error messages (in the front-end or admin links above), hit reload/refresh in that browser tab until it clears (2-3 times). ' .
'It may take a few requests for ProcessWire to apply any necessary database schema changes. ' .
'Should you determine that the upgrade failed for some reason and you want to revert back to the previous version, ' .
'below are the steps to undo what was just applied.'
) .
$this->p('Step 1: Remove the installed updates by renaming or deleting them') .
$this->ul(array(
"Delete `/wire/` directory OR rename it to `/.wire-$newVersion/`",
"If you replaced the `.htaccess` file: Delete `.htaccess` OR rename it to `.htaccess-$newVersion`",
"If you replaced the `index.php` file: Delete `index.php` OR rename it to `.index-$newVersion.php`"
)) .
$this->p('Step 2: Restore backed up files') .
$this->ul(array(
"Rename directory `/.wire-$oldVersion/` to `/wire/`",
"If applicable: Rename `.htaccess-$oldVersion` to `.htaccess`",
"If applicable: Rename `.index-$oldVersion.php` to `index.php`",
));
if($dbNote) $out .=
$this->p('Step 3: Restore backed up database (if necessary)') .
$this->ul(array($dbNote));
return $out;
}
/**
* Schedule a rename operation, which will occur at __destruct
*
* @param string $oldPath
* @param string $newPath
*
*/
protected function renameLater($oldPath, $newPath) {
$this->renames[$oldPath] = $newPath;
$old = basename(rtrim($oldPath, '/'));
$new = basename(rtrim($newPath, '/'));
$this->message("Rename $old => $new");
}
/**
* Perform a rename now
*
* @param string $old
* @param string $new
* @return bool
* @throws WireException
*
*/
protected function renameNow($old, $new) {
$result = true;
// for labels
$_old = str_replace($this->config->paths->root, '/', $old);
$_new = str_replace($this->config->paths->root, '/', $new);
if(!file_exists($old)) {
$this->error("$_old does not exist");
return $result;
}
if(file_exists($new)) {
$this->message("$_new already exists (we left it untouched)");
return $result;
}
$result = rename($old, $new);
if($result) {
$this->message("Renamed $_old => $_new");
} else {
$this->error("Unable to rename $_old => $_new");
if(basename(rtrim($new, '/')) == 'wire') {
throw new WireException("Upgrade aborted. Unable to rename $_old => $_new");
}
}
return $result;
}
protected function coreUpgrade() {
$this->headline($this->_('Core upgrade'));
}
/**
* @param bool $showInHeader
* @return InputfieldButton
*
*/
protected function refreshButton($showInHeader = false) {
/** @var InputfieldButton $btn */
$btn = $this->modules->get('InputfieldButton');
$btn->href = './refresh';
$btn->value = $this->_('Refresh');
$btn->icon = 'refresh';
if($showInHeader && method_exists($btn, 'showInHeader')) $btn->showInHeader(true);
return $btn;
}
/**
* @param bool $paragraph
* @return string
*
*/
protected function lastRefreshInfo($paragraph = true) {
$lastRefresh = $this->session->getFor($this, 'lastRefresh');
$lastRefresh = $lastRefresh ? wireRelativeTimeStr($lastRefresh) : $this->_('N/A');
$out = sprintf($this->_('Last refresh: %s'), $lastRefresh);
if($paragraph) $out = $this->p($out, 'detail last-refresh-info');
return $out;
}
/**
* Process rename operations
*
*/
public function __destruct() {
if(!count($this->renames)) return;
//$rootPath = dirname(rtrim($this->wirePath, '/')) . '/';
foreach($this->renames as $oldPath => $newPath) {
if(file_exists($newPath)) {
$n = 0;
do {
$newPath2 = $newPath . "-" . (++$n);
} while(file_exists($newPath2));
if(rename($newPath, $newPath2)) {
$this->message("Renamed $newPath => $newPath2");
}
}
$old = basename(rtrim($oldPath, '/'));
$new = basename(rtrim($newPath, '/'));
if(rename($oldPath, $newPath)) {
$this->message("Renamed $old => $new");
} else {
$this->error("Unable to rename $old => $new");
}
}
$this->renames = array();
}
public function h($str, $h = 2) { return "<h$h>$str</h$h>"; }
public function p($str, $class = '') { return $class ? "<p class='$class'>$str</p>" : "<p>$str</p>"; }
public function a($href, $label, $class = '') { return ($class ? "<a class='$class' " : "<a ") . "href='" . $this->sanitizer->entities($href) . "'>$label</a>"; }
public function aa($href, $label, $class = '') { return str_replace('<a', "<a target='_blank'", $this->a($href, $label, $class)); }
public function span($str, $class = '') { return $class ? "<span class='$class'>$str</span>" : "<span>$str</span>"; }
public function b($str, $class = '') { return $class ? "<strong class='$class'>$str</strong>" : "<strong>$str</strong>"; }
public function ul(array $items) { return "<ul class='bullets'><li>" . implode('</li><li>', $items) . '</li></ul>'; }
public function form($action, $content) { return "<form action='$action' method='post'>$content</form>"; }
public function icon($name, $fw = true) { return wireIconMarkup($name, ($fw ? 'fw' : '')); }
public function iconLink($icon, $href, $tooltip) { return str_replace('<a', "<a title='$tooltip'", $this->aa($href, $this->icon($icon), 'pw-tooltip')); }
public function tooltip($tooltip, $markup) { return "<span class='pw-tooltip' title='$tooltip'>$markup</span>"; }
public function checkbox($name, $label, $note = '', $checked = false) {
$adminTheme = $this->wire()->adminTheme;
$checkboxClass = $adminTheme ? $this->wire()->adminTheme->getClass('input-checkbox') : '';
$checked = $checked ? 'checked' : '';
$note = $note ? $this->span($note, 'detail') : '';
return "<div><label><input type='checkbox' class='$checkboxClass' name='$name' $checked value='1'> $label $note</label></div>";
}
protected function out($out) {
if(strpos($out, '`') === false) return $out;
$out = preg_replace('/[`]([^<\n`]+?)[`]/', '<code>$1</code>', $out);
return $out;
}
/**
* Install
*
*/
public function ___install() {
// create the page our module will be assigned to
$page = new Page();
$page->template = 'admin';
$page->name = self::pageName;
// installs to the admin "Setup" menu ... change as you see fit
$page->parent = $this->pages->get($this->config->adminRootPageID)->child('name=setup');
$page->process = $this;
// we will make the page title the same as our module title
// but you can make it whatever you want
$info = self::getModuleInfo();
$page->title = $info['title'];
// save the page
$page->save();
// tell the user we created this page
$this->message("Created Page: {$page->path}");
}
/**
* Uninstall
*
*/
public function ___uninstall() {
// find the page we installed, locating it by the process field (which has the module ID)
// it would probably be sufficient just to locate by name, but this is just to be extra sure.
$moduleID = $this->modules->getModuleID($this);
$page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName . "|core-upgrade");
if($page->id) {
// if we found the page, let the user know and delete it
$this->message("Deleting Page: {$page->path}");
$page->delete();
}
wireRmdir($this->tempPath, true);
}
}