OSDN Git Service

Show unfreeze icon. Added secret option 'paraedit' to avoid showing unfreeze icon
[pukiwiki/pukiwiki.git] / plugin / edit.inc.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: edit.inc.php,v 1.22 2004/11/20 06:38:32 henoheno Exp $
6 //
7
8 // Edit plugin
9 // cmd=edit
10 function plugin_edit_action()
11 {
12         global $vars, $_title_edit, $load_template_func;
13
14         $page = isset($vars['page']) ? $vars['page'] : '';
15
16         check_editable($page, true, true);
17
18         if (isset($vars['preview']) || ($load_template_func && isset($vars['template']))) {
19                 return plugin_edit_preview();
20         } else if (isset($vars['write'])) {
21                 return plugin_edit_write();
22         } else if (isset($vars['cancel'])) {
23                 return plugin_edit_cancel();
24         }
25
26         $postdata = @join('', get_source($page));
27         if ($postdata == '') {
28                 $postdata = auto_template($page);
29         }
30
31         return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
32 }
33
34 // Preview
35 function plugin_edit_preview()
36 {
37         global $vars;
38         global $_title_preview, $_msg_preview, $_msg_preview_delete;
39
40         $page = isset($vars['page']) ? $vars['page'] : '';
41
42         // Loading template
43         if (isset($vars['template_page']) && is_page($vars['template_page'])) {
44
45                 $vars['msg'] = join('', get_source($vars['template_page']));
46
47                 // Cut fixed anchors
48                 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);
49         }
50
51         // ¼ê½ñ¤­¤Î#freeze¤òºï½ü
52         $vars['msg'] = preg_replace('/^#freeze\s*$/im', '', $vars['msg']);
53         $postdata = $vars['msg'];
54
55         if (isset($vars['add']) && $vars['add']) {
56                 if (isset($vars['add_top']) && $vars['add_top']) {
57                         $postdata  = $postdata . "\n\n" . @join('', get_source($page));
58                 } else {
59                         $postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
60                 }
61         }
62
63         $body = "$_msg_preview<br />\n";
64         if ($postdata == '')
65                 $body .= "<strong>$_msg_preview_delete</strong>";
66         $body .= "<br />\n";
67
68         if ($postdata) {
69                 $postdata = make_str_rules($postdata);
70                 $postdata = explode("\n", $postdata);
71                 $postdata = drop_submit(convert_html($postdata));
72                 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
73         }
74         $body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
75
76         return array('msg'=>$_title_preview, 'body'=>$body);
77 }
78
79 // Inline: Show edit (or unfreeze text) link
80 function plugin_edit_inline()
81 {
82         static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
83
84         global $script, $vars;
85         global $_symbol_paraedit, $fixed_heading_anchor_edit;
86
87         // Arguments
88         $args = func_get_args();
89         $s_label = array_pop($args); // {label}
90         $page    = array_shift($args);
91         if($page == NULL) $page = '';
92         $_noicon = $_nolabel = $_paraedit = FALSE;
93         foreach($args as $arg){
94                 switch($arg){
95                 case '': break;
96                 case 'noicon':   $_noicon   = TRUE; break;
97                 case 'nolabel':  $_nolabel  = TRUE; break;
98                 case 'paraedit': $_paraedit = TRUE; break; // Paragraph-edit
99                 default: return $usage;
100                 }
101         }
102         if($_paraedit) $_nolabel = TRUE;
103
104         // Separate a page-name and a fixed anchor
105         list($s_page, $id, $editable) = anchor_explode($page, TRUE);
106         // Default: This one
107         if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
108         // $s_page fixed
109         $isfreeze = is_freeze($s_page);
110         $ispage   = is_page($s_page);
111
112         // Paragraph edit enabled or not
113         $short = htmlspecialchars('Edit');
114         if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
115                 // Paragraph editing
116                 $id    = rawurlencode($id);
117                 $title = htmlspecialchars(sprintf('Edit %s', $page));
118                 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
119                         '" width="9" height="9" alt="' .
120                         $short . '" title="' . $title . '" /> ';
121                 $class = 'anchor_super';
122         } else {
123                 // Normal editing / unfreeze
124                 $id    = '';
125                 if ($isfreeze) {
126                         $title = 'Unfreeze %s';
127                         $icon  = 'unfreeze.png';
128                 } else {
129                         $title = 'Edit %s';
130                         $icon  = 'edit.png';
131                 }
132                 $title = htmlspecialchars(sprintf($title, $s_page));
133                 $icon = '<img src="' . IMAGE_DIR . $icon .
134                         '" width="20" height="20" alt="' .
135                         $short . '" title="' . $title . '" />';
136                 $class = '';
137         }
138         if ($_noicon || $_paraedit) $icon = ''; // No more icon
139         if ($_nolabel) {
140                 if (!$_noicon) {
141                         $s_label = '';     // No label with an icon
142                 } else {
143                         $s_label = $short; // Short label without an icon
144                 }
145         } else {
146                 if ($s_label == '') $s_label = $title; // Rich label with an icon
147         }
148
149         // URL
150         if ($isfreeze) {
151                 $url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
152         } else {
153                 if ($id != '') {
154                         $s_id = '&amp;id=' . $id;
155                 } else {
156                         $s_id = '';
157                 }
158                 $url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
159         }
160         $atag  = '<a class="' . $class . '" href="' . $url . '" title="' . $title . '">';
161         static $atags = '</a>';
162
163         if ($ispage) {
164                 // Normal edit link
165                 return $atag . $icon . $s_label . $atags;
166         } else {
167                 // Dangling edit link
168                 return $atag . $icon . $atags . '<span class="noexists">' .
169                         $s_label . $atag . '?' . $atags . '</span>';
170         }
171 }
172
173 // Write, add, or insert new comment
174 function plugin_edit_write()
175 {
176         global $vars;
177         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
178
179         $page = isset($vars['page']) ? $vars['page'] : '';
180         $retvars = array();
181
182         // ¼ê½ñ¤­¤Î#freeze¤òºï½ü
183         $vars['msg'] = preg_replace('/^#freeze\s*$/im','',$vars['msg']);
184         $postdata = $postdata_input = $vars['msg'];
185
186         if (isset($vars['add']) && $vars['add']) {
187                 if (isset($vars['add_top']) && $vars['add_top']) {
188                         $postdata  = $postdata . "\n\n" . @join('', get_source($page));
189                 } else {
190                         $postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
191                 }
192         }
193
194         $oldpagesrc = join('', get_source($page));
195         $oldpagemd5 = md5($oldpagesrc);
196
197         if (! isset($vars['digest']) || $vars['digest'] != $oldpagemd5) {
198                 $vars['digest'] = $oldpagemd5;
199
200                 $retvars['msg'] = $_title_collided;
201                 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $postdata_input, $vars['original']);
202
203                 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided)."\n";
204
205                 if (TRUE) {
206                         global $do_update_diff_table;
207                         $retvars['body'] .= $do_update_diff_table;
208                 }
209
210                 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
211         }
212         else {
213                 $notimestamp = (isset($vars['notimestamp']) && $vars['notimestamp'] != '');
214                 page_write($page, $postdata, $notimestamp);
215
216                 if ($postdata) {
217                         header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
218                         exit;
219                 }
220
221                 $retvars['msg'] = $_title_deleted;
222                 $retvars['body'] = str_replace('$1', htmlspecialchars($page), $_title_deleted);
223                 tb_delete($page);
224         }
225
226         return $retvars;
227 }
228
229 // Cancel (Back to the page / Escape edit page)
230 function plugin_edit_cancel()
231 {
232         global $vars;
233         header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));
234         exit;
235 }
236
237 ?>