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 diff -urNX ignorepats ikiwiki/IkiWiki/CGI.pm ikidev/IkiWiki/CGI.pm
46 --- ikiwiki/IkiWiki/CGI.pm 2007-02-14 18:17:12.000000000 -0800
47 +++ ikidev/IkiWiki/CGI.pm 2007-02-22 18:54:23.194982000 -0800
52 +sub cgi_movepage($$) {
54 + my $session = shift;
55 + eval q{use CGI::FormBuilder};
57 + my @fields=qw(do from rcsinfo page newdir newname comments);
58 + my @buttons=("Rename Page", "Cancel");
60 + my $form = CGI::FormBuilder->new(
65 + action => $config{cgiurl},
66 + template => (-e "$config{templatedir}/movepage.tmpl" ?
67 + {template_params("movepage.tmpl")} : ""),
69 + run_hooks(formbuilder_setup => sub {
70 + shift->(form => $form, cgi => $q, session => $session);
73 + decode_form_utf8($form);
75 + # This untaint is safe because if the page doesn't exist, bail.
76 + my $page = $form->field('page');
77 + $page = possibly_foolish_untaint($page);
78 + if (! exists $pagesources{$page}) {
79 + error("page does not exist");
81 + my $file=$pagesources{$page};
82 + my $type=pagetype($file);
85 + if (defined $form->field('from')) {
86 + ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
89 + $form->field(name => "do", type => 'hidden');
90 + $form->field(name => "from", type => 'hidden');
91 + $form->field(name => "rcsinfo", type => 'hidden');
92 + $form->field(name => "newdir", type => 'text', size => 80);
93 + $form->field(name => "page", value => $page, force => 1);
94 + $form->field(name => "newname", type => "text", size => 80);
95 + $form->field(name => "comments", type => "text", size => 80);
96 + $form->tmpl_param("can_commit", $config{rcs});
97 + $form->tmpl_param("indexlink", indexlink());
98 + $form->tmpl_param("baseurl", baseurl());
100 + if (! $form->submitted) {
101 + $form->field(name => "rcsinfo", value => rcs_prepedit($file),
105 + if ($form->submitted eq "Cancel") {
106 + redirect($q, "$config{url}/".htmlpage($page));
110 + if (! $form->submitted || ! $form->validate) {
111 + check_canedit($page, $q, $session);
112 + $form->tmpl_param("page_select", 0);
113 + $form->field(name => "page", type => 'hidden');
114 + $form->field(name => "type", type => 'hidden');
115 + $form->title(sprintf(gettext("moving %s"), pagetitle($page)));
116 + my $pname = basename($page);
117 + my $dname = dirname($page);
118 + if (! defined $form->field('newname') ||
119 + ! length $form->field('newname')) {
120 + $form->field(name => "newname",
121 + value => pagetitle($pname, 1), force => 1);
123 + if (! defined $form->field('newdir') ||
124 + ! length $form->field('newdir')) {
125 + $form->field(name => "newdir",
126 + value => pagetitle($dname, 1), force => 1);
128 + print $form->render(submit => \@buttons);
131 + # This untaint is safe because titlepage removes any problematic
133 + my ($newname)=$form->field('newname');
134 + $newname=titlepage(possibly_foolish_untaint($newname));
135 + my ($newdir)=$form->field('newdir');
136 + $newdir=titlepage(possibly_foolish_untaint($newdir));
137 + if (! defined $newname || ! length $newname || file_pruned($newname, $config{srcdir}) || $newname=~/^\//) {
138 + error("bad page name");
140 + check_canedit($page, $q, $session);
142 + my $newpage = ($newdir?"$newdir/":"") . $newname;
143 + my $newfile = $newpage . ".$type";
144 + my $message = $form->field('comments');
146 + rcs_move($file, $newfile, $message, $form->field("rcsinfo"),
147 + $session->param("name"), $ENV{REMOTE_ADDR});
148 + redirect($q, "$config{url}/".htmlpage($newpage));
152 sub cgi_getsession ($) { #{{{
156 elsif (defined $session->param("postsignin")) {
157 cgi_postsignin($q, $session);
159 + elsif ($do eq 'move') {
160 + cgi_movepage($q, $session);
162 elsif ($do eq 'prefs') {
163 cgi_prefs($q, $session);
165 diff -urNX ignorepats ikiwiki/IkiWiki/Rcs/svn.pm ikidev/IkiWiki/Rcs/svn.pm
166 --- ikiwiki/IkiWiki/Rcs/svn.pm 2007-01-27 16:04:48.000000000 -0800
167 +++ ikidev/IkiWiki/Rcs/svn.pm 2007-02-22 01:51:29.923626000 -0800
172 +sub rcs_move ($$$$;$$) {
176 + my $rcstoken=shift;
179 + if (defined $user) {
180 + $message="web commit by $user".(length $message ? ": $message" : "");
182 + elsif (defined $ipaddr) {
183 + $message="web commit from $ipaddr".(length $message ? ": $message" : "");
186 + chdir($config{srcdir}); # svn merge wants to be here
188 + if (system("svn", "move", "--quiet",
189 + "$file", "$newname") != 0) {
192 + if (system("svn", "commit", "--quiet",
193 + "--encoding", "UTF-8", "-m",
194 + possibly_foolish_untaint($message)) != 0) {
197 + return undef # success
200 sub rcs_commit ($$$;$$) { #{{{
201 # Tries to commit the page; returns undef on _success_ and
202 # a version of the page with the rcs's conflict markers on failure.
203 diff -urNX ignorepats ikiwiki/IkiWiki/Render.pm ikidev/IkiWiki/Render.pm
204 --- ikiwiki/IkiWiki/Render.pm 2007-02-14 17:00:05.000000000 -0800
205 +++ ikidev/IkiWiki/Render.pm 2007-02-22 18:30:00.451755000 -0800
208 if (length $config{cgiurl}) {
209 $template->param(editurl => cgiurl(do => "edit", page => $page));
210 + $template->param(moveurl => cgiurl(do => "move", page => $page));
211 $template->param(prefsurl => cgiurl(do => "prefs"));
213 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
214 diff -urNX ignorepats ikiwiki/templates/movepage.tmpl ikidev/templates/movepage.tmpl
215 --- ikiwiki/templates/movepage.tmpl 1969-12-31 16:00:00.000000000 -0800
216 +++ ikidev/templates/movepage.tmpl 2007-02-22 18:40:39.751763000 -0800
218 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
219 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
222 +<base href="<TMPL_VAR BASEURL>" />
223 +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
224 +<title><TMPL_VAR FORM-TITLE></title>
225 +<link rel="stylesheet" href="<TMPL_VAR BASEURL>style.css" type="text/css" />
226 +<link rel="stylesheet" href="<TMPL_VAR BASEURL>local.css" type="text/css" />
227 +<TMPL_IF NAME="FAVICON">
228 +<link rel="icon" href="<TMPL_VAR BASEURL><TMPL_VAR FAVICON>" type="image/x-icon" />
232 +<TMPL_IF NAME="FAILED_SAVE">
234 +<b>Failed to save your changes.</b>
237 +Your changes were not able to be saved to disk. The system gave the error:
239 +<TMPL_VAR ERROR_MESSAGE>
241 +Your changes are preserved below, and you can try again to save them.
244 +<TMPL_VAR FORM-START>
245 +<div class="header">
246 +<span><TMPL_VAR INDEXLINK>/ <TMPL_VAR FORM-TITLE></span>
249 +<TMPL_VAR FIELD-FROM>
250 +<TMPL_VAR FIELD-RCSINFO>
251 +<TMPL_VAR FIELD-PAGE>
252 +New location: <TMPL_VAR FIELD-NEWDIR>/ <TMPL_VAR FIELD-NEWNAME>
254 +<TMPL_IF NAME="CAN_COMMIT">
255 +Optional comment about this change:<br />
256 +<TMPL_VAR FIELD-COMMENTS><br />
258 +<input id="_submit" name="_submit" type="submit" value="Rename Page" /><input id="_submit_2" name="_submit" type="submit" value="Cancel" />
262 diff -urNX ignorepats ikiwiki/templates/page.tmpl ikidev/templates/page.tmpl
263 --- ikiwiki/templates/page.tmpl 2006-12-28 12:27:01.000000000 -0800
264 +++ ikidev/templates/page.tmpl 2007-02-22 01:52:33.078464000 -0800
266 <TMPL_IF NAME="EDITURL">
267 <li><a href="<TMPL_VAR EDITURL>">Edit</a></li>
269 +<TMPL_IF NAME="MOVEURL">
270 +<li><a href="<TMPL_VAR MOVEURL>">Move</a></li>
272 <TMPL_IF NAME="RECENTCHANGESURL">
273 <li><a href="<TMPL_VAR RECENTCHANGESURL>">RecentChanges</a></li>