forked from NatLibFi/RecordManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdline.php
More file actions
141 lines (133 loc) · 3.82 KB
/
cmdline.php
File metadata and controls
141 lines (133 loc) · 3.82 KB
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
<?php
/**
* Command Line Utility Functions
*
* PHP version 5
*
* Copyright (C) Patrick Fisher 2009
* Copyright (C) The National Library of Finland 2011-2013.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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
*
* @category DataManagement
* @package RecordManager
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://github.com/KDK-Alli/RecordManager
*/
/**
* Command Line Utility Functions
*
* Helper functions for command line utilities.
*/
ini_set('display_errors', '1');
// Initialize command line environment
$basePath = substr(__FILE__, 0, strrpos(__FILE__, DIRECTORY_SEPARATOR));
require_once 'classes/RecordManager.php';
$configArray = parse_ini_file($basePath . '/conf/recordmanager.ini', true);
/**
* Apply any configuration overrides defined on command line
*
* @param array $params Command line parameters
*
* @return void
*/
function applyConfigOverrides($params)
{
global $configArray;
foreach ($params as $key => $value) {
$setting = explode('.', $key);
if ($setting[0] == 'config') {
$configArray[$setting[1]][$setting[2]] = $value;
}
}
}
/**
* Command Line Interface (CLI) utility function.
*
* @param string[] $argv Arguments
*
* @return string[] Parsed keys and values
* @usage $args = parseArgs($_SERVER['argv']);
* @author Patrick Fisher <patrick@pwfisher.com>
* @source https://github.com/pwfisher/CommandLine.php
*/
function parseArgs($argv)
{
array_shift($argv);
$params = array();
foreach ($argv as $arg) {
if (substr($arg, 0, 2) == '--') {
$eqPos = strpos($arg, '=');
if ($eqPos === false) {
$key = substr($arg, 2);
$params[$key] = isset($params[$key]) ? $params[$key] : true;
} else {
$key = substr($arg, 2, $eqPos - 2);
$params[$key] = substr($arg, $eqPos + 1);
}
} else if (substr($arg, 0, 1) == '-') {
if (substr($arg, 2, 1) == '=') {
$key = substr($arg, 1, 1);
$params[$key] = substr($arg, 3);
} else {
$chars = str_split(substr($arg, 1));
foreach ($chars as $char) {
$key = $char;
$params[$key] = isset($params[$key]) ? $params[$key] : true;
}
}
} else {
$params[] = $arg;
}
}
return $params;
}
/**
* Try to acquire a lock on a lock file
*
* @param string $lockfile Lock file
*
* @return handle|bool|null Returns file handle on success, null if no lock was
* required or false on failure
*/
function acquireLock($lockfile)
{
if (empty($lockfile)) {
return null;
}
$handle = fopen($lockfile, 'c+');
if (!is_resource($handle)) {
return false;
}
if (!flock($handle, LOCK_EX | LOCK_NB)) {
fclose($handle);
return false;
}
return $handle;
}
/**
* Release a lock on a lock file
*
* @param handle $handle Lock file handle
*
* @return void
*/
function releaseLock($handle)
{
if ($handle) {
flock($handle, LOCK_UN);
fclose($handle);
}
}