public function renamePageEvent($runData) { $pl = $runData->getParameterList(); $pageId = $pl->getParameterValue("page_id"); $newName = trim($pl->getParameterValue("new_name")); $newName = WDStringUtils::toUnixName($newName); // purify! (for sure) $fixDeps = $pl->getParameterValue('fixdeps'); $site = $runData->getTemp("site"); if ($newName == null || $newName == '') { throw new ProcessException(_("Destination page name should be given."), "no_new_name"); } $db = Database::connection(); $db->begin(); $c = new Criteria(); $c->add("page_id", $pageId); $c->setForUpdate(true); $page = DB_PagePeer::instance()->selectOne($c); if ($page == null || $page->getSiteId() != $site->getSiteId()) { throw new ProcessException(_("Error getting page information."), "no_page"); } if ($newName == $page->getUnixName()) { throw new ProcessException(_("The current and new names are the same."), "page_exists"); } // check for permissions again $category = $page->getCategory(); $user = $runData->getUser(); WDPermissionManager::instance()->hasPagePermission('move', $user, $category, $page); // check if the new page exists or not. $conflictPage = DB_PagePeer::instance()->selectByName($site->getSiteId(), $newName); if ($conflictPage != null) { throw new ProcessException(_("The destination page already exists."), "page_exists"); } $oldName = $page->getUnixName(); // check if new page exists! // check for locks first DB_PageEditLockPeer::instance()->deleteOutdated($pageId); $c = new Criteria(); $c->add("page_id", $page->getPageId()); if ($pl->getParameterValue("force") === "yes") { DB_PageEditLockPeer::instance()->delete($c); } $locks = DB_PageEditLockPeer::instance()->select($c); if (count($locks) > 0) { $runData->ajaxResponseAdd("locks", true); $runData->contextAdd("locks", $locks); $runData->setModuleTemplate("rename/PageLockedWin"); $db->rollback(); return; } // success so far... // create new revision, new metadata and alter the page object too. $oldMetadata = $page->getMetadata(); $metadata = clone $oldMetadata; $metadata->setNew(true); $metadata->setMetadataId(null); $metadata->setUnixName($newName); $metadata->save(); $oldRevision = $page->getCurrentRevision(); $revision = new DB_PageRevision(); $revision->setSiteId($site->getSiteId()); $revision->setPageId($page->getPageId()); // copy source id $revision->setSourceId($oldRevision->getSourceId()); $revision->setSinceFullSource($oldRevision->getSinceFullSource()); $revision->setDiffSource($oldRevision->getDiffSource()); $revision->setMetadataId($metadata->getMetadataId()); $revision->setFlagRename(true); $revision->setRevisionNumber($oldRevision->getRevisionNumber() + 1); $revision->setComments(_("Page name changed") . ": \"{$oldName}\" " . _("to") . " \"{$newName}\"."); $userId = $runData->getUserId(); if ($userId == null) { $userString = $runData->createIpString(); } if ($userId) { $revision->setUserId($userId); $page->setLastEditUserId($userId); } else { $revision->setUserId(0); $page->setLastEditUserId(0); $revision->setUserString($userString); $page->setLastEditUserString($userString); } $now = new ODate(); $revision->setDateLastEdited($now); $revision->save(); // alter the page info $page->setRevisionId($revision->getRevisionId()); $page->setDateLastEdited($now); $page->setUnixName($newName); $page->setRevisionNumber($revision->getRevisionNumber()); // handle the categories // extract category name if (strpos($newName, ':') != false) { // ok, there is category! $exp = explode(':', $newName); $categoryName = $exp[0]; } else { // no category name, "_default" assumed $categoryName = "_default"; } if (strpos($oldName, ':') != false) { // ok, there is category! $exp = explode(':', $oldName); $oldCategoryName = $exp[0]; } else { // no category name, "_default" assumed $oldCategoryName = "_default"; } $page->save(); $outdater = new Outdater(); if ($categoryName !== $oldCategoryName) { // check if new category exists. if not - create it! $category = DB_CategoryPeer::instance()->selectByName($categoryName, $site->getSiteId(), false); if ($category == null) { // create the category - just clone the default category!!! $category = DB_CategoryPeer::instance()->selectByName("_default", $site->getSiteId(), false); $category->setName($categoryName); // fill with some important things - we assume the _default category exists!!! IT REALLY SHOULD!!! $category->setCategoryId(null); $category->setNew(true); // this will make it INSERT, not UPDATE on save() $category->setPermissionsDefault(true); $category->setThemeDefault(true); $category->setLicenseDefault(true); $category->setNavDefault(true); $category->save(); } else { //also check if one has permissions to create new pages in // the new category!!! try { WDPermissionManager::instance()->hasPagePermission('create', $user, $category); } catch (Exception $e) { throw new ProcessException(_("You are not allowed to create new pages in the destination category") . " \"" . $category->getName() . "\".", "not_allowed"); } } $page->setCategoryId($category->getCategoryId()); $page->save(); // also see if the old category is empty - if yes - delete it! if ($oldCategoryName != "_default") { $category = DB_CategoryPeer::instance()->selectByName($oldCategoryName, $site->getSiteId(), false); $c = new Criteria(); $c->add("category_id", $category->getCategoryId()); $count = DB_PagePeer::instance()->selectCount($c); if ($count == 0) { // delete the category DB_CategoryPeer::instance()->delete($c); $outdater->categoryEvent('delete', $category, $site); } } } // outdate party! $outdater->pageEvent("rename", $page, $oldName); // index page // move files too $oldDir = WIKIDOT_ROOT . "/web/files--sites/" . $site->getUnixName() . "/files/" . $oldName; $newDir = WIKIDOT_ROOT . "/web/files--sites/" . $site->getUnixName() . "/files/" . $newName; if (file_exists($oldDir)) { if (rename($oldDir, $newDir) == false) { throw new ProcessException(_("Error moving attached files."), "error_files"); } } $oldRDir = WIKIDOT_ROOT . "/web/files--sites/" . $site->getUnixName() . "/resized-images/" . $oldName; $newRDir = WIKIDOT_ROOT . "/web/files--sites/" . $site->getUnixName() . "/resized-images/" . $newName; if (file_exists($oldRDir)) { if (rename($oldRDir, $newRDir) == false) { throw new ProcessException(_("Error moving attached (resized) files."), "error_files"); } } // try to fix dependencies if ($fixDeps && preg_match('/^[0-9]+(,[0-9]+)*$/', $fixDeps)) { $fixPageIds = explode(',', $fixDeps); foreach ($fixPageIds as $pageId) { $page = DB_PagePeer::instance()->selectByPrimaryKey($pageId); if ($page == null || $page->getSiteId() !== $site->getSiteId()) { continue; } // check for any locks $c = new Criteria(); $c->add("page_id", $pageId); $lock = DB_PageEditLockPeer::instance()->selectOne($c); if ($lock) { continue; } $fixer = new DependencyFixer($page, $oldName, $newName); $fixer->setUser($user); $fixer->fixLinks(); $od = new Outdater(); $od->pageEvent('source_changed', $page); } } // check any dependency left $c = new Criteria(); $q = "SELECT page_id, title, unix_name FROM page_link, page " . "WHERE page_link.to_page_name='" . db_escape_string($oldName) . "' " . "AND page_link.from_page_id=page.page_id AND page.site_id={$site->getSiteId()} ORDER BY COALESCE(title, unix_name)"; $c->setExplicitQuery($q); $pages = DB_PagePeer::instance()->select($c); $q = "SELECT page_id, title, unix_name FROM page, page_inclusion " . "WHERE page_inclusion.included_page_name='" . db_escape_string($oldName) . "' " . "AND page_inclusion.including_page_id=page.page_id AND page.site_id={$site->getSiteId()} ORDER BY COALESCE(title, unix_name)"; $c->setExplicitQuery($q); $pagesI = DB_PagePeer::instance()->select($c); if (count($pages) > 0 || count($pagesI) > 0) { $runData->setModuleTemplate("rename/LeftDepsModule"); $runData->contextAdd("pagesI", $pagesI); $runData->contextAdd("pages", $pages); $runData->ajaxResponseAdd("leftDeps", true); } $runData->ajaxResponseAdd("newName", $newName); EventLogger::instance()->logPageRename($page, $oldName); $db->commit(); sleep(0.5); }
public function deletePage($page, $site = null) { // I hope everything here is within a TRANSACTION!!! if (!$page) { return; } if (!$site) { $site = DB_SitePeer::instance()->selectByPrimaryKey($page->getSiteId()); } // delete the sources and metadatas $db = Database::connection(); // get descandants first $rec = 0; $c = new Criteria(); $c->add("parent_page_id", $page->getPageId()); $pages = DB_PagePeer::instance()->select($c); // ok, these are direct children. need to clear the perent_page_id field $descs = array(); while ($pages !== null && count($pages) > 0 && $rec < 10) { $p2 = array(); foreach ($pages as $p) { $c = new Criteria(); $c->add("parent_page_id", $p->getPageId()); $ptmp = DB_PagePeer::instance()->select($c); $p2 = array_merge($p2, $ptmp); if ($rec === 0) { $p->setParentPageId(null); $p->save(); // clear metadata $m = $p->getMetadata(); $m->setParentPageId(null); $m->save(); } } $descs = array_merge($descs, $pages, $p2); $pages = $p2; $rec++; } $category = $page->getCategory(); // sources $q = "DELETE FROM page_source WHERE source_id IN (SELECT page_revision.source_id FROM page_revision WHERE page_id='" . $page->getPageId() . "')"; $db->query($q); //metadatas $q = "DELETE FROM page_metadata WHERE metadata_id IN (SELECT page_revision.metadata_id FROM page_revision WHERE page_id='" . $page->getPageId() . "')"; $db->query($q); // delete the page too $q = "DELETE FROM page WHERE page_id='" . $page->getPageId() . "'"; $db->query($q); // remove from cache too. $outdater = new Outdater(); $outdater->pageEvent('delete', $page->getUnixName()); // outdate descs too foreach ($descs as $desc) { $outdater->outdatePageCache($desc); } // delete the category if empty if ($category->getName() != "_default") { $c = new Criteria(); $c->add("category_id", $category->getCategoryId()); $count = DB_PagePeer::instance()->selectCount($c); if ($count == 0) { // delete the category DB_CategoryPeer::instance()->delete($c); $outdater->categoryEvent('delete', $category, $site); } } // remove FILES (if any) $path = WIKIDOT_ROOT . "/web/files--sites/" . $site->getUnixName() . "/files/" . $page->getUnixName(); exec('rm -r ' . escapeshellarg($path) . ' &> /dev/null'); // }
public function savePageRateSettingsEvent($runData) { $pl = $runData->getParameterList(); $site = $runData->getTemp("site"); $siteId = $site->getSiteId(); $json = new JSONService(SERVICES_JSON_LOOSE_TYPE); $cats0 = $json->decode($pl->getParameterValue("categories")); $db = Database::connection(); $db->begin(); $outdater = new Outdater(); foreach ($cats0 as $category) { $categoryId = $category['category_id']; $c = new Criteria(); $c->add("category_id", $categoryId); $c->add("site_id", $siteId); // for sure ;-) $dCategory = DB_CategoryPeer::instance()->selectOne($c); // now compare $changed = false; if ($category['rating'] !== $dCategory->getRating()) { $dCategory->setRating($category['rating']); $changed = true; } if ($changed) { $dCategory->save(); // outdate category too $outdater->categoryEvent("category_save", $dCategory); } } $db->commit(); if (GlobalProperties::$UI_SLEEP) { sleep(1); } }