OSDN Git Service

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