Ejemplo n.º 1
0
 public function save($args)
 {
     $db = Database::connection();
     $db->begin();
     // simple argument checking
     if (!isset($args['page'])) {
         throw new Wikidot_Facade_Exception_WrongArguments("Page argument must be passed");
     }
     $pm = new WDPermissionManager();
     $now = new ODate();
     // page (existant or not) name
     $arg_page = WDStringUtils::toUnixName($args['page']);
     // parse the rest (beside page name)
     unset($args['page']);
     $this->parseArgs($args, array("performer", "site"));
     try {
         // parse page name to figure out if it points to an existant page
         $page = $this->_parsePage($this->site, $arg_page);
         $new = false;
         // check permissions to edit the page
         $pm->hasPagePermission('edit', $this->performer, $page->getCategory(), $page);
     } catch (Wikidot_Facade_Exception_WrongArguments $e) {
         if ($this->source === null) {
             $this->source = "";
         }
         if ($this->title === null) {
             $this->title = $arg_page;
         }
         $new = true;
         $category_name = preg_replace('/^([^:]*):.*$/', '\\1', $arg_page);
         if ($category_name == $arg_page) {
             $category_name = '_default';
         }
         $category = $this->_getOrCreateCategory($this->site, $category_name);
         $page = new DB_Page();
         $page->setSiteId($this->site->getSiteId());
         $page->setCategoryId($category->getCategoryId());
         $page->setUnixName($arg_page);
         $page->setDateCreated(new ODate());
         $page->setOwnerUserId($this->performer->getUserId());
         $page->save();
         $compiled = new DB_PageCompiled();
         $compiled->setPageId($page->getPageId());
         $compiled->save();
     }
     // get current revision and metadata
     if (!$new) {
         $cur_rev = $page->getCurrentRevision();
         $cur_meta = $cur_rev->getMetadata();
     }
     // construct new metadata
     if ($new) {
         $new_meta = new DB_PageMetadata();
         $new_meta->setUnixName($arg_page);
         $new_meta->setOwnerUserId($this->performer->getUserId());
     } else {
         $new_meta = clone $cur_meta;
         $new_meta->setNew(true);
         $new_meta->setMetadataId(null);
     }
     // construct new revision
     $new_rev = new DB_PageRevision();
     $new_rev->setSiteId($this->site->getSiteId());
     $new_rev->setPageId($page->getPageId());
     $new_rev->setUserId($this->performer->getUserId());
     $new_rev->setDateLastEdited($now);
     if ($new) {
         $new_rev->setRevisionNumber(0);
     } else {
         $new_rev->setRevisionNumber($cur_rev->getRevisionNumber() + 1);
     }
     $src_changed = false;
     $title_changed = false;
     $parent_changed = false;
     $tags_changed = false;
     // handle source change
     if ($new || $this->source !== null && $page->getSource() != $this->source) {
         $new_src = new DB_PageSource();
         $new_src->setText($this->source);
         $new_src->save();
         $new_rev->setSourceId($new_src->getSourceId());
         $src_changed = true;
     } else {
         $new_rev->setSourceId($cur_rev->getSourceId());
         $new_rev->setSinceFullSource($cur_rev->getSinceFullSource());
         $new_rev->setDiffSource($cur_rev->getDiffSource());
     }
     // handle tags change
     if ($this->tags) {
         $new_tags = $this->tags;
         $cur_tags = $page->getTagsAsArray();
         sort($cur_tags);
         sort($new_tags);
         if ($cur_tags != $new_tags) {
             $tags_changed = true;
             $tags_deleted = array();
             $tags_added = array();
             foreach ($cur_tags as $tag) {
                 if (!in_array($tag, $new_tags)) {
                     $c = new Criteria();
                     $c->add('page_id', $page->getPageId());
                     $c->add('tag', $tag);
                     if ($t = DB_PageTagPeer::instance()->selectOne($c)) {
                         $t->delete();
                         $tags_deleted[] = $tag;
                     }
                 }
             }
             foreach ($new_tags as $tag) {
                 if (!in_array($tag, $cur_tags)) {
                     $t = new DB_PageTag();
                     $t->getPageId($page->getPageId());
                     $t->setSiteId($this->site->getSiteId());
                     $t->setTag($tag);
                     $t->save();
                     $tags_added[] = $tag;
                 }
             }
         }
     }
     // handle metadata: title change
     if ($new || $this->title !== null && $cur_meta->getTitle() != $this->title) {
         $new_meta->setTitle($this->title);
         $page->setTitle($this->title);
         $title_changed = true;
     }
     // handle metadata: parent page change
     if ($this->parent_page) {
         if (!$cur_meta->getParentPageId() || $cur_meta->getParentPageId() != $this->parent_page->getPageId()) {
             $new_meta->setParentPageId($this->parent_page->getPageId());
             $parent_changed = true;
         }
     }
     if ($this->clear_parent_page && $page->getParentPageId()) {
         $new_meta->setParentPageId(null);
         $parent_changed = true;
     }
     $meta_changed = $title_changed || $parent_changed;
     // decide whether to use previous metadata or create a new object
     if ($meta_changed) {
         $new_meta->save();
         $new_rev->setMetadataId($new_meta->getMetadataId());
     } else {
         $new_rev->setMetadataId($cur_meta->getMetadataId());
     }
     // set flag on revision
     if ($new) {
         $new_rev->setFlagNew(true);
     } else {
         if ($src_changed) {
             $new_rev->setFlagText(true);
         }
         if ($title_changed) {
             $new_rev->setFlagTitle(true);
         }
         if ($parent_changed) {
             $new_rev->setFlagMeta(true);
         }
     }
     if ($src_changed || $meta_changed || $tags_changed) {
         $new_rev->save();
         $page->setSourceId($new_rev->getSourceId());
         $page->setDateLastEdited($now);
         $page->setMetadataId($new_rev->getMetadataId());
         $page->setRevisionNumber($new_rev->getRevisionNumber());
         $page->setRevisionId($new_rev->getRevisionId());
         $page->save();
         $db->commit();
         $GLOBALS['site'] = $this->site;
         $outdater = new Outdater();
         if ($src_changed) {
             $outdater->pageEvent("source_changed", $page);
         }
         if ($title_changed) {
             $outdater->pageEvent("title_changed", $page);
         }
         if ($parent_changed) {
             $outdater->pageEvent("parent_changed", $page);
         }
         if ($tags_changed) {
             $outdater->pageEvent("tag_changed", $page);
         }
     } else {
         /* This place is reached when API client tries to set source or
          * title or parent page or tags that are already set (in the DB)
          * to the same value.
          * 
          * Let's suppose doing nothing is the desired behavior in this case
          * 
          * Other possible way to react can be raising an exception.
          * But it should be different from Wikidot_Facade_Exception_WrongArguments
          * because this one implies client error (and client does not need
          * to know the exact database state).
          */
     }
 }
Ejemplo n.º 2
0
 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);
 }