PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/phing/tasks/system/DeleteTask.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 358 lines | 266 code | 24 blank | 68 comment | 33 complexity | 657b06bb51993bd49b3f1b6d5f90a0de MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /*
  3. * $Id: DeleteTask.php 552 2009-08-29 12:18:13Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. /**
  23. * Deletes a file or directory, or set of files defined by a fileset.
  24. *
  25. * @version $Revision: 552 $
  26. * @package phing.tasks.system
  27. */
  28. class DeleteTask extends Task
  29. {
  30. protected $file;
  31. protected $dir;
  32. protected $filesets = array();
  33. protected $includeEmpty = false;
  34. protected $quiet = false;
  35. protected $failonerror = true;
  36. protected $verbosity = Project :: MSG_VERBOSE;
  37. /** Any filelists of files that should be deleted. */
  38. private $filelists = array();
  39. /**
  40. * Set the name of a single file to be removed.
  41. * @param PhingFile $file
  42. */
  43. function setFile(PhingFile $file)
  44. {
  45. $this->file = $file;
  46. }
  47. /**
  48. * Set the directory from which files are to be deleted.
  49. * @param PhingFile $dir
  50. */
  51. function setDir(PhingFile $dir)
  52. {
  53. $this->dir = $dir;
  54. }
  55. /**
  56. * Used to force listing of all names of deleted files.
  57. * @param boolean $verbosity
  58. */
  59. function setVerbose($verbosity)
  60. {
  61. if ($verbosity)
  62. {
  63. $this->verbosity = Project :: MSG_INFO;
  64. }
  65. else
  66. {
  67. $this->verbosity = Project :: MSG_VERBOSE;
  68. }
  69. }
  70. /**
  71. * If the file does not exist, do not display a diagnostic
  72. * message or modify the exit status to reflect an error.
  73. * This means that if a file or directory cannot be deleted,
  74. * then no error is reported. This setting emulates the
  75. * -f option to the Unix rm command. Default is false
  76. * meaning things are verbose
  77. */
  78. function setQuiet($bool)
  79. {
  80. $this->quiet = $bool;
  81. if ($this->quiet)
  82. {
  83. $this->failonerror = false;
  84. }
  85. }
  86. /** this flag means 'note errors to the output, but keep going' */
  87. function setFailOnError($bool)
  88. {
  89. $this->failonerror = $bool;
  90. }
  91. /** Used to delete empty directories.*/
  92. function setIncludeEmptyDirs($includeEmpty)
  93. {
  94. $this->includeEmpty = (boolean) $includeEmpty;
  95. }
  96. /** Nested creator, adds a set of files (nested fileset attribute). */
  97. function createFileSet()
  98. {
  99. $num = array_push($this->filesets, new FileSet());
  100. return $this->filesets[$num - 1];
  101. }
  102. /** Nested creator, adds a set of files (nested fileset attribute). */
  103. function createFileList()
  104. {
  105. $num = array_push($this->filelists, new FileList());
  106. return $this->filelists[$num - 1];
  107. }
  108. /** Delete the file(s). */
  109. function main()
  110. {
  111. if ($this->file === null && $this->dir === null && count($this->filesets) === 0 && count($this->filelists) === 0)
  112. {
  113. throw new BuildException("At least one of the file or dir attributes, or a fileset element, or a filelist element must be set.");
  114. }
  115. if ($this->quiet && $this->failonerror)
  116. {
  117. throw new BuildException("quiet and failonerror cannot both be set to true", $this->location);
  118. }
  119. // delete a single file
  120. if ($this->file !== null)
  121. {
  122. if ($this->file->exists())
  123. {
  124. if ($this->file->isDirectory())
  125. {
  126. $this->log("Directory " . $this->file->__toString() . " cannot be removed using the file attribute. Use dir instead.");
  127. }
  128. else
  129. {
  130. $this->log("Deleting: " . $this->file->__toString());
  131. try
  132. {
  133. $this->file->delete();
  134. }
  135. catch (Exception $e)
  136. {
  137. $message = "Unable to delete file " . $this->file->__toString() . ": " . $e->getMessage();
  138. if ($this->failonerror)
  139. {
  140. throw new BuildException($message);
  141. }
  142. else
  143. {
  144. $this->log($message, $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  145. }
  146. }
  147. }
  148. }
  149. else
  150. {
  151. $this->log("Could not find file " . $this->file->getAbsolutePath() . " to delete.", Project :: MSG_VERBOSE);
  152. }
  153. }
  154. // delete the directory
  155. if ($this->dir !== null && $this->dir->exists() && $this->dir->isDirectory())
  156. {
  157. if ($this->verbosity === Project :: MSG_VERBOSE)
  158. {
  159. $this->log("Deleting directory " . $this->dir->__toString());
  160. }
  161. $this->removeDir($this->dir);
  162. }
  163. // delete the files in the filelists
  164. foreach ($this->filelists as $fl)
  165. {
  166. try
  167. {
  168. $files = $fl->getFiles($this->project);
  169. $this->removeFiles($fl->getDir($this->project), $files, $empty = array());
  170. }
  171. catch (BuildException $be)
  172. {
  173. // directory doesn't exist or is not readable
  174. if ($this->failonerror)
  175. {
  176. throw $be;
  177. }
  178. else
  179. {
  180. $this->log($be->getMessage(), $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  181. }
  182. }
  183. }
  184. // delete the files in the filesets
  185. foreach ($this->filesets as $fs)
  186. {
  187. try
  188. {
  189. $ds = $fs->getDirectoryScanner($this->project);
  190. $files = $ds->getIncludedFiles();
  191. $dirs = $ds->getIncludedDirectories();
  192. $this->removeFiles($fs->getDir($this->project), $files, $dirs);
  193. }
  194. catch (BuildException $be)
  195. {
  196. // directory doesn't exist or is not readable
  197. if ($this->failonerror)
  198. {
  199. throw $be;
  200. }
  201. else
  202. {
  203. $this->log($be->getMessage(), $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * Recursively removes a directory.
  210. * @param PhingFile $d The directory to remove.
  211. */
  212. private function removeDir($d)
  213. {
  214. $list = $d->listDir();
  215. if ($list === null)
  216. {
  217. $list = array();
  218. }
  219. foreach ($list as $s)
  220. {
  221. $f = new PhingFile($d, $s);
  222. if ($f->isDirectory())
  223. {
  224. $this->removeDir($f);
  225. }
  226. else
  227. {
  228. $this->log("Deleting " . $f->__toString(), $this->verbosity);
  229. try
  230. {
  231. $f->delete();
  232. }
  233. catch (Exception $e)
  234. {
  235. $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
  236. if ($this->failonerror)
  237. {
  238. throw new BuildException($message);
  239. }
  240. else
  241. {
  242. $this->log($message, $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  243. }
  244. }
  245. }
  246. }
  247. $this->log("Deleting directory " . $d->getAbsolutePath(), $this->verbosity);
  248. try
  249. {
  250. $d->delete();
  251. }
  252. catch (Exception $e)
  253. {
  254. $message = "Unable to delete directory " . $d->__toString() . ": " . $e->getMessage();
  255. if ($this->failonerror)
  256. {
  257. throw new BuildException($message);
  258. }
  259. else
  260. {
  261. $this->log($message, $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  262. }
  263. }
  264. }
  265. /**
  266. * remove an array of files in a directory, and a list of subdirectories
  267. * which will only be deleted if 'includeEmpty' is true
  268. * @param PhingFile $d directory to work from
  269. * @param array &$files array of files to delete; can be of zero length
  270. * @param array &$dirs array of directories to delete; can of zero length
  271. */
  272. private function removeFiles(PhingFile $d, &$files, &$dirs)
  273. {
  274. if (count($files) > 0)
  275. {
  276. $this->log("Deleting " . count($files) . " files from " . $d->__toString());
  277. for($j = 0, $_j = count($files); $j < $_j; $j ++)
  278. {
  279. $f = new PhingFile($d, $files[$j]);
  280. $this->log("Deleting " . $f->getAbsolutePath(), $this->verbosity);
  281. try
  282. {
  283. $f->delete();
  284. }
  285. catch (Exception $e)
  286. {
  287. $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
  288. if ($this->failonerror)
  289. {
  290. throw new BuildException($message);
  291. }
  292. else
  293. {
  294. $this->log($message, $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  295. }
  296. }
  297. }
  298. }
  299. if (count($dirs) > 0 && $this->includeEmpty)
  300. {
  301. $dirCount = 0;
  302. for($j = count($dirs) - 1; $j >= 0; -- $j)
  303. {
  304. $dir = new PhingFile($d, $dirs[$j]);
  305. $dirFiles = $dir->listDir();
  306. if ($dirFiles === null || count($dirFiles) === 0)
  307. {
  308. $this->log("Deleting " . $dir->__toString(), $this->verbosity);
  309. try
  310. {
  311. $dir->delete();
  312. $dirCount ++;
  313. }
  314. catch (Exception $e)
  315. {
  316. $message = "Unable to delete directory " . $dir->__toString();
  317. if ($this->failonerror)
  318. {
  319. throw new BuildException($message);
  320. }
  321. else
  322. {
  323. $this->log($message, $this->quiet ? Project :: MSG_VERBOSE : Project :: MSG_WARN);
  324. }
  325. }
  326. }
  327. }
  328. if ($dirCount > 0)
  329. {
  330. $this->log("Deleted $dirCount director" . ($dirCount == 1 ? "y" : "ies") . " from " . $d->__toString());
  331. }
  332. }
  333. }
  334. }