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_ireadit_migration extends DokuWiki_Action_Plugin 17{ 18 /** 19 * @inheritDoc 20 */ 21 public function register(Doku_Event_Handler $controller) 22 { 23 $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'AFTER', $this, 'handle_migrations'); 24 } 25 26 /** 27 * Call our custom migrations when defined 28 * 29 * @param Doku_Event $event 30 * @param $param 31 */ 32 public function handle_migrations(Doku_Event $event, $param) 33 { 34 if ($event->data['sqlite']->getAdapter()->getDbname() !== 'ireadit') { 35 return; 36 } 37 $to = $event->data['to']; 38 39 if (is_callable([$this, "migration$to"])) { 40 $event->result = call_user_func([$this, "migration$to"], $event->data); 41 } 42 } 43 44 /** 45 * Convenience function to run an INSERT ... ON CONFLICT IGNORE operation 46 * 47 * The function takes a key-value array with the column names in the key and the actual value in the value, 48 * build the appropriate query and executes it. 49 * 50 * @param string $table the table the entry should be saved to (will not be escaped) 51 * @param array $entry A simple key-value pair array (only values will be escaped) 52 * @return bool|SQLiteResult 53 */ 54 protected function insertOrIgnore(helper_plugin_sqlite $sqlite, $table, $entry) { 55 $keys = join(',', array_keys($entry)); 56 $vals = join(',', array_fill(0,count($entry),'?')); 57 58 $sql = "INSERT OR IGNORE INTO $table ($keys) VALUES ($vals)"; 59 return $sqlite->query($sql, array_values($entry)); 60 } 61 62 protected function migration5($data) 63 { 64 /** @var DokuWiki_Auth_Plugin */ 65 global $auth; 66 67 /** @var helper_plugin_sqlite $sqlite */ 68 $sqlite = $data['sqlite']; 69 $db = $sqlite->getAdapter()->getDb(); 70 71 /** @var helper_plugin_ireadit $helper */ 72 $helper = plugin_load('helper', 'ireadit'); 73 74 //remove old rows 75 $sqlite->query('DELETE FROM ireadit WHERE timestamp IS NULL'); 76 77 $res = $sqlite->query('SELECT page,meta FROM meta'); 78 while ($row = $sqlite->res_fetch_assoc($res)) { 79 $page = $row['page']; 80 $meta = json_decode($row['meta'], true); 81 $last_change_date = p_get_metadata($page, 'last_change date'); 82 83 $users = $auth->retrieveUsers(); 84 foreach ($users as $user => $info) { 85 $res2 = $sqlite->query('SELECT user FROM ireadit WHERE page=? AND rev=? AND user=?', $page, $last_change_date, $user); 86 $existsAlready = $sqlite->res2single($res2); 87 if (!$existsAlready && $helper->in_users_set($user, $meta)) { 88 $sqlite->storeEntry('ireadit', [ 89 'page' => $page, 90 'rev' => $last_change_date, 91 'user' => $user 92 ]); 93 } 94 } 95 } 96 } 97 98 protected function migration3($data) 99 { 100 global $conf; 101 102 /** @var helper_plugin_sqlite $sqlite */ 103 $sqlite = $data['sqlite']; 104 $db = $sqlite->getAdapter()->getDb(); 105 106 $res = $sqlite->query('SELECT page,meta FROM meta'); 107 while ($row = $sqlite->res_fetch_assoc($res)) { 108 $last_change_date = p_get_metadata($row['page'], 'last_change date'); 109 $sqlite->storeEntry('meta2', [ 110 'page' => $row['page'], 111 'meta' => $row['meta'], 112 'last_change_date' => $last_change_date 113 ]); 114 } 115 $sqlite->query('DROP TABLE meta'); 116 $sqlite->query('ALTER TABLE meta2 RENAME TO meta'); 117 } 118 119 protected function migration2($data) 120 { 121 global $conf; 122 123 /** @var helper_plugin_sqlite $sqlite */ 124 $sqlite = $data['sqlite']; 125 $db = $sqlite->getAdapter()->getDb(); 126 127 /* @var \helper_plugin_ireadit $helper */ 128 $helper = plugin_load('helper', 'ireadit'); 129 130 131 $datadir = $conf['datadir']; 132 if (substr($datadir, -1) != '/') { 133 $datadir .= '/'; 134 } 135 136 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($datadir)); 137 $pages = []; 138 foreach ($rii as $file) { 139 if ($file->isDir()){ 140 continue; 141 } 142 143 //remove start path and extension 144 $page = substr($file->getPathname(), strlen($datadir), -4); 145 $pages[] = str_replace('/', ':', $page); 146 } 147 148 $db->beginTransaction(); 149 150 foreach ($pages as $page) { 151 //import historic data 152 $meta = p_get_metadata($page, 'plugin ireadit'); 153 if (!$meta) continue; 154 155 $sqlite->storeEntry('meta', [ 156 'page' => $page, 157 'meta' => json_encode($meta) 158 ]); 159 } 160 $db->commit(); 161 162 163 } 164 165 protected function migration1($data) 166 { 167 global $conf; 168 169 /** @var helper_plugin_sqlite $sqlite */ 170 $sqlite = $data['sqlite']; 171 $db = $sqlite->getAdapter()->getDb(); 172 173 /* @var \helper_plugin_ireadit $helper */ 174 $helper = plugin_load('helper', 'ireadit'); 175 176 177 $datadir = $conf['datadir']; 178 if (substr($datadir, -1) != '/') { 179 $datadir .= '/'; 180 } 181 182 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($datadir)); 183 $pages = []; 184 foreach ($rii as $file) { 185 if ($file->isDir()){ 186 continue; 187 } 188 189 //remove start path and extension 190 $page = substr($file->getPathname(), strlen($datadir), -4); 191 $pages[] = str_replace('/', ':', $page); 192 } 193 $db->beginTransaction(); 194 195 foreach ($pages as $page) { 196 //import historic data 197 $meta = p_get_metadata($page, 'plugin_ireadit'); 198 if (!$meta) continue; 199 200 foreach ($meta as $rev => $data) { 201 if ($rev === '' || count($data) == 0) continue; 202 foreach ($data as $user_read) { 203 $sqlite->storeEntry('ireadit', [ 204 'page' => $page, 205 'rev' => $rev, 206 'user' => $user_read['client'], 207 'timestamp' => date('c', $user_read['time']) 208 ]); 209 } 210 } 211 212 //import current data 213 $content = file_get_contents($datadir . str_replace(':', '/', $page) . '.txt'); 214 $status = preg_match('/~~IREADIT.*~~/', $content, $matches); 215 //no ireadit on page 216 if ($status !== 1) continue; 217 218 $match = trim(substr($matches[0], strlen('~~IREADIT'), -2)); 219 $splits = preg_split('/\s+/', $match, -1, PREG_SPLIT_NO_EMPTY); 220 221 $users = []; 222 $groups = []; 223 foreach ($splits as $split) { 224 if ($split[0] == '@') { 225 $group = substr($split, 1); 226 $groups[] = $group; 227 } else { 228 $users[] = $split; 229 } 230 } 231 232 $usersToInsert = $helper->users_set($users, $groups); 233 234 if ($usersToInsert) { 235 $last_change_date = p_get_metadata($page, 'last_change date'); 236 foreach ($usersToInsert as $user => $info) { 237 $this->insertOrIgnore($sqlite,'ireadit', [ 238 'page' => $page, 239 'rev' => $last_change_date, 240 'user' => $user 241 ]); 242 } 243 } 244 245 } 246 $db->commit(); 247 248 return true; 249 } 250} 251