]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
Recursive array editing: support global_data
[SourceForge/phpwiki.git] / lib / plugin / EditMetaData.php
1 <?php // -*-php-*-
2 rcs_id('$Id: EditMetaData.php,v 1.13 2007-05-15 16:32:25 rurban Exp $');
3 /**
4  Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Plugin EditMetaData
25  *
26  * This plugin shows the current page-level metadata and gives an
27  * entry box for adding a new field or changing an existing one. (A
28  * field can be deleted by specifying a blank value.) Certain fields,
29  * such as 'hits' cannot be changed.
30  *
31  * If there is a reason to do so, I will add support for revision-
32  * level metadata as well.
33  *
34  * Access by restricted to ADMIN_USER
35  *
36  * Written by MichaelVanDam, to test out some ideas about
37  * PagePermissions and PageTypes.
38  *
39  * Rewritten for recursive array support by ReiniUrban.
40  */
41
42 require_once('lib/plugin/_BackendInfo.php');
43
44 class WikiPlugin_EditMetaData 
45 extends WikiPlugin__BackendInfo
46 {
47     function getName () {
48         return _("EditMetaData");
49     }
50
51     function getDescription () {
52         return sprintf(_("Edit metadata for %s"), '[pagename]');
53     }
54
55     function getVersion() {
56         return preg_replace("/[Revision: $]/", '',
57                             "\$Revision: 1.13 $");
58     }
59
60     function getDefaultArguments() {
61         return array('page'       => '[pagename]'
62                     );
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66         $this->_args = $this->getArgs($argstr, $request);
67         extract($this->_args);
68         if (!$page)
69             return '';
70
71         $this->hidden_pagemeta = array ('_cached_html');
72         $this->readonly_pagemeta = array ('hits', 'passwd');
73         $dbi = $request->getDbh();
74         $p = $dbi->getPage($page);
75         $pagemeta = $p->getMetaData();
76         $this->chunk_split = false;
77         
78         // Look at arguments to see if submit was entered. If so,
79         // process this request before displaying.
80         //
81         if ($request->isPost() 
82             and $request->_user->isAdmin() 
83             and $request->getArg('metaedit')) 
84         {
85             $metafield = trim($request->getArg('metafield'));
86             $metavalue = trim($request->getArg('metavalue'));
87             $meta = $request->getArg('meta');
88             $changed = 0;
89             // meta[__global[_upgrade][name]] => 1030.13
90             foreach ($meta as $key => $val) {
91                 if ($val != $pagemeta[$key] 
92                     and !in_array($key, $this->readonly_pagemeta)) 
93                 {
94                     $changed++;
95                     $p->set($key, $val);
96                 }
97             }
98             if ($metafield and !in_array($metafield, $this->readonly_pagemeta)) {
99                 // __global[_upgrade][name] => 1030.13
100                 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
101                     list(, $array_field, $array_key) = $matches;
102                     $array_value = $pagemeta[$array_field];
103                     $array_value[$array_key] = $metavalue;
104                     if ($pagemeta[$array_field] != $array_value) {
105                         $changed++;
106                         $p->set($array_field, $array_value);
107                     }
108                 } elseif ($pagemeta[$metafield] != $metavalue) {
109                     $changed++;
110                     $p->set($metafield, $metavalue);
111                 }
112             }
113             if ($changed) {
114                 $dbi->touch();
115                 $url = $request->getURLtoSelf(false, 
116                                           array('meta','metaedit','metafield','metavalue'));
117                 $request->redirect($url);
118                 // The rest of the output will not be seen due to the
119                 // redirect.
120                 return;
121             }
122         }
123
124         // Now we show the meta data and provide entry box for new data.
125         $html = HTML();
126         //$html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:",
127         //                              $page)));
128         //$dl = $this->_display_values('', $pagemeta);
129         //$html->pushContent($dl);
130         if (!$pagemeta) {
131             // FIXME: invalid HTML
132             $html->pushContent(HTML::p(fmt("No metadata for %s", $page)));
133             $table = HTML();
134         }
135         else {
136             $table = HTML::table(array('border' => 1,
137                                        'cellpadding' => 2,
138                                        'cellspacing' => 0));
139             $this->_fixupData($pagemeta);
140             $table->pushContent($this->_showhash("MetaData('$page')", $pagemeta));
141         }
142
143         if ($request->_user->isAdmin()) {
144             $action = $request->getPostURL();
145             $hiddenfield = HiddenInputs($request->getArgs());
146             $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving the value-box empty.");
147             $keyfield = HTML::input(array('name' => 'metafield'), '');
148             $valfield = HTML::input(array('name' => 'metavalue'), '');
149             $button = Button('submit:metaedit', _("Submit"), false);
150             $form = HTML::form(array('action' => $action,
151                                      'method' => 'post',
152                                      'accept-charset' => $GLOBALS['charset']),
153                                $hiddenfield,
154                                // edit existing fields
155                                $table,
156                                // add new ones
157                                $instructions, HTML::br(),
158                                $keyfield, ' => ', $valfield,
159                                HTML::raw('&nbsp;'), $button
160                                );
161
162             $html->pushContent($form);
163         } else {
164             $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
165         }
166         return $html;
167     }
168
169     function _showvalue ($key, $val, $prefix='') {
170         if (is_array($val) or is_object($val)) return $val;
171         if (in_array($key, $this->hidden_pagemeta)) return '';
172         if ($prefix) {
173             $fullkey = $prefix . '[' . $key . ']';
174             if (substr($fullkey,0,1) == '[') {
175                 $meta = "meta".$fullkey;
176                 $fullkey = preg_replace("/\]\[/", "[", substr($fullkey, 1), 1);
177             } else {
178                 $meta = preg_replace("/^([^\[]+)\[/", "meta[$1][", $fullkey, 1);
179             }
180         } else {
181             $fullkey = $key;
182             $meta = "meta[".$key."]";
183         }
184         //$meta = "meta[".$fullkey."]";
185         $arr = array('name' => $meta, 'value' => $val);
186         if (strlen($val) > 20)
187             $arr['size'] = strlen($val);
188         if (in_array($key, $this->readonly_pagemeta)) {
189             $arr['readonly'] = 'readonly';
190             return HTML::input($arr);
191         } else {
192             return HTML(HTML::em($fullkey), HTML::br(),
193                         HTML::input($arr));
194         }
195     }
196 };
197
198 // $Log: not supported by cvs2svn $
199 // Revision 1.12  2007/01/04 16:46:31  rurban
200 // Make the header a h3
201 //
202 // Revision 1.11  2004/06/01 16:48:11  rurban
203 // dbi->touch
204 // security fix to allow post admin only.
205 //
206 // Revision 1.10  2004/04/18 01:11:52  rurban
207 // more numeric pagename fixes.
208 // fixed action=upload with merge conflict warnings.
209 // charset changed from constant to global (dynamic utf-8 switching)
210 //
211 // Revision 1.9  2004/02/17 12:11:36  rurban
212 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
213 //
214 // Revision 1.8  2003/11/27 17:05:41  carstenklapp
215 // Update: Omit page cache object ('_cached_html') from metadata display.
216 //
217 // Revision 1.7  2003/11/22 17:41:42  carstenklapp
218 // Minor internal change: Removed redundant call to gettext within
219 // fmt(). (locale make: EditMetaData.php:113: warning: keyword nested in
220 // keyword arg)
221 //
222 // Revision 1.6  2003/02/26 01:56:52  dairiki
223 // Tuning/fixing of POST action URLs and hidden inputs.
224 //
225 // Revision 1.5  2003/02/21 04:17:13  dairiki
226 // Delete now irrelevant comment.
227 //
228 // Revision 1.4  2003/01/18 21:41:01  carstenklapp
229 // Code cleanup:
230 // Reformatting & tabs to spaces;
231 // Added copyleft, getVersion, getDescription, rcs_id.
232 //
233
234 // For emacs users
235 // Local Variables:
236 // mode: php
237 // tab-width: 8
238 // c-basic-offset: 4
239 // c-hanging-comment-ender-p: nil
240 // indent-tabs-mode: nil
241 // End:
242 ?>