]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSelect.php
Use real list instead of middot
[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, 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     public $_list;
41     public $_args;
42
43     function getDescription()
44     {
45         return _("Allows selection of multiple pages which get passed to other WikiAdmin plugins.");
46     }
47
48     function getDefaultArguments()
49     {
50         return array('s' => '', // preselect pages
51             /* select pages by meta-data: */
52             'author' => false,
53             'owner' => false,
54             'creator' => false,
55             'only' => '',
56             'exclude' => '',
57             'info' => 'most',
58             'sortby' => 'pagename',
59             'limit' => 0,
60             'paging' => 'none'
61         );
62     }
63
64     /**
65      * Default collector for all WikiAdmin* plugins.
66      * preSelectS() is similar, but fills $this->_list
67      *
68      * @param array $list
69      * @param WikiDB $dbi
70      * @param string $sortby
71      * @param int $limit
72      * @param string $exclude
73      * @return array
74      */
75     protected function collectPages(&$list, &$dbi, $sortby, $limit = 0, $exclude = '')
76     {
77         $allPages = $dbi->getAllPages(0, $sortby, $limit, $exclude);
78         while ($pagehandle = $allPages->next()) {
79             $pagename = $pagehandle->getName();
80             if (empty($list[$pagename]))
81                 $list[$pagename] = 0;
82         }
83         return $list;
84     }
85
86     /**
87      * Preselect a list of pagenames by the supporting the following args:
88      * 's': comma-separated list of pagename wildcards
89      * 'author', 'owner', 'creator': from WikiDB_Page
90      * 'only: forgot what the difference to 's' was.
91      * Sets $this->_list, which is picked up by collectPages() and is a default for p[]
92      *
93      * @param array $args
94      * @param WikiRequest $request
95      * @return array
96      */
97     protected function preSelectS(&$args, &$request)
98     {
99         // override plugin argument by GET: probably not needed if s||="" is used
100         // anyway, we force it for unique interface.
101         if (!empty($request->getArg['s'])) {
102             $args['s'] = $request->getArg['s'];
103         }
104         if (!empty($args['owner'])) {
105             $sl = PageList::allPagesByOwner($args['owner'], false, $args['sortby'], $args['limit'], $args['exclude']);
106         } elseif (!empty($args['author'])) {
107             $sl = PageList::allPagesByAuthor($args['author'], false, $args['sortby'], $args['limit'], $args['exclude']);
108         } elseif (!empty($args['creator'])) {
109             $sl = PageList::allPagesByCreator($args['creator'], false, $args['sortby'], $args['limit'], $args['exclude']);
110         } elseif (!empty($args['s']) or !empty($args['only'])) {
111             // all pages by name
112             $sl = explodePageList(empty($args['only']) ? $args['s'] : $args['only']);
113         }
114         $this->_list = array();
115         if (!empty($sl)) {
116             $request->setArg('verify', 1);
117             foreach ($sl as $name) {
118                 if (!empty($args['exclude'])) {
119                     if (!in_array($name, $args['exclude']))
120                         $this->_list[$name] = 1;
121                 } else {
122                     $this->_list[$name] = 1;
123                 }
124             }
125         }
126         return $this->_list;
127     }
128
129     /**
130      * @param WikiDB $dbi
131      * @param string $argstr
132      * @param WikiRequest $request
133      * @param string $basepage
134      * @return mixed
135      */
136     function run($dbi, $argstr, &$request, $basepage)
137     {
138         //if ($request->getArg('action') != 'browse')
139         //    return $this->disabled("(action != 'browse')");
140         $args = $this->getArgs($argstr, $request);
141         $this->_args = $args;
142         extract($args);
143         $this->preSelectS($args, $request);
144
145         $info = $args['info'];
146
147         $form = HTML::form(array('action' => $request->getPostURL(), 'method' => 'post'));
148         if ($request->getArg('WikiAdminSelect') == _("Go"))
149             $p = false;
150         else
151             $p = $request->getArg('p');
152         $form->pushContent(HTML::p(array('class' => 'wikitext'), _("Select: "),
153             HTML::input(array('type' => 'text',
154                 'name' => 's',
155                 'value' => $args['s'])),
156             HTML::input(array('type' => 'submit',
157                 'name' => 'WikiAdminSelect',
158                 'value' => _("Go")))));
159         if (!$request->getArg('verify')) {
160             $form->pushContent(HTML::input(array('type' => 'hidden',
161                 'name' => 'action',
162                 'value' => 'verify')));
163             $form->pushContent(Button('submit:verify', _("Select pages"),
164                     'wikiadmin'),
165                 Button('submit:cancel', _("Cancel"), 'button'));
166         } else {
167             global $WikiTheme;
168             $form->pushContent(HTML::input(array('type' => 'hidden',
169                     'name' => 'action',
170                     'value' => 'WikiAdminSelect'))
171             );
172             // Add the Buttons for all registered WikiAdmin plugins
173             $plugin_dir = 'lib/plugin';
174             if (defined('PHPWIKI_DIR'))
175                 $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
176             $fs = new fileSet($plugin_dir, 'WikiAdmin*.php');
177             $actions = $fs->getFiles();
178             sort($actions);
179             foreach ($actions as $f) {
180                 $f = preg_replace('/.php$/', '', $f);
181                 $s = preg_replace('/^WikiAdmin/', '', $f);
182                 if (!in_array($s, array("Select", "Utils"))) { // disable Select and Utils
183                     $form->pushContent(Button("submit:wikiadmin[$f]", _($s), "wikiadmin"));
184                     $form->pushContent($WikiTheme->getButtonSeparator());
185                 }
186             }
187             // $form->pushContent(Button('submit:cancel', _("Cancel"), 'button'));
188         }
189
190         if ($request->isPost()
191             && !$request->getArg('wikiadmin')
192             && !empty($p)
193         ) {
194             $this->_list = array();
195             // List all selected pages again.
196             foreach ($p as $page => $name) {
197                 $this->_list[$name] = 1;
198             }
199         } elseif ($request->isPost()
200             and $request->_user->isAdmin()
201                 and !empty($p)
202                     //and $request->getArg('verify')
203                     and ($request->getArg('action') == 'WikiAdminSelect')
204                         and $request->getArg('wikiadmin')
205         ) {
206             // handle external plugin
207             $loader = new WikiPluginLoader();
208             $a = array_keys($request->getArg('wikiadmin'));
209             $plugin_action = $a[0];
210             $single_arg_plugins = array("Remove");
211             if (in_array($plugin_action, $single_arg_plugins)) {
212                 $plugin = $loader->getPlugin($plugin_action);
213                 $ul = HTML::ul();
214                 foreach ($p as $page => $name) {
215                     $plugin_args = "run_page=$name";
216                     $request->setArg($plugin_action, 1);
217                     $request->setArg('p', array($page => $name));
218                     // if the plugin requires more args than the pagename,
219                     // then this plugin will not return. (Rename, SearchReplace, ...)
220                     $action_result = $plugin->run($dbi, $plugin_args, $request, $basepage);
221                     $ul->pushContent(HTML::li(fmt("Selected page ā€œ%sā€ passed to ā€œ%sā€.",
222                         $name, $select)));
223                     $ul->pushContent(HTML::ul(HTML::li($action_result)));
224                 }
225             } else {
226                 // redirect to the plugin page.
227                 // in which page is this plugin?
228                 $plugin_action = preg_replace("/^WikiAdmin/", "", $plugin_action);
229                 $args = array();
230                 foreach ($p as $page => $x) {
231                     $args["p[$page]"] = 1;
232                 }
233                 header("Location: " .
234                     WikiURL(__("PhpWikiAdministration")."/".__($plugin_action), $args, 1));
235                 exit();
236             }
237         } elseif (empty($args['s'])) {
238             // List all pages to select from.
239             $this->_list = $this->collectPages($this->_list, $dbi, $args['sortby'], $args['limit']);
240         }
241         $pagelist = new PageList_Selectable($info, $args['exclude'], $args);
242         $pagelist->addPageList($this->_list);
243         $form->pushContent($pagelist->getContent());
244         foreach ($args as $k => $v) {
245             if (!in_array($k, array('s', 'WikiAdminSelect', 'action', 'verify')))
246                 $form->pushContent(HiddenInputs(array($k => $v))); // plugin params
247         }
248         if (!$request->getArg('select')) {
249             return $form;
250         } else {
251             ; //return $action_result;
252         }
253         return '';
254     }
255
256     protected function tablePush(&$table, $first, $second)
257     {
258         $table->pushContent(
259             HTML::tr(
260                 HTML::td($first),
261                 HTML::td($second)));
262     }
263
264 }
265
266 // Local Variables:
267 // mode: php
268 // tab-width: 8
269 // c-basic-offset: 4
270 // c-hanging-comment-ender-p: nil
271 // indent-tabs-mode: nil
272 // End: