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