summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/types/selectors/SizeSelector.php
blob: bbc264236747d42bccfd5e6ae89e3f83ae3d77ff (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php

/*
 * $Id: SizeSelector.php,v 1.8 2005/05/26 13:10:53 mrook Exp $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */


/**
 * Selector that filters files based on their size.
 *
 * @author Hans Lellelid <hans@xmpl.org> (Phing)
 * @author Bruce Atherton <bruce@callenish.com> (Ant)
 * @package phing.types.selectors
 */
class SizeSelector extends BaseExtendSelector {

    private $size = -1;
    private $multiplier = 1;
    private $sizelimit = -1;
    private $cmp = 2;
    const SIZE_KEY = "value";
    const UNITS_KEY = "units";
    const WHEN_KEY = "when";

    private static $sizeComparisons =  array("less", "more", "equal");
    private static $byteUnits = array("K", "k", "kilo", "KILO",
                                 "Ki", "KI", "ki", "kibi", "KIBI",
                                 "M", "m", "mega", "MEGA",
                                 "Mi", "MI", "mi", "mebi", "MEBI",
                                 "G", "g", "giga", "GIGA",
                                 "Gi", "GI", "gi", "gibi", "GIBI",
                                 "T", "t", "tera", "TERA",
            /* You wish! */      "Ti", "TI", "ti", "tebi", "TEBI"
                                 );

    public function toString() {
        $buf = "{sizeselector value: ";
        $buf .= $this->sizelimit;
        $buf .= "compare: ";
        if ($this->cmp === 0) {
            $buf .= "less";
        } elseif ($this->cmp === 1) {
            $buf .= "more";
        } else {
            $buf .= "equal";
        }
        $buf .= "}";
        return $buf;
    }

    /**
     * A size selector needs to know what size to base its selecting on.
     * This will be further modified by the multiplier to get an
     * actual size limit.
     *
     * @param size the size to select against expressed in units
     */
    public function setValue($size) {
        $this->size = $size;
        if (($this->multiplier !== 0) && ($this->size > -1)) {
            $this->sizelimit = $size * $this->multiplier;
        }
    }

    /**
     * Sets the units to use for the comparison. This is a little
     * complicated because common usage has created standards that
     * play havoc with capitalization rules. Thus, some people will
     * use "K" for indicating 1000's, when the SI standard calls for
     * "k". Others have tried to introduce "K" as a multiple of 1024,
     * but that falls down when you reach "M", since "m" is already
     * defined as 0.001.
     * <p>
     * To get around this complexity, a number of standards bodies
     * have proposed the 2^10 standard, and at least one has adopted
     * it. But we are still left with a populace that isn't clear on
     * how capitalization should work.
     * <p>
     * We therefore ignore capitalization as much as possible.
     * Completely mixed case is not possible, but all upper and lower
     * forms are accepted for all long and short forms. Since we have
     * no need to work with the 0.001 case, this practice works here.
     * <p>
     * This function translates all the long and short forms that a
     * unit prefix can occur in and translates them into a single
     * multiplier.
     *
     * @param $units The units to compare the size to.
     * @return void
     */
    public function setUnits($units) {
        $i = array_search($units, self::$byteUnits, true);
        if ($i === false) $i = -1; // make it java-like
        
        $this->multiplier = 0;
        if (($i > -1) && ($i < 4)) {
            $this->multiplier = 1000;
        } elseif (($i > 3) && ($i < 9)) {
            $this->multiplier = 1024;
        } elseif (($i > 8) && ($i < 13)) {
            $this->multiplier = 1000000;
        } elseif (($i > 12) && ($i < 18)) {
            $this->multiplier = 1048576;
        } elseif (($i > 17) && ($i < 22)) {
            $this->multiplier = 1000000000;
        } elseif (($i > 21) && ($i < 27)) {
            $this->multiplier = 1073741824;
        } elseif (($i > 26) && ($i < 31)) {
            $this->multiplier = 1000000000000;
        } elseif (($i > 30) && ($i < 36)) {
            $this->multiplier = 1099511627776;
        }
        if (($this->multiplier > 0) && ($this->size > -1)) {
            $this->sizelimit = $this->size * $this->multiplier;
        }
    }

    /**
     * This specifies when the file should be selected, whether it be
     * when the file matches a particular size, when it is smaller,
     * or whether it is larger.
     *
     * @param cmp The comparison to perform, an EnumeratedAttribute
     */
    public function setWhen($cmp) {
        $c = array_search($cmp, self::$sizeComparisons, true);
        if ($c !== false) {
            $this->cmp = $c;
        }
    }

    /**
     * When using this as a custom selector, this method will be called.
     * It translates each parameter into the appropriate setXXX() call.
     *
     * @param parameters the complete set of parameters for this selector
     */
    public function setParameters($parameters) {
        parent::setParameters($parameters);
        if ($parameters !== null) {
            for ($i = 0, $size=count($parameters); $i < $size; $i++) {
                $paramname = $parameters[$i]->getName();
                switch(strtolower($paramname)) {
                    case self::SIZE_KEY:
                        try {
                            $this->setValue($parameters[$i]->getValue());
                           } catch (Exception $nfe) {
                               $this->setError("Invalid size setting "
                                . $parameters[$i]->getValue());
                           }
                        break;
                    case self::UNITS_KEY:                                                
                        $this->setUnits($parameters[$i]->getValue());
                        break;
                    case self::WHEN_KEY:
                        $this->setWhen($parameters[$i]->getValue());
                        break;
                    default:    
                        $this->setError("Invalid parameter " . $paramname);
                }
            }
        }
    }

    /**
     * <p>Checks to make sure all settings are kosher. In this case, it
     * means that the size attribute has been set (to a positive value),
     * that the multiplier has a valid setting, and that the size limit
     * is valid. Since the latter is a calculated value, this can only
     * fail due to a programming error.
     * </p>
     * <p>If a problem is detected, the setError() method is called.
     * </p>
     */
    public function verifySettings() {
        if ($this->size < 0) {
            $this->setError("The value attribute is required, and must be positive");
        } elseif ($this->multiplier < 1) {
            $this->setError("Invalid Units supplied, must be K,Ki,M,Mi,G,Gi,T,or Ti");
        } elseif ($this->sizelimit < 0) {
            $this->setError("Internal error: Code is not setting sizelimit correctly");
        }
    }

    /**
     * The heart of the matter. This is where the selector gets to decide
     * on the inclusion of a file in a particular fileset.
     *
     * @param basedir A PhingFile object for the base directory
     * @param filename The name of the file to check
     * @param file A PhingFile object for this filename
     * @return whether the file should be selected or not
     */
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file) {

        $this->validate();

        // Directory size never selected for
        if ($file->isDirectory()) {
            return true;
        }
        if ($this->cmp === 0) {
            return ($file->length() < $this->sizelimit);
        } elseif ($this->cmp === 1) {
            return ($file->length() > $this->sizelimit);
        } else {
            return ($file->length() === $this->sizelimit);
        }
    }
    
}