]> sipb.mit.edu Git - ikiwiki.git/blob - doc/patchqueue/move_page.mdwn
fix
[ikiwiki.git] / doc / patchqueue / move_page.mdwn
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).
3
4 A few shortcomings exist: 
5
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
15   FileChooser thing.
16 * No redirect pages like those mentioned on [[todo/Moving_Pages]] exist yet, 
17   so none are created.
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.
23
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.
27 > --[[Joey]]
28
29 Hmm. Shouldn't that be done on a by-RCS basis, though? (i.e. implemented
30 by backends in the `rcs_move` function)
31
32 > Probably, yes, but maybe there's a way to avoid duplicating code for that
33 > in several of them.
34
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?
37
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
43 > with an error.
44
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
48     @@ -561,6 +561,106 @@
49         }
50      } #}}}
51
52     +sub cgi_movepage($$) {
53     +   my $q = shift;
54     +   my $session = shift;
55     +   eval q{use CGI::FormBuilder};
56     +   error($@) if $@;
57     +   my @fields=qw(do from rcsinfo page newdir newname comments);
58     +   my @buttons=("Rename Page", "Cancel");
59     +
60     +   my $form = CGI::FormBuilder->new(
61     +           fields => \@fields,
62     +                header => 1,
63     +                charset => "utf-8",
64     +                method => 'POST',
65     +           action => $config{cgiurl},
66     +                template => (-e "$config{templatedir}/movepage.tmpl" ?
67     +                        {template_params("movepage.tmpl")} : ""),
68     +   );
69     +   run_hooks(formbuilder_setup => sub {
70     +           shift->(form => $form, cgi => $q, session => $session);
71     +   });
72     +
73     +   decode_form_utf8($form);
74     +
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");
80     +   }
81     +   my $file=$pagesources{$page};
82     +   my $type=pagetype($file);
83     +
84     +   my $from;
85     +   if (defined $form->field('from')) {
86     +           ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
87     +   }
88     +
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());
99     +
100     +   if (! $form->submitted) {
101     +           $form->field(name => "rcsinfo", value => rcs_prepedit($file),
102     +                        force => 1);
103     +   }
104     +
105     +   if ($form->submitted eq "Cancel") {
106     +           redirect($q, "$config{url}/".htmlpage($page));
107     +           return;
108     +   }
109     +
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);
122     +           }
123     +           if (! defined $form->field('newdir') ||
124     +               ! length $form->field('newdir')) {
125     +                   $form->field(name => "newdir",
126     +                                value => pagetitle($dname, 1), force => 1);
127     +           }
128     +           print $form->render(submit => \@buttons);
129     +   }
130     +   else{
131     +           # This untaint is safe because titlepage removes any problematic
132     +           # characters.
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");
139     +           }
140     +           check_canedit($page, $q, $session);
141     +
142     +           my $newpage = ($newdir?"$newdir/":"") . $newname;
143     +           my $newfile = $newpage . ".$type";
144     +           my $message = $form->field('comments');
145     +           unlockwiki();
146     +           rcs_move($file, $newfile, $message, $form->field("rcsinfo"),
147     +                    $session->param("name"), $ENV{REMOTE_ADDR});
148     +           redirect($q, "$config{url}/".htmlpage($newpage));
149     +   }
150     +}
151     +
152      sub cgi_getsession ($) { #{{{
153         my $q=shift;
154
155     @@ -656,6 +756,9 @@
156         elsif (defined $session->param("postsignin")) {
157                 cgi_postsignin($q, $session);
158         }
159     +   elsif ($do eq 'move') {
160     +           cgi_movepage($q, $session);
161     +   }
162         elsif ($do eq 'prefs') {
163                 cgi_prefs($q, $session);
164         }
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
168     @@ -60,6 +60,34 @@
169         }
170      } #}}}
171
172     +sub rcs_move ($$$$;$$) {
173     +   my $file=shift;
174     +   my $newname=shift;
175     +   my $message=shift;
176     +   my $rcstoken=shift;
177     +   my $user=shift;
178     +   my $ipaddr=shift;
179     +   if (defined $user) {
180     +           $message="web commit by $user".(length $message ? ": $message" : "");
181     +   }
182     +   elsif (defined $ipaddr) {
183     +           $message="web commit from $ipaddr".(length $message ? ": $message" : "");
184     +   }
185     +
186     +   chdir($config{srcdir}); # svn merge wants to be here
187     +
188     +   if (system("svn", "move", "--quiet",
189     +              "$file", "$newname") != 0) {
190     +           return 1;
191     +   }
192     +   if (system("svn", "commit", "--quiet",
193     +              "--encoding", "UTF-8", "-m",
194     +              possibly_foolish_untaint($message)) != 0) {
195     +           return 1;
196     +   }
197     +   return undef # success
198     +}
199     +
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
206     @@ -80,6 +80,7 @@
207
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"));
212                 if ($config{rcs}) {
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
217     @@ -0,0 +1,44 @@
218     +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
219     + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
220     +<html>
221     +<head>
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" />
229     +</TMPL_IF>
230     +</head>
231     +<body>
232     +<TMPL_IF NAME="FAILED_SAVE">
233     +<p>
234     +<b>Failed to save your changes.</b>
235     +</p>
236     +<p>
237     +Your changes were not able to be saved to disk. The system gave the error:
238     +<blockquote>
239     +<TMPL_VAR ERROR_MESSAGE>
240     +</blockquote>
241     +Your changes are preserved below, and you can try again to save them.
242     +</p>
243     +</TMPL_IF>
244     +<TMPL_VAR FORM-START>
245     +<div class="header">
246     +<span><TMPL_VAR INDEXLINK>/ <TMPL_VAR FORM-TITLE></span>
247     +</div>
248     +<TMPL_VAR FIELD-DO>
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>
253     +<br />
254     +<TMPL_IF NAME="CAN_COMMIT">
255     +Optional comment about this change:<br />
256     +<TMPL_VAR FIELD-COMMENTS><br />
257     +</TMPL_IF>
258     +<input id="_submit" name="_submit" type="submit" value="Rename Page" /><input id="_submit_2" name="_submit" type="submit" value="Cancel" />
259     +<TMPL_VAR FORM-END>
260     +</body>
261     +</html>
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
265     @@ -32,6 +32,9 @@
266      <TMPL_IF NAME="EDITURL">
267      <li><a href="<TMPL_VAR EDITURL>">Edit</a></li>
268      </TMPL_IF>
269     +<TMPL_IF NAME="MOVEURL">
270     +<li><a href="<TMPL_VAR MOVEURL>">Move</a></li>
271     +</TMPL_IF>
272      <TMPL_IF NAME="RECENTCHANGESURL">
273      <li><a href="<TMPL_VAR RECENTCHANGESURL>">RecentChanges</a></li>
274      </TMPL_IF>