ExtractBaseTask.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. require_once 'phing/tasks/system/MatchingTask.php';
  21. /**
  22. * Base class for extracting tasks such as Unzip and Untar.
  23. *
  24. * @author Joakim Bodin <joakim.bodin+phing@gmail.com>
  25. * @version $Id: ExtractBaseTask.php 905 2010-10-05 16:28:03Z mrook $
  26. * @package phing.tasks.ext
  27. * @since 2.2.0
  28. */
  29. abstract class ExtractBaseTask extends MatchingTask {
  30. /**
  31. * @var PhingFile $file
  32. */
  33. protected $file;
  34. /**
  35. * @var PhingFile $todir
  36. */
  37. protected $todir;
  38. protected $removepath;
  39. protected $filesets = array(); // all fileset objects assigned to this task
  40. /**
  41. * Add a new fileset.
  42. * @return FileSet
  43. */
  44. public function createFileSet() {
  45. $this->fileset = new FileSet();
  46. $this->filesets[] = $this->fileset;
  47. return $this->fileset;
  48. }
  49. /**
  50. * Set the name of the zip file to extract.
  51. * @param PhingFile $file zip file to extract
  52. */
  53. public function setFile(PhingFile $file) {
  54. $this->file = $file;
  55. }
  56. /**
  57. * This is the base directory to look in for things to zip.
  58. * @param PhingFile $baseDir
  59. */
  60. public function setToDir(PhingFile $todir) {
  61. $this->todir = $todir;
  62. }
  63. public function setRemovePath($removepath)
  64. {
  65. $this->removepath = $removepath;
  66. }
  67. /**
  68. * do the work
  69. * @throws BuildException
  70. */
  71. public function main() {
  72. $this->validateAttributes();
  73. $filesToExtract = array();
  74. if ($this->file !== null) {
  75. if(!$this->isDestinationUpToDate($this->file)) {
  76. $filesToExtract[] = $this->file;
  77. } else {
  78. $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' . $this->file->getCanonicalPath(), Project::MSG_INFO);
  79. }
  80. }
  81. foreach($this->filesets as $compressedArchiveFileset) {
  82. $compressedArchiveDirScanner = $compressedArchiveFileset->getDirectoryScanner($this->project);
  83. $compressedArchiveFiles = $compressedArchiveDirScanner->getIncludedFiles();
  84. $compressedArchiveDir = $compressedArchiveFileset->getDir($this->project);
  85. foreach ($compressedArchiveFiles as $compressedArchiveFilePath) {
  86. $compressedArchiveFile = new PhingFile($compressedArchiveDir, $compressedArchiveFilePath);
  87. if($compressedArchiveFile->isDirectory())
  88. {
  89. throw new BuildException($compressedArchiveFile->getAbsolutePath() . ' compressed archive cannot be a directory.');
  90. }
  91. if(!$this->isDestinationUpToDate($compressedArchiveFile)) {
  92. $filesToExtract[] = $compressedArchiveFile;
  93. } else {
  94. $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' . $compressedArchiveFile->getCanonicalPath(), Project::MSG_INFO);
  95. }
  96. }
  97. }
  98. foreach ($filesToExtract as $compressedArchiveFile) {
  99. $this->extractArchive($compressedArchiveFile);
  100. }
  101. }
  102. abstract protected function extractArchive(PhingFile $compressedArchiveFile);
  103. /**
  104. * @param array $files array of filenames
  105. * @param PhingFile $dir
  106. * @return boolean
  107. */
  108. protected function isDestinationUpToDate(PhingFile $compressedArchiveFile) {
  109. if (!$compressedArchiveFile->exists()) {
  110. throw new BuildException("Could not find file " . $compressedArchiveFile->__toString() . " to extract.");
  111. }
  112. $compressedArchiveContent = $this->listArchiveContent($compressedArchiveFile);
  113. if(is_array($compressedArchiveContent)) {
  114. $fileSystem = FileSystem::getFileSystem();
  115. foreach ($compressedArchiveContent as $compressArchivePathInfo) {
  116. $compressArchiveFilename = $compressArchivePathInfo['filename'];
  117. if(!empty($this->removepath) && strlen($compressArchiveFilename) >= strlen($this->removepath))
  118. {
  119. $compressArchiveFilename = preg_replace('/^' . $this->removepath . '/','', $compressArchiveFilename);
  120. }
  121. $compressArchivePath = new PhingFile($this->todir, $compressArchiveFilename);
  122. if(!$compressArchivePath->exists() ||
  123. $fileSystem->compareMTimes($compressedArchiveFile->getCanonicalPath(), $compressArchivePath->getCanonicalPath()) == 1) {
  124. return false;
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. abstract protected function listArchiveContent(PhingFile $compressedArchiveFile);
  131. /**
  132. * Validates attributes coming in from XML
  133. *
  134. * @access private
  135. * @return void
  136. * @throws BuildException
  137. */
  138. protected function validateAttributes() {
  139. if ($this->file === null && count($this->filesets) === 0) {
  140. throw new BuildException("Specify at least one source compressed archive - a file or a fileset.");
  141. }
  142. if ($this->todir === null) {
  143. throw new BuildException("todir must be set.");
  144. }
  145. if ($this->todir !== null && $this->todir->exists() && !$this->todir->isDirectory()) {
  146. throw new BuildException("todir must be a directory.");
  147. }
  148. if ($this->file !== null && $this->file->exists() && $this->file->isDirectory()) {
  149. throw new BuildException("Compressed archive file cannot be a directory.");
  150. }
  151. if ($this->file !== null && !$this->file->exists()) {
  152. throw new BuildException("Could not find compressed archive file " . $this->file->__toString() . " to extract.");
  153. }
  154. }
  155. }