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 migration3($data) 63 { 64 global $conf; 65 66 /** @var helper_plugin_sqlite $sqlite */ 67 $sqlite = $data['sqlite']; 68 $db = $sqlite->getAdapter()->getDb(); 69 70 $res = $sqlite->query('SELECT page,meta FROM meta'); 71 while ($row = $sqlite->res_fetch_assoc($res)) { 72 $last_change_date = p_get_metadata($row['page'], 'last_change date'); 73 $sqlite->storeEntry('meta2', [ 74 'page' => $row['page'], 75 'meta' => $row['meta'], 76 'last_change_date' => $last_change_date 77 ]); 78 } 79 $sqlite->query('DROP TABLE meta'); 80 $sqlite->query('ALTER TABLE meta2 RENAME TO meta'); 81 } 82 83 protected function migration2($data) 84 { 85 global $conf; 86 87 /** @var helper_plugin_sqlite $sqlite */ 88 $sqlite = $data['sqlite']; 89 $db = $sqlite->getAdapter()->getDb(); 90 91 /* @var \helper_plugin_ireadit $helper */ 92 $helper = plugin_load('helper', 'ireadit'); 93 94 95 $datadir = $conf['datadir']; 96 if (substr($datadir, -1) != '/') { 97 $datadir .= '/'; 98 } 99 100 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($datadir)); 101 $pages = []; 102 foreach ($rii as $file) { 103 if ($file->isDir()){ 104 continue; 105 } 106 107 //remove start path and extension 108 $page = substr($file->getPathname(), strlen($datadir), -4); 109 $pages[] = str_replace('/', ':', $page); 110 } 111 112 $db->beginTransaction(); 113 114 foreach ($pages as $page) { 115 //import historic data 116 $meta = p_get_metadata($page, 'plugin ireadit'); 117 if (!$meta) continue; 118 119 $sqlite->storeEntry('meta', [ 120 'page' => $page, 121 'meta' => json_encode($meta) 122 ]); 123 } 124 $db->commit(); 125 126 127 } 128 129 protected function migration1($data) 130 { 131 global $conf; 132 133 /** @var helper_plugin_sqlite $sqlite */ 134 $sqlite = $data['sqlite']; 135 $db = $sqlite->getAdapter()->getDb(); 136 137 /* @var \helper_plugin_ireadit $helper */ 138 $helper = plugin_load('helper', 'ireadit'); 139 140 141 $datadir = $conf['datadir']; 142 if (substr($datadir, -1) != '/') { 143 $datadir .= '/'; 144 } 145 146 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($datadir)); 147 $pages = []; 148 foreach ($rii as $file) { 149 if ($file->isDir()){ 150 continue; 151 } 152 153 //remove start path and extension 154 $page = substr($file->getPathname(), strlen($datadir), -4); 155 $pages[] = str_replace('/', ':', $page); 156 } 157 $db->beginTransaction(); 158 159 foreach ($pages as $page) { 160 //import historic data 161 $meta = p_get_metadata($page, 'plugin_ireadit'); 162 if (!$meta) continue; 163 164 foreach ($meta as $rev => $data) { 165 if ($rev === '' || count($data) == 0) continue; 166 foreach ($data as $user_read) { 167 $sqlite->storeEntry('ireadit', [ 168 'page' => $page, 169 'rev' => $rev, 170 'user' => $user_read['client'], 171 'timestamp' => date('c', $user_read['time']) 172 ]); 173 } 174 } 175 176 //import current data 177 $content = file_get_contents($datadir . str_replace(':', '/', $page) . '.txt'); 178 $status = preg_match('/~~IREADIT.*~~/', $content, $matches); 179 //no ireadit on page 180 if ($status !== 1) continue; 181 182 $match = trim(substr($matches[0], strlen('~~IREADIT'), -2)); 183 $splits = preg_split('/\s+/', $match, -1, PREG_SPLIT_NO_EMPTY); 184 185 $users = []; 186 $groups = []; 187 foreach ($splits as $split) { 188 if ($split[0] == '@') { 189 $group = substr($split, 1); 190 $groups[] = $group; 191 } else { 192 $users[] = $split; 193 } 194 } 195 196 $usersToInsert = $helper->users_set($users, $groups); 197 198 if ($usersToInsert) { 199 $last_change_date = p_get_metadata($page, 'last_change date'); 200 foreach ($usersToInsert as $user => $info) { 201 $this->insertOrIgnore($sqlite,'ireadit', [ 202 'page' => $page, 203 'rev' => $last_change_date, 204 'user' => $user 205 ]); 206 } 207 } 208 209 } 210 $db->commit(); 211 212 return true; 213 } 214} 215