| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @brief maintenance, a plugin for Dotclear 2 |
|---|
| 4 | * |
|---|
| 5 | * @package Dotclear |
|---|
| 6 | * @subpackage Plugins |
|---|
| 7 | * |
|---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 9 | * @copyright GPL-2.0-only |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | if (!defined('DC_CONTEXT_ADMIN')) {return;} |
|---|
| 13 | |
|---|
| 14 | // Sidebar menu |
|---|
| 15 | $_menu['Plugins']->addItem( |
|---|
| 16 | __('Maintenance'), |
|---|
| 17 | $core->adminurl->get('admin.plugin.maintenance'), |
|---|
| 18 | dcPage::getPF('maintenance/icon.png'), |
|---|
| 19 | preg_match('/' . preg_quote($core->adminurl->get('admin.plugin.maintenance')) . '(&.*)?$/', $_SERVER['REQUEST_URI']), |
|---|
| 20 | $core->auth->check('admin', $core->blog->id) |
|---|
| 21 | ); |
|---|
| 22 | |
|---|
| 23 | // Admin behaviors |
|---|
| 24 | $core->addBehavior('dcMaintenanceInit', ['dcMaintenanceAdmin', 'dcMaintenanceInit']); |
|---|
| 25 | $core->addBehavior('adminDashboardFavorites', ['dcMaintenanceAdmin', 'adminDashboardFavorites']); |
|---|
| 26 | $core->addBehavior('adminDashboardContents', ['dcMaintenanceAdmin', 'adminDashboardItems']); |
|---|
| 27 | $core->addBehavior('adminDashboardOptionsForm', ['dcMaintenanceAdmin', 'adminDashboardOptionsForm']); |
|---|
| 28 | $core->addBehavior('adminAfterDashboardOptionsUpdate', ['dcMaintenanceAdmin', 'adminAfterDashboardOptionsUpdate']); |
|---|
| 29 | $core->addBehavior('adminPageHelpBlock', ['dcMaintenanceAdmin', 'adminPageHelpBlock']); |
|---|
| 30 | $core->addBehavior('pluginsToolsHeaders', ['dcMaintenanceAdmin', 'pluginsToolsHeaders']); |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | @ingroup PLUGIN_MAINTENANCE |
|---|
| 34 | @nosubgrouping |
|---|
| 35 | @brief Maintenance plugin admin class. |
|---|
| 36 | |
|---|
| 37 | Group of methods used on behaviors. |
|---|
| 38 | */ |
|---|
| 39 | class dcMaintenanceAdmin |
|---|
| 40 | { |
|---|
| 41 | /** |
|---|
| 42 | * Register default tasks. |
|---|
| 43 | * |
|---|
| 44 | * @param $maintenance <b>dcMaintenance</b> dcMaintenance instance |
|---|
| 45 | */ |
|---|
| 46 | public static function dcMaintenanceInit($maintenance) |
|---|
| 47 | { |
|---|
| 48 | $maintenance |
|---|
| 49 | ->addTab('maintenance', __('Servicing'), ['summary' => __('Tools to maintain the performance of your blogs.')]) |
|---|
| 50 | ->addTab('backup', __('Backup'), ['summary' => __('Tools to back up your content.')]) |
|---|
| 51 | ->addTab('dev', __('Development'), ['summary' => __('Tools to assist in development of plugins, themes and core.')]) |
|---|
| 52 | |
|---|
| 53 | ->addGroup('optimize', __('Optimize')) |
|---|
| 54 | ->addGroup('index', __('Count and index')) |
|---|
| 55 | ->addGroup('purge', __('Purge')) |
|---|
| 56 | ->addGroup('other', __('Other')) |
|---|
| 57 | ->addGroup('zipblog', __('Current blog')) |
|---|
| 58 | ->addGroup('zipfull', __('All blogs')) |
|---|
| 59 | |
|---|
| 60 | ->addGroup('l10n', __('Translations'), ['summary' => __('Maintain translations')]) |
|---|
| 61 | |
|---|
| 62 | ->addTask('dcMaintenanceCache') |
|---|
| 63 | ->addTask('dcMaintenanceCSP') |
|---|
| 64 | ->addTask('dcMaintenanceIndexposts') |
|---|
| 65 | ->addTask('dcMaintenanceIndexcomments') |
|---|
| 66 | ->addTask('dcMaintenanceCountcomments') |
|---|
| 67 | ->addTask('dcMaintenanceSynchpostsmeta') |
|---|
| 68 | ->addTask('dcMaintenanceLogs') |
|---|
| 69 | ->addTask('dcMaintenanceVacuum') |
|---|
| 70 | ->addTask('dcMaintenanceZipmedia') |
|---|
| 71 | ->addTask('dcMaintenanceZiptheme') |
|---|
| 72 | ; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Favorites. |
|---|
| 77 | * |
|---|
| 78 | * @param $core <b>dcCore</b> dcCore instance |
|---|
| 79 | * @param $favs <b>arrayObject</b> Array of favs |
|---|
| 80 | */ |
|---|
| 81 | public static function adminDashboardFavorites($core, $favs) |
|---|
| 82 | { |
|---|
| 83 | $favs->register('maintenance', [ |
|---|
| 84 | 'title' => __('Maintenance'), |
|---|
| 85 | 'url' => $core->adminurl->get('admin.plugin.maintenance'), |
|---|
| 86 | 'small-icon' => dcPage::getPF('maintenance/icon.png'), |
|---|
| 87 | 'large-icon' => dcPage::getPF('maintenance/icon-big.png'), |
|---|
| 88 | 'permissions' => 'admin', |
|---|
| 89 | 'active_cb' => ['dcMaintenanceAdmin', 'adminDashboardFavoritesActive'], |
|---|
| 90 | 'dashboard_cb' => ['dcMaintenanceAdmin', 'adminDashboardFavoritesCallback'] |
|---|
| 91 | ]); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Favorites selection. |
|---|
| 96 | * |
|---|
| 97 | * @param $request <b>string</b> Requested page |
|---|
| 98 | * @param $params <b>array</b> Requested parameters |
|---|
| 99 | */ |
|---|
| 100 | public static function adminDashboardFavoritesActive($request, $params) |
|---|
| 101 | { |
|---|
| 102 | return $request == 'plugin.php' && isset($params['p']) && $params['p'] == 'maintenance'; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Favorites hack. |
|---|
| 107 | * |
|---|
| 108 | * This updates maintenance fav icon text |
|---|
| 109 | * if there are tasks required maintenance. |
|---|
| 110 | * |
|---|
| 111 | * @param $core <b>dcCore</b> dcCore instance |
|---|
| 112 | * @param $fav <b>arrayObject</b> fav attributes |
|---|
| 113 | */ |
|---|
| 114 | public static function adminDashboardFavoritesCallback($core, $fav) |
|---|
| 115 | { |
|---|
| 116 | // Check user option |
|---|
| 117 | $core->auth->user_prefs->addWorkspace('maintenance'); |
|---|
| 118 | if (!$core->auth->user_prefs->maintenance->dashboard_icon) { |
|---|
| 119 | return; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | // Check expired tasks |
|---|
| 123 | $maintenance = new dcMaintenance($core); |
|---|
| 124 | $count = 0; |
|---|
| 125 | foreach ($maintenance->getTasks() as $t) { |
|---|
| 126 | if ($t->expired() !== false) { |
|---|
| 127 | $count++; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | if (!$count) { |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | $fav['title'] .= '<br />' . sprintf(__('One task to execute', '%s tasks to execute', $count), $count); |
|---|
| 136 | $fav['large-icon'] = dcPage::getPF('maintenance/icon-big-update.png'); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Dashboard items stack. |
|---|
| 141 | * |
|---|
| 142 | * @param $core <b>dcCore</b> dcCore instance |
|---|
| 143 | * @param $items <b>arrayObject</b> Dashboard items |
|---|
| 144 | */ |
|---|
| 145 | public static function adminDashboardItems($core, $items) |
|---|
| 146 | { |
|---|
| 147 | $core->auth->user_prefs->addWorkspace('maintenance'); |
|---|
| 148 | if (!$core->auth->user_prefs->maintenance->dashboard_item) { |
|---|
| 149 | return; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | $maintenance = new dcMaintenance($core); |
|---|
| 153 | |
|---|
| 154 | $lines = []; |
|---|
| 155 | foreach ($maintenance->getTasks() as $t) { |
|---|
| 156 | $ts = $t->expired(); |
|---|
| 157 | if ($ts === false) { |
|---|
| 158 | continue; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | $lines[] = |
|---|
| 162 | '<li title="' . ($ts === null ? |
|---|
| 163 | __('This task has never been executed.') |
|---|
| 164 | : |
|---|
| 165 | sprintf(__('Last execution of this task was on %s.'), |
|---|
| 166 | dt::dt2str($core->blog->settings->system->date_format, $ts) . ' ' . |
|---|
| 167 | dt::dt2str($core->blog->settings->system->time_format, $ts) |
|---|
| 168 | ) |
|---|
| 169 | ) . '">' . $t->task() . '</li>'; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | if (empty($lines)) { |
|---|
| 173 | return; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | $items[] = new ArrayObject([ |
|---|
| 177 | '<div id="maintenance-expired" class="box small">' . |
|---|
| 178 | '<h3><img src="' . dcPage::getPF('maintenance/icon-small.png') . '" alt="" /> ' . __('Maintenance') . '</h3>' . |
|---|
| 179 | '<p class="warning no-margin">' . sprintf(__('There is a task to execute.', 'There are %s tasks to execute.', count($lines)), count($lines)) . '</p>' . |
|---|
| 180 | '<ul>' . implode('', $lines) . '</ul>' . |
|---|
| 181 | '<p><a href="' . $core->adminurl->get('admin.plugin.maintenance') . '">' . __('Manage tasks') . '</a></p>' . |
|---|
| 182 | '</div>' |
|---|
| 183 | ]); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * User preferences form. |
|---|
| 188 | * |
|---|
| 189 | * This add options for superadmin user |
|---|
| 190 | * to show or not expired taks. |
|---|
| 191 | * |
|---|
| 192 | * @param $args <b>object</b> dcCore instance or record |
|---|
| 193 | */ |
|---|
| 194 | public static function adminDashboardOptionsForm($core) |
|---|
| 195 | { |
|---|
| 196 | $core->auth->user_prefs->addWorkspace('maintenance'); |
|---|
| 197 | |
|---|
| 198 | echo |
|---|
| 199 | '<div class="fieldset">' . |
|---|
| 200 | '<h4>' . __('Maintenance') . '</h4>' . |
|---|
| 201 | |
|---|
| 202 | '<p><label for="maintenance_dashboard_icon" class="classic">' . |
|---|
| 203 | form::checkbox('maintenance_dashboard_icon', 1, $core->auth->user_prefs->maintenance->dashboard_icon) . |
|---|
| 204 | __('Display overdue tasks counter on maintenance dashboard icon') . '</label></p>' . |
|---|
| 205 | |
|---|
| 206 | '<p><label for="maintenance_dashboard_item" class="classic">' . |
|---|
| 207 | form::checkbox('maintenance_dashboard_item', 1, $core->auth->user_prefs->maintenance->dashboard_item) . |
|---|
| 208 | __('Display overdue tasks list on dashboard items') . '</label></p>' . |
|---|
| 209 | |
|---|
| 210 | '</div>'; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /** |
|---|
| 214 | * User preferences update. |
|---|
| 215 | * |
|---|
| 216 | * @param $user_id <b>string</b> User ID |
|---|
| 217 | */ |
|---|
| 218 | public static function adminAfterDashboardOptionsUpdate($user_id = null) |
|---|
| 219 | { |
|---|
| 220 | global $core; |
|---|
| 221 | |
|---|
| 222 | if (is_null($user_id)) { |
|---|
| 223 | return; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | $core->auth->user_prefs->addWorkspace('maintenance'); |
|---|
| 227 | $core->auth->user_prefs->maintenance->put('dashboard_icon', !empty($_POST['maintenance_dashboard_icon']), 'boolean'); |
|---|
| 228 | $core->auth->user_prefs->maintenance->put('dashboard_item', !empty($_POST['maintenance_dashboard_item']), 'boolean'); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * Build a well sorted help for tasks. |
|---|
| 233 | * |
|---|
| 234 | * This method is not so good if used with lot of tranlsations |
|---|
| 235 | * as it grows memory usage and translations files size, |
|---|
| 236 | * it is better to use help ressource files |
|---|
| 237 | * but keep it for exemple of how to use behavior adminPageHelpBlock. |
|---|
| 238 | * Cheers, JC |
|---|
| 239 | * |
|---|
| 240 | * @param $block <b>arrayObject</b> Called helpblocks |
|---|
| 241 | */ |
|---|
| 242 | public static function adminPageHelpBlock($blocks) |
|---|
| 243 | { |
|---|
| 244 | $found = false; |
|---|
| 245 | foreach ($blocks as $block) { |
|---|
| 246 | if ($block == 'maintenancetasks') { |
|---|
| 247 | $found = true; |
|---|
| 248 | break; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | if (!$found) { |
|---|
| 252 | return; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | $maintenance = new dcMaintenance($GLOBALS['core']); |
|---|
| 256 | |
|---|
| 257 | $res_tab = ''; |
|---|
| 258 | foreach ($maintenance->getTabs() as $tab_obj) { |
|---|
| 259 | $res_group = ''; |
|---|
| 260 | foreach ($maintenance->getGroups() as $group_obj) { |
|---|
| 261 | $res_task = ''; |
|---|
| 262 | foreach ($maintenance->getTasks() as $t) { |
|---|
| 263 | if ($t->group() != $group_obj->id() |
|---|
| 264 | || $t->tab() != $tab_obj->id()) { |
|---|
| 265 | continue; |
|---|
| 266 | } |
|---|
| 267 | if (($desc = $t->description()) != '') { |
|---|
| 268 | $res_task .= |
|---|
| 269 | '<dt>' . $t->task() . '</dt>' . |
|---|
| 270 | '<dd>' . $desc . '</dd>'; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | if (!empty($res_task)) { |
|---|
| 274 | $desc = $group_obj->description ?: $group_obj->summary; |
|---|
| 275 | |
|---|
| 276 | $res_group .= |
|---|
| 277 | '<h5>' . $group_obj->name() . '</h5>' . |
|---|
| 278 | ($desc ? '<p>' . $desc . '</p>' : '') . |
|---|
| 279 | '<dl>' . $res_task . '</dl>'; |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | if (!empty($res_group)) { |
|---|
| 283 | $desc = $tab_obj->description ?: $tab_obj->summary; |
|---|
| 284 | |
|---|
| 285 | $res_tab .= |
|---|
| 286 | '<h4>' . $tab_obj->name() . '</h4>' . |
|---|
| 287 | ($desc ? '<p>' . $desc . '</p>' : '') . |
|---|
| 288 | $res_group; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | if (!empty($res_tab)) { |
|---|
| 292 | $res = new ArrayObject(); |
|---|
| 293 | $res->content = $res_tab; |
|---|
| 294 | $blocks[] = $res; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /** |
|---|
| 299 | * Add javascript for plugin configuration. |
|---|
| 300 | * |
|---|
| 301 | * @param $core <b>dcCore</b> dcCore instance |
|---|
| 302 | * @param $module <b>mixed</b> Module ID or false if none |
|---|
| 303 | * @return <b>string</b> Header code for js inclusion |
|---|
| 304 | */ |
|---|
| 305 | public static function pluginsToolsHeaders($core, $module) |
|---|
| 306 | { |
|---|
| 307 | if ($module == 'maintenance') { |
|---|
| 308 | return dcPage::jsLoad(dcPage::getPF('maintenance/js/settings.js')); |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | } |
|---|