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.
 
 
 
 
 

271 lines
9.0 KiB

  1. <?php
  2. /**
  3. * Plugin : Move
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Michael Hamann <michael@content-space.de>
  7. * @author Gary Owen,
  8. */
  9. // must be run within Dokuwiki
  10. if(!defined('DOKU_INC')) die();
  11. /**
  12. * Admin component of the move plugin. Provides the user interface.
  13. */
  14. class admin_plugin_move_main extends DokuWiki_Admin_Plugin {
  15. /** @var helper_plugin_move_plan $plan */
  16. protected $plan;
  17. public function __construct() {
  18. $this->plan = plugin_load('helper', 'move_plan');
  19. }
  20. /**
  21. * @param $language
  22. * @return string
  23. */
  24. public function getMenuText($language) {
  25. $label = $this->getLang('menu');
  26. if($this->plan->isCommited()) $label .= ' '.$this->getLang('inprogress');
  27. return $label;
  28. }
  29. /**
  30. * Get the sort number that defines the position in the admin menu.
  31. *
  32. * @return int The sort number
  33. */
  34. function getMenuSort() {
  35. return 1011;
  36. }
  37. /**
  38. * If this admin plugin is for admins only
  39. *
  40. * @return bool false
  41. */
  42. function forAdminOnly() {
  43. return false;
  44. }
  45. /**
  46. * Handle the input
  47. */
  48. function handle() {
  49. global $INPUT;
  50. // create a new plan if possible and sufficient data was given
  51. $this->createPlanFromInput();
  52. // handle workflow button presses
  53. if($this->plan->isCommited()) {
  54. helper_plugin_move_rewrite::addLock(); //todo: right place?
  55. switch($INPUT->str('ctl')) {
  56. case 'continue':
  57. $this->plan->nextStep();
  58. break;
  59. case 'skip':
  60. $this->plan->nextStep(true);
  61. break;
  62. case 'abort':
  63. $this->plan->abort();
  64. break;
  65. }
  66. }
  67. }
  68. /**
  69. * Display the interface
  70. */
  71. function html() {
  72. // decide what to do based on the plan's state
  73. if($this->plan->isCommited()) {
  74. $this->GUI_progress();
  75. } else {
  76. // display form
  77. $this->GUI_simpleForm();
  78. }
  79. }
  80. /**
  81. * Get input variables and create a move plan from them
  82. *
  83. * @return bool
  84. */
  85. protected function createPlanFromInput() {
  86. global $INPUT;
  87. global $ID;
  88. if($this->plan->isCommited()) return false;
  89. $this->plan->setOption('autoskip', $INPUT->bool('autoskip'));
  90. $this->plan->setOption('autorewrite', $INPUT->bool('autorewrite'));
  91. if($ID && $INPUT->has('dst')) {
  92. $dst = trim($INPUT->str('dst'));
  93. if($dst == '') {
  94. msg($this->getLang('nodst'), -1);
  95. return false;
  96. }
  97. // input came from form
  98. if($INPUT->str('class') == 'namespace') {
  99. $src = getNS($ID);
  100. if($INPUT->str('type') == 'both') {
  101. $this->plan->addPageNamespaceMove($src, $dst);
  102. $this->plan->addMediaNamespaceMove($src, $dst);
  103. } else if($INPUT->str('type') == 'page') {
  104. $this->plan->addPageNamespaceMove($src, $dst);
  105. } else if($INPUT->str('type') == 'media') {
  106. $this->plan->addMediaNamespaceMove($src, $dst);
  107. }
  108. } else {
  109. $this->plan->addPageMove($ID, $INPUT->str('dst'));
  110. }
  111. $this->plan->commit();
  112. return true;
  113. } elseif($INPUT->has('json')) {
  114. // input came via JSON from tree manager
  115. $json = new JSON(JSON_LOOSE_TYPE);
  116. $data = $json->decode($INPUT->str('json'));
  117. foreach((array) $data as $entry) {
  118. if($entry['class'] == 'ns') {
  119. if($entry['type'] == 'page') {
  120. $this->plan->addPageNamespaceMove($entry['src'], $entry['dst']);
  121. } elseif($entry['type'] == 'media') {
  122. $this->plan->addMediaNamespaceMove($entry['src'], $entry['dst']);
  123. }
  124. } elseif($entry['class'] == 'doc') {
  125. if($entry['type'] == 'page') {
  126. $this->plan->addPageMove($entry['src'], $entry['dst']);
  127. } elseif($entry['type'] == 'media') {
  128. $this->plan->addMediaMove($entry['src'], $entry['dst']);
  129. }
  130. }
  131. }
  132. $this->plan->commit();
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Display the simple move form
  139. */
  140. protected function GUI_simpleForm() {
  141. global $ID;
  142. echo $this->locale_xhtml('move');
  143. $treelink = wl($ID, array('do'=>'admin', 'page'=>'move_tree'));
  144. echo '<p id="plugin_move__treelink">';
  145. printf($this->getLang('treelink'), $treelink);
  146. echo '</p>';
  147. $form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'plugin_move_form'));
  148. $form->addHidden('page', 'move_main');
  149. $form->addHidden('id', $ID);
  150. $form->startFieldset($this->getLang('legend'));
  151. $form->addElement(form_makeRadioField('class', 'page', $this->getLang('movepage') . ' <code>' . $ID . '</code>', '', 'block radio click-page', array('checked' => 'checked')));
  152. $form->addElement(form_makeRadioField('class', 'namespace', $this->getLang('movens') . ' <code>' . getNS($ID) . '</code>', '', 'block radio click-ns'));
  153. $form->addElement(form_makeTextField('dst', $ID, $this->getLang('dst'), '', 'block indent'));
  154. $form->addElement(form_makeMenuField('type', array('pages' => $this->getLang('move_pages'), 'media' => $this->getLang('move_media'), 'both' => $this->getLang('move_media_and_pages')), 'both', $this->getLang('content_to_move'), '', 'block indent select'));
  155. $form->addElement(form_makeCheckboxField('autoskip', '1', $this->getLang('autoskip'), '', 'block', ($this->getConf('autoskip') ? array('checked' => 'checked') : array())));
  156. $form->addElement(form_makeCheckboxField('autorewrite', '1', $this->getLang('autorewrite'), '', 'block', ($this->getConf('autorewrite') ? array('checked' => 'checked') : array())));
  157. $form->addElement(form_makeButton('submit', 'admin', $this->getLang('btn_start')));
  158. $form->endFieldset();
  159. $form->printForm();
  160. }
  161. /**
  162. * Display the GUI while the move progresses
  163. */
  164. protected function GUI_progress() {
  165. echo '<div id="plugin_move__progress">';
  166. echo $this->locale_xhtml('progress');
  167. $progress = $this->plan->getProgress();
  168. if(!$this->plan->inProgress()) {
  169. echo '<div id="plugin_move__preview">';
  170. echo '<p>';
  171. echo '<strong>' . $this->getLang('intro') . '</strong> ';
  172. echo '<span>' . $this->getLang('preview') . '</span>';
  173. echo '</p>';
  174. echo $this->plan->previewHTML();
  175. echo '</div>';
  176. }
  177. echo '<div class="progress" data-progress="' . $progress . '">' . $progress . '%</div>';
  178. echo '<div class="output">';
  179. if($this->plan->getLastError()) {
  180. echo '<p><div class="error">' . $this->plan->getLastError() . '</div></p>';
  181. } elseif ($this->plan->inProgress()) {
  182. echo '<p><div class="info">' . $this->getLang('inexecution') . '</div></p>';
  183. }
  184. echo '</div>';
  185. // display all buttons but toggle visibility according to state
  186. echo '<p></p>';
  187. echo '<div class="controls">';
  188. echo '<img src="' . DOKU_BASE . 'lib/images/throbber.gif" class="hide" />';
  189. $this->btn('start', !$this->plan->inProgress());
  190. $this->btn('retry', $this->plan->getLastError());
  191. $this->btn('skip', $this->plan->getLastError());
  192. $this->btn('continue', $this->plan->inProgress() && !$this->plan->getLastError());
  193. $this->btn('abort');
  194. echo '</div>';
  195. echo '</div>';
  196. }
  197. /**
  198. * Display a move workflow button
  199. *
  200. * continue, start, retry - continue next steps
  201. * abort - abort the whole move
  202. * skip - skip error and continue
  203. *
  204. * @param string $control
  205. * @param bool $show should this control be visible?
  206. */
  207. protected function btn($control, $show = true) {
  208. global $ID;
  209. $skip = 0;
  210. $label = $this->getLang('btn_' . $control);
  211. $id = $control;
  212. if($control == 'start') $control = 'continue';
  213. if($control == 'retry') {
  214. $control = 'continue';
  215. $skip = 0;
  216. }
  217. $class = 'move__control ctlfrm-' . $id;
  218. if(!$show) $class .= ' hide';
  219. $form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => $class));
  220. $form->addHidden('page', 'move_main');
  221. $form->addHidden('id', $ID);
  222. $form->addHidden('ctl', $control);
  223. $form->addHidden('skip', $skip);
  224. $form->addElement(form_makeButton('submit', 'admin', $label, array('class' => 'btn ctl-' . $control)));
  225. $form->printForm();
  226. }
  227. }