]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
Use HTML 5 DOCTYPE; force CHARSET=utf-8
[SourceForge/phpwiki.git] / lib / plugin / EditMetaData.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 Michael Van Dam, to test out some ideas about
37  * PagePermissions and PageTypes.
38  *
39  * Rewritten for recursive array support by Reini Urban.
40  */
41
42 require_once 'lib/plugin/_BackendInfo.php';
43
44 class WikiPlugin_EditMetaData
45     extends WikiPlugin__BackendInfo
46 {
47     function getName()
48     {
49         return _("EditMetaData");
50     }
51
52     function getDescription()
53     {
54         return sprintf(_("Edit metadata for %s."), '[pagename]');
55     }
56
57     function getDefaultArguments()
58     {
59         return array('page' => '[pagename]');
60     }
61
62     function run($dbi, $argstr, &$request, $basepage)
63     {
64         $this->_args = $this->getArgs($argstr, $request);
65         extract($this->_args);
66         if (!$page)
67             return '';
68
69         $this->hidden_pagemeta = array('_cached_html');
70         $this->readonly_pagemeta = array('hits', 'passwd');
71         $dbi = $request->getDbh();
72         $p = $dbi->getPage($page);
73         $pagemeta = $p->getMetaData();
74         $this->chunk_split = false;
75
76         // Look at arguments to see if submit was entered. If so,
77         // process this request before displaying.
78         //
79         if ($request->isPost()
80             and $request->_user->isAdmin()
81                 and $request->getArg('metaedit')
82         ) {
83             $metafield = trim($request->getArg('metafield'));
84             $metavalue = trim($request->getArg('metavalue'));
85             $meta = $request->getArg('meta');
86             $changed = 0;
87             // meta[__global[_upgrade][name]] => 1030.13
88             foreach ($meta as $key => $val) {
89                 if ($val != $pagemeta[$key]
90                     and !in_array($key, $this->readonly_pagemeta)
91                 ) {
92                     $changed++;
93                     $p->set($key, $val);
94                 }
95             }
96             if ($metafield and !in_array($metafield, $this->readonly_pagemeta)) {
97                 // __global[_upgrade][name] => 1030.13
98                 if (preg_match('/^(.*?)\[(.*?)\]$/', $metafield, $matches)) {
99                     list(, $array_field, $array_key) = $matches;
100                     $array_value = $pagemeta[$array_field];
101                     $array_value[$array_key] = $metavalue;
102                     if ($pagemeta[$array_field] != $array_value) {
103                         $changed++;
104                         $p->set($array_field, $array_value);
105                     }
106                 } elseif ($pagemeta[$metafield] != $metavalue) {
107                     $changed++;
108                     $p->set($metafield, $metavalue);
109                 }
110             }
111             if ($changed) {
112                 $dbi->touch();
113                 $url = $request->getURLtoSelf(false,
114                     array('meta', 'metaedit', 'metafield', 'metavalue'));
115                 $request->redirect($url);
116                 // The rest of the output will not be seen due to the
117                 // redirect.
118                 return '';
119             }
120         }
121
122         // Now we show the meta data and provide entry box for new data.
123         $html = HTML();
124         //$html->pushContent(HTML::h3(fmt("Existing page-level metadata for %s:",
125         //                                $page)));
126         //$dl = $this->_display_values('', $pagemeta);
127         //$html->pushContent($dl);
128         if (!$pagemeta) {
129             // FIXME: invalid HTML
130             $html->pushContent(HTML::p(fmt("No metadata for %s", $page)));
131             $table = HTML();
132         } else {
133             $table = HTML::table(array('border' => 1,
134                 'cellpadding' => 2,
135                 'cellspacing' => 0));
136             $this->_fixupData($pagemeta);
137             $table->pushContent($this->_showhash("MetaData('$page')", $pagemeta));
138         }
139
140         if ($request->_user->isAdmin()) {
141             $action = $request->getPostURL();
142             $hiddenfield = HiddenInputs($request->getArgs());
143             $instructions = _("Add or change a page-level metadata 'key=>value' pair. Note that you can remove a key by leaving the value-box empty.");
144             $keyfield = HTML::input(array('name' => 'metafield'), '');
145             $valfield = HTML::input(array('name' => 'metavalue'), '');
146             $button = Button('submit:metaedit', _("Submit"), false);
147             $form = HTML::form(array('action' => $action,
148                     'method' => 'post',
149                     'accept-charset' => 'UTF-8'),
150                 $hiddenfield,
151                 // edit existing fields
152                 $table,
153                 // add new ones
154                 $instructions, HTML::br(),
155                 $keyfield, ' => ', $valfield,
156                 HTML::raw('&nbsp;'), $button
157             );
158
159             $html->pushContent($form);
160         } else {
161             $html->pushContent(HTML::em(_("Requires WikiAdmin privileges to edit.")));
162         }
163         return $html;
164     }
165
166     function _showvalue($key, $val, $prefix = '')
167     {
168         if (is_array($val) or is_object($val)) return $val;
169         if (in_array($key, $this->hidden_pagemeta)) return '';
170         if ($prefix) {
171             $fullkey = $prefix . '[' . $key . ']';
172             if (substr($fullkey, 0, 1) == '[') {
173                 $meta = "meta" . $fullkey;
174                 $fullkey = preg_replace("/\]\[/", "[", substr($fullkey, 1), 1);
175             } else {
176                 $meta = preg_replace("/^([^\[]+)\[/", "meta[$1][", $fullkey, 1);
177             }
178         } else {
179             $fullkey = $key;
180             $meta = "meta[" . $key . "]";
181         }
182         //$meta = "meta[".$fullkey."]";
183         $arr = array('name' => $meta, 'value' => $val);
184         if (strlen($val) > 20)
185             $arr['size'] = strlen($val);
186         if (in_array($key, $this->readonly_pagemeta)) {
187             $arr['readonly'] = 'readonly';
188             return HTML::input($arr);
189         } else {
190             return HTML(HTML::em($fullkey), HTML::br(),
191                 HTML::input($arr));
192         }
193     }
194 }
195
196 // Local Variables:
197 // mode: php
198 // tab-width: 8
199 // c-basic-offset: 4
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil
202 // End: