4d385f67abe0a2e28ded068cf348df15584e0db1
[ikiwiki.git] / doc / patchqueue / decode-from.mdwn
1 From [[Faidon]]:
2
3 I consolidated all decode_utf8 in FormBuilder's fields
4 to make the code more readable and avoid future mistakes. The patch
5 seems to work and for the first time I have a UTF-8 username ;-)
6 I think that with this patch and the previous one about locales, we're
7 done with UTF-8 support. Yay!
8
9         Index: IkiWiki/CGI.pm
10         ===================================================================
11         --- IkiWiki/CGI.pm      (revision 904)
12         +++ IkiWiki/CGI.pm      (working copy)
13         @@ -41,6 +41,17 @@
14                 return 0;
15          } #}}}
16          
17         +sub decode_form_utf8 ($) { #{{{
18         +       my $form = shift;
19         +       foreach my $f ($form->field) {
20         +               next if Encode::is_utf8(scalar $form->field($f));
21         +               $form->field(name  => $f,
22         +                            value => decode_utf8($form->field($f)),
23         +                            force => 1,
24         +                           );
25         +       }
26         +} #}}}
27         +
28          sub cgi_recentchanges ($) { #{{{
29                 my $q=shift;
30                 
31         @@ -166,6 +177,8 @@
32                 }
33          
34                 if ($form->submitted && $form->validate) {
35         +               decode_form_utf8($form);
36         +               
37                         if ($form->submitted eq 'Login') {
38                                 $session->param("name", $form->field("name"));
39                                 if (defined $form->field("do") && 
40         @@ -282,6 +295,8 @@
41                                 value => userinfo_get($user_name, "locked_pages"));
42                 }
43                 
44         +       decode_form_utf8($form);
45         +       
46                 if ($form->submitted eq 'Logout') {
47                         $session->delete();
48                         redirect($q, $config{url});
49         @@ -326,7 +341,7 @@
50                 );
51                 my @buttons=("Save Page", "Preview", "Cancel");
52                 
53         -       # This untaint is safe because titlepage removes any problimatic
54         +       # This untaint is safe because titlepage removes any problematic
55                 # characters.
56                 my ($page)=decode_utf8($form->param('page'));
57                 $page=titlepage(possibly_foolish_untaint(lc($page)));
58         @@ -367,16 +382,16 @@
59                                 force => 1);
60                 }
61                 
62         +       decode_form_utf8($form);
63         +       
64                 if ($form->submitted eq "Cancel") {
65                         redirect($q, "$config{url}/".htmlpage($page));
66                         return;
67                 }
68                 elsif ($form->submitted eq "Preview") {
69                         require IkiWiki::Render;
70         -               # Apparently FormBuilder doesn't not treat input as
71         -               # utf-8, so decode from it.
72         -               my $content=decode_utf8($form->field('editcontent'));
73         -               my $comments=decode_utf8($form->field('comments'));
74         +               my $content=$form->field('editcontent');
75         +               my $comments=$form->field('comments');
76                         $form->field(name => "editcontent",
77                                         value => $content, force => 1);
78                         $form->field(name => "comments",
79         @@ -463,8 +478,7 @@
80                         # save page
81                         page_locked($page, $session);
82                         
83         -               # Decode utf-8 since FormBuilder does not
84         -               my $content=decode_utf8($form->field('editcontent'));
85         +               my $content=$form->field('editcontent');
86          
87                         $content=~s/\r\n/\n/g;
88                         $content=~s/\r/\n/g;
89         @@ -480,7 +494,7 @@
90                         }
91                         if (defined $form->field('comments') &&
92                             length $form->field('comments')) {
93         -                       $message.=": ".decode_utf8($form->field('comments'));
94         +                       $message.=": ".$form->field('comments');
95                         }
96                         
97                         if ($config{rcs}) {
98         @@ -499,7 +513,7 @@
99                                                 force => 1);
100                                         $form->tmpl_param("page_conflict", 1);
101                                         $form->field("editcontent", value => $conflict, force => 1);
102         -                               $form->field(name => "comments", value => decode_utf8($form->field('comments')), force => 1);
103         +                               $form->field(name => "comments", value => $form->field('comments'), force => 1);
104                                         $form->field("do", "edit)");
105                                         $form->tmpl_param("page_select", 0);
106                                         $form->field(name => "page", type => 'hidden');