2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: edit.inc.php,v 1.30 2005/01/23 05:22:25 henoheno Exp $
8 function plugin_edit_action()
10 global $vars, $_title_edit, $load_template_func;
12 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits editing');
14 $page = isset($vars['page']) ? $vars['page'] : '';
16 check_editable($page, true, true);
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();
26 $postdata = @join('', get_source($page));
27 if ($postdata == '') $postdata = auto_template($page);
29 return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
33 function plugin_edit_preview()
36 global $_title_preview, $_msg_preview, $_msg_preview_delete;
38 $page = isset($vars['page']) ? $vars['page'] : '';
41 if (isset($vars['template_page']) && is_page($vars['template_page'])) {
43 $vars['msg'] = join('', get_source($vars['template_page']));
46 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);
49 // ¼ê½ñ¤¤Î#freeze¤òºï½ü
50 $vars['msg'] = preg_replace('/^#freeze\s*$/im', '', $vars['msg']);
51 $postdata = $vars['msg'];
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));
57 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
61 $body = "$_msg_preview<br />\n";
63 $body .= "<strong>$_msg_preview_delete</strong>";
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";
72 $body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
74 return array('msg'=>$_title_preview, 'body'=>$body);
77 // Inline: Show edit (or unfreeze text) link
78 function plugin_edit_inline()
80 static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
82 global $script, $vars, $fixed_heading_anchor_edit;
84 if (PKWK_READONLY) return ''; // Show nothing
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){
95 case 'nolabel': $_nolabel = TRUE; break;
96 case 'noicon': $_noicon = TRUE; break;
97 default: return $usage;
101 // Separate a page-name and a fixed anchor
102 list($s_page, $id, $editable) = anchor_explode($page, TRUE);
104 if ($s_page == '') $s_page = isset($vars['page']) ? $vars['page'] : '';
106 $isfreeze = is_freeze($s_page);
107 $ispage = is_page($s_page);
109 // Paragraph edit enabled or not
110 $short = htmlspecialchars('Edit');
111 if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
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"';
120 // Normal editing / unfreeze
123 $title = 'Unfreeze %s';
124 $icon = 'unfreeze.png';
129 $title = htmlspecialchars(sprintf($title, $s_page));
130 $icon = '<img src="' . IMAGE_DIR . $icon .
131 '" width="20" height="20" alt="' .
132 $short . '" title="' . $title . '" />';
135 if ($_noicon) $icon = ''; // No more icon
138 $s_label = ''; // No label with an icon
140 $s_label = $short; // Short label without an icon
143 if ($s_label == '') $s_label = $title; // Rich label with an icon
148 $url = $script . '?cmd=unfreeze&page=' . rawurlencode($s_page);
151 $s_id = '&id=' . $id;
155 $url = $script . '?cmd=edit&page=' . rawurlencode($s_page) . $s_id;
157 $atag = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
158 static $atags = '</a>';
162 return $atag . $icon . $s_label . $atags;
164 // Dangling edit link
165 return '<span class="noexists">' . $atag . $icon . $atags .
166 $s_label . $atag . '?' . $atags . '</span>';
170 // Write, add, or insert new comment
171 function plugin_edit_write()
174 global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
176 $page = isset($vars['page']) ? $vars['page'] : '';
179 // ¼ê½ñ¤¤Î#freeze¤òºï½ü
180 $vars['msg'] = preg_replace('/^#freeze\s*$/im','',$vars['msg']);
181 $postdata = $postdata_input = $vars['msg'];
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));
187 $postdata = @join('', get_source($page)) . "\n\n" . $postdata;
191 $oldpagesrc = join('', get_source($page));
192 $oldpagemd5 = md5($oldpagesrc);
194 if (! isset($vars['digest']) || $vars['digest'] != $oldpagemd5) {
195 $vars['digest'] = $oldpagemd5;
197 $retvars['msg'] = $_title_collided;
198 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $postdata_input, $vars['original']);
200 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
203 global $do_update_diff_table;
204 $retvars['body'] .= $do_update_diff_table;
207 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
210 $notimestamp = (isset($vars['notimestamp']) && $vars['notimestamp'] != '');
211 page_write($page, $postdata, $notimestamp);
215 header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
219 $retvars['msg'] = $_title_deleted;
220 $retvars['body'] = str_replace('$1', htmlspecialchars($page), $_title_deleted);
227 // Cancel (Back to the page / Escape edit page)
228 function plugin_edit_cancel()
232 header('Location: ' . get_script_uri() . '?' . rawurlencode($vars['page']));