1<?php 2/** 3 * DokuWiki Plugin bez (Action Component) 4 * 5 */ 6 7// must be run within Dokuwiki 8 9if(!defined('DOKU_INC')) die(); 10 11/** 12 * Class action_plugin_bez_migration 13 * 14 * Handle migrations that need more than just SQL 15 */ 16class action_plugin_bez_migration extends DokuWiki_Action_Plugin { 17 /** 18 * @inheritDoc 19 */ 20 public function register(Doku_Event_Handler $controller) { 21 $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handle_migrations'); 22 } 23 24 /** 25 * Call our custom migrations when defined 26 * 27 * @param Doku_Event $event 28 * @param $param 29 */ 30 public function handle_migrations(Doku_Event $event, $param) { 31 if ($event->data['sqlite']->getAdapter()->getDbname() !== 'b3p') { 32 return; 33 } 34 $to = $event->data['to']; 35 36 if(is_callable(array($this, "migration$to"))) { 37 $event->preventDefault(); 38 $event->result = call_user_func(array($this, "migration$to"), $event->data); 39 } 40 } 41 42 /** 43 * Executes Migration 1 44 * 45 * Add a latest column to all existing multi tables 46 * 47 * @param helper_plugin_sqlite $sqlite 48 * @return bool 49 */ 50 protected function migration1($data) { 51 global $INFO; 52 53 $file = $data['file']; 54 /** @var helper_plugin_sqlite $sqlite */ 55 $sqlite = $data['sqlite']; 56 57 $sql = file_get_contents($file); 58 if ($sql === false) { 59 throw new Exception('cannot open file '.$file); 60 } 61 62 $matches = array(); 63 preg_match_all('/.*?(?(?=BEGIN)BEGIN.*?END)\s*;/is', $sql, $matches); 64 $queries = $matches[0]; 65 66 $db = $sqlite->getAdapter()->getDb(); 67 68 $db->beginTransaction(); 69 foreach ($queries as $query) { 70 $res = $db->query($query); 71 if($res === false) { 72 $err = $db->errorInfo(); 73 msg($err[0].' '.$err[1].' '.$err[2].':<br /><pre>'.hsc($sql).'</pre>', -1); 74 $db->rollBack(); 75 return false; 76 } 77 } 78 79 //import from bez 80 $bez = new \PDO('sqlite:' . DOKU_INC . 'data/meta/bez.sqlite3'); 81 82 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 83 84 $stmt = $bez->query('SELECT * FROM issuetypes'); 85 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 86 $sqlite->storeEntry('label', array('id' => $row['id'], 87 'name' => $row['pl'], 88 'added_by' => $INFO['client'], 89 'added_date' => date('c'))); 90 } 91 92 $stmt = $bez->query('SELECT * FROM tasktypes'); 93 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 94 $sqlite->storeEntry('task_program', array('id' => $row['id'], 95 'name' => $row['pl'], 96 'added_by' => $INFO['client'], 97 'added_date' => date('c'))); 98 } 99 100 $stmt = $bez->query('SELECT *, (SELECT COUNT(*) FROM tasks 101 WHERE tasks.cause = commcauses.id) AS task_count FROM commcauses'); 102 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 103 if ($row['type'] == '0') { 104 $type = 'comment'; 105 } elseif ($row['type'] == '1') { 106 $type = 'cause_real'; 107 } elseif ($row['type'] == '2') { 108 $type = 'cause_potential'; 109 } 110 $sqlite->storeEntry('thread_comment', 111 array('id' => $row['id'], 112 'thread_id' => $row['issue'], 113 'type' => $type, 114 'author' => $row['reporter'], 115 'create_date' => date('c', strtotime($row['datetime'])), 116 'last_modification_date' => date('c', strtotime($row['datetime'])), 117 'content' => $row['content'], 118 'content_html' => $row['content_cache'], 119 'task_count' => $row['task_count'])); 120 } 121 122 $stmt = $bez->query('SELECT tasks.*, commcauses.type AS cause_type 123 FROM tasks 124 LEFT JOIN commcauses ON tasks.cause = commcauses.id'); 125 //thread_id => array('user_id' => 'user_id') 126 $task_assignee = array(); 127 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 128 if ($row['close_date'] != '') { 129 $last_mod = date('c', (int) $row['close_date']); 130 } else { 131 $last_mod = date('c', (int) $row['date']); 132 } 133 134 $data = array('id' => $row['id'], 135 'original_poster' => $row['reporter'], 136 'assignee' => $row['executor'], 137 'create_date' => date('c', (int) $row['date']), 138 'last_activity_date' => $last_mod, 139 'last_modification_date' => $last_mod, 140 'plan_date' => $row['plan_date'], 141 'all_day_event' => $row['all_day_event'], 142 'start_time' => $row['start_time'], 143 'finish_time' => $row['finish_time'], 144 'content' => $row['task'], 145 'content_html' => $row['task_cache'], 146 'thread_id' => $row['issue'], 147 'thread_comment_id' => $row['cause'], 148 'task_program_id' => $row['tasktype'] 149 ); 150 151 if ($data['thread_id'] != '') { 152 if (!is_array($task_assignee[$data['thread_id']])) { 153 $task_assignee[$data['thread_id']] = array(); 154 } 155 $task_assignee[$data['thread_id']][$row['executor']] = $row['executor']; 156 } 157 158 if ($row['cost'] != '0') { 159 $data['cost'] = $row['cost']; 160 } 161 162 if ($row['issue'] == '') { 163 $data['type'] = 'program'; 164 } elseif ($row['cause'] == '') { 165 $data['type'] = 'correction'; 166 } elseif ($row['cause_type'] == '1') { 167 $data['type'] = 'corrective'; 168 } else { 169 $data['type'] = 'preventive'; 170 } 171 172 if ($row['state'] == '0') { 173 $data['state'] = 'opened'; 174 } elseif ($row['state'] == '1' || $row['state'] == '2') { 175 $data['state'] = 'done'; 176 $data['closed_by'] = $row['executor']; 177 $data['close_date'] = date('c', (int) $row['close_date']); 178 179 if ($row['reason'] != '') { 180 $sqlite->storeEntry('task_comment', 181 array('task_id' => $row['id'], 182 'author' => $row['executor'], 183 'create_date' => $data['close_date'], 184 'last_modification_date' => $data['close_date'], 185 'content' => $row['reason'], 186 'content_html' => $row['reason_cache'])); 187 } 188 } 189 190 //user_id => array() 191 $participants = array(); 192 $subscribents = explode(',', $row['subscribents']); 193 foreach ($subscribents as $user_id) { 194 $participants[$user_id] = array('user_id' => $user_id, 'subscribent' => '1'); 195 } 196 197 $op = $data['original_poster']; 198 if (!isset($participants[$op])) { 199 $participants[$op] = array('user_id' => $op, 'original_poster' => '1'); 200 } else { 201 $participants[$op]['original_poster'] = '1'; 202 } 203 204 $as = $data['assignee']; 205 if (!isset($participants[$as])) { 206 $participants[$as] = array('user_id' => $as, 'assignee' => '1'); 207 } else { 208 $participants[$as]['assignee'] = '1'; 209 } 210 211 foreach($participants as $part) { 212 $part['task_id'] = $row['id']; 213 $part['added_by'] = $INFO['client']; 214 $part['added_date'] = date('c'); 215 216 $res = $sqlite->storeEntry('task_participant', $part); 217 if ($res === false) { 218 throw new Exception($db->errorInfo()); 219 } 220 } 221 222 $sqlite->storeEntry('task', $data); 223 } 224 225 226 227 $stmt = $bez->query('SELECT *, 228 (SELECT COUNT(*) FROM tasks 229 WHERE tasks.issue = issues.id) AS task_count, 230 (SELECT COUNT(*) FROM tasks 231 WHERE tasks.issue = issues.id AND tasks.state != 0) AS task_closed_count, 232 (SELECT SUM(cost) FROM tasks 233 WHERE tasks.issue = issues.id) AS task_sum_cost 234 FROM issues'); 235 236 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 237 238 $data = array('id' => $row['id'], 239 'original_poster' => $row['reporter'], 240 'coordinator' => $row['coordinator'], 241 'create_date' => date('c', (int) $row['date']), 242 'last_activity_date' => date('c', strtotime($row['last_activity'])), 243 'last_modification_date' => date('c', (int) $row['last_mod']), 244 'title' => $row['title'], 245 'content' => $row['description'], 246 'content_html' => $row['description_cache'], 247 'task_count' => $row['task_count'], 248 'task_count_closed' => $row['task_closed_count'] 249 ); 250 251 if ($row['task_sum_cost'] != '0') { 252 $data['task_sum_cost'] = $row['task_sum_cost']; 253 } 254 255 if ($row['coordinator'] == '') { 256 $data['state'] = 'proposal'; 257 } elseif ($row['state'] == '1') { 258 $data['closed_by'] = $row['coordinator']; 259 $data['state'] = 'closed'; 260 $data['close_date'] = $data['last_modification_date']; 261 262 $sqlite->storeEntry('thread_comment', 263 array('thread_id' => $row['id'], 264 'type' => 'comment', 265 'author' => $row['coordinator'], 266 'create_date' => $data['close_date'], 267 'last_modification_date' => $data['close_date'], 268 'content' => $row['opinion'], 269 'content_html' => $row['opinion_cache'])); 270 271 272 } elseif ($row['state'] == '2') { 273 $data['closed_by'] = $row['coordinator']; 274 $data['state'] = 'rejected'; 275 $data['close_date'] = $data['last_modification_date']; 276 277 if ($row['opinion'] != '') { 278 $res = $sqlite->storeEntry('thread_comment', 279 array('thread_id' => $row['id'], 280 'type' => 'comment', 281 'author' => $row['coordinator'], 282 'create_date' => $data['close_date'], 283 'last_modification_date' => $data['close_date'], 284 'content' => $row['opinion'], 285 'content_html' => $row['opinion_cache'])); 286 if ($res === false) { 287 throw new Exception($db->errorInfo()); 288 } 289 } 290 291 } elseif ($row['task_count'] == $row['task_closed_count']) { 292 $data['state'] = 'done'; 293 } else { 294 $data['state'] = 'opened'; 295 } 296 297 $sqlite->storeEntry('thread', $data); 298 299 $sqlite->storeEntry('thread_label', 300 array('thread_id' => $row['id'], 301 'label_id' => $row['type'])); 302 303 //participants 304 //user_id => array() 305 $participants = array(); 306 $org_participants = array_filter(explode(',', $row['participants'])); 307 foreach ($org_participants as $user_id) { 308 $participants[$user_id] = array('user_id' => $user_id); 309 } 310 311 $subscribents = array_filter(explode(',', $row['subscribents'])); 312 foreach ($subscribents as $user_id) { 313 if (!isset($participants[$user_id])) { 314 $participants[$user_id] = array('user_id' => $user_id); 315 } 316 $participants[$user_id]['subscribent'] = '1'; 317 } 318 319 if (is_array($task_assignee[$row['id']])) foreach ($task_assignee[$row['id']] as $user_id) { 320 if (!isset($participants[$user_id])) { 321 $participants[$user_id] = array('user_id' => $user_id); 322 } 323 $participants[$user_id]['task_assignee'] = '1'; 324 } 325 326 $op = $data['original_poster']; 327 if (!isset($participants[$op])) { 328 $participants[$op] = array('user_id' => $op, 'original_poster' => '1'); 329 } else { 330 $participants[$op]['original_poster'] = '1'; 331 } 332 333 $cor = $data['coordinator']; 334 if (!isset($participants[$cor])) { 335 $participants[$cor] = array('user_id' => $cor, 'coordinator' => '1'); 336 } else { 337 $participants[$cor]['coordinator'] = '1'; 338 } 339 340 foreach($participants as $part) { 341 $part['thread_id'] = $row['id']; 342 $part['added_by'] = $INFO['client']; 343 $part['added_date'] = date('c'); 344 345 $sqlite->storeEntry('thread_participant', $part); 346 } 347 348 } 349 350 $db->commit(); 351 352 return true; 353 } 354 355} 356