1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* Selector that chooses files based on their last modified date. Ant uses
|
22
|
|
* millisecond precision (thanks to Java); PHP is forced to use only seconds
|
23
|
|
* precision.
|
24
|
|
*
|
25
|
|
* @author Hans Lellelid <hans@xmpl.org> (Phing)
|
26
|
|
* @author Bruce Atherton <bruce@callenish.com> (Ant)
|
27
|
|
* @package phing.types.selectors
|
28
|
|
*/
|
29
|
|
class DateSelector extends BaseExtendSelector
|
30
|
|
{
|
31
|
|
private $seconds = -1; // millis in Ant, but PHP doesn't support that level of precision
|
32
|
|
private $dateTime = null;
|
33
|
|
private $includeDirs = false;
|
34
|
|
private $granularity = 0;
|
35
|
|
private $cmp = 2;
|
36
|
|
public const MILLIS_KEY = "millis";
|
37
|
|
public const DATETIME_KEY = "datetime";
|
38
|
|
public const CHECKDIRS_KEY = "checkdirs";
|
39
|
|
public const GRANULARITY_KEY = "granularity";
|
40
|
|
public const WHEN_KEY = "when";
|
41
|
|
private static $timeComparisons = ["before", "after", "equal"];
|
42
|
|
|
43
|
|
/**
|
44
|
|
*
|
45
|
|
*/
|
46
|
1
|
public function __construct()
|
47
|
|
{
|
48
|
1
|
parent::__construct();
|
49
|
|
//if (Os.isFamily("dos")) {
|
50
|
|
// granularity = 2000;
|
51
|
|
//}
|
52
|
|
}
|
53
|
|
|
54
|
|
/**
|
55
|
|
* @return string
|
56
|
|
*/
|
57
|
0
|
public function __toString()
|
58
|
|
{
|
59
|
0
|
$buf = "{dateselector date: ";
|
60
|
0
|
$buf .= $this->dateTime;
|
61
|
0
|
$buf .= " compare: ";
|
62
|
0
|
if ($this->cmp === 0) {
|
63
|
0
|
$buf .= "before";
|
64
|
0
|
} elseif ($this->cmp === 1) {
|
65
|
0
|
$buf .= "after";
|
66
|
|
} else {
|
67
|
0
|
$buf .= "equal";
|
68
|
|
}
|
69
|
0
|
$buf .= " granularity: ";
|
70
|
0
|
$buf .= $this->granularity;
|
71
|
0
|
$buf .= "}";
|
72
|
|
|
73
|
0
|
return $buf;
|
74
|
|
}
|
75
|
|
|
76
|
|
/**
|
77
|
|
* For users that prefer to express time in seconds since 1970
|
78
|
|
*
|
79
|
|
* @param int $seconds the time to compare file's last modified date to,
|
80
|
|
* expressed in seconds
|
81
|
|
*/
|
82
|
1
|
public function setSeconds($seconds)
|
83
|
|
{
|
84
|
1
|
$this->seconds = (int) $seconds;
|
85
|
|
}
|
86
|
|
|
87
|
|
/**
|
88
|
|
* Returns the seconds value the selector is set for.
|
89
|
|
*/
|
90
|
0
|
public function getSeconds()
|
91
|
|
{
|
92
|
0
|
return $this->seconds;
|
93
|
|
}
|
94
|
|
|
95
|
|
/**
|
96
|
|
* @param int $millis the time to compare file's last modified date to, expressed in milliseconds
|
97
|
|
*/
|
98
|
0
|
public function setMillis($millis)
|
99
|
|
{
|
100
|
0
|
$this->setSeconds((int) $millis * 1000);
|
101
|
|
}
|
102
|
|
|
103
|
|
/**
|
104
|
|
* Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM
|
105
|
|
* format
|
106
|
|
*
|
107
|
|
* @param string $dateTime a string in MM/DD/YYYY HH:MM AM_PM format
|
108
|
|
*/
|
109
|
1
|
public function setDatetime($dateTime)
|
110
|
|
{
|
111
|
1
|
$dt = strtotime($dateTime);
|
112
|
1
|
if ($dt == -1) {
|
113
|
0
|
$this->setError(
|
114
|
0
|
"Date of " . $dateTime
|
115
|
|
. " Cannot be parsed correctly. It should be in"
|
116
|
0
|
. " a format parsable by PHP's strtotime() function."
|
117
|
|
);
|
118
|
|
} else {
|
119
|
1
|
$this->dateTime = $dateTime;
|
120
|
1
|
$this->setSeconds($dt);
|
121
|
|
}
|
122
|
|
}
|
123
|
|
|
124
|
|
/**
|
125
|
|
* Should we be checking dates on directories?
|
126
|
|
*
|
127
|
|
* @param boolean $includeDirs whether to check the timestamp on directories
|
128
|
|
*/
|
129
|
0
|
public function setCheckdirs($includeDirs)
|
130
|
|
{
|
131
|
0
|
$this->includeDirs = (bool) $includeDirs;
|
132
|
|
}
|
133
|
|
|
134
|
|
/**
|
135
|
|
* Sets the number of milliseconds leeway we will give before we consider
|
136
|
|
* a file not to have matched a date.
|
137
|
|
*
|
138
|
|
* @param int $granularity
|
139
|
|
*/
|
140
|
0
|
public function setGranularity($granularity)
|
141
|
|
{
|
142
|
0
|
$this->granularity = (int) $granularity;
|
143
|
|
}
|
144
|
|
|
145
|
|
/**
|
146
|
|
* Sets the type of comparison to be done on the file's last modified
|
147
|
|
* date.
|
148
|
|
*
|
149
|
|
* @param string $cmp The comparison to perform
|
150
|
|
*/
|
151
|
1
|
public function setWhen($cmp)
|
152
|
|
{
|
153
|
1
|
$idx = array_search($cmp, self::$timeComparisons, true);
|
154
|
1
|
if ($idx === null) {
|
155
|
0
|
$this->setError("Invalid value for " . self::WHEN_KEY . ": " . $cmp);
|
156
|
|
} else {
|
157
|
1
|
$this->cmp = $idx;
|
158
|
|
}
|
159
|
|
}
|
160
|
|
|
161
|
|
/**
|
162
|
|
* When using this as a custom selector, this method will be called.
|
163
|
|
* It translates each parameter into the appropriate setXXX() call.
|
164
|
|
*
|
165
|
|
* @param array $parameters the complete set of parameters for this selector
|
166
|
|
* @return mixed|void
|
167
|
|
*/
|
168
|
0
|
public function setParameters(array $parameters): void
|
169
|
|
{
|
170
|
0
|
parent::setParameters($parameters);
|
171
|
0
|
if ($parameters !== null) {
|
172
|
0
|
for ($i = 0, $size = count($parameters); $i < $size; $i++) {
|
173
|
0
|
$paramname = $parameters[$i]->getName();
|
174
|
0
|
switch (strtolower($paramname)) {
|
175
|
|
case self::MILLIS_KEY:
|
176
|
0
|
$this->setMillis($parameters[$i]->getValue());
|
177
|
0
|
break;
|
178
|
|
case self::DATETIME_KEY:
|
179
|
0
|
$this->setDatetime($parameters[$i]->getValue());
|
180
|
0
|
break;
|
181
|
|
case self::CHECKDIRS_KEY:
|
182
|
0
|
$this->setCheckdirs($parameters[$i]->getValue());
|
183
|
0
|
break;
|
184
|
|
case self::GRANULARITY_KEY:
|
185
|
0
|
$this->setGranularity($parameters[$i]->getValue());
|
186
|
0
|
break;
|
187
|
|
case self::WHEN_KEY:
|
188
|
0
|
$this->setWhen($parameters[$i]->getValue());
|
189
|
0
|
break;
|
190
|
|
default:
|
191
|
0
|
$this->setError("Invalid parameter " . $paramname);
|
192
|
|
} // switch
|
193
|
|
}
|
194
|
|
}
|
195
|
|
}
|
196
|
|
|
197
|
|
/**
|
198
|
|
* This is a consistency check to ensure the selector's required
|
199
|
|
* values have been set.
|
200
|
|
*/
|
201
|
1
|
public function verifySettings()
|
202
|
|
{
|
203
|
1
|
if ($this->dateTime === null && $this->seconds < 0) {
|
204
|
0
|
$this->setError(
|
205
|
|
"You must provide a datetime or the number of "
|
206
|
0
|
. "seconds."
|
207
|
|
);
|
208
|
1
|
} elseif ($this->seconds < 0) {
|
209
|
0
|
$this->setError(
|
210
|
0
|
"Date of " . $this->dateTime
|
211
|
|
. " results in negative seconds"
|
212
|
0
|
. " value relative to epoch (January 1, 1970, 00:00:00 GMT)."
|
213
|
|
);
|
214
|
|
}
|
215
|
|
}
|
216
|
|
|
217
|
|
/**
|
218
|
|
* The heart of the matter. This is where the selector gets to decide
|
219
|
|
* on the inclusion of a file in a particular fileset.
|
220
|
|
*
|
221
|
|
* @param PhingFile $basedir the base directory the scan is being done from
|
222
|
|
* @param string $filename is the name of the file to check
|
223
|
|
* @param PhingFile $file is a PhingFile object the selector can use
|
224
|
|
* @return boolean Whether the file should be selected or not
|
225
|
|
*/
|
226
|
1
|
public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
|
227
|
|
{
|
228
|
1
|
$this->validate();
|
229
|
1
|
if ($file->isDirectory() && ($this->includeDirs === false)) {
|
230
|
0
|
return true;
|
231
|
|
}
|
232
|
1
|
if ($this->cmp === 0) {
|
233
|
1
|
return (($file->lastModified() - $this->granularity) < $this->seconds);
|
234
|
|
}
|
235
|
|
|
236
|
1
|
if ($this->cmp === 1) {
|
237
|
1
|
return (($file->lastModified() - $this->granularity) > $this->seconds);
|
238
|
|
}
|
239
|
|
|
240
|
0
|
return (abs($file->lastModified() - $this->seconds) <= $this->granularity);
|
241
|
|
}
|
242
|
|
}
|