]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSelect.php
Whitespace only
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSelect.php
1 <?php
2
3 /*
4  * Copyright 2002 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Allows selection of multiple pages which get passed to other
26  * WikiAdmin plugins then. Then do Rename, Remove, Chmod, Chown, ...
27  *
28  * Usage:   <<WikiAdminSelect>>
29  * Author:  Reini Urban <rurban@x-ray.at>
30  *
31  * This is the base class for most WikiAdmin* classes, using
32  * collectPages() and preSelectS().
33  * "list" PagePermissions supported implicitly by PageList.
34  */
35 require_once 'lib/PageList.php';
36
37 class WikiPlugin_WikiAdminSelect
38 extends WikiPlugin
39 {
40     function getName() {
41         return _("WikiAdminSelect");
42     }
43
44     function getDescription() {
45         return _("Allows selection of multiple pages which get passed to other WikiAdmin plugins.");
46     }
47
48     function getDefaultArguments() {
49         return array('s'       => '', // preselect pages
50                      /* select pages by meta-data: */
51                      'author'   => false,
52                      'owner'    => false,
53                      'creator'  => false,
54                      'only'    => '',
55                      'exclude' => '',
56                      'info'    => 'most',
57                      'sortby'  => 'pagename',
58                      'limit'   => 0,
59                      'paging'  => 'none'
60                     );
61     }
62
63     /**
64      * Default collector for all WikiAdmin* plugins.
65      * preSelectS() is similar, but fills $this->_list
66      */
67     function collectPages(&$list, &$dbi, $sortby, $limit=0, $exclude='') {
68         $allPages = $dbi->getAllPages(0, $sortby, $limit, $exclude);
69         while ($pagehandle = $allPages->next()) {
70             $pagename = $pagehandle->getName();
71             if (empty($list[$pagename]))
72                 $list[$pagename] = 0;
73         }
74         return $list;
75     }
76
77     /**
78      * Preselect a list of pagenames by the supporting the following args:
79      * 's': comma-seperated list of pagename wildcards
80      * 'author', 'owner', 'creator': from WikiDB_Page
81      * 'only: forgot what the difference to 's' was.
82      * Sets $this->_list, which is picked up by collectPages() and is a default for p[]
83      */
84     function preSelectS (&$args, &$request) {
85         // override plugin argument by GET: probably not needed if s||="" is used
86         // anyway, we force it for unique interface.
87         if (!empty($request->getArg['s']))
88             $args['s'] = $request->getArg['s'];
89         if ( !empty($args['owner']) )
90             $sl = PageList::allPagesByOwner($args['owner'],false,$args['sortby'],$args['limit'],$args['exclude']);
91         elseif ( !empty($args['author']) )
92             $sl = PageList::allPagesByAuthor($args['author'],false,$args['sortby'],$args['limit'],$args['exclude']);
93         elseif ( !empty($args['creator']) )
94             $sl = PageList::allPagesByCreator($args['creator'],false,$args['sortby'],$args['limit'],$args['exclude']);
95         elseif ( !empty($args['s']) or !empty($args['only']) ) {
96             // all pages by name
97             $sl = explodePageList(empty($args['only']) ? $args['s'] : $args['only']);
98         }
99         $this->_list = array();
100         if (!empty($sl)) {
101             $request->setArg('verify', 1);
102             foreach ($sl as $name) {
103                 if (!empty($args['exclude'])) {
104                     if (!in_array($name, $args['exclude']))
105                         $this->_list[$name] = 1;
106                 } else {
107                     $this->_list[$name] = 1;
108                 }
109             }
110         }
111         return $this->_list;
112     }
113
114     function run($dbi, $argstr, &$request, $basepage) {
115         //if ($request->getArg('action') != 'browse')
116         //    return $this->disabled("(action != 'browse')");
117         $args = $this->getArgs($argstr, $request);
118         $this->_args = $args;
119         extract($args);
120         $this->preSelectS($args, $request);
121
122         $info = $args['info'];
123
124         // array_multisort($this->_list, SORT_NUMERIC, SORT_DESC);
125         $pagename = $request->getArg('pagename');
126         // GetUrlToSelf() with all given params
127         //$uri = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']; // without s would be better.
128         //$uri = $request->getURLtoSelf();//false, array('verify'));
129         $form = HTML::form(array('action' => $request->getPostURL(), 'method' => 'post'));
130         if ($request->getArg('WikiAdminSelect') == _("Go"))
131             $p = false;
132         else
133             $p = $request->getArg('p');
134         //$p = @$GLOBALS['HTTP_POST_VARS']['p'];
135         $form->pushContent(HTML::p(array('class' => 'wikitext'), _("Select: "),
136                                    HTML::input(array('type' => 'text',
137                                                      'name' => 's',
138                                                      'value' => $args['s'])),
139                                    HTML::input(array('type' => 'submit',
140                                                      'name' => 'WikiAdminSelect',
141                                                      'value' => _("Go")))));
142         if (! $request->getArg('verify')) {
143             $form->pushContent(HTML::input(array('type' => 'hidden',
144                                                  'name' => 'action',
145                                                  'value' => 'verify')));
146             $form->pushContent(Button('submit:verify', _("Select pages"),
147                                       'wikiadmin'),
148                                Button('submit:cancel', _("Cancel"), 'button'));
149         } else {
150             global $WikiTheme;
151             $form->pushContent(HTML::input(array('type' => 'hidden',
152                                                  'name' => 'action',
153                                                  'value' => 'WikiAdminSelect'))
154                                );
155             // Add the Buttons for all registered WikiAdmin plugins
156             $plugin_dir = 'lib/plugin';
157             if (defined('PHPWIKI_DIR'))
158                 $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
159             $fs = new fileSet($plugin_dir, 'WikiAdmin*.php');
160             $actions = $fs->getFiles();
161             sort($actions);
162             foreach ($actions as $f) {
163                 $f = preg_replace('/.php$/','', $f);
164                 $s = preg_replace('/^WikiAdmin/','', $f);
165                 if (!in_array($s,array("Select","Utils"))) { // disable Select and Utils
166                     $form->pushContent(Button("submit:wikiadmin[$f]", _($s), "wikiadmin"));
167                     $form->pushContent($WikiTheme->getButtonSeparator());
168                 }
169             }
170             // $form->pushContent(Button('submit:cancel', _("Cancel"), 'button'));
171         }
172
173         if ($request->isPost()
174             && ! $request->getArg('wikiadmin')
175             && !empty($p)) {
176             $this->_list = array();
177             // List all selected pages again.
178             foreach ($p as $page => $name) {
179                 $this->_list[$name] = 1;
180             }
181         }
182         elseif ($request->isPost()
183                 and $request->_user->isAdmin()
184                 and !empty($p)
185                 //and $request->getArg('verify')
186                 and ($request->getArg('action') == 'WikiAdminSelect')
187                 and $request->getArg('wikiadmin')
188                )
189         {
190             // handle external plugin
191             $loader = new WikiPluginLoader();
192             $a = array_keys($request->getArg('wikiadmin'));
193             $plugin_action = $a[0];
194             $single_arg_plugins = array("Remove");
195             if (in_array($plugin_action, $single_arg_plugins)) {
196                 $plugin = $loader->getPlugin($plugin_action);
197                 $ul = HTML::ul();
198                 foreach ($p as $page => $name) {
199                     $plugin_args = "run_page=$name";
200                     $request->setArg($plugin_action, 1);
201                     $request->setArg('p', array($page => $name));
202                     // if the plugin requires more args than the pagename,
203                     // then this plugin will not return. (Rename, SearchReplace, ...)
204                     $action_result = $plugin->run($dbi, $plugin_args, $request, $basepage);
205                     $ul->pushContent(HTML::li(fmt("Selected page '%s' passed to '%s'.",
206                                                   $name, $select)));
207                     $ul->pushContent(HTML::ul(HTML::li($action_result)));
208                 }
209             } else {
210                 // redirect to the plugin page.
211                 // in which page is this plugin?
212                 $plugin_action = preg_replace("/^WikiAdmin/","",$plugin_action);
213                 $args = array();
214                 foreach ($p as $page => $x) {
215                   $args["p[$page]"] = 1;
216                 }
217                 header("Location: ".
218                   WikiURL(_("PhpWikiAdministration")."/"._($plugin_action),$args,1));
219                 exit();
220             }
221         } elseif (empty($args['s'])) {
222             // List all pages to select from.
223             $this->_list = $this->collectPages($this->_list, $dbi, $args['sortby'], $args['limit']);
224         }
225         $pagelist = new PageList_Selectable($info, $args['exclude'], $args);
226         $pagelist->addPageList($this->_list);
227         $form->pushContent($pagelist->getContent());
228         foreach ($args as $k => $v) {
229             if (!in_array($k,array('s','WikiAdminSelect','action','verify')))
230                 $form->pushContent(HiddenInputs(array($k => $v))); // plugin params
231         }
232         if (! $request->getArg('select')) {
233             return $form;
234         } else {
235             ; //return $action_result;
236         }
237     }
238
239     function _tablePush(&$table, $first, $second) {
240         $table->pushContent(
241                             HTML::tr(
242                                      HTML::td($first),
243                                      HTML::td($second)));
244     }
245
246 }
247
248 // Local Variables:
249 // mode: php
250 // tab-width: 8
251 // c-basic-offset: 4
252 // c-hanging-comment-ender-p: nil
253 // indent-tabs-mode: nil
254 // End: