| Conditions | 19 |
| Paths | 18 |
| Total Lines | 240 |
| Code Lines | 135 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 78 | function ScheduledTasks() |
||
| 79 | { |
||
| 80 | global $context, $txt, $sourcedir, $smcFunc, $scripturl; |
||
| 81 | |||
| 82 | // Mama, setup the template first - cause it's like the most important bit, like pickle in a sandwich. |
||
| 83 | // ... ironically I don't like pickle. </grudge> |
||
| 84 | $context['sub_template'] = 'view_scheduled_tasks'; |
||
| 85 | $context['page_title'] = $txt['maintain_tasks']; |
||
| 86 | |||
| 87 | // Saving changes? |
||
| 88 | if (isset($_REQUEST['save']) && isset($_POST['enable_task'])) |
||
| 89 | { |
||
| 90 | checkSession(); |
||
| 91 | |||
| 92 | // We'll recalculate the dates at the end! |
||
| 93 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
| 94 | |||
| 95 | // Enable and disable as required. |
||
| 96 | $enablers = array(0); |
||
| 97 | foreach ($_POST['enable_task'] as $id => $enabled) |
||
| 98 | if ($enabled) |
||
| 99 | $enablers[] = (int) $id; |
||
| 100 | |||
| 101 | // Do the update! |
||
| 102 | $smcFunc['db_query']('', ' |
||
| 103 | UPDATE {db_prefix}scheduled_tasks |
||
| 104 | SET disabled = CASE WHEN id_task IN ({array_int:id_task_enable}) THEN 0 ELSE 1 END', |
||
| 105 | array( |
||
| 106 | 'id_task_enable' => $enablers, |
||
| 107 | ) |
||
| 108 | ); |
||
| 109 | |||
| 110 | // Update the "allow_expire_redirect" setting... |
||
| 111 | $get_info = $smcFunc['db_query']('', ' |
||
| 112 | SELECT disabled |
||
| 113 | FROM {db_prefix}scheduled_tasks |
||
| 114 | WHERE task = {string:remove_redirect}', |
||
| 115 | array( |
||
| 116 | 'remove_redirect' => 'remove_topic_redirect' |
||
| 117 | ) |
||
| 118 | ); |
||
| 119 | |||
| 120 | $temp = $smcFunc['db_fetch_assoc']($get_info); |
||
| 121 | $task_disabled = !empty($temp['disabled']) ? 0 : 1; |
||
| 122 | $smcFunc['db_free_result']($get_info); |
||
| 123 | |||
| 124 | updateSettings(array('allow_expire_redirect' => $task_disabled)); |
||
| 125 | |||
| 126 | // Pop along... |
||
| 127 | CalculateNextTrigger(); |
||
| 128 | } |
||
| 129 | |||
| 130 | // Want to run any of the tasks? |
||
| 131 | if (isset($_REQUEST['run']) && isset($_POST['run_task'])) |
||
| 132 | { |
||
| 133 | $task_string = ''; |
||
| 134 | |||
| 135 | // Lets figure out which ones they want to run. |
||
| 136 | $tasks = array(); |
||
| 137 | foreach ($_POST['run_task'] as $task => $dummy) |
||
| 138 | $tasks[] = (int) $task; |
||
| 139 | |||
| 140 | // Load up the tasks. |
||
| 141 | $request = $smcFunc['db_query']('', ' |
||
| 142 | SELECT id_task, task, callable |
||
| 143 | FROM {db_prefix}scheduled_tasks |
||
| 144 | WHERE id_task IN ({array_int:tasks}) |
||
| 145 | LIMIT {int:limit}', |
||
| 146 | array( |
||
| 147 | 'tasks' => $tasks, |
||
| 148 | 'limit' => count($tasks), |
||
| 149 | ) |
||
| 150 | ); |
||
| 151 | |||
| 152 | // Lets get it on! |
||
| 153 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
| 154 | ignore_user_abort(true); |
||
| 155 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 156 | { |
||
| 157 | // What kind of task are we handling? |
||
| 158 | if (!empty($row['callable'])) |
||
| 159 | $task_string = $row['callable']; |
||
| 160 | |||
| 161 | // Default SMF task or old mods? |
||
| 162 | elseif (function_exists('scheduled_' . $row['task'])) |
||
| 163 | $task_string = 'scheduled_' . $row['task']; |
||
| 164 | |||
| 165 | // One last resource, the task name. |
||
| 166 | elseif (!empty($row['task'])) |
||
| 167 | $task_string = $row['task']; |
||
| 168 | |||
| 169 | $start_time = microtime(true); |
||
| 170 | // The functions got to exist for us to use it. |
||
| 171 | if (empty($task_string)) |
||
| 172 | continue; |
||
| 173 | |||
| 174 | // Try to stop a timeout, this would be bad... |
||
| 175 | @set_time_limit(300); |
||
| 176 | if (function_exists('apache_reset_timeout')) |
||
| 177 | @apache_reset_timeout(); |
||
| 178 | |||
| 179 | // Get the callable. |
||
| 180 | $callable_task = call_helper($task_string, true); |
||
| 181 | |||
| 182 | // Perform the task. |
||
| 183 | if (!empty($callable_task)) |
||
| 184 | $completed = call_user_func($callable_task); |
||
|
|
|||
| 185 | |||
| 186 | else |
||
| 187 | $completed = false; |
||
| 188 | |||
| 189 | // Log that we did it ;) |
||
| 190 | if ($completed) |
||
| 191 | { |
||
| 192 | $total_time = round(microtime(true) - $start_time, 3); |
||
| 193 | $smcFunc['db_insert']('', |
||
| 194 | '{db_prefix}log_scheduled_tasks', |
||
| 195 | array('id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float'), |
||
| 196 | array($row['id_task'], time(), $total_time), |
||
| 197 | array('id_task') |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | $smcFunc['db_free_result']($request); |
||
| 202 | |||
| 203 | // If we had any errors, push them to session so we can pick them up next time to tell the user. |
||
| 204 | if (!empty($context['scheduled_errors'])) |
||
| 205 | $_SESSION['st_error'] = $context['scheduled_errors']; |
||
| 206 | |||
| 207 | redirectexit('action=admin;area=scheduledtasks;done'); |
||
| 208 | } |
||
| 209 | |||
| 210 | if (isset($_SESSION['st_error'])) |
||
| 211 | { |
||
| 212 | $context['scheduled_errors'] = $_SESSION['st_error']; |
||
| 213 | unset ($_SESSION['st_error']); |
||
| 214 | } |
||
| 215 | |||
| 216 | $listOptions = array( |
||
| 217 | 'id' => 'scheduled_tasks', |
||
| 218 | 'title' => $txt['maintain_tasks'], |
||
| 219 | 'base_href' => $scripturl . '?action=admin;area=scheduledtasks', |
||
| 220 | 'get_items' => array( |
||
| 221 | 'function' => 'list_getScheduledTasks', |
||
| 222 | ), |
||
| 223 | 'columns' => array( |
||
| 224 | 'name' => array( |
||
| 225 | 'header' => array( |
||
| 226 | 'value' => $txt['scheduled_tasks_name'], |
||
| 227 | 'style' => 'width: 40%;', |
||
| 228 | ), |
||
| 229 | 'data' => array( |
||
| 230 | 'sprintf' => array( |
||
| 231 | 'format' => ' |
||
| 232 | <a href="' . $scripturl . '?action=admin;area=scheduledtasks;sa=taskedit;tid=%1$d">%2$s</a><br><span class="smalltext">%3$s</span>', |
||
| 233 | 'params' => array( |
||
| 234 | 'id' => false, |
||
| 235 | 'name' => false, |
||
| 236 | 'desc' => false, |
||
| 237 | ), |
||
| 238 | ), |
||
| 239 | ), |
||
| 240 | ), |
||
| 241 | 'next_due' => array( |
||
| 242 | 'header' => array( |
||
| 243 | 'value' => $txt['scheduled_tasks_next_time'], |
||
| 244 | ), |
||
| 245 | 'data' => array( |
||
| 246 | 'db' => 'next_time', |
||
| 247 | 'class' => 'smalltext', |
||
| 248 | ), |
||
| 249 | ), |
||
| 250 | 'regularity' => array( |
||
| 251 | 'header' => array( |
||
| 252 | 'value' => $txt['scheduled_tasks_regularity'], |
||
| 253 | ), |
||
| 254 | 'data' => array( |
||
| 255 | 'db' => 'regularity', |
||
| 256 | 'class' => 'smalltext', |
||
| 257 | ), |
||
| 258 | ), |
||
| 259 | 'run_now' => array( |
||
| 260 | 'header' => array( |
||
| 261 | 'value' => $txt['scheduled_tasks_run_now'], |
||
| 262 | 'style' => 'width: 12%;', |
||
| 263 | 'class' => 'centercol', |
||
| 264 | ), |
||
| 265 | 'data' => array( |
||
| 266 | 'sprintf' => array( |
||
| 267 | 'format' => |
||
| 268 | '<input type="checkbox" name="run_task[%1$d]" id="run_task_%1$d">', |
||
| 269 | 'params' => array( |
||
| 270 | 'id' => false, |
||
| 271 | ), |
||
| 272 | ), |
||
| 273 | 'class' => 'centercol', |
||
| 274 | ), |
||
| 275 | ), |
||
| 276 | 'enabled' => array( |
||
| 277 | 'header' => array( |
||
| 278 | 'value' => $txt['scheduled_tasks_enabled'], |
||
| 279 | 'style' => 'width: 6%;', |
||
| 280 | 'class' => 'centercol', |
||
| 281 | ), |
||
| 282 | 'data' => array( |
||
| 283 | 'sprintf' => array( |
||
| 284 | 'format' => |
||
| 285 | '<input type="hidden" name="enable_task[%1$d]" id="task_%1$d" value="0"><input type="checkbox" name="enable_task[%1$d]" id="task_check_%1$d" %2$s>', |
||
| 286 | 'params' => array( |
||
| 287 | 'id' => false, |
||
| 288 | 'checked_state' => false, |
||
| 289 | ), |
||
| 290 | ), |
||
| 291 | 'class' => 'centercol', |
||
| 292 | ), |
||
| 293 | ), |
||
| 294 | ), |
||
| 295 | 'form' => array( |
||
| 296 | 'href' => $scripturl . '?action=admin;area=scheduledtasks', |
||
| 297 | ), |
||
| 298 | 'additional_rows' => array( |
||
| 299 | array( |
||
| 300 | 'position' => 'below_table_data', |
||
| 301 | 'value' => ' |
||
| 302 | <input type="submit" name="save" value="' . $txt['scheduled_tasks_save_changes'] . '" class="button"> |
||
| 303 | <input type="submit" name="run" value="' . $txt['scheduled_tasks_run_now'] . '" class="button">', |
||
| 304 | ), |
||
| 305 | array( |
||
| 306 | 'position' => 'after_title', |
||
| 307 | 'value' => $txt['scheduled_tasks_time_offset'], |
||
| 308 | ), |
||
| 309 | ), |
||
| 310 | ); |
||
| 311 | |||
| 312 | require_once($sourcedir . '/Subs-List.php'); |
||
| 313 | createList($listOptions); |
||
| 314 | |||
| 315 | $context['sub_template'] = 'view_scheduled_tasks'; |
||
| 316 | |||
| 317 | $context['tasks_were_run'] = isset($_GET['done']); |
||
| 318 | } |
||
| 688 | ?> |