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