Dotclear

source: plugins/dcAdvancedCleaner/inc/class.dc.uninstaller.php @ 2199

Revision 2199, 8.5 KB checked in by JcDenis, 13 years ago (diff)

dcAdvancedCleaner 0.4:

  • Fixed list of tables (closes #440)
  • Fixed direct uninstaller
  • Added readable errors return
  • Added DC 2.2 compatibility (new settings)
  • Changed admin design
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of dcAdvancedCleaner, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009-2010 JC Denis and contributors
6# jcdenis@gdwd.com
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11# -- END LICENSE BLOCK ------------------------------------
12
13if (!defined('DC_ADMIN_CONTEXT')){return;}
14
15/**
16@brief Modules uninstall features handler
17
18Provides an object to handle modules uninstall features
19(themes or plugins).
20This class used dcAdvancedCleaner.
21*/
22class dcUninstaller
23{
24     protected $path;
25
26     protected $modules = array(); ///< <b>array</b> Modules informations array
27     protected $actions = array('user'=>array(),'callback'=>array());
28     protected $callbacks = array('user'=>array(),'callback'=>array());
29
30     protected $id = null;
31     protected $mroot = null;
32
33     /**
34     Array of all allowed properties to uninstall parts of modules.
35     'settings' : settings set on dcSettings,
36     'tables' : if module creates table,
37     'plugins' : if module has files on plugin path,
38     'themes' : if module has files on theme path, (on current blog)
39     'caches' : if module has files on DC caches path,
40     'versions' : if module set a versions on DC table 'version'
41     */
42     protected static $allowed_properties = array(
43          'versions' => array(
44               'delete' => 'delete version in dc'
45          ),
46          'settings' => array(
47               'delete_global' => 'delete global settings',
48               'delete_local' => 'delete local settings',
49               'delete_all' => 'delete all settings'
50          ),
51          'tables' => array(
52               'empty' => 'empty table',
53               'delete' => 'delete table'
54          ),
55          'plugins' => array(
56               'empty' => 'empty plugin folder',
57               'delete' => 'delete plugin folder'
58          ),
59          'themes' => array(
60               'empty' => 'empty theme folder',
61               'delete' => 'delete theme folder'
62          ),
63          'caches' => array(
64               'empty' => 'empty cache folder',
65               'delete' => 'delete cache folder'
66          )
67     );
68
69     protected static $priority_properties = array(
70          'versions','settings','tables','themes','plugins','caches'
71     );
72
73     public $core;  ///< <b>dcCore</b>  dcCore instance
74     
75     /**
76     Object constructor.
77     
78     @param    core      <b>dcCore</b>  dcCore instance
79     */
80     public function __construct($core)
81     {
82          $this->core =& $core;
83     }
84
85     public static function getAllowedProperties()
86     {
87          return self::$allowed_properties;
88     }
89     
90     /**
91     Loads modules.
92     Files _defines.php and _uninstall.php must be present on module
93     to be recognized.
94     (path separator depends on your OS).
95     
96     @param    path           <b>string</b>       Separated list of paths
97     */
98     public function loadModules($path)
99     {
100          $this->path = explode(PATH_SEPARATOR,$path);
101
102          foreach ($this->path as $root)
103          {
104               if (!is_dir($root) || !is_readable($root)) continue;
105
106               if (substr($root,-1) != '/') $root .= '/';
107
108               if (($d = @dir($root)) === false) continue;
109
110               while (($entry = $d->read()) !== false)
111               {
112                    $full_entry = $root.'/'.$entry;
113
114                    if ($entry != '.' && $entry != '..' && is_dir($full_entry))
115                    {
116                         $this->loadModule($full_entry);
117                    }
118               }
119               $d->close();
120          }
121
122          # Sort modules by name
123          uasort($this->modules,array($this,'sortModules'));
124     }
125     
126     /**
127     Load one module.
128     Files _defines.php and _uninstall.php must be present on module
129     to be recognized.
130     
131     @param    root           <b>string</b>       path of module
132     */
133     public function loadModule($root)
134     {
135          if (file_exists($root.'/_define.php')
136           && file_exists($root.'/_uninstall.php')) {
137
138               $this->id = basename($root);
139               $this->mroot = $root;
140
141               require $root.'/_define.php';
142               require $root.'/_uninstall.php';
143
144               $this->id = null;
145               $this->mroot = null;
146          }
147     }
148     
149     /**
150     This method registers a module in modules list. You should use
151     this to register a new module.
152     
153     @param    name           <b>string</b>       Module name
154     @param    desc           <b>string</b>       Module description
155     @param    author         <b>string</b>       Module author name
156     @param    version        <b>string</b>       Module version
157     */
158     public function registerModule($name,$desc,$author,$version)
159     {
160          if ($this->id) {
161               $this->modules[$this->id] = array(
162               'root' => $this->mroot,
163               'name' => $name,
164               'desc' => $desc,
165               'author' => $author,
166               'version' => $version,
167               'root_writable' => is_writable($this->mroot)
168               );
169          }
170     }
171     
172     /**
173     Returns all modules associative array or only one module if <var>$id</var>
174     is present.
175     
176     @param    id        <b>string</b>       Optionnal module ID
177     @return   <b>array</b>
178     */
179     public function getModules($id=null)
180     {
181          if ($id && isset($this->modules[$id])) {
182               return $this->modules[$id];
183          }
184          return $this->modules;
185     }
186     
187     /**
188     Returns true if the module with ID <var>$id</var> exists.
189     
190     @param    id        <b>string</b>       Module ID
191     @return   <b>boolean</b>
192     */
193     public function moduleExists($id)
194     {
195          return isset($this->modules[$id]);
196     }
197
198     /**
199     Add a predefined action to unsintall features.
200     This action is set in _uninstall.php.
201     
202     @param    type      <b>string</b>       Type of action (from $allowed_properties)
203     @param    action    <b>string</b>       Action (from $allowed_properties)
204     @param    ns        <b>string</b>       Name of setting related to module.
205     @param    desc      <b>string</b>       Description of action
206     */
207     protected function addUserAction($type,$action,$ns,$desc='')
208     {
209          $this->addAction('user',$type,$action,$ns,$desc);
210     }
211
212     protected function addDirectAction($type,$action,$ns,$desc='')
213     {
214          $this->addAction('direct',$type,$action,$ns,$desc);
215     }
216
217     private function addAction($group,$type,$action,$ns,$desc)
218     {
219          $group = self::group($group);
220
221          if (null === $this->id) return;
222
223          if (empty($type) || empty($ns)) return;
224
225          if (!isset(self::$allowed_properties[$type][$action])) return;
226
227          if (empty($desc)) $desc = __($action);
228
229          $this->actions[$group][$this->id][$type][] = array(
230               'ns' => $ns,
231               'action' => $action,
232               'desc' => $desc
233          );
234     }
235
236     /**
237     Returns modules <var>$id</var> predefined actions associative array
238     ordered by priority
239     
240     @param    id        <b>string</b>       Optionnal module ID
241     @return   <b>array</b>
242     */
243     public function getUserActions($id)
244     {
245          return $this->getActions('user',$id);
246     }
247
248     public function getDirectActions($id)
249     {
250          return $this->getActions('direct',$id);
251     }
252
253     protected function getActions($group,$id)
254     {
255          $group = self::group($group);
256
257          if (!isset($this->actions[$group][$id])) return array();
258
259          $res = array();
260          foreach(self::$priority_properties as $k => $v)
261          {
262               if (!isset($this->actions[$group][$id][$v])) continue;
263               $res[$v] = $this->actions[$group][$id][$v];
264          }
265
266          return $res;
267     }
268
269     /**
270     Add a callable function for unsintall features.
271     This action is set in _uninstall.php.
272     
273     @param    func      <b>string</b>       Callable function
274     @param    desc      <b>string</b>       Description of action
275     */
276     protected function addUserCallback($func,$desc='')
277     {
278          $this->addCallback('user',$func,$desc);
279     }
280
281     protected function addDirectCallback($func,$desc='')
282     {
283          $this->addCallback('direct',$func,$desc);
284     }
285
286     private function addCallback($group,$func,$desc)
287     {
288          $group = self::group($group);
289
290          if (null === $this->id) return;
291
292          if (empty($desc)) $desc = __('extra action');
293
294          if (!is_callable($func)) return;
295
296          $this->callbacks[$group][$this->id][] = array(
297               'func' => $func,
298               'desc' => $desc
299          );
300     }
301     
302     /**
303     Returns modules <var>$id</var> callback actions associative array
304     
305     @param    id        <b>string</b>       Optionnal module ID
306     @return   <b>array</b>
307     */
308     public function getUserCallbacks($id)
309     {
310          return $this->getCallbacks('user',$id);
311     }
312
313     public function getDirectCallbacks($id)
314     {
315          return $this->getCallbacks('direct',$id);
316     }
317
318     protected function getCallbacks($group,$id)
319     {
320          $group = self::group($group);
321
322          if (!isset($this->callbacks[$group][$id])) return array();
323
324          return $this->callbacks[$group][$id];
325     }
326     
327     /**
328     Execute a predifined action. This function call dcAdvancedCleaner
329     to do actions.
330     
331     @param    type      <b>string</b>       Type of action (from $allowed_properties)
332     @param    action    <b>string</b>       Action (from $allowed_properties)
333     @param    ns        <b>string</b>       Name of setting related to module.
334     @return   <b>array</b>
335     */
336     public function execute($type,$action,$ns)
337     {
338          $prop = $this->getAllowedProperties();
339
340          if (!isset($prop[$type][$action]) || empty($ns)) return;
341
342          dcAdvancedCleaner::execute($this->core,$type,$action,$ns);
343     }
344
345     private function sortModules($a,$b)
346     {
347          return strcasecmp($a['name'],$b['name']);
348     }
349
350     private function group($group)
351     {
352          return in_array($group,array('user','direct')) ? $group : null;
353     }
354}
355?>
Note: See TracBrowser for help on using the repository browser.

Sites map