1 This is my second cut at a feature like that requested in [[todo/Moving_Pages]].
2 It can also be found [here](http://ikidev.betacantrips.com/patches/move.patch).
4 A few shortcomings exist:
6 * No precautions whatsoever are made to protect against race conditions or failures
7 in the rcs\_move function. I didn't even do the `cgi_editpage` thing where I hold
8 the lock and render afterwards (mostly because the copy I was editing was not
9 up-to-date enough to have that code). Although FAILED_SAVE is in movepage.tmpl,
10 no code activates it yet.
11 * Some code is duplicated between cgi\_movepage and cgi\_editpage, as well
12 as rcs\_commit and rcs\_move.
13 * The user interface is pretty lame. I couldn't figure out a good way to let
14 the user specify which directory to move things to without implementing a
16 * No redirect pages like those mentioned on [[todo/Moving_Pages]] exist yet,
18 * I added a Move link to page.tmpl but it may belong better someplace else --
19 maybe editpage.tmpl? Not sure.
20 * from is redundant with page so far -- but since the Move links could someday
21 come from someplace other than the page itself I kept it around.
22 * If I move foo.mdwn to bar.mdwn, foo/* should move too, probably.
24 > Looks like a good start, although I agree about many of the points above,
25 > and also feel that something needs to be done about rcses that don't
26 > implement a move operation -- falling back to an add and delete.
29 Hmm. Shouldn't that be done on a by-RCS basis, though? (i.e. implemented
30 by backends in the `rcs_move` function)
32 > Probably, yes, but maybe there's a way to avoid duplicating code for that
35 Also, how should ikiwiki react if a page is edited (say, by another user)
36 before it is moved? Bail, or shrug and proceed?
38 > The important thing is to keep in mind that the page could be edited,
39 > moved, deleted, etc in between the user starting the move and the move
40 > happening. So, the code really needs to deal with all of these cases in
41 > some way. It seems fine to me to go ahead with the move even if the page
42 > was edited. If the page was deleted or moved, it seems reasonable to exit
45 > Another set of issues exists if a separate web user is trying to edit the
46 > page at the same time. We'll have to make sure that something sane
47 > happens there; will ikiwiki re-add the moved page under the old name if
48 > the user saves their edit after the move? Could be confusing.. I think it
49 > probably errors out instead, but I'm not sure. Of course, this is an
50 > issue that occurs if the page is moved using a regular svn commit too, so
51 > it's not really your concern in a way. :-)
53 >> I wrote a seperate patch to address this -- it's in [[disappearing_pages]]. --Ethan
55 diff -urNX ignorepats ikiwiki/IkiWiki/CGI.pm ikidev/IkiWiki/CGI.pm
56 --- ikiwiki/IkiWiki/CGI.pm 2007-02-14 18:17:12.000000000 -0800
57 +++ ikidev/IkiWiki/CGI.pm 2007-02-22 18:54:23.194982000 -0800
62 +sub cgi_movepage($$) {
64 + my $session = shift;
65 + eval q{use CGI::FormBuilder};
67 + my @fields=qw(do from rcsinfo page newdir newname comments);
68 + my @buttons=("Rename Page", "Cancel");
70 + my $form = CGI::FormBuilder->new(
75 + action => $config{cgiurl},
76 + template => (-e "$config{templatedir}/movepage.tmpl" ?
77 + {template_params("movepage.tmpl")} : ""),
79 + run_hooks(formbuilder_setup => sub {
80 + shift->(form => $form, cgi => $q, session => $session);
83 + decode_form_utf8($form);
85 + # This untaint is safe because if the page doesn't exist, bail.
86 + my $page = $form->field('page');
87 + $page = possibly_foolish_untaint($page);
88 + if (! exists $pagesources{$page}) {
89 + error("page does not exist");
91 + my $file=$pagesources{$page};
92 + my $type=pagetype($file);
95 + if (defined $form->field('from')) {
96 + ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
99 + $form->field(name => "do", type => 'hidden');
100 + $form->field(name => "from", type => 'hidden');
101 + $form->field(name => "rcsinfo", type => 'hidden');
102 + $form->field(name => "newdir", type => 'text', size => 80);
103 + $form->field(name => "page", value => $page, force => 1);
104 + $form->field(name => "newname", type => "text", size => 80);
105 + $form->field(name => "comments", type => "text", size => 80);
106 + $form->tmpl_param("can_commit", $config{rcs});
107 + $form->tmpl_param("indexlink", indexlink());
108 + $form->tmpl_param("baseurl", baseurl());
110 + if (! $form->submitted) {
111 + $form->field(name => "rcsinfo", value => rcs_prepedit($file),
115 + if ($form->submitted eq "Cancel") {
116 + redirect($q, "$config{url}/".htmlpage($page));
120 + if (! $form->submitted || ! $form->validate) {
121 + check_canedit($page, $q, $session);
122 + $form->tmpl_param("page_select", 0);
123 + $form->field(name => "page", type => 'hidden');
124 + $form->field(name => "type", type => 'hidden');
125 + $form->title(sprintf(gettext("moving %s"), pagetitle($page)));
126 + my $pname = basename($page);
127 + my $dname = dirname($page);
128 + if (! defined $form->field('newname') ||
129 + ! length $form->field('newname')) {
130 + $form->field(name => "newname",
131 + value => pagetitle($pname, 1), force => 1);
133 + if (! defined $form->field('newdir') ||
134 + ! length $form->field('newdir')) {
135 + $form->field(name => "newdir",
136 + value => pagetitle($dname, 1), force => 1);
138 + print $form->render(submit => \@buttons);
141 + # This untaint is safe because titlepage removes any problematic
143 + my ($newname)=$form->field('newname');
144 + $newname=titlepage(possibly_foolish_untaint($newname));
145 + my ($newdir)=$form->field('newdir');
146 + $newdir=titlepage(possibly_foolish_untaint($newdir));
147 + if (! defined $newname || ! length $newname || file_pruned($newname, $config{srcdir}) || $newname=~/^\//) {
148 + error("bad page name");
150 + check_canedit($page, $q, $session);
152 + my $newpage = ($newdir?"$newdir/":"") . $newname;
153 + my $newfile = $newpage . ".$type";
154 + my $message = $form->field('comments');
156 + rcs_move($file, $newfile, $message, $form->field("rcsinfo"),
157 + $session->param("name"), $ENV{REMOTE_ADDR});
158 + redirect($q, "$config{url}/".htmlpage($newpage));
162 sub cgi_getsession ($) { #{{{
166 elsif (defined $session->param("postsignin")) {
167 cgi_postsignin($q, $session);
169 + elsif ($do eq 'move') {
170 + cgi_movepage($q, $session);
172 elsif ($do eq 'prefs') {
173 cgi_prefs($q, $session);
175 diff -urNX ignorepats ikiwiki/IkiWiki/Rcs/svn.pm ikidev/IkiWiki/Rcs/svn.pm
176 --- ikiwiki/IkiWiki/Rcs/svn.pm 2007-01-27 16:04:48.000000000 -0800
177 +++ ikidev/IkiWiki/Rcs/svn.pm 2007-02-22 01:51:29.923626000 -0800
182 +sub rcs_move ($$$$;$$) {
186 + my $rcstoken=shift;
189 + if (defined $user) {
190 + $message="web commit by $user".(length $message ? ": $message" : "");
192 + elsif (defined $ipaddr) {
193 + $message="web commit from $ipaddr".(length $message ? ": $message" : "");
196 + chdir($config{srcdir}); # svn merge wants to be here
198 + if (system("svn", "move", "--quiet",
199 + "$file", "$newname") != 0) {
202 + if (system("svn", "commit", "--quiet",
203 + "--encoding", "UTF-8", "-m",
204 + possibly_foolish_untaint($message)) != 0) {
207 + return undef # success
210 sub rcs_commit ($$$;$$) { #{{{
211 # Tries to commit the page; returns undef on _success_ and
212 # a version of the page with the rcs's conflict markers on failure.
213 diff -urNX ignorepats ikiwiki/IkiWiki/Render.pm ikidev/IkiWiki/Render.pm
214 --- ikiwiki/IkiWiki/Render.pm 2007-02-14 17:00:05.000000000 -0800
215 +++ ikidev/IkiWiki/Render.pm 2007-02-22 18:30:00.451755000 -0800
218 if (length $config{cgiurl}) {
219 $template->param(editurl => cgiurl(do => "edit", page => $page));
220 + $template->param(moveurl => cgiurl(do => "move", page => $page));
221 $template->param(prefsurl => cgiurl(do => "prefs"));
223 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
224 diff -urNX ignorepats ikiwiki/templates/movepage.tmpl ikidev/templates/movepage.tmpl
225 --- ikiwiki/templates/movepage.tmpl 1969-12-31 16:00:00.000000000 -0800
226 +++ ikidev/templates/movepage.tmpl 2007-02-22 18:40:39.751763000 -0800
228 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
229 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
232 +<base href="<TMPL_VAR BASEURL>" />
233 +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
234 +<title><TMPL_VAR FORM-TITLE></title>
235 +<link rel="stylesheet" href="<TMPL_VAR BASEURL>style.css" type="text/css" />
236 +<link rel="stylesheet" href="<TMPL_VAR BASEURL>local.css" type="text/css" />
237 +<TMPL_IF NAME="FAVICON">
238 +<link rel="icon" href="<TMPL_VAR BASEURL><TMPL_VAR FAVICON>" type="image/x-icon" />
242 +<TMPL_IF NAME="FAILED_SAVE">
244 +<b>Failed to save your changes.</b>
247 +Your changes were not able to be saved to disk. The system gave the error:
249 +<TMPL_VAR ERROR_MESSAGE>
251 +Your changes are preserved below, and you can try again to save them.
254 +<TMPL_VAR FORM-START>
255 +<div class="header">
256 +<span><TMPL_VAR INDEXLINK>/ <TMPL_VAR FORM-TITLE></span>
259 +<TMPL_VAR FIELD-FROM>
260 +<TMPL_VAR FIELD-RCSINFO>
261 +<TMPL_VAR FIELD-PAGE>
262 +New location: <TMPL_VAR FIELD-NEWDIR>/ <TMPL_VAR FIELD-NEWNAME>
264 +<TMPL_IF NAME="CAN_COMMIT">
265 +Optional comment about this change:<br />
266 +<TMPL_VAR FIELD-COMMENTS><br />
268 +<input id="_submit" name="_submit" type="submit" value="Rename Page" /><input id="_submit_2" name="_submit" type="submit" value="Cancel" />
272 diff -urNX ignorepats ikiwiki/templates/page.tmpl ikidev/templates/page.tmpl
273 --- ikiwiki/templates/page.tmpl 2006-12-28 12:27:01.000000000 -0800
274 +++ ikidev/templates/page.tmpl 2007-02-22 01:52:33.078464000 -0800
276 <TMPL_IF NAME="EDITURL">
277 <li><a href="<TMPL_VAR EDITURL>">Edit</a></li>
279 +<TMPL_IF NAME="MOVEURL">
280 +<li><a href="<TMPL_VAR MOVEURL>">Move</a></li>
282 <TMPL_IF NAME="RECENTCHANGESURL">
283 <li><a href="<TMPL_VAR RECENTCHANGESURL>">RecentChanges</a></li>