]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/EditMetaData.php
Normalize header
[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 getVersion() {
56         return preg_replace("/[Revision: $]/", '',
57                             "\$Revision$");
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 // For emacs users
199 // Local Variables:
200 // mode: php
201 // tab-width: 8
202 // c-basic-offset: 4
203 // c-hanging-comment-ender-p: nil
204 // indent-tabs-mode: nil
205 // End:
206 ?>