Skip to content

Instantly share code, notes, and snippets.

@lrealdi
Last active September 13, 2018 00:18
Show Gist options
  • Select an option

  • Save lrealdi/9bec206033ebb7be3f1c419ef597463e to your computer and use it in GitHub Desktop.

Select an option

Save lrealdi/9bec206033ebb7be3f1c419ef597463e to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
$arguments = $GLOBALS['argv'];
$program = $arguments[0];
array_shift($arguments);
$home = getenv("HOME");
$configFile = "$home/.opencensimento";
if (!file_exists($configFile)) {
fputs(STDERR, "\033[1;33m" . "\nFile $configFile non trovato\n\n" . "\033[0;39m");
fputs(STDERR, "Devi creare il file $configFile con contenuto:\n\n");
fputs(STDERR, "this.server.name=(e.g. nginx) \n");
fputs(STDERR, "dev.support.login=(dev.support username) \n");
fputs(STDERR, "dev.support.password=(dev.support password) \n\n");
exit(1);
} else {
$config = array();
$configData = file_get_contents($configFile);
$rows = explode("\n", $configData);
foreach ($rows as $row) {
if (strpos($row, '=') !== false) {
list($name, $value) = explode('=', $row);
$name = trim($name);
if (!empty($name)) {
$config[$name] = trim($value);
}
}
}
}
$documentRoot = rtrim($arguments[0], '/');
$isEzSite = false;
if (file_exists("$documentRoot/lib/version.php")) {
$isEzSite = true;
} elseif (file_exists("$documentRoot/ezpublish_legacy/lib/version.php")) {
$documentRoot = "$documentRoot/ezpublish_legacy";
$isEzSite = true;
} elseif (file_exists("$documentRoot/html/lib/version.php")) {
$documentRoot = "$documentRoot/html";
$isEzSite = true;
} elseif (file_exists("$documentRoot/html/ezpublish_legacy/lib/version.php")) {
$documentRoot = "$documentRoot/html/ezpublish_legacy";
$isEzSite = true;
}
if (!$isEzSite) {
if($documentRoot){
fputs(STDERR, "\033[1;33m" . "Document root non trovata in $documentRoot\n" . "\033[0;39m");
}else{
fputs(STDERR, "\033[1;33m" . "Argomento non passato: php opencensimento.php path/to/document/root\n" . "\033[0;39m");
}
exit(1);
}
class DevSupportClient
{
protected $server = 'http://dev.support.opencontent.it';
protected $login;
protected $password;
protected $proxy;
protected $proxyPort;
protected $proxyLogin;
protected $proxyPassword;
protected $proxyAuthType;
protected $apiEnvironmentPreset = 'content';
protected $apiEndPointBase = '/api/opendata/v2';
public static $connectionTimeout = 60;
public static $processTimeout = 60;
public function __construct($login, $password)
{
$this->login = $login;
$this->password = $password;
}
public function setProxy(
$proxy,
$proxyPort,
$proxyLogin = null,
$proxyPassword = null,
$proxyAuthType = 1
) {
$this->proxy = $proxy;
$this->proxyPort = $proxyPort;
$this->proxyLogin = $proxyLogin;
$this->proxyPassword = $proxyPassword;
$this->proxyAuthType = $proxyAuthType;
}
/**
* @param $data
*
* @return mixed
* @throws Exception
*/
public function create($data)
{
return $this->request('POST', '/create', json_encode($data));
}
/**
* @param $data
*
* @return mixed
* @throws Exception
*/
public function createUpdate($data)
{
try {
$result = $this->create($data);
} catch (\Exception $e) {
$result = $this->update($data);
}
return $result;
}
/**
* @param $id
*
* @return mixed
* @throws Exception
*/
public function read($id)
{
return $this->request('GET', '/read/' . $id);
}
/**
* @param $data
*
* @return mixed
* @throws Exception
*/
public function update($data)
{
return $this->request('POST', '/update', json_encode($data));
}
/**
* @param $method
* @param $path
* @param null $data
*
* @return mixed
* @throws Exception
*/
public function request($method, $path, $data = null)
{
$url = $this->server . $this->apiEndPointBase . '/' . $this->apiEnvironmentPreset . $path;
$headers = array();
if ($this->login && $this->password) {
$credentials = "{$this->login}:{$this->password}";
$headers[] = "Authorization: Basic " . base64_encode($credentials);
}
$ch = curl_init();
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
}
if ($data !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($data);
}
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectionTimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$processTimeout);
if ($this->proxy !== null) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy . ':' . $this->proxyPort);
if ($this->proxyLogin !== null) {
curl_setopt(
$ch,
CURLOPT_PROXYUSERPWD,
$this->proxyLogin . ':' . $this->proxyPassword
);
curl_setopt($ch, CURLOPT_PROXYAUTH, $this->proxyAuthType);
}
}
$data = curl_exec($ch);
if ($data === false) {
$errorCode = curl_errno($ch) * -1;
$errorMessage = curl_error($ch);
curl_close($ch);
throw new \Exception($errorMessage, $errorCode);
}
$info = curl_getinfo($ch);
curl_close($ch);
$body = substr($data, -$info['download_content_length']);
return $this->parseResponse($info, $body);
}
/**
* @param $info
* @param $body
*
* @return mixed
* @throws Exception
*/
protected function parseResponse($info, $body)
{
$data = json_decode($body);
if (isset($data->error_message)) {
$errorMessage = '';
if (isset($data->error_type)) {
$errorMessage = $data->error_type . ': ';
}
$errorMessage .= $data->error_message;
throw new \Exception($errorMessage);
}
if ($info['http_code'] == 401) {
throw new \Exception("Authorization Required");
}
if (!in_array($info['http_code'], array(100, 200, 201, 202))) {
throw new \Exception("Unknown error");
}
$data = json_decode($body, true);
return $data;
}
}
chdir($documentRoot);
require_once 'autoload.php';
$cli = eZCLI::instance();
$GLOBALS['eZCurrentAccess'] = null;
$rootDir = eZSys::rootDir();
$list = array();
$siteAccessList = eZSiteAccess::siteAccessList();
$count = count($siteAccessList);
if ($count > 0){
$output = new ezcConsoleOutput();
$progressBarOptions = array('emptyChar' => ' ', 'barChar' => '=');
$progressBar = new ezcConsoleProgressbar($output, $count, $progressBarOptions);
$cli->output("Leggo la lista dei siteaccess in $rootDir");
$progressBar->start();
foreach ($siteAccessList as $siteAccess) {
//$cli->output($siteAccess['name']);
$siteINI = eZSiteAccess::getIni($siteAccess['name']);
$solrINI = eZSiteAccess::getIni($siteAccess['name'], 'solr.ini');
$dataBaseSettings = $siteINI->group('DatabaseSettings');
$dbImplementation = $dataBaseSettings['DatabaseImplementation'];
$dbServer = $dataBaseSettings['Server'];
$dbName = $dataBaseSettings['Database'];
$uniqueIdentifier = implode('#', array($rootDir, $dbImplementation, $dbServer, $dbName));
if (!isset($list[$uniqueIdentifier])) {
$list[$uniqueIdentifier] = array();
}
$designList = array(
$siteINI->variable('DesignSettings', 'SiteDesign')
);
$designList = array_merge(
$designList,
$siteINI->variable('DesignSettings', 'AdditionalSiteDesignList')
);
$list[$uniqueIdentifier]['siteaccess_list'][] = array(
'siteaccess' => $siteAccess['name'],
'uri' => $siteINI->variable('SiteSettings', 'SiteURL'),
'design_settings' => implode(', ', $designList),
'name' => $siteINI->variable('SiteSettings', 'SiteName'),
);
$list[$uniqueIdentifier]['solr'][] = $solrINI->variable('SolrBase', 'SearchServerURI');
$list[$uniqueIdentifier]['solr'] = array_unique($list[$uniqueIdentifier]['solr']);
$progressBar->advance();
}
$progressBar->finish();
$cli->output();
$cli->output();
$client = new DevSupportClient($config['dev.support.login'], $config['dev.support.password']);
$countData = count($list);
$progressBarOptions = array('emptyChar' => ' ', 'barChar' => '=');
$progressBar = new ezcConsoleProgressbar($output, $countData, $progressBarOptions);
$cli->output("Invio i dati di $rootDir a dev.support...");
$progressBar->start();
foreach ($list as $uniqueIdentifier => $item) {
list($rootDir, $dbImplementation, $dbServer, $dbName) = explode('#', $uniqueIdentifier);
$payload = array(
'metadata' => array(
'remoteId' => $config['this.server.name'] . '_' . md5($uniqueIdentifier),
'classIdentifier' => 'ez_instance',
'parentNodes' => array('ez_instance_root'),
'languages' => array('ita-IT'),
),
'data' => array(
'ita-IT' => array(
'siteaccess' => $item['siteaccess_list'],
'web_server' => $config['this.server.name'],
'document_root' => $rootDir,
'database_server' => $dbServer,
'database_name' => $dbName,
'database_implementation' => $dbImplementation,
'search_server_uri' => implode(', ', $item['solr']),
)
)
);
try {
$client->createUpdate($payload);
} catch (Exception $e) {
fputs(STDERR, "Error: " . $e->getMessage() . "\n\n");
exit(1);
}
$progressBar->advance();
}
$progressBar->finish();
}
$cli->output();
@lrealdi
Copy link
Author

lrealdi commented Sep 12, 2018

find . -maxdepth 1 -type d -exec bash -c 'php opencensimento.php "$0"' {} \;

@lrealdi
Copy link
Author

lrealdi commented Sep 12, 2018

find . -maxdepth 1 -type d -exec bash -c 'cd "$0" && pwd' {} \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment