/** * Rewrite pages when they are read and they need to be updated. * * @param Doku_Event $event The event object * @param mixed $param Optional parameters (not used) */ function handle_read(Doku_Event $event, $param) { global $ACT, $conf; static $stack = array(); // handle only reads of the current revision if ($event->data[3]) { return; } // only rewrite if not in move already $save = true; if (helper_plugin_move_rewrite::isLocked()) { $save = false; } $id = $event->data[2]; if ($event->data[1]) { $id = $event->data[1] . ':' . $id; } if (!$id) { // try to reconstruct the id from the filename $path = $event->data[0][0]; if (strpos($path, $conf['datadir']) === 0) { $path = substr($path, strlen($conf['datadir']) + 1); $id = pathID($path); } } if (isset($stack[$id])) { return; } // Don't change the page when the user is currently changing the page content or the page is locked $forbidden_actions = array('save', 'preview', 'recover', 'revert'); if (isset($ACT) && (in_array($ACT, $forbidden_actions) || is_array($ACT) && in_array(key($ACT), $forbidden_actions)) || checklock($id) !== false || @file_exists(wikiLockFN($id))) { return; } /** @var helper_plugin_move_rewrite $helper */ $helper = plugin_load('helper', 'move_rewrite', true); if (!is_null($helper)) { $stack[$id] = true; $event->result = $helper->rewritePage($id, $event->result, $save); unset($stack[$id]); } }
/** * Handle the input */ function handle() { global $INPUT; // create a new plan if possible and sufficient data was given $this->createPlanFromInput(); // handle workflow button presses if ($this->plan->isCommited()) { helper_plugin_move_rewrite::addLock(); //todo: right place? switch ($INPUT->str('ctl')) { case 'continue': $this->plan->nextStep(); break; case 'skip': $this->plan->nextStep(true); break; case 'abort': $this->plan->abort(); break; } } }
/** * Execute the next steps * * @param bool $skip set to true to skip the next first step (skip error) * @return bool|int false on errors, otherwise the number of remaining steps * @throws Exception */ public function nextStep($skip = false) { if (!$this->options['commited']) { throw new Exception('plan is not committed yet!'); } // execution has started if (!$this->options['started']) { $this->options['started'] = time(); } helper_plugin_move_rewrite::addLock(); if (@filesize($this->files['pagelist']) > 1) { $todo = $this->stepThroughDocuments(self::TYPE_PAGES, $skip); if ($todo === false) { return $this->storeError(); } return max($todo, 1); // force one more call } if (@filesize($this->files['medialist']) > 1) { $todo = $this->stepThroughDocuments(self::TYPE_MEDIA, $skip); if ($todo === false) { return $this->storeError(); } return max($todo, 1); // force one more call } if (@filesize($this->files['missing']) > 1 && @filesize($this->files['affected']) > 1) { $todo = $this->stepThroughMissingDocuments(self::TYPE_PAGES); if ($todo === false) { return $this->storeError(); } return max($todo, 1); // force one more call } if (@filesize($this->files['missing_media']) > 1 && @filesize($this->files['affected']) > 1) { $todo = $this->stepThroughMissingDocuments(self::TYPE_MEDIA); if ($todo === false) { return $this->storeError(); } return max($todo, 1); // force one more call } if (@filesize($this->files['namespaces']) > 1) { $todo = $this->stepThroughNamespaces(); if ($todo === false) { return $this->storeError(); } return max($todo, 1); // force one more call } helper_plugin_move_rewrite::removeAllLocks(); if ($this->options['autorewrite'] && @filesize($this->files['affected']) > 1) { $todo = $this->stepThroughAffectedPages(); if ($todo === false) { return $this->storeError(); } return max($todo, 1); // force one more call } // we're done here, clean up $this->abort(); return 0; }
/** * Execute a page move/rename * * @param string $src original ID * @param string $dst new ID * @return bool */ public function movePage($src, $dst) { if (!$this->checkPage($src, $dst)) { return false; } // lock rewrites helper_plugin_move_rewrite::addLock(); /** @var helper_plugin_move_rewrite $Rewriter */ $Rewriter = plugin_load('helper', 'move_rewrite'); // remember what this page was called before the move in meta data $Rewriter->setSelfMoveMeta($src); // ft_backlinks() is not used here, as it does a hidden page and acl check but we really need all pages $affected_pages = idx_get_indexer()->lookupKey('relation_references', $src); $affected_pages[] = $dst; // the current page is always affected, because all relative links may have changed $affected_pages = array_unique($affected_pages); $src_ns = getNS($src); $src_name = noNS($src); $dst_ns = getNS($dst); $dst_name = noNS($dst); // pass this info on to other plugins $eventdata = array('opts' => array('ns' => $src_ns, 'name' => $src_name, 'newns' => $dst_ns, 'newname' => $dst_name), 'affected_pages' => &$affected_pages, 'src_id' => $src, 'dst_id' => $dst); // give plugins the option to add their own meta files to the list of files that need to be moved // to the oldfiles/newfiles array or to adjust their own metadata, database, ... // and to add other pages to the affected pages $event = new Doku_Event('PLUGIN_MOVE_PAGE_RENAME', $eventdata); if ($event->advise_before()) { lock($src); /** @var helper_plugin_move_file $FileMover */ $FileMover = plugin_load('helper', 'move_file'); // Move the Subscriptions & Indexes (new feature since Spring 2013 release) $Indexer = idx_get_indexer(); if (($idx_msg = $Indexer->renamePage($src, $dst)) !== true || ($idx_msg = $Indexer->renameMetaValue('relation_references', $src, $dst)) !== true) { msg(sprintf($this->getLang('indexerror'), $idx_msg), -1); return false; } if (!$FileMover->movePageMeta($src_ns, $src_name, $dst_ns, $dst_name)) { msg(sprintf($this->getLang('metamoveerror'), $src), -1); return false; } // prepare the summary for the changelog entry if ($src_ns == $dst_ns) { $lang_key = 'renamed'; } elseif ($src_name == $dst_name) { $lang_key = 'moved'; } else { $lang_key = 'move_rename'; } $summary = $this->symbol . ' ' . sprintf($this->getLang($lang_key), $src, $dst); // Wait a second when the page has just been rewritten $oldRev = filemtime(wikiFN($src)); if ($oldRev == time()) { sleep(1); } // Save the updated document in its new location $text = rawWiki($src); saveWikiText($dst, $text, $summary); // Delete the orginal file if (@file_exists(wikiFN($dst))) { saveWikiText($src, '', $summary); } // Move the old revisions if (!$FileMover->movePageAttic($src_ns, $src_name, $dst_ns, $dst_name)) { // it's too late to stop the move, so just display a message. msg(sprintf($this->getLang('atticmoveerror'), $src), -1); } // Add meta data to all affected pages, so links get updated later foreach ($affected_pages as $id) { $Rewriter->setMoveMeta($id, $src, $dst, 'pages'); } unlock($src); } $event->advise_after(); // store this for later use $this->affectedPages = $affected_pages; // unlock rewrites helper_plugin_move_rewrite::removeLock(); return true; }