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_approve_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() !== 'approve') { 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 migration1($data) 63 { 64 global $conf; 65 66 /** @var helper_plugin_sqlite $sqlite */ 67 $sqlite = $data['sqlite']; 68 $db = $sqlite->getAdapter()->getDb(); 69 70 71 $datadir = $conf['datadir']; 72 if (substr($datadir, -1) != '/') { 73 $datadir .= '/'; 74 } 75 76 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($datadir)); 77 $pages = []; 78 foreach ($rii as $file) { 79 if ($file->isDir()){ 80 continue; 81 } 82 83 //remove start path and extension 84 $page = substr($file->getPathname(), strlen($datadir), -4); 85 $pages[] = str_replace('/', ':', $page); 86 } 87 88 $db->beginTransaction(); 89 90 $apr_namespaces = preg_split('/\s+/', $this->getConf('apr_namespaces', ''), 91 -1,PREG_SPLIT_NO_EMPTY); 92 93 if (!$apr_namespaces) { 94 $sqlite->storeEntry('maintainer',[ 95 'namespace' => '**' 96 ]); 97 } else { 98 foreach ($apr_namespaces as $namespace) { 99 $namespace = rtrim($namespace, ':'); 100 $namespace .= ':**'; 101 $sqlite->storeEntry('maintainer',[ 102 'namespace' => $namespace 103 ]); 104 } 105 } 106 107 //store config 108 $no_apr_namespaces = $this->getConf('no_apr_namespaces', ''); 109 $sqlite->storeEntry('config',[ 110 'key' => 'no_apr_namespaces', 111 'value' => $no_apr_namespaces 112 ]); 113 114 $no_apr_namespaces_list = preg_split('/\s+/', $no_apr_namespaces,-1, 115 PREG_SPLIT_NO_EMPTY); 116 $no_apr_namespaces_list = array_map(function ($namespace) { 117 return trim($namespace, ':'); 118 }, $no_apr_namespaces_list); 119 120 121 foreach ($pages as $page) { 122 //import historic data 123 $versions = p_get_metadata($page, 'plugin_approve_versions'); 124 if (!$versions) { 125 $versions = $this->render_metadata_for_approved_page($page); 126 } 127 128// $last_change_date = p_get_metadata($page, 'last_change date'); 129 $last_change_date = @filemtime(wikiFN($page)); 130 $last_version = $versions[0]; 131 132 //remove current versions to not process it here 133 unset($versions[0]); 134 unset($versions[$last_change_date]); 135 136 $revision_editors = $this->revision_editors($page); 137 foreach ($versions as $rev => $version) { 138 $data = [ 139 'page' => $page, 140 'rev' => $rev, 141 'approved' => date('c', $rev), 142 'approved_by' => $revision_editors[$rev], 143 'version' => $version 144 ]; 145 $sqlite->storeEntry('revision', $data); 146 } 147 148 //process current data 149 $summary = p_get_metadata($page, 'last_change sum'); 150 $user = p_get_metadata($page, 'last_change user'); 151 $data = [ 152 'page' => $page, 153 'rev' => $last_change_date, 154 'current' => 1 155 ]; 156 if ($this->getConf('ready_for_approval') && 157 $summary == $this->getConf('sum ready for approval')) { 158 $data['ready_for_approval'] = date('c', $last_change_date); 159 $data['ready_for_approval_by'] = $user; 160 } elseif($summary == $this->getConf('sum approved')) { 161 $data['approved'] = date('c', $last_change_date); 162 $data['approved_by'] = $user; 163 $data['version'] = $last_version; 164 } 165 $sqlite->storeEntry('revision', $data); 166 167 168 //empty apr_namespaces - all match 169 if (!$apr_namespaces) { 170 $in_apr_namespace = true; 171 } else { 172 $in_apr_namespace = false; 173 foreach ($apr_namespaces as $namespace) { 174 if (substr($page, 0, strlen($namespace)) == $namespace) { 175 $in_apr_namespace = true; 176 break; 177 } 178 } 179 } 180 181 if ($in_apr_namespace) { 182 $hidden = '0'; 183 foreach ($no_apr_namespaces_list as $namespace) { 184 if (substr($page, 0, strlen($namespace)) == $namespace) { 185 $hidden = '1'; 186 break; 187 } 188 } 189 $sqlite->storeEntry('page', [ 190 'page' => $page, 191 'hidden' => $hidden 192 ]); 193 } 194 } 195 196 197 $db->commit(); 198 199 return true; 200 } 201 202 /** 203 * Calculate current version 204 * 205 * @param $id 206 * @return array 207 */ 208 protected function render_metadata_for_approved_page($id, $currev=false) { 209 if (!$currev) $currev = @filemtime(wikiFN($id)); 210 211 $version = $this->approved($id); 212 //version for current page 213 $curver = $version + 1; 214 $versions = array(0 => $curver, $currev => $curver); 215 216 $changelog = new PageChangeLog($id); 217 $first = 0; 218 $num = 100; 219 while (count($revs = $changelog->getRevisions($first, $num)) > 0) { 220 foreach ($revs as $rev) { 221 $revInfo = $changelog->getRevisionInfo($rev); 222 if ($revInfo['sum'] == $this->getConf('sum approved')) { 223 $versions[$rev] = $version; 224 $version -= 1; 225 } 226 } 227 $first += $num; 228 } 229 230// p_set_metadata($id, array(ApproveConst::METADATA_VERSIONS_KEY => $versions)); 231 232 return $versions; 233 } 234 235 /** 236 * Get the number of approved pages 237 * @param $id 238 * @return int 239 */ 240 protected function approved($id) { 241 $count = 0; 242 243 $changelog = new PageChangeLog($id); 244 $first = 0; 245 $num = 100; 246 while (count($revs = $changelog->getRevisions($first, $num)) > 0) { 247 foreach ($revs as $rev) { 248 $revInfo = $changelog->getRevisionInfo($rev); 249 if ($revInfo['sum'] == $this->getConf('sum approved')) { 250 $count += 1; 251 } 252 } 253 $first += $num; 254 } 255 256 return $count; 257 } 258 259 /** 260 * Calculate current version 261 * 262 * @param $id 263 * @return array 264 */ 265 protected function revision_editors($id) 266 { 267 $currev = @filemtime(wikiFN($id)); 268 $user = p_get_metadata($id, 'last_change user'); 269 270 $revision_editors = array($currev => $user); 271 272 $changelog = new PageChangeLog($id); 273 $first = 0; 274 $num = 100; 275 while (count($revs = $changelog->getRevisions($first, $num)) > 0) { 276 foreach ($revs as $rev) { 277 $revInfo = $changelog->getRevisionInfo($rev); 278 $revision_editors[$rev] = $revInfo['user']; 279 } 280 $first += $num; 281 } 282 283 return $revision_editors; 284 } 285} 286