4 * Copyright 1999,2000,2001,2002,2007 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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.
31 * If there is a reason to do so, I will add support for revision-
32 * level metadata as well.
34 * Access by restricted to ADMIN_USER
36 * Written by Michael Van Dam, to test out some ideas about
37 * PagePermissions and PageTypes.
39 * Rewritten for recursive array support by Reini Urban.
42 require_once 'lib/plugin/DebugBackendInfo.php';
44 class WikiPlugin_EditMetaData
45 extends WikiPlugin_DebugBackendInfo
47 function getDescription()
49 return sprintf(_("Edit metadata for %s."), '[pagename]');
52 function getDefaultArguments()
54 return array('page' => '[pagename]');
57 function run($dbi, $argstr, &$request, $basepage)
59 $this->_args = $this->getArgs($argstr, $request);
60 extract($this->_args);
64 $this->hidden_pagemeta = array('_cached_html');
65 $this->readonly_pagemeta = array('hits', 'passwd');
66 $dbi = $request->getDbh();
67 $p = $dbi->getPage($page);
68 $pagemeta = $p->getMetaData();
69 $this->chunk_split = false;
71 // Look at arguments to see if submit was entered. If so,
72 // process this request before displaying.
74 if ($request->isPost()
75 and $request->_user->isAdmin()
76 and $request->getArg('metaedit')
78 $metafield = trim($request->getArg('metafield'));
79 $metavalue = trim($request->getArg('metavalue'));
80 $meta = $request->getArg('meta');
82 // meta[__global[_upgrade][name]] => 1030.13
83 foreach ($meta as $key => $val) {
84 if ($val != $pagemeta[$key]
85 and !in_array($key, $this->readonly_pagemeta)
91 if ($metafield and !in_array($metafield, $this->readonly_pagemeta)) {
92 // __global[_upgrade][name] => 1030.13
93 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
94 list(, $array_field, $array_key) = $matches;
95 $array_value = $pagemeta[$array_field];
96 $array_value[$array_key] = $metavalue;
97 if ($pagemeta[$array_field] != $array_value) {
99 $p->set($array_field, $array_value);
101 } elseif ($pagemeta[$metafield] != $metavalue) {
103 $p->set($metafield, $metavalue);
108 $url = $request->getURLtoSelf(false,
109 array('meta', 'metaedit', 'metafield', 'metavalue'));
110 $request->redirect($url);
111 // The rest of the output will not be seen due to the
117 // Now we show the meta data and provide entry box for new data.
119 //$html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:",
121 //$dl = $this->_display_values('', $pagemeta);
122 //$html->pushContent($dl);
124 // FIXME: invalid HTML
125 $html->pushContent(HTML::p(fmt("No metadata for %s", $page)));
128 $table = HTML::table(array('border' => 1,
130 'cellspacing' => 0));
131 $this->_fixupData($pagemeta);
132 $table->pushContent($this->_showhash("MetaData('$page')", $pagemeta));
135 if ($request->_user->isAdmin()) {
136 $action = $request->getPostURL();
137 $hiddenfield = HiddenInputs($request->getArgs());
138 $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving the value-box empty.");
139 $keyfield = HTML::input(array('name' => 'metafield'), '');
140 $valfield = HTML::input(array('name' => 'metavalue'), '');
141 $button = Button('submit:metaedit', _("Submit"), false);
142 $form = HTML::form(array('action' => $action,
144 'accept-charset' => 'UTF-8'),
146 // edit existing fields
149 $instructions, HTML::br(),
150 $keyfield, ' => ', $valfield,
151 HTML::raw(' '), $button
154 $html->pushContent($form);
156 $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
161 protected function _showvalue($key, $val, $prefix = '')
163 if (is_array($val) or is_object($val)) return $val;
164 if (in_array($key, $this->hidden_pagemeta)) return '';
166 $fullkey = $prefix . '[' . $key . ']';
167 if (substr($fullkey, 0, 1) == '[') {
168 $meta = "meta" . $fullkey;
169 $fullkey = preg_replace("/\]\[/", "[", substr($fullkey, 1), 1);
171 $meta = preg_replace("/^([^\[]+)\[/", "meta[$1][", $fullkey, 1);
175 $meta = "meta[" . $key . "]";
177 //$meta = "meta[".$fullkey."]";
178 $arr = array('name' => $meta, 'value' => $val);
179 if (strlen($val) > 20)
180 $arr['size'] = strlen($val);
181 if (in_array($key, $this->readonly_pagemeta)) {
182 $arr['readonly'] = 'readonly';
183 return HTML::input($arr);
185 return HTML(HTML::em($fullkey), HTML::br(),
195 // c-hanging-comment-ender-p: nil
196 // indent-tabs-mode: nil