10173e75dSAndreas Gohr<?php 2*d6d97f60SAnna Dabrowska 30173e75dSAndreas Gohr/** 40173e75dSAndreas Gohr * DokuWiki Plugin struct (Action Component) 50173e75dSAndreas Gohr * 60173e75dSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 70173e75dSAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 80173e75dSAndreas Gohr */ 90173e75dSAndreas Gohr 100173e75dSAndreas Gohr// must be run within Dokuwiki 11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 125a1a3bb1SAndreas Gohruse dokuwiki\plugin\struct\meta\Column; 13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 145a1a3bb1SAndreas Gohruse dokuwiki\plugin\struct\types\Lookup; 15aa4884afSAndreas Gohruse dokuwiki\plugin\struct\types\Media; 165a1a3bb1SAndreas Gohruse dokuwiki\plugin\struct\types\Page; 170173e75dSAndreas Gohr 180173e75dSAndreas Gohrif (!defined('DOKU_INC')) die(); 190173e75dSAndreas Gohr 20*d6d97f60SAnna Dabrowskaclass action_plugin_struct_move extends DokuWiki_Action_Plugin 21*d6d97f60SAnna Dabrowska{ 220173e75dSAndreas Gohr 235a1a3bb1SAndreas Gohr /** @var helper_plugin_sqlite */ 245a1a3bb1SAndreas Gohr protected $db = null; 255a1a3bb1SAndreas Gohr 260173e75dSAndreas Gohr /** 270173e75dSAndreas Gohr * Registers a callback function for a given event 280173e75dSAndreas Gohr * 290173e75dSAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object 300173e75dSAndreas Gohr * @return void 310173e75dSAndreas Gohr */ 32*d6d97f60SAnna Dabrowska public function register(Doku_Event_Handler $controller) 33*d6d97f60SAnna Dabrowska { 34aa4884afSAndreas Gohr $controller->register_hook('PLUGIN_MOVE_PAGE_RENAME', 'AFTER', $this, 'handle_move', true); 35aa4884afSAndreas Gohr $controller->register_hook('PLUGIN_MOVE_MEDIA_RENAME', 'AFTER', $this, 'handle_move', false); 360173e75dSAndreas Gohr } 370173e75dSAndreas Gohr 380173e75dSAndreas Gohr /** 390173e75dSAndreas Gohr * Renames all occurances of a page ID in the database 400173e75dSAndreas Gohr * 410173e75dSAndreas Gohr * @param Doku_Event $event event object by reference 42aa4884afSAndreas Gohr * @param bool $ispage is this a page move operation? 430173e75dSAndreas Gohr * @return bool 440173e75dSAndreas Gohr */ 45*d6d97f60SAnna Dabrowska public function handle_move(Doku_Event $event, $ispage) 46*d6d97f60SAnna Dabrowska { 470173e75dSAndreas Gohr /** @var helper_plugin_struct_db $hlp */ 480173e75dSAndreas Gohr $hlp = plugin_load('helper', 'struct_db'); 495a1a3bb1SAndreas Gohr $this->db = $hlp->getDB(false); 505a1a3bb1SAndreas Gohr if (!$this->db) return false; 510173e75dSAndreas Gohr $old = $event->data['src_id']; 520173e75dSAndreas Gohr $new = $event->data['dst_id']; 530173e75dSAndreas Gohr 542cbf8951SAndreas Gohr // prepare work 552cbf8951SAndreas Gohr $this->db->query('BEGIN TRANSACTION'); 562cbf8951SAndreas Gohr 575a1a3bb1SAndreas Gohr // general update of our meta tables 58aa4884afSAndreas Gohr if ($ispage) { 595a1a3bb1SAndreas Gohr $this->updateDataTablePIDs($old, $new); 605a1a3bb1SAndreas Gohr $this->updateAssignments($old, $new); 615a1a3bb1SAndreas Gohr $this->updateTitles($old, $new); 62aa4884afSAndreas Gohr } 6307e48105SAndreas Gohr 645a1a3bb1SAndreas Gohr // apply updates to all columns in all schemas depending on type 655a1a3bb1SAndreas Gohr $schemas = Schema::getAll(); 665a1a3bb1SAndreas Gohr foreach ($schemas as $table) { 675a1a3bb1SAndreas Gohr $schema = new Schema($table); 685a1a3bb1SAndreas Gohr foreach ($schema->getColumns() as $col) { 69aa4884afSAndreas Gohr if ($ispage) { 705a1a3bb1SAndreas Gohr switch (get_class($col->getType())) { 715a1a3bb1SAndreas Gohr case Page::class: 72aa4884afSAndreas Gohr $this->updateColumnID($schema, $col, $old, $new, true); 735a1a3bb1SAndreas Gohr break; 745a1a3bb1SAndreas Gohr case Lookup::class: 755a1a3bb1SAndreas Gohr $this->updateColumnLookup($schema, $col, $old, $new); 765a1a3bb1SAndreas Gohr break; 770173e75dSAndreas Gohr } 78aa4884afSAndreas Gohr } else { 79aa4884afSAndreas Gohr switch (get_class($col->getType())) { 80aa4884afSAndreas Gohr case Media::class: 81aa4884afSAndreas Gohr $this->updateColumnID($schema, $col, $old, $new); 82aa4884afSAndreas Gohr break; 835a1a3bb1SAndreas Gohr } 845a1a3bb1SAndreas Gohr } 85aa4884afSAndreas Gohr } 86aa4884afSAndreas Gohr } 872cbf8951SAndreas Gohr 882cbf8951SAndreas Gohr // execute everything 892cbf8951SAndreas Gohr $ok = $this->db->query('COMMIT TRANSACTION'); 902cbf8951SAndreas Gohr if (!$ok) { 912cbf8951SAndreas Gohr $this->db->query('ROLLBACK TRANSACTION'); 922cbf8951SAndreas Gohr return false; 932cbf8951SAndreas Gohr } 9407e48105SAndreas Gohr 950173e75dSAndreas Gohr return true; 960173e75dSAndreas Gohr } 9707e48105SAndreas Gohr 9807e48105SAndreas Gohr /** 995a1a3bb1SAndreas Gohr * Update the pid column of ALL data tables 10007e48105SAndreas Gohr * 1015a1a3bb1SAndreas Gohr * (we don't trust the assigments are still there) 1025a1a3bb1SAndreas Gohr * 1035a1a3bb1SAndreas Gohr * @param string $old old page id 1045a1a3bb1SAndreas Gohr * @param string $new new page id 10507e48105SAndreas Gohr */ 106*d6d97f60SAnna Dabrowska protected function updateDataTablePIDs($old, $new) 107*d6d97f60SAnna Dabrowska { 1085a1a3bb1SAndreas Gohr foreach (Schema::getAll('page') as $tbl) { 1095a1a3bb1SAndreas Gohr /** @noinspection SqlResolve */ 1105a1a3bb1SAndreas Gohr $sql = "UPDATE data_$tbl SET pid = ? WHERE pid = ?"; 1115a1a3bb1SAndreas Gohr $this->db->query($sql, array($new, $old)); 11207e48105SAndreas Gohr 1135a1a3bb1SAndreas Gohr /** @noinspection SqlResolve */ 1145a1a3bb1SAndreas Gohr $sql = "UPDATE multi_$tbl SET pid = ? WHERE pid = ?"; 1155a1a3bb1SAndreas Gohr $this->db->query($sql, array($new, $old)); 1165a1a3bb1SAndreas Gohr } 1175a1a3bb1SAndreas Gohr } 11807e48105SAndreas Gohr 1195a1a3bb1SAndreas Gohr /** 1205a1a3bb1SAndreas Gohr * Update the page-schema assignments 1215a1a3bb1SAndreas Gohr * 1225a1a3bb1SAndreas Gohr * @param string $old old page id 1235a1a3bb1SAndreas Gohr * @param string $new new page id 1245a1a3bb1SAndreas Gohr */ 125*d6d97f60SAnna Dabrowska protected function updateAssignments($old, $new) 126*d6d97f60SAnna Dabrowska { 1275a1a3bb1SAndreas Gohr // assignments 1285a1a3bb1SAndreas Gohr $sql = "UPDATE schema_assignments SET pid = ? WHERE pid = ?"; 1295a1a3bb1SAndreas Gohr $this->db->query($sql, array($new, $old)); 1305a1a3bb1SAndreas Gohr // make sure assignments still match patterns; 1315a1a3bb1SAndreas Gohr $assignments = Assignments::getInstance(); 1325a1a3bb1SAndreas Gohr $assignments->reevaluatePageAssignments($new); 1335a1a3bb1SAndreas Gohr } 1345a1a3bb1SAndreas Gohr 1355a1a3bb1SAndreas Gohr /** 1365a1a3bb1SAndreas Gohr * Update the Title information for the moved page 1375a1a3bb1SAndreas Gohr * 1385a1a3bb1SAndreas Gohr * @param string $old old page id 1395a1a3bb1SAndreas Gohr * @param string $new new page id 1405a1a3bb1SAndreas Gohr */ 141*d6d97f60SAnna Dabrowska protected function updateTitles($old, $new) 142*d6d97f60SAnna Dabrowska { 1435a1a3bb1SAndreas Gohr $sql = "UPDATE titles SET pid = ? WHERE pid = ?"; 1445a1a3bb1SAndreas Gohr $this->db->query($sql, array($new, $old)); 1455a1a3bb1SAndreas Gohr } 1465a1a3bb1SAndreas Gohr 1475a1a3bb1SAndreas Gohr /** 148aa4884afSAndreas Gohr * Update the ID in a given column 1495a1a3bb1SAndreas Gohr * 1505a1a3bb1SAndreas Gohr * @param Schema $schema 1515a1a3bb1SAndreas Gohr * @param Column $col 1525a1a3bb1SAndreas Gohr * @param string $old old page id 1535a1a3bb1SAndreas Gohr * @param string $new new page id 154aa4884afSAndreas Gohr * @param bool $hashes could the ID have a hash part? (for Page type) 1555a1a3bb1SAndreas Gohr */ 156*d6d97f60SAnna Dabrowska protected function updateColumnID(Schema $schema, Column $col, $old, $new, $hashes = false) 157*d6d97f60SAnna Dabrowska { 15807e48105SAndreas Gohr $colref = $col->getColref(); 1595a1a3bb1SAndreas Gohr $table = $schema->getTable(); 1605a1a3bb1SAndreas Gohr 16107e48105SAndreas Gohr if ($col->isMulti()) { 16207e48105SAndreas Gohr /** @noinspection SqlResolve */ 16307e48105SAndreas Gohr $sql = "UPDATE multi_$table 16407e48105SAndreas Gohr SET value = REPLACE(value, ?, ?) 16507e48105SAndreas Gohr WHERE value LIKE ? 16607e48105SAndreas Gohr AND colref = $colref 16707e48105SAndreas Gohr AND latest = 1"; 16807e48105SAndreas Gohr } else { 16907e48105SAndreas Gohr /** @noinspection SqlResolve */ 17007e48105SAndreas Gohr $sql = "UPDATE data_$table 17107e48105SAndreas Gohr SET col$colref = REPLACE(col$colref, ?, ?) 17207e48105SAndreas Gohr WHERE col$colref LIKE ? 17307e48105SAndreas Gohr AND latest = 1"; 17407e48105SAndreas Gohr } 1755a1a3bb1SAndreas Gohr $this->db->query($sql, $old, $new, $old); // exact match 176aa4884afSAndreas Gohr if ($hashes) { 1775a1a3bb1SAndreas Gohr $this->db->query($sql, $old, $new, "$old#%"); // match with hashes 17807e48105SAndreas Gohr } 179aa4884afSAndreas Gohr } 1805a1a3bb1SAndreas Gohr 1815a1a3bb1SAndreas Gohr /** 1825a1a3bb1SAndreas Gohr * Update a Lookup type column 1835a1a3bb1SAndreas Gohr * 1845a1a3bb1SAndreas Gohr * Lookups contain a page id when the referenced schema is a data schema 1855a1a3bb1SAndreas Gohr * 1865a1a3bb1SAndreas Gohr * @param Schema $schema 1875a1a3bb1SAndreas Gohr * @param Column $col 1885a1a3bb1SAndreas Gohr * @param string $old old page id 1895a1a3bb1SAndreas Gohr * @param string $new new page id 1905a1a3bb1SAndreas Gohr */ 191*d6d97f60SAnna Dabrowska protected function updateColumnLookup(Schema $schema, Column $col, $old, $new) 192*d6d97f60SAnna Dabrowska { 1935a1a3bb1SAndreas Gohr $tconf = $col->getType()->getConfig(); 1945a1a3bb1SAndreas Gohr $ref = new Schema($tconf['schema']); 1955a1a3bb1SAndreas Gohr if (!$ref->getId()) return; // this schema does not exist 1960ceefd5cSAnna Dabrowska if (!$ref->getTimeStamp()) return; // a lookup is referenced, nothing to do 19710575566SAnna Dabrowska // FIXME does this make sense at all? it's always a lookup, isn't it? 19810575566SAnna Dabrowska $this->updateColumnID($schema, $col, $old, $new); 19907e48105SAndreas Gohr } 2000173e75dSAndreas Gohr} 201