コード例 #1
0
ファイル: WikiPageAction.php プロジェクト: jbzdak/wikidot
 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);
 }