]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/ModeratedPage.php
ModeratePage part1: change status
[SourceForge/phpwiki.git] / lib / plugin / ModeratedPage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: ModeratedPage.php,v 1.1 2004-11-19 19:22:35 rurban Exp $');
3 /*
4  Copyright 2004 $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  * This plugin requires an action page (Default: ModeratedPage)
24  * and provides delayed execution of restricted actions, after
25  * a special moderators request:
26  *   http://mywiki/SomeModeratedPage?action=ModeratedPage&id=kdclcr78431zr43uhrn&pass=approve
27  *
28  * Author: ReiniUrban
29  */
30
31 require_once("lib/WikiPlugin.php");
32
33 class WikiPlugin_ModeratedPage
34 extends WikiPlugin
35 {
36     function getName () {
37         return _("ModeratedPage");
38     }
39     function getDescription () {
40         return _("Support moderated pages");
41     }
42     function getVersion() {
43         return preg_replace("/[Revision: $]/", '',
44                             "\$Revision: 1.1 $");
45     }
46     function getDefaultArguments() {
47         return array('page'          => '[pagename]',
48                      'moderators'    => false,
49                      'require_level' => false,   // 1=bogo
50                      'require_access' => 'edit,remove,change',
51                      'id'   => '',
52                      'pass' => '',
53                     );
54     }
55
56     function run($dbi, $argstr, &$request, $basepage) {
57         $args = $this->getArgs($argstr, $request);
58
59         // handle moderation request from the email
60         if (!empty($args['id']) and !empty($args['pass'])) {
61             if (!$args['page'])
62                 return $this->error("No page specified");
63             $page = $dbi->getPage($args['page']);
64             $moderation = $page->get("moderation");
65             if ($moderation) {
66               if (isset($moderation['id']) and $moderation['id'] == $args['id']) {
67                 // handle defaults:
68                 //   approve or reject
69                 if ($args['pass'] == 'approve')
70                     return $this->approve($args, $moderation);
71                 elseif ($args['pass'] == 'reject')
72                     return $this->reject($args, $moderation);
73                 else
74                     return $this->error("Wrong pass ".$args['pass']);
75               } else {
76                 return $this->error("Wrong id");
77               }
78             }
79         }
80         return '';
81     }
82
83     /**
84      * resolve moderators and require_access from actionpage plugin argstr
85      */
86     function resolve_argstr(&$request, $argstr) {
87         $args = $this->getArgs($argstr);
88         $group = $request->getGroup();
89         if (empty($args['moderators'])) {
90             $admins = $group->getSpecialMembersOf(GROUP_ADMIN);
91             // email or usernames?
92             $args['moderators'] = array_merge($admins, array(ADMIN_USER));
93         } else { 
94             // resolve possible group names
95             $moderators = explode(',',$args['moderators']); 
96             for ($i=0; $i<count($moderators); $i++) {
97                 $members = $group->getMembersOf($moderators[$i]);
98                 if (!empty($members)) {
99                     array_splice($moderators,$i,1,$members);
100                 }
101             }
102             if (!$moderators) $moderators = array(ADMIN_USER);
103             $args['moderators'] = $moderators;
104         }
105         //resolve email for $args['moderators']
106         $page = $request->getPage();
107         $users = array();
108         foreach ($args['moderators'] as $userid) {
109             $users[$userid] = 0;
110         }
111         list($args['emails'], $args['moderators']) = $page->getPageChangeEmails(array($page->getName() => $users));
112         unset($args['id']);
113         unset($args['page']);
114         unset($args['pass']);
115         return $args;
116     }
117     
118     /**
119      * Handle moderation change request by the user.
120      * Hook called on the lock action, if moderation metadata already exists.
121      */
122     function lock_check(&$request, &$page, $moderated) {
123         $action_page = $request->getPage(_("ModeratedPage"));
124         $old_moderation = $this->getStatus($request, $page, $action_page);
125         if (is_array($old_moderation)) {
126             $page->set('moderation', $moderation);
127             return $this->notice(
128                        fmt("ModeratedPage status update:\n  Moderators: '%s'\n  require_access: '%s'", 
129                        join(',',$moderation['moderators']), $moderation['require_access']));
130         } else {
131             $page->set('moderation', false);
132             return $this->notice(HTML($old_moderation,
133                         fmt("'%s' is no ModeratedPage anymore.", $page->getName()))); 
134         }
135     }
136
137     /**
138      * Handle moderation change request by the user.
139      * Hook called on the lock action, if moderation metadata should be added.
140      * Need to store the the plugin args (who, when) in the page meta-data
141      */
142     function lock_add(&$request, &$page, &$action_page) {
143         $moderation = $this->getStatus($request, $page, $action_page);
144         if (is_array($moderation)) {
145             $page->set('moderation', $moderation);
146             return $this->notice(
147                        fmt("ModeratedPage status update: '%s' is now a ModeratedPage.\n  Moderators: '%s'\n  require_access: '%s'", 
148                        $page->getName(), join(',',$moderation['moderators']), $moderation['require_access']));
149         }
150         else { // error
151             return $moderation;
152         }
153     }
154     
155     function notice($msg) {
156         return HTML::div(array('class' => 'wiki-edithelp'), $msg);
157     }
158
159     function getStatus(&$request, &$page, &$action_page) {
160         $loader = new WikiPluginLoader();
161         $rev = $action_page->getCurrentRevision();
162         $content = $rev->getPackedContent();
163         list($pi) = explode("\n", $content, 2); // plugin ModeratedPage must be first line!
164         if ($parsed = $loader->parsePI($pi)) {
165             $plugin =& $parsed[1];
166             if ($plugin->getName() != _("ModeratedPage"))
167                 return $this->error(sprintf(_("<?plugin ModeratedPage ... ?> not found in first line of %s"),
168                                             $action_page->getName()));
169             if (!$action_page->get('locked'))
170                 return $this->error(sprintf(_("%s is not locked!"),
171                                             $action_page->getName()));
172             return $plugin->resolve_argstr($request, $parsed[2]);
173         } else {
174             return $this->error(sprintf(_("<?plugin ModeratedPage ... ?> not found in first line of %s"),
175                                         $action_page->getName()));
176         }
177     }
178     
179 };
180
181 // $Log: not supported by cvs2svn $
182
183 // For emacs users
184 // Local Variables:
185 // mode: php
186 // tab-width: 8
187 // c-basic-offset: 4
188 // c-hanging-comment-ender-p: nil
189 // indent-tabs-mode: nil
190 // End:
191 ?>