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