This repository has been archived by the owner on Nov 18, 2018. It is now read-only.
forked from camptocamp/cartoweb3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cw3setup.php
executable file
·1583 lines (1283 loc) · 49 KB
/
cw3setup.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
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
995
996
997
998
999
1000
<?php
/**
* CartoWeb3 Installer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @copyright 2005 Camptocamp SA
* @package Core
* @version $Id$
* @rev $Revision$
*/
/*
* It is necessary for the error recovery procedures that the install script
* be idempotent. This means that if it is run successfully, and then it is
* called again, it doesn't bomb out or cause any harm, but just ensures
* that everything is the way it ought to be. If the first call failed, or
* aborted half way through for some reason, the second call should merely
* do the things that were left undone the first time, if any, and exit with
* a success status if everything is OK.
*/
error_reporting(E_ALL);
define('CW3_SETUP_REVISION', '$Revision$');
define('MINIMUM_REVISION', 42);
define('CW3_SETUP_INCLUDED', true);
// URL of required libraries (md5sum: 0521c8a180b59ee150a536352f23d356):
// define('CW3_LIBS_URL', 'http://www.cartoweb.org/downloads/cw3.5/cartoweb-includes-3.5.5.tar.gz');
// URL of required libraries e2f49f75b1dc4cc9cf710a34b09a0adc cartoweb-includes-3.5.5.tar.gz
define('CW3_LIBS_URL', 'http://www.cartoweb.org/downloads/cw3.5/cartoweb-includes-3.5.5.tar.gz');
define('CW3_LIBS_MD5', 'e2f49f75b1dc4cc9cf710a34b09a0adc');
// URL of demo data (md5sum: 781f6d3207fda3ebf4d6e51d354a4336):
define('CW3_DEMO_URL', 'http://www.cartoweb.org/downloads/cw3.5/cartoweb-demodata-3.5.0.tar.gz');
// New cw3.6 need minimal version
define ('SUPPORTED_MS_VERSION', '6.0.2');
define ('SUPPORTED_PHP_VERSION', '5.3.8');
// Directories to create from cw3 root:
$CW3_DIRS_TO_CREATE = array(
'htdocs/generated',
'htdocs/generated/images',
'htdocs/generated/icons',
'htdocs/generated/pdf',
'htdocs/generated/geostat',
'www-data',
'www-data/mapinfo_cache',
'www-data/mapresult_cache',
'www-data/soapxml_cache',
'www-data/saved_posts',
'www-data/wsdl_cache',
'www-data/pdf_cache',
'www-data/views',
'www-data/accounting',
'www-data/wms_cache',
'www-data/wfs_cache',
'www-data/throttling',
'templates_c'
);
// Directories which should be writable by the php process
$CW3_WRITABLE_DIRS = array('log',
'htdocs/generated',
'www-data',
'templates_c'
);
function usage() {
?>Usage: <?php echo $_SERVER['argv'][0]; ?> ACTION [OPTION_1] ... [OPTION_N]
Possible actions:
--help, or -h Display this help and exit.
--version or -v Output version information and exit.
--install Install CartoWeb.
--fetch-demo Fetch the demo data from cartoweb.org, and extract
it in the demo project if not already there.
--clean Clean generated files and caches.
List of options:
--debug Turn on output debugging.
--writableowner OWNER The user who should have write permissions for
generated files.
--cvs-root CVS Root directory to use when fetching
CartoWeb/project out of CVS:
:pserver:username@servername:/path/to/the/repository
--svn-root SVN URL to use when fetching CartoWeb/project out
of SVN:
http://servername/path/to/the/repository/
--fetch-from-cvs Fetch CartoWeb from CVS and install it in the
current directory, or in the directory given by
the --install-location parameter.
NOTE: You must be located where cartoweb3 directory
will be created, not inside like other commands.
--cartoweb-cvs-option OPTIONS A string which will be given to the cvs checkout
command of CartoWeb (not projects!).
For instance, to fetch a specific branch,
use '-r MY_BRANCH'. Or for a specific date,
use '-D "2005-09-05 11:00"'.
--fetch-from-svn Fetch CartoWeb from SVN and install it in the
current directory, or in the directory given by
the --install-location parameter.
Use the trunk by default, change this to use a branche or
tag.
NOTE: You must be located where cartoweb3 directory
will be created, not inside like other commands.
--cartoweb-svn-option OPTIONS A string which will be given to the svn checkout
command of CartoWeb (not projects!).
For instance, to fetch a specific revision,
use '-r revisionnumber'. Or for a specific date,
use '-r {"2005-09-05 11:00"}'.
--fetch-from-dir DIRECTORY Copy CartoWeb from the specified directory into the
current directory, or in the directory given by the
--install-location parameter.
NOTE 1: You must be located where cartoweb3
directory will be created, not inside like other
commands.
NOTE 2: You may either use a path relative to the
target cartoweb3 directory or an absolute path.
--install-location Directory where to install CartoWeb
(when using --fetch-from-cvs/dir options).
--delete-existing Overwrite existing directories if any.
--no-symlinks Do not use symbolic links, even if your operating
system supports them.
--config-from-file FILE Location of a configuration file for automatic
variable replacement in .in files.
NOTE: You may either use a path relative to the
target cartoweb3 directory or an absolute path.
--config-from-project PROJECT Read the configuration file containing variables
to replace in .in files from the specified project.
--fetch-project-cvs PROJECT Fetch the given project from CVS (see --cvs-root
option). To fetch several projects at a time,
specify this option as many times as necessary.
--fetch-project-svn PROJECT Fetch the given project from SVN (you will need to
give --svn-co-options to specifiy the checkout
command to be used).
To fetch several projects at a time,
specify this option as many times as necessary.
--svn-co-options Checkout command to use for fetching project with
SVN. For instance "--username foo --no-auth-cache
checkout https://myproject/svn/bar/".
--fetch-project-dir DIRECTORY Fetch the given project from a directory. To
fetch several projects at a time, specify this
option as many times as necessary.
--project PROJECT Installation is launched only for given project. To
install several projects at a time, specify this
option as many times as necessary.
--default-project PROJECT Default project to use.
--base-url BASEURL URL where you can find client.php.
--profile PROFILENAME The profile to use (development/production/custom).
NOTE: default is 'production'
--clean-views Clean views (must be used with --clean).
--clean-accounting Clean accounting (must be used with --clean).
--keep-directories Do not remove the generated directories during
cleaning (must be used with --clean).
--keep-permissions Do not alter the permissions of writable directories.
<?php
exit();
}
class InstallException extends Exception {
}
/**
* We need "." in the include path, otherwise parse_ini_file won't work
*/
function checkIncludePath() {
$includePath = get_include_path();
if (!in_array('.', explode(PATH_SEPARATOR, $includePath))) {
set_include_path(get_include_path() . PATH_SEPARATOR . '.' . PATH_SEPARATOR . '..');
}
}
/**
* register_argc_argv must be set to "on" in php.ini
*/
function checkRegisterArgcArgv() {
if (!ini_get('register_argc_argv')) {
die("Parameter register_argc_argv must be set to On in your php.ini\n");
}
}
/**
* Stuff for checking minimal version without having already cw3 deployed
*
*/
/**
* Returns the MapServer version.
* @return string
*/
function getMsVersion() {
if (preg_match('/^MapServer version ([0-9.]+) (.*)/',
ms_GetVersion(), $regs)) {
return $regs[1];
}
return '';
}
/**
* Tells if version type is newer or same than the given version.
*
* The reference version might only specify the major version (eg. '5')
* or the major+minor versions (eg. '5.3')
* or the full version (eg. '5.3.10').
* @param string version to compare to
* @param string type ('PHP','MAPSERVER')
* @return boolean
*/
function isNewerOrSameThan($version,$type='PHP') {
$refVersion = explode('.', $version);
$typeVersion = (strtoupper($type) == 'PHP') ? explode('.', PHP_VERSION) : explode('.', getMsVersion());
$diff = count($refVersion) - count($typeVersion);
// Tests if arrays have the same size, if not adds 0s to the shorter one.
switch (true) {
case $diff == 0:
break;
case $diff < 0:
$refVersion = array_pad($refVersion, count($typeVersion), 0);
break;
default: //$diff > 0
$msVersion = array_pad($typeVersion, count($refVersion), 0);
}
foreach ($typeVersion as $level => $part) {
if ($part != $refVersion[$level]) {
return $part > $refVersion[$level];
}
}
// At this point, versions are equal.
return true;
}
/**
* Check version for PhP & Mapserver
* @throws InstallException
*/
function checkMinimalVersions(){
if (!extension_loaded('mapscript')) {
if (!dl('php_mapscript.' . PHP_SHLIB_SUFFIX))
throw new InstallException("can't load mapscript " . 'library');
}
// Safety check for MapServer >= 6.0.2
// PhP >= 5.3.10
if (!isNewerOrSameThan(SUPPORTED_MS_VERSION,'MAPSERVER'))
throw new InstallException("Mapserver/Mapscript version can't be less than ".SUPPORTED_MS_VERSION . PHP_EOL);
if (!isNewerOrSameThan(SUPPORTED_PHP_VERSION,'PHP'))
throw new InstallException("PhP version can't be less than ".SUPPORTED_PHP_VERSION . PHP_EOL);
// Everything okay
}
checkIncludePath();
checkRegisterArgcArgv();
function setOption(&$i, $takesArgument = false) {
global $OPTIONS;
$option = substr($_SERVER['argv'][$i], 2);
$argument = true;
if ($takesArgument) {
$i++;
if (!isset($_SERVER['argv'][$i])) {
fail("Missing argument for option $option");
exit(-1);
}
$argument = $_SERVER['argv'][$i];
}
$OPTIONS[$option] = $argument;
}
$OPTIONS['debug'] = false;
$OPTIONS['test'] = false;
$OPTIONS['force'] = "";
$OPTIONS['writableowner'] = 'www-data';
define('ACTION_NOP', 0);
define('ACTION_INSTALL', 1);
define('ACTION_CLEAN', 2);
define('ACTION_FETCH_DEMO', 3);
define('ACTION_INSTALL_DEMO_DATA', 4);
define('ACTION_PREPARE_ARCHIVE', 5);
define('LOG_LEVEL_DEBUG', 0);
define('LOG_LEVEL_INFO', 1);
define('LOG_LEVEL_WARN', 2);
define('LOG_LEVEL_FAIL', 3);
/* default log level is debug so that po2mo and others show messages */
$logLevel = LOG_LEVEL_DEBUG;
function processArgs() {
global $OPTIONS;
global $logLevel;
$action = ACTION_NOP;
// default log level
$logLevel = LOG_LEVEL_INFO;
//loop through our arguments and see what the user selected
for ($i = 1; $i < $_SERVER['argc']; $i++) {
switch ($_SERVER['argv'][$i]) {
case '-v':
case '--version':
info($_SERVER['argv'][0] . ' ' . CW3_SETUP_REVISION);
exit;
break;
case '--delete-existing':
case '--fetch-from-cvs':
case '--fetch-from-svn':
case '--no-symlinks':
case '--clean-views':
case '--clean-accounting':
case '--with-demo':
case '--keep-directories':
case '--keep-permissions':
setOption($i);
break;
case '--cvs-root':
case '--cartoweb-cvs-option':
case '--svn-root':
case '--cartoweb-svn-option':
case '--svn-co-options':
case '--config-from-file':
case '--install-location':
case '--fetch-from-dir':
case '--config-from-project':
case '--default-project':
case '--base-url':
case '--profile':
case '--writableowner':
setOption($i, true);
break;
case '--fetch-project-cvs':
case '--fetch-project-svn':
case '--fetch-project-dir':
case '--project':
$option = substr($_SERVER['argv'][$i], 2);
$oldOptions = array();
if (isset($OPTIONS[$option])) {
$oldOptions[$option] = $OPTIONS[$option];
} else {
$oldOptions[$option] = array();
}
setOption($i, true);
array_push($oldOptions[$option], $OPTIONS[$option]);
$OPTIONS[$option] = $oldOptions[$option];
break;
case '--debug':
$logLevel = LOG_LEVEL_DEBUG;
break;
case '--clean':
$action = ACTION_CLEAN;
break;
case '--fetch-demo':
$action = ACTION_FETCH_DEMO;
break;
case '--install-demo-data':
$action = ACTION_INSTALL_DEMO_DATA;
break;
case '--prepare-archive':
$action = ACTION_PREPARE_ARCHIVE;
break;
case '--install':
$action = ACTION_INSTALL;
break;
case '-h':
case '--help':
usage();
break;
default:
throw new InstallException('Unknown option ' .
$_SERVER['argv'][$i] . " \nUse --help for usage");
}
}
debug('Installer version ' . CW3_SETUP_REVISION . (isWin32() ? ' win32' : ''));
switch ($action) {
case ACTION_NOP:
info('No action given, doing nothing. Use --help to see usage');
exit(0);
break;
case ACTION_INSTALL:
info('installing');
fetchCartoWeb();
// sanity check
if (!file_exists('common'))
throw new InstallException('Looks like we are not inside a cartoweb3 directory');
removeDevFilesIfProd();
fetchProjects();
fetchLibs();
makeDirs();
setPermissions();
createConfig();
setupLinks();
replaceDotIn();
init();
removeInstallWarning();
launchProjectsFinalDeployScript();
info('Installation finished...');
break;
case ACTION_CLEAN:
cleanFiles();
break;
case ACTION_FETCH_DEMO:
info('Fetching demo');
fetchDemo();
info('Demo data installed');
// launch init() for running po2mo
init();
break;
case ACTION_INSTALL_DEMO_DATA:
installDemoData();
break;
case ACTION_PREPARE_ARCHIVE:
fetchLibs();
if (isset($OPTIONS['with-demo']))
fetchDemo();
break;
default:
fail('Should not happen');
exit(-1);
}
}
if (strpos($_SERVER['argv'][0], 'cw3setup.php') !== false) {
try {
checkMinimalVersions();
processArgs();
} catch (InstallException $e) {
showFailure($e);
}
}
function logMessage($level, $message) {
global $logLevel;
if ($level >= $logLevel) {
print "$message\n";
}
}
function debug($message) {
logMessage(LOG_LEVEL_DEBUG, $message);
}
function info($message) {
logMessage(LOG_LEVEL_INFO, $message);
}
function warn($message) {
logMessage(LOG_LEVEL_WARN, $message);
}
function fail($message) {
logMessage(LOG_LEVEL_FAIL, $message);
}
function showFailure(InstallException $exception) {
fail("\n Error during installation:");
fail(" ==========================\n");
fail('The installation process encountered an error and was aborted.');
fail('See the message below for an explanation of the problem.');
fail('If you want more information to find out the problem, try again with the --debug parameter.');
fail('If you think you found a bug in the installer or want support,');
fail('mail [email protected] with the full output');
fail('of the installer launched with the --debug parameter.');
fail("\n\nError message: {$exception->getMessage()}\n");
fail('Installation aborted');
exit(-1);
}
/**************************************************************/
/* Utility functions */
/**************************************************************/
/**
* Recursively remove
*/
function rmdirr($dir, $keepDirs = false) {
global $OPTIONS;
if (!isset($OPTIONS['clean-views'])) {
if ($dir == 'www-data/views') {
print("Skipping views delete\n");
return;
}
}
if (!isset($OPTIONS['clean-accounting'])) {
if ($dir == 'www-data/accounting') {
print("Skipping accounting delete\n");
return;
}
}
$dh = @opendir($dir);
if (!$dh)
return;
while ($file = readdir($dh)) {
if ($file == '.' || $file == '..')
continue;
$fullpath = $dir . '/' . $file;
if (!is_dir($fullpath) || is_link($fullpath))
unlink($fullpath);
else
rmdirr($fullpath, $keepDirs);
}
closedir($dh);
if (!$keepDirs)
@rmdir($dir);
}
/**
* rmdirr() if target exists
*/
function rmdirrIfExists($target) {
if (file_exists($target)) {
rmdirr($target);
}
}
/**
* Recursive copy
*/
function copyr($source, $dest) {
//debug("rec copy $source --> $dest");
// Simple copy for a file
if (is_file($source)) {
return copy($source, $dest);
}
// Make destination directory
if (!is_dir($dest)) {
mkdir($dest);
}
// Loop through the folder
$dir = @dir($source);
if (!$dir)
return false;
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Deep copy directories
if ($dest !== "$source/$entry") {
copyr("$source/$entry", "$dest/$entry");
}
}
// Clean up
$dir->close();
return true;
}
/**
* Throws an exception if copyr() failed
*/
function tryToCopyr($source, $target) {
if (!copyr($source, $target)) {
throw new InstallException("Failed copying $source => $target");
}
}
/**
* Crawls recursively a directory, and calls a callback function for each files/directories.
*/
function crawl($dir, $function, $context=null) {
if (!$dir)
return false;
$function($dir, $context);
$dh = @opendir($dir);
if (!$dh)
return false;
while ($file = readdir($dh)) {
if ($file == '.' || $file == '..')
continue;
$fullpath = "$dir/$file";
//$function($fullpath, $context);
if (is_dir($fullpath)) {
crawl($fullpath, $function, $context);
} else {
//print "handling file $fullpath \n";
$function($fullpath, $context);
}
}
closedir($dh);
}
/**************************************************************/
/**************************************************************/
function getCvsRoot() {
global $OPTIONS;
if (isset($OPTIONS['cvs-root']))
return $OPTIONS['cvs-root'];
throw new InstallException("\n\nThe official Cartoweb CVS repository has been deactivated on x.x.2011.\nPlease either use the --fetch-from-svn option or specify an alternate cvs repository using the --cvs-root option.\nUse --help for more informations on the options");
}
function getSvnRoot() {
global $OPTIONS;
if (isset($OPTIONS['svn-root']))
return $OPTIONS['svn-root'];
return 'https://project.camptocamp.com/svn/cartoweb3/trunk/';
}
function execWrapper($command, $quiet=false, $usePassThru=false) {
$output = '';
$status = 0;
debug("Executing command: $command");
if ($usePassThru) {
$output = array(); // no output returned
passthru($command, $status);
} else {
exec("$command 2>&1", $output, $status);
}
$output = implode("\n", $output);
if ($status)
throw new InstallException("Failure while launching \"$command\" (output is $output)");
if (!$quiet)
debug($output);
}
function removeDirectory($directory) {
global $OPTIONS;
if (!file_exists($directory))
return;
if (!isset($OPTIONS['delete-existing'])) {
throw new InstallException("Directory $directory already exists and " .
"should be overwritten. This script will not overwrite it " .
"unless you provide the --delete-existing option.");
}
info("removing directory $directory recursively");
rmdirr($directory);
}
/**
* Compare this script cvs revision, and the one from the just fetched CartoWeb, and tell the user
* to update of CartoWeb one is more recent.
*/
function checkCw3setupVersion() {
if (isset($_ENV['CW3_NO_VERSION_CHECK']))
return;
$content = file_get_contents('cartoweb3/cw3setup.php');
$revision_pattern = '/\$Rev.sion: ([\.\d]+) \$/';
preg_match($revision_pattern, $content, $matches);
if (!isset($matches[1]))
die("Unable to find Revision in cartoweb3/cw3setup.php\n");
// FIXME: This cvs revision comparison alghorithm is broken with branches
$cvs_revision = (int)(substr($matches[1], 2));
preg_match($revision_pattern, CW3_SETUP_REVISION, $matches);
$this_revision = (int)(substr($matches[1], 2));
debug("Installer revision of fetched cartoweb: $cvs_revision");
debug("Revision of this installer: $this_revision");
/*
if (defined('MINIMUM_REVISION')) {
if ($cvs_revision < MINIMUM_REVISION)
throw new InstallException('The version of CartoWeb you have just ' .
'retrieved is not compatible with the installer.');
}
*/
if ($cvs_revision > $this_revision)
throw new InstallException('The version of cw3setup.php that has just been ' .
'installed is more recent that the one you are currently running. ' .
'You MUST update cw3setup.php and try again.');
}
function fetchCartoWeb() {
global $OPTIONS;
if (!isset($OPTIONS['fetch-from-cvs']) && !isset($OPTIONS['fetch-from-dir']) && !isset($OPTIONS['fetch-from-svn']))
return;
if (isset($OPTIONS['install-location'])) {
$installLocation = $OPTIONS['install-location'];
if (!file_exists($installLocation))
throw new InstallException("install-location \"$installLocation\" is not accessible");
chdir($installLocation);
}
removeDirectory('cartoweb3');
if (isset($OPTIONS['fetch-from-cvs'])) {
info('fetching cartoweb from cvs');
$coOptions = '';
if (isset($OPTIONS['cartoweb-cvs-option'])) {
$coOptions = $OPTIONS['cartoweb-cvs-option'];
}
$cvsRoot = getCvsRoot();
if (!hasCommand('cvs --version'))
throw new InstallException("You need to install the cvs command " .
"to be able to fetch CartoWeb from cvs");
execWrapper("cvs -d $cvsRoot co $coOptions cartoweb3 2>&1");
} else if (isset($OPTIONS['fetch-from-svn'])) {
info('fetching cartoweb from svn');
$coOptions = '';
if (isset($OPTIONS['cartoweb-svn-option'])) {
$coOptions = $OPTIONS['cartoweb-svn-option'];
}
$svnRoot = getSvnRoot();
if (!hasCommand('svn --version'))
throw new InstallException("You need to install the svn command " .
"to be able to fetch CartoWeb from svn");
execWrapper("svn co $svnRoot cartoweb3 $coOptions 2>&1");
} else if (isset($OPTIONS['fetch-from-dir'])) {
$sourcePath = $OPTIONS['fetch-from-dir'];
if (!file_exists($sourcePath))
throw new InstallException("Source directory $sourcePath not found. " .
"Warning: paths are relative to cartoweb3 target directory");
info("Copying cartoweb from $sourcePath to cartoweb3");
copyr($sourcePath, 'cartoweb3');
} else {
throw new InstallException('Unhandled fetch type');
}
checkCw3setupVersion();
// if we were fetching cartoweb, get inside
if (file_exists('cartoweb3'))
chdir('cartoweb3');
}
function fetchProjects() {
/* The two types of project CVS layout:
* type 1) [cvs module]projectname/cartoweb3/projects/projectname/
* type 2) [cvs module]projectname/
*/
global $OPTIONS;
if (isset($OPTIONS['fetch-project-cvs'])) {
foreach($OPTIONS['fetch-project-cvs'] as $project) {
info("Fetching project $project from CVS");
removeDirectory("projects/$project");
// TODO: make this win32 portable
if (isWin32())
throw new InstallException('Sorry, project fetching from CVS is not supported on Win32 yet');
$cvsRoot = getCvsRoot();
execWrapper("cd projects ; cvs -d $cvsRoot co $project 2>&1");
if (file_exists("projects/$project/cartoweb3")) {
// type 1
execWrapper("mv projects/$project projects/{$project}.tmp");
execWrapper("mv projects/{$project}.tmp/cartoweb3/projects/$project projects");
rmdirr("projects/{$project}.tmp");
}
}
}
if (isset($OPTIONS['fetch-project-svn'])) {
foreach($OPTIONS['fetch-project-svn'] as $project) {
info("Fetching project $project from SVN");
removeDirectory("projects/$project");
// TODO: make this win32 portable
if (isWin32())
throw new InstallException('Sorry, project fetching from SVN is not supported on Win32 yet');
if (!isset($OPTIONS['svn-co-options']))
throw new InstallException("You need the --svn-co-options when fetching a project fron SVN");
$svnCoOptions = $OPTIONS['svn-co-options'];
execWrapper("cd projects ; svn $svnCoOptions $project 2>&1", false, true);
}
}
if (isset($OPTIONS['fetch-project-dir'])) {
foreach($OPTIONS['fetch-project-dir'] as $directory) {
info("Fetching project from directory $directory");
$project = basename($directory);
removeDirectory("projects/$project");
if (!file_exists($directory))
throw new InstallException("Source directory $directory not found");
info("Copying project from $directory to cartoweb3/projects");
if (file_exists("$directory/cartoweb3"))
// type 1
copyr("$directory/cartoweb3/projects/$project", "projects/$project");
else
// type 2
copyr("$directory", "projects/$project");
}
}
// launch project deploy script
$projects = getRequestedProjects();
if (empty($projects)) {
$projects = cw3setupGetProjects('projects');
}
foreach ($projects as $project) {
if (is_file("projects/$project/deployment/install.php")) {
$_ENV['project'] = $project;
info("Lanching project $project install script");
include("projects/$project/deployment/install.php");
}
}
if (isset($OPTIONS['default-project'])) {
setDefaultProject($OPTIONS['default-project']);
}
// Broken compatibity warning
foreach ($projects as $project) {
if (is_file("projects/$project/deployment/cw3_cvs_pin.txt")) {
throw new InstallException("Using the cw3_cvs_pin.txt is deprecated, " .
"you now have to use the --cartoweb-cvs-option" .
" parameter to have the same effect\n. You have to remove" .
"this file from the project to avoid this failure.");
}
}
}
function launchProjectsFinalDeployScript() {
// launch project deploy script
$projects = getRequestedProjects();
if (empty($projects)) {
$projects = cw3setupGetProjects('projects');
}
foreach ($projects as $project) {
if (is_file("projects/$project/deployment/install_final.php")) {
$_ENV['project'] = $project;
info("Lanching project $project final install script");
include("projects/$project/deployment/install_final.php");
}
}
}
function hasCommand($command) {
try {
execWrapper("$command", true);
} catch (Exception $e) {
return false;
}
return true;
}
function fetchArchive($archiveUrl, $targetDirectory) {
$versionRegexp = '/-([^-]*).tar.gz$/';
if (preg_match($versionRegexp, $archiveUrl, $matches) == 1) {
$version = $matches[1];
debug("Archive upstream version: $version");
}
$versionFile = "$targetDirectory/version";
if ($version && file_exists($versionFile)) {
$actualVersion = file_get_contents($versionFile);
$actualVersion = trim($actualVersion);
debug("actual version: $actualVersion");
if ($actualVersion == $version) {
debug("Archive version match, skipping");
return;
} else {
// TODO: remove old archive files
// rmdirr(archive_output_directory);
}
}
$destFile = dirname($targetDirectory) . "/archive_tmp";
if (hasCommand('tar --help')) {
$destFile .= '.tar.gz';
$extractCmd = "tar xzf " . basename($destFile);
} else if (hasCommand('unzip')) {
$archiveUrl = str_replace('.tar.gz', '.zip', $archiveUrl);
$destFile .= '.zip';
$extractCmd = "unzip " . basename($destFile);
} else {
throw new InstallException("Can't find a program to extract the include files. " .
"Install tar or unzip, and be sure it is on your path");
}
debug("Dest file $destFile");
info("Fetching archive file from $archiveUrl, this may take some time...");
if (extension_loaded('curl')) {
debug('Fetching archive using curl extension');
$ch = curl_init($archiveUrl);
$fp = fopen($destFile, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
} else {
debug('Fetching archive with file_get_contents');
$cnt = file_get_contents($archiveUrl);
if ($cnt === false)
throw new InstallException('Unable to retrieve the archive at ' . $archiveUrl);
$fd = fopen($destFile, "wb");
fwrite($fd, $cnt);