forked from JimmyBillan/Geojson-To-Gwern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostgresToGwern.php
85 lines (74 loc) · 2.44 KB
/
PostgresToGwern.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
<?php
namespace POSTGRESTOGWERN\PostgresToGwern;
include 'zipFolder.php';
/**
* Permet de generer un fichier SHP avec ses données attributaires
*/
class PostgresToGwern{
private $pg_host;
private $pg_userName;
private $pg_password;
private $tempDestinationFolder;
private $destinationFolder;
private $globalTmp = "tmp";
/**
* Instanciation de PostgresToGwern, permet de générer un fichier SHP
*
* @param string $host
* @param string $username
* @param string $password
* @param string $destinationFolder chemin où sera sauvegarder le zip contenant de le shp
*/
public function __construct(string $host,string $username,string $password, string $destinationFolder){
$this->pg_host = $host;
$this->pg_userName = $username;
$this->pg_password = $password;
$this->destinationFolder = $destinationFolder;
}
/**
* Execute la requete passée en paramètre et génère un ZIP contenant le SHP.
*
*
* @param string $filename
* @param string $dbname
* @param string $queryString
* @return void
*/
public function query(string $filename,string $dbname, string $queryString){
if(self::isFnameValid($filename)){
$uniqueName = $this->generateUniqueName($filename);
$tempFolder = "$this->globalTmp/$uniqueName";
mkdir($tempFolder);
exec("cd $tempFolder && pgsql2shp -f '$filename' -k -h $this->pg_host -u $this->pg_userName -P $this->pg_password $dbname $queryString ");
$ZF = new zipFolder($tempFolder);
rename("$tempFolder.zip", "$this->destinationFolder/$uniqueName.zip");
}
return false;
}
public function setGlobalTmp(string $foldername){
$this->globalTmp($foldername);
}
/**
* Genere un nom unique.
*
* @param [type] $filename
* @return string
*/
private function generateUniqueName($filename):string{
return $filename."_".\uniqid()."_".time();
}
/**
* Verifie qu'une string peut être un nom de fichier valide,( lettre chiffre _ - de 3 à 60 char)
*
* @param string $fname
* @return boolean
*/
public static function isFnameValid(string $fname):bool{
$t = preg_match('/^[a-z-A-Z0-9_-]{3,60}$/', $fname, $matches);
if($t) {
return true;
} else {
return false;
}
}
}