1<?php
2/*
3 *
4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 *
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the LGPL. For more information please see
18 * <http://phing.info>.
19 */
20
21require_once 'phing/tasks/system/MatchingTask.php';
22
23/**
24 * Base class for extracting tasks such as Unzip and Untar.
25 *
26 * @author    Joakim Bodin <joakim.bodin+phing@gmail.com>
27 * @version   $Id: 8aa7996b72792da30f1ec94174f09b9c612bcc1a $
28 * @package   phing.tasks.ext
29 * @since     2.2.0
30 */
31abstract class ExtractBaseTask extends MatchingTask {
32    /**
33     * @var PhingFile $file
34     */
35    protected $file;
36    /**
37     * @var PhingFile $todir
38     */
39    protected $todir;
40    protected $removepath;
41    protected $filesets = array(); // all fileset objects assigned to this task
42
43    /**
44     * Set to true to always extract (and possibly overwrite)
45     * all files from the archive
46     * @var boolean
47     */
48    protected $forceExtract = false;
49
50    /**
51     * Add a new fileset.
52     * @return FileSet
53     */
54    public function createFileSet() {
55        $this->fileset = new FileSet();
56        $this->filesets[] = $this->fileset;
57        return $this->fileset;
58    }
59
60    /**
61     * Set the name of the zip file to extract.
62     * @param PhingFile $file zip file to extract
63     */
64    public function setFile(PhingFile $file) {
65        $this->file = $file;
66    }
67
68    /**
69     * This is the base directory to look in for things to zip.
70     * @param PhingFile $baseDir
71     */
72    public function setToDir(PhingFile $todir) {
73        $this->todir = $todir;
74    }
75
76    public function setRemovePath($removepath)
77    {
78        $this->removepath = $removepath;
79    }
80
81    /**
82     * Sets the forceExtract attribute
83     * @param boolean $forceExtract
84     */
85    public function setForceExtract($forceExtract)
86    {
87        $this->forceExtract = (bool) $forceExtract;
88    }
89
90    /**
91     * do the work
92     * @throws BuildException
93     */
94    public function main() {
95
96        $this->validateAttributes();
97
98        $filesToExtract = array();
99        if ($this->file !== null) {
100            if(!$this->isDestinationUpToDate($this->file)) {
101                $filesToExtract[] = $this->file;
102            } else {
103                $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' .  $this->file->getCanonicalPath(), Project::MSG_INFO);
104            }
105        }
106
107        foreach($this->filesets as $compressedArchiveFileset) {
108            $compressedArchiveDirScanner = $compressedArchiveFileset->getDirectoryScanner($this->project);
109            $compressedArchiveFiles = $compressedArchiveDirScanner->getIncludedFiles();
110            $compressedArchiveDir = $compressedArchiveFileset->getDir($this->project);
111
112            foreach ($compressedArchiveFiles as $compressedArchiveFilePath) {
113                $compressedArchiveFile = new PhingFile($compressedArchiveDir, $compressedArchiveFilePath);
114                if($compressedArchiveFile->isDirectory())
115                {
116                    throw new BuildException($compressedArchiveFile->getAbsolutePath() . ' compressed archive cannot be a directory.');
117                }
118
119                if ($this->forceExtract || !$this->isDestinationUpToDate($compressedArchiveFile)) {
120                   $filesToExtract[] = $compressedArchiveFile;
121                } else {
122                    $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' .  $compressedArchiveFile->getCanonicalPath(), Project::MSG_INFO);
123                }
124            }
125        }
126
127        foreach ($filesToExtract as $compressedArchiveFile) {
128            $this->extractArchive($compressedArchiveFile);
129        }
130    }
131
132    abstract protected function extractArchive(PhingFile $compressedArchiveFile);
133
134    /**
135     * @param array $files array of filenames
136     * @param PhingFile $dir
137     * @return boolean
138     */
139    protected function isDestinationUpToDate(PhingFile $compressedArchiveFile) {
140        if (!$compressedArchiveFile->exists()) {
141            throw new BuildException("Could not find file " . $compressedArchiveFile->__toString() . " to extract.");
142        }
143
144        $compressedArchiveContent = $this->listArchiveContent($compressedArchiveFile);
145        if(is_array($compressedArchiveContent)) {
146
147            $fileSystem = FileSystem::getFileSystem();
148            foreach ($compressedArchiveContent as $compressArchivePathInfo) {
149                $compressArchiveFilename = $compressArchivePathInfo['filename'];
150                if(!empty($this->removepath) && strlen($compressArchiveFilename) >= strlen($this->removepath))
151                {
152                    $compressArchiveFilename = preg_replace('/^' . $this->removepath . '/','', $compressArchiveFilename);
153                }
154                $compressArchivePath = new PhingFile($this->todir, $compressArchiveFilename);
155
156                if(!$compressArchivePath->exists() ||
157                    $fileSystem->compareMTimes($compressedArchiveFile->getCanonicalPath(), $compressArchivePath->getCanonicalPath()) == 1) {
158                    return false;
159                }
160            }
161
162        }
163
164        return true;
165    }
166
167    abstract protected function listArchiveContent(PhingFile $compressedArchiveFile);
168
169    /**
170     * Validates attributes coming in from XML
171     *
172     * @access  private
173     * @return  void
174     * @throws  BuildException
175     */
176    protected function validateAttributes() {
177
178        if ($this->file === null && count($this->filesets) === 0) {
179            throw new BuildException("Specify at least one source compressed archive - a file or a fileset.");
180        }
181
182        if ($this->todir === null) {
183            throw new BuildException("todir must be set.");
184        }
185
186        if ($this->todir !== null && $this->todir->exists() && !$this->todir->isDirectory()) {
187            throw new BuildException("todir must be a directory.");
188        }
189
190        if ($this->file !== null && $this->file->exists() && $this->file->isDirectory()) {
191            throw new BuildException("Compressed archive file cannot be a directory.");
192        }
193
194        if ($this->file !== null && !$this->file->exists()) {
195            throw new BuildException("Could not find compressed archive file " . $this->file->__toString() . " to extract.");
196        }
197    }
198
199}
200