xref: /plugin/struct/action/move.php (revision 07e481057ffda38c2a6654063f79614d606231f3)
10173e75dSAndreas Gohr<?php
20173e75dSAndreas Gohr/**
30173e75dSAndreas Gohr * DokuWiki Plugin struct (Action Component)
40173e75dSAndreas Gohr *
50173e75dSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
60173e75dSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
70173e75dSAndreas Gohr */
80173e75dSAndreas Gohr
90173e75dSAndreas Gohr// must be run within Dokuwiki
10ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
120173e75dSAndreas Gohr
130173e75dSAndreas Gohrif(!defined('DOKU_INC')) die();
140173e75dSAndreas Gohr
150173e75dSAndreas Gohrclass action_plugin_struct_move extends DokuWiki_Action_Plugin {
160173e75dSAndreas Gohr
170173e75dSAndreas Gohr    /**
180173e75dSAndreas Gohr     * Registers a callback function for a given event
190173e75dSAndreas Gohr     *
200173e75dSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
210173e75dSAndreas Gohr     * @return void
220173e75dSAndreas Gohr     */
230173e75dSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
240173e75dSAndreas Gohr        $controller->register_hook('PLUGIN_MOVE_PAGE_RENAME', 'AFTER', $this, 'handle_move');
250173e75dSAndreas Gohr    }
260173e75dSAndreas Gohr
270173e75dSAndreas Gohr    /**
280173e75dSAndreas Gohr     * Renames all occurances of a page ID in the database
290173e75dSAndreas Gohr     *
300173e75dSAndreas Gohr     * @param Doku_Event $event event object by reference
310173e75dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
320173e75dSAndreas Gohr     *                           handler was registered]
330173e75dSAndreas Gohr     * @return bool
340173e75dSAndreas Gohr     */
350173e75dSAndreas Gohr    public function handle_move(Doku_Event $event, $param) {
360173e75dSAndreas Gohr        /** @var helper_plugin_struct_db $hlp */
370173e75dSAndreas Gohr        $hlp = plugin_load('helper', 'struct_db');
387cbcfbdbSAndreas Gohr        $db = $hlp->getDB(false);
390173e75dSAndreas Gohr        if(!$db) return false;
400173e75dSAndreas Gohr        $old = $event->data['src_id'];
410173e75dSAndreas Gohr        $new = $event->data['dst_id'];
420173e75dSAndreas Gohr
430173e75dSAndreas Gohr        // ALL data tables (we don't trust the assigments are still there)
44e0e1cf44SAndreas Gohr        foreach(Schema::getAll('page') as $tbl) {
45*07e48105SAndreas Gohr            /** @noinspection SqlResolve */
460173e75dSAndreas Gohr            $sql = "UPDATE data_$tbl SET pid = ? WHERE pid = ?";
470173e75dSAndreas Gohr            $db->query($sql, array($new, $old));
48*07e48105SAndreas Gohr
49*07e48105SAndreas Gohr            /** @noinspection SqlResolve */
500173e75dSAndreas Gohr            $sql = "UPDATE multi_$tbl SET pid = ? WHERE pid = ?";
510173e75dSAndreas Gohr            $db->query($sql, array($new, $old));
520173e75dSAndreas Gohr        }
530173e75dSAndreas Gohr        // assignments
540173e75dSAndreas Gohr        $sql = "UPDATE schema_assignments SET pid = ? WHERE pid = ?";
550173e75dSAndreas Gohr        $db->query($sql, array($new, $old));
560173e75dSAndreas Gohr        // make sure assignments still match patterns;
57025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
580173e75dSAndreas Gohr        $assignments->reevaluatePageAssignments($new);
590173e75dSAndreas Gohr
60606635f3SAndreas Gohr        // titles
61606635f3SAndreas Gohr        $sql = "UPDATE titles SET pid = ? WHERE pid = ?";
62606635f3SAndreas Gohr        $db->query($sql, array($new, $old));
63606635f3SAndreas Gohr
64*07e48105SAndreas Gohr        // Page Type references
65*07e48105SAndreas Gohr        $this->movePageLinks($db, $old, $new);
66*07e48105SAndreas Gohr
670173e75dSAndreas Gohr        return true;
680173e75dSAndreas Gohr    }
69*07e48105SAndreas Gohr
70*07e48105SAndreas Gohr    /**
71*07e48105SAndreas Gohr     * Handles current values for all Page type columns
72*07e48105SAndreas Gohr     *
73*07e48105SAndreas Gohr     * @param helper_plugin_sqlite $db
74*07e48105SAndreas Gohr     * @param string $old
75*07e48105SAndreas Gohr     * @param string $new
76*07e48105SAndreas Gohr     */
77*07e48105SAndreas Gohr    protected function movePageLinks(helper_plugin_sqlite $db, $old, $new) {
78*07e48105SAndreas Gohr        $schemas = Schema::getAll();
79*07e48105SAndreas Gohr
80*07e48105SAndreas Gohr        foreach($schemas as $table) {
81*07e48105SAndreas Gohr            $schema = new Schema($table);
82*07e48105SAndreas Gohr            foreach($schema->getColumns() as $col) {
83*07e48105SAndreas Gohr                if(!is_a($col->getType(), dokuwiki\plugin\struct\types\Page::class)) continue;
84*07e48105SAndreas Gohr
85*07e48105SAndreas Gohr                $colref = $col->getColref();
86*07e48105SAndreas Gohr                if($col->isMulti()) {
87*07e48105SAndreas Gohr                    /** @noinspection SqlResolve */
88*07e48105SAndreas Gohr                    $sql = "UPDATE multi_$table
89*07e48105SAndreas Gohr                               SET value = REPLACE(value, ?, ?)
90*07e48105SAndreas Gohr                             WHERE value LIKE ?
91*07e48105SAndreas Gohr                               AND colref = $colref
92*07e48105SAndreas Gohr                               AND latest = 1";
93*07e48105SAndreas Gohr
94*07e48105SAndreas Gohr                } else {
95*07e48105SAndreas Gohr                    /** @noinspection SqlResolve */
96*07e48105SAndreas Gohr                    $sql = "UPDATE data_$table
97*07e48105SAndreas Gohr                               SET col$colref = REPLACE(col$colref, ?, ?)
98*07e48105SAndreas Gohr                             WHERE col$colref LIKE ?
99*07e48105SAndreas Gohr                               AND latest = 1";
100*07e48105SAndreas Gohr                }
101*07e48105SAndreas Gohr                $db->query($sql, $old, $new, $old); // exact match
102*07e48105SAndreas Gohr                $db->query($sql, $old, $new, "$old#%"); // match with hashes
103*07e48105SAndreas Gohr            }
104*07e48105SAndreas Gohr        }
105*07e48105SAndreas Gohr    }
1060173e75dSAndreas Gohr}
107