11#!/usr/bin/env php
22<?php
3- require ('/www/lizmap/vendor/autoload.php ' );
4- use \Jelix \IniFile \IniModifier ;
3+
4+ require '/www/lizmap/vendor/autoload.php ' ;
5+ use Jelix \IniFile \IniModifier ;
56
67/**
7- * @param $varname
88 * @param IniModifier $iniFileModifier
9- * @return void
109 */
1110function load_include_config ($ varname , $ iniFileModifier )
1211{
1312 $ includeConfigDir = getenv ($ varname );
1413 if ($ includeConfigDir !== false and is_dir ($ includeConfigDir )) {
15- echo ( " Checking for lizmap configuration files in " .$ includeConfigDir ."\n" ) ;
16- foreach (glob (rtrim ($ includeConfigDir ," / " ). " /*.ini.php " ) as $ includeFile ) {
17- echo ( " * Loading lizmap configuration: " .$ includeFile ."\n" ) ;
14+ echo ' Checking for lizmap configuration files in ' .$ includeConfigDir ."\n" ;
15+ foreach (glob (rtrim ($ includeConfigDir , ' / ' ). ' /*.ini.php ' ) as $ includeFile ) {
16+ echo ' * Loading lizmap configuration: ' .$ includeFile ."\n" ;
1817 $ includeConfig = new IniModifier ($ includeFile );
1918 $ iniFileModifier ->import ($ includeConfig );
2019 }
@@ -29,29 +28,31 @@ function load_include_config($varname, $iniFileModifier)
2928}
3029
3130/**
32- * connect to Postgresql with the given profile
31+ * connect to Postgresql with the given profile.
32+ *
3333 * @param array
34+ * @param mixed $profile
3435 */
35- function pgSqlConnect ($ profile )
36+ function pgSqlConnect ($ profile )
3637{
3738 $ str = '' ;
3839
3940 // Service is PostgreSQL way to store credentials in a file :
4041 // http://www.postgresql.org/docs/9.1/static/libpq-pgservice.html
4142 // If given, no need to add host, user, database, port and password
42- if (isset ($ profile ['service ' ]) && $ profile ['service ' ] != '' ){
43+ if (isset ($ profile ['service ' ]) && $ profile ['service ' ] != '' ) {
4344 $ str = 'service= \'' .$ profile ['service ' ].'\'' .$ str ;
4445
4546 // Database name may be given, even if service is used
4647 // dbname should not be mandatory in service file
4748 if (isset ($ profile ['database ' ]) && $ profile ['database ' ] != '' ) {
4849 $ str .= ' dbname= \'' .$ profile ['database ' ].'\'' ;
4950 }
50- }
51- else {
51+ } else {
5252 // we do a distinction because if the host is given == TCP/IP connection else unix socket
53- if ($ profile ['host ' ] != '' )
53+ if ($ profile ['host ' ] != '' ) {
5454 $ str = 'host= \'' .$ profile ['host ' ].'\'' .$ str ;
55+ }
5556
5657 if (isset ($ profile ['port ' ])) {
5758 $ str .= ' port= \'' .$ profile ['port ' ].'\'' ;
@@ -82,29 +83,33 @@ function pgSqlConnect ($profile)
8283
8384 if (isset ($ profile ['force_new ' ]) && $ profile ['force_new ' ]) {
8485 $ cnx = pg_connect ($ str , PGSQL_CONNECT_FORCE_NEW );
85- }
86- else {
86+ } else {
8787 $ cnx = pg_connect ($ str );
8888 }
8989
9090 // let's do the connection
9191 if ($ cnx ) {
9292 pg_close ($ cnx );
93+
9394 return true ;
9495 }
96+
9597 return false ;
9698}
9799
98100/**
99101 * Try to connect to the postgresql database.
100102 *
101103 * @param IniModifier $profilesConfig
102- * @param string $profileName The profile to use
103- * @param int $nbRetries
104+ * @param string $profileName The profile to use
105+ * @param int $nbRetries
106+ * @param mixed $wait
107+ *
104108 * @return bool
109+ *
105110 * @throws Exception
106111 */
107- function checkAndWaitPostgresql ($ profilesConfig , $ profileName , $ nbRetries= 10 , $ wait= 2 )
112+ function checkAndWaitPostgresql ($ profilesConfig , $ profileName , $ nbRetries = 10 , $ wait = 2 )
108113{
109114 $ origProfileName = $ profileName ;
110115 $ profileAlias = $ profilesConfig ->getValue ($ profileName , 'jdb ' );
@@ -113,17 +118,18 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
113118 }
114119 $ profile = $ profilesConfig ->getValues ('jdb: ' .$ profileName );
115120 if ($ profile === null ) {
116- throw new Exception ("Database profile jdb: $ profileName not found " );
121+ throw new Exception ("Database profile jdb: { $ profileName} not found " );
117122 }
118123 $ profile = (new jDbParameters ($ profile ))->getParameters ();
119124 if ($ profile ['driver ' ] != 'pgsql ' ) {
120125 return true ;
121126 }
122127 $ profile ['timeout ' ] = 30 ;
123- echo " trying to connect to the Postgresql database " .$ profile ['database ' ]." at " .$ profile ['host ' ]." with the profile $ origProfileName... \n" ;
124- for ($ i= 0 ; $ i < $ nbRetries ; $ i ++ ) {
128+ echo ' trying to connect to the Postgresql database ' .$ profile ['database ' ].' at ' .$ profile ['host ' ]." with the profile { $ origProfileName} ... \n" ;
129+ for ($ i = 0 ; $ i < $ nbRetries ; ++ $ i ) {
125130 if (pgSqlConnect ($ profile )) {
126131 echo " Ok, Postgresql is alive. \n" ;
132+
127133 return true ;
128134 }
129135 // if there is nothing on the host/port, pg_connect fails immediately,
@@ -134,28 +140,29 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
134140 }
135141 echo "Error: cannot connect to the Postgresql database. \n" ;
136142 echo "Lizmap cannot starts. \n" ;
143+
137144 return false ;
138145}
139146
140147/**
141- * lizmapConfig.ini.php
148+ * lizmapConfig.ini.php.
142149 */
143- $ lizmapConfig = new \ Jelix \ IniFile \ IniModifier ('/www/lizmap/var/config/lizmapConfig.ini.php ' );
150+ $ lizmapConfig = new IniModifier ('/www/lizmap/var/config/lizmapConfig.ini.php ' );
144151
145152$ lizmapConfig ->setValue ('wmsServerURL ' , getenv ('LIZMAP_WMSSERVERURL ' ), 'services ' );
146153$ lizmapConfig ->setValue ('lizmapPluginAPIURL ' , getenv ('LIZMAP_LIZMAPPLUGINAPIURL ' ), 'services ' );
147154$ lizmapConfig ->setValue ('rootRepositories ' , getenv ('LIZMAP_ROOT_REPOSITORIES ' ), 'services ' );
148155$ lizmapConfig ->setValue ('relativeWMSPath ' , true , 'services ' );
149156
150- foreach (array (
151- 'cacheExpiration ' => 'LIZMAP_CACHEEXPIRATION ' ,
152- 'debugMode ' => 'LIZMAP_DEBUGMODE ' ,
153- 'cacheStorageType ' => 'LIZMAP_CACHESTORAGETYPE ' ,
154- 'cacheRedisDb ' => 'LIZMAP_CACHEREDISDB ' ,
155- 'cacheRedisKeyPrefix ' => 'LIZMAP_CACHEREDISKEYPREFIX ' ,
156- 'cacheRedisHost ' => 'LIZMAP_CACHEREDISHOST ' ,
157- 'cacheRedisPort ' => 'LIZMAP_CACHEREDISPORT ' ,
158- ) as $ key => $ envValue
157+ foreach (array (
158+ 'cacheExpiration ' => 'LIZMAP_CACHEEXPIRATION ' ,
159+ 'debugMode ' => 'LIZMAP_DEBUGMODE ' ,
160+ 'cacheStorageType ' => 'LIZMAP_CACHESTORAGETYPE ' ,
161+ 'cacheRedisDb ' => 'LIZMAP_CACHEREDISDB ' ,
162+ 'cacheRedisKeyPrefix ' => 'LIZMAP_CACHEREDISKEYPREFIX ' ,
163+ 'cacheRedisHost ' => 'LIZMAP_CACHEREDISHOST ' ,
164+ 'cacheRedisPort ' => 'LIZMAP_CACHEREDISPORT ' ,
165+ ) as $ key => $ envValue
159166) {
160167 if (getenv ($ envValue ) !== false ) {
161168 $ lizmapConfig ->setValue ($ key , getenv ($ envValue ), 'services ' );
@@ -174,10 +181,10 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
174181$ lizmapConfig ->save ();
175182
176183/**
177- * localconfig.ini.php
184+ * localconfig.ini.php.
178185 */
179- $ localConfig = new \ Jelix \ IniFile \ IniModifier ('/www/lizmap/var/config/localconfig.ini.php ' );
180- $ mainConfig = new \ Jelix \ IniFile \ IniModifier ('/www/lizmap/app/system/mainconfig.ini.php ' );
186+ $ localConfig = new IniModifier ('/www/lizmap/var/config/localconfig.ini.php ' );
187+ $ mainConfig = new IniModifier ('/www/lizmap/app/system/mainconfig.ini.php ' );
181188
182189// Let's modify the install configuration of jcommunity, to not create a default
183190// admin account (no `defaultusers` parameter). We're relying on
@@ -186,7 +193,6 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
186193unset($ jCommunityInstallParams ['defaultusers ' ]);
187194$ localConfig ->setValue ('jcommunity.installparam ' , $ jCommunityInstallParams , 'modules ' );
188195
189-
190196if ($ logger_metric !== false ) {
191197 $ localConfig ->setValue ('metric ' , $ logger_metric , 'logger ' );
192198}
@@ -201,8 +207,7 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
201207 if (getenv ('LIZMAP_PROXYURL_HTTPS_PORT ' ) !== false ) {
202208 $ config ['forceHTTPSPort ' ] = getenv ('LIZMAP_PROXYURL_HTTPS_PORT ' );
203209 $ localConfig ->setValue ('forceHTTPSPort ' , getenv ('LIZMAP_PROXYURL_HTTPS_PORT ' ));
204- }
205- else {
210+ } else {
206211 $ localConfig ->setValue ('forceHTTPSPort ' , 443 );
207212 }
208213}
@@ -236,7 +241,7 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
236241$ localConfig ->save ();
237242
238243/**
239- * profiles.ini.php
244+ * profiles.ini.php.
240245 */
241246$ profilesConfig = new IniModifier ('/www/lizmap/var/config/profiles.ini.php ' );
242247
@@ -260,9 +265,9 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries=10, $w
260265// its table and so on.
261266// Try with the default profile
262267if (!checkAndWaitPostgresql ($ profilesConfig , 'default ' , $ retries , $ waits )) {
263- exit (1 );
268+ exit (1 );
264269}
265270// try with the lizlog profile, it may be a different database.
266271if (!checkAndWaitPostgresql ($ profilesConfig , 'lizlog ' , $ retries , $ waits )) {
267- exit (1 );
272+ exit (1 );
268273}
0 commit comments