You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

156 lines
6.1 KiB

  1. <?php
  2. /**
  3. * Move Plugin File Mover
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Michael Hamann <michael@content-space.de>
  7. * @author Andreas Gohr <gohr@cosmocode.de>
  8. */
  9. // must be run within Dokuwiki
  10. if(!defined('DOKU_INC')) die();
  11. /**
  12. * Class helper_plugin_move_file
  13. *
  14. * This helps with moving files from one folder to another. It simply matches files and moves them
  15. * arround. No fancy rewriting happens here.
  16. */
  17. class helper_plugin_move_file extends DokuWiki_Plugin {
  18. /**
  19. * Move the meta files of a page
  20. *
  21. * @param string $src_ns The original namespace
  22. * @param string $src_name The original basename of the moved doc (empty for namespace moves)
  23. * @param string $dst_ns The namespace after the move
  24. * @param string $dst_name The basename after the move (empty for namespace moves)
  25. * @return bool If the meta files were moved successfully
  26. */
  27. public function movePageMeta($src_ns, $src_name, $dst_ns, $dst_name) {
  28. global $conf;
  29. $regex = '\.[^.]+';
  30. return $this->execute($conf['metadir'], $src_ns, $src_name, $dst_ns, $dst_name, $regex);
  31. }
  32. /**
  33. * Move the old revisions of a page
  34. *
  35. * @param string $src_ns The original namespace
  36. * @param string $src_name The original basename of the moved doc (empty for namespace moves)
  37. * @param string $dst_ns The namespace after the move
  38. * @param string $dst_name The basename after the move (empty for namespace moves)
  39. * @return bool If the attic files were moved successfully
  40. */
  41. public function movePageAttic($src_ns, $src_name, $dst_ns, $dst_name) {
  42. global $conf;
  43. $regex = '\.\d+\.txt(?:\.gz|\.bz2)?';
  44. return $this->execute($conf['olddir'], $src_ns, $src_name, $dst_ns, $dst_name, $regex);
  45. }
  46. /**
  47. * Move the meta files of the page that is specified in the options.
  48. *
  49. * @param string $src_ns The original namespace
  50. * @param string $src_name The original basename of the moved doc (empty for namespace moves)
  51. * @param string $dst_ns The namespace after the move
  52. * @param string $dst_name The basename after the move (empty for namespace moves)
  53. * @return bool If the meta files were moved successfully
  54. */
  55. public function moveMediaMeta($src_ns, $src_name, $dst_ns, $dst_name) {
  56. global $conf;
  57. $regex = '\.[^.]+';
  58. return $this->execute($conf['mediametadir'], $src_ns, $src_name, $dst_ns, $dst_name, $regex);
  59. }
  60. /**
  61. * Move the old revisions of the media file that is specified in the options
  62. *
  63. * @param string $src_ns The original namespace
  64. * @param string $src_name The original basename of the moved doc (empty for namespace moves)
  65. * @param string $dst_ns The namespace after the move
  66. * @param string $dst_name The basename after the move (empty for namespace moves)
  67. * @return bool If the attic files were moved successfully
  68. */
  69. public function moveMediaAttic($src_ns, $src_name, $dst_ns, $dst_name) {
  70. global $conf;
  71. $ext = mimetype($src_name);
  72. if($ext[0] !== false) {
  73. $name = substr($src_name, 0, -1 * strlen($ext[0]) - 1);
  74. } else {
  75. $name = $src_name;
  76. }
  77. $newext = mimetype($dst_name);
  78. if($newext[0] !== false) {
  79. $newname = substr($dst_name, 0, -1 * strlen($newext[0]) - 1);
  80. } else {
  81. $newname = $dst_name;
  82. }
  83. $regex = '\.\d+\.' . preg_quote((string) $ext[0], '/');
  84. return $this->execute($conf['mediaolddir'], $src_ns, $name, $dst_ns, $newname, $regex);
  85. }
  86. /**
  87. * Moves the subscription file for a namespace
  88. *
  89. * @param string $src_ns
  90. * @param string $dst_ns
  91. * @return bool
  92. */
  93. public function moveNamespaceSubscription($src_ns, $dst_ns){
  94. global $conf;
  95. $regex = '\.mlist';
  96. return $this->execute($conf['metadir'], $src_ns, '', $dst_ns, '', $regex);
  97. }
  98. /**
  99. * Executes the move op
  100. *
  101. * @param string $dir The root path of the files (e.g. $conf['metadir'] or $conf['olddir']
  102. * @param string $src_ns The original namespace
  103. * @param string $src_name The original basename of the moved doc (empty for namespace moves)
  104. * @param string $dst_ns The namespace after the move
  105. * @param string $dst_name The basename after the move (empty for namespace moves)
  106. * @param string $extregex Regular expression for matching the extension of the file that shall be moved
  107. * @return bool If the files were moved successfully
  108. */
  109. protected function execute($dir, $src_ns, $src_name, $dst_ns, $dst_name, $extregex) {
  110. $old_path = $dir;
  111. if($src_ns != '') $old_path .= '/' . utf8_encodeFN(str_replace(':', '/', $src_ns));
  112. $new_path = $dir;
  113. if($dst_ns != '') $new_path .= '/' . utf8_encodeFN(str_replace(':', '/', $dst_ns));
  114. $regex = '/^' . preg_quote(utf8_encodeFN($src_name)) . '(' . $extregex . ')$/u';
  115. if(!is_dir($old_path)) return true; // no media files found
  116. $dh = @opendir($old_path);
  117. if($dh) {
  118. while(($file = readdir($dh)) !== false) {
  119. if($file == '.' || $file == '..') continue;
  120. $match = array();
  121. if(is_file($old_path . '/' . $file) && preg_match($regex, $file, $match)) {
  122. if(!is_dir($new_path)) {
  123. if(!io_mkdir_p($new_path)) {
  124. msg('Creating directory ' . hsc($new_path) . ' failed.', -1);
  125. return false;
  126. }
  127. }
  128. if(!io_rename($old_path . '/' . $file, $new_path . '/' . utf8_encodeFN($dst_name . $match[1]))) {
  129. msg('Moving ' . hsc($old_path . '/' . $file) . ' to ' . hsc($new_path . '/' . utf8_encodeFN($dst_name . $match[1])) . ' failed.', -1);
  130. return false;
  131. }
  132. }
  133. }
  134. closedir($dh);
  135. } else {
  136. msg('Directory ' . hsc($old_path) . ' couldn\'t be opened.', -1);
  137. return false;
  138. }
  139. return true;
  140. }
  141. }