OSDN Git Service

InterWikiName: Correct Google.jp => Google, Yahoo.jp => Yahoo
[pukiwiki/pukiwiki.git] / plugin / edit.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: edit.inc.php,v 1.30 2005/01/23 05:22:25 henoheno Exp $
4 //
5 // Edit plugin
6 // cmd=edit
7
8 function plugin_edit_action()
9 {
10         global $vars, $_title_edit, $load_template_func;
11
12         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
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         if (PKWK_READONLY) return ''; // Show nothing 
85
86         // Arguments
87         $args = func_get_args();
88         $s_label = array_pop($args); // {label}
89         $page    = array_shift($args);
90         if($page == NULL) $page = '';
91         $_noicon = $_nolabel = FALSE;
92         foreach($args as $arg){
93                 switch($arg){
94                 case '': break;
95                 case 'nolabel': $_nolabel = TRUE; break;
96                 case 'noicon':  $_noicon  = TRUE; break;
97                 default: return $usage;
98                 }
99         }
100
101         // Separate a page-name and a fixed anchor
102         list($s_page, $id, $editable) = anchor_explode($page, TRUE);
103         // Default: This one
104         if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
105         // $s_page fixed
106         $isfreeze = is_freeze($s_page);
107         $ispage   = is_page($s_page);
108
109         // Paragraph edit enabled or not
110         $short = htmlspecialchars('Edit');
111         if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
112                 // Paragraph editing
113                 $id    = rawurlencode($id);
114                 $title = htmlspecialchars(sprintf('Edit %s', $page));
115                 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
116                         '" width="9" height="9" alt="' .
117                         $short . '" title="' . $title . '" /> ';
118                 $class = ' class="anchor_super"';
119         } else {
120                 // Normal editing / unfreeze
121                 $id    = '';
122                 if ($isfreeze) {
123                         $title = 'Unfreeze %s';
124                         $icon  = 'unfreeze.png';
125                 } else {
126                         $title = 'Edit %s';
127                         $icon  = 'edit.png';
128                 }
129                 $title = htmlspecialchars(sprintf($title, $s_page));
130                 $icon = '<img src="' . IMAGE_DIR . $icon .
131                         '" width="20" height="20" alt="' .
132                         $short . '" title="' . $title . '" />';
133                 $class = '';
134         }
135         if ($_noicon) $icon = ''; // No more icon
136         if ($_nolabel) {
137                 if (!$_noicon) {
138                         $s_label = '';     // No label with an icon
139                 } else {
140                         $s_label = $short; // Short label without an icon
141                 }
142         } else {
143                 if ($s_label == '') $s_label = $title; // Rich label with an icon
144         }
145
146         // URL
147         if ($isfreeze) {
148                 $url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
149         } else {
150                 if ($id != '') {
151                         $s_id = '&amp;id=' . $id;
152                 } else {
153                         $s_id = '';
154                 }
155                 $url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
156         }
157         $atag  = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
158         static $atags = '</a>';
159
160         if ($ispage) {
161                 // Normal edit link
162                 return $atag . $icon . $s_label . $atags;
163         } else {
164                 // Dangling edit link
165                 return '<span class="noexists">' . $atag . $icon . $atags .
166                         $s_label . $atag . '?' . $atags . '</span>';
167         }
168 }
169
170 // Write, add, or insert new comment
171 function plugin_edit_write()
172 {
173         global $vars;
174         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
175
176         $page = isset($vars['page']) ? $vars['page'] : '';
177         $retvars = array();
178
179         // ¼ê½ñ¤­¤Î#freeze¤òºï½ü
180         $vars['msg'] = preg_replace('/^#freeze\s*$/im','',$vars['msg']);
181         $postdata = $postdata_input = $vars['msg'];
182
183         if (isset($vars['add']) && $vars['add']) {
184                 if (isset($vars['add_top']) && $vars['add_top']) {
185                         $postdata  = $postdata . "\n\n" . @join('', get_source($page));
186                 } else {
187                         $postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
188                 }
189         }
190
191         $oldpagesrc = join('', get_source($page));
192         $oldpagemd5 = md5($oldpagesrc);
193
194         if (! isset($vars['digest']) || $vars['digest'] != $oldpagemd5) {
195                 $vars['digest'] = $oldpagemd5;
196
197                 $retvars['msg'] = $_title_collided;
198                 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $postdata_input, $vars['original']);
199
200                 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
201
202                 if (TRUE) {
203                         global $do_update_diff_table;
204                         $retvars['body'] .= $do_update_diff_table;
205                 }
206
207                 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
208         }
209         else {
210                 $notimestamp = (isset($vars['notimestamp']) && $vars['notimestamp'] != '');
211                 page_write($page, $postdata, $notimestamp);
212
213                 if ($postdata) {
214                         pkwk_headers_sent();
215                         header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
216                         exit;
217                 }
218
219                 $retvars['msg'] = $_title_deleted;
220                 $retvars['body'] = str_replace('$1', htmlspecialchars($page), $_title_deleted);
221                 tb_delete($page);
222         }
223
224         return $retvars;
225 }
226
227 // Cancel (Back to the page / Escape edit page)
228 function plugin_edit_cancel()
229 {
230         global $vars;
231         pkwk_headers_sent();
232         header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));
233         exit;
234 }
235
236 ?>