Beispiel #1
0
 /**
  * Simply removes page edit lock from a page.
  */
 public function removePageEditLockEvent($runData)
 {
     $pageId = $runData->getParameterList()->getParameterValue("page_id");
     $c = new Criteria();
     $c->add("page_id", $pageId);
     DB_PageEditLockPeer::instance()->delete($c);
 }
Beispiel #2
0
 public function revertEvent($runData)
 {
     $pl = $runData->getParameterList();
     $pageId = $pl->getParameterValue("pageId");
     $revisionId = $pl->getParameterValue("revisionId");
     $site = $runData->getTemp("site");
     $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");
     }
     // check for permissions again
     $category = $page->getCategory();
     $user = $runData->getUser();
     WDPermissionManager::instance()->hasPagePermission('edit', $user, $category, $page);
     // get the revision
     $toRevision = DB_PageRevisionPeer::instance()->selectByPrimaryKey($revisionId);
     $toMeta = DB_PageMetadataPeer::instance()->selectByPrimaryKey($toRevision->getMetadataId());
     $currentRevision = $page->getCurrentRevision();
     $currentMeta = $currentRevision->getMetadata();
     // compare title and source (ids and contents)
     if ($toMeta->getTitle() === $currentMeta->getTitle() && $toRevision->getSourceId() === $currentRevision->getSourceId()) {
         throw new ProcessException(_("The title and content source of the current revision and the destination revision are identical. No change has been applied."), "no_change");
     }
     // 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("history/RevertPageLockedWin");
         $db->rollback();
         return;
     }
     // success so far...
     $titleChanged = false;
     if ($toMeta->getTitle() !== $currentMeta->getTitle()) {
         // change the title, need to create a new metadata...
         $metadata = clone $currentMeta;
         $metadata->setMetadataId(null);
         $metadata->setNew(true);
         $metadata->setTitle($toMeta->getTitle());
         $metadata->save();
         $titleChanged = true;
     }
     $userId = $runData->getUserId();
     if ($userId == null) {
         $userString = $runData->createIpString();
     }
     if ($toRevision->getSourceId() !== $currentRevision->getSourceId()) {
         $sourceChanged = true;
         $nSource = $toRevision->getSourceText();
         $oSource = $currentRevision->getSourceText();
         if ($nSource === $oSource) {
             $sourceChanged = false;
         }
     }
     if (!$sourceChanged && !$titleChanged) {
         throw new ProcessException(_("The title and content source of the current revision and the destination revision are identical. No change has been applied."), "no_change");
     }
     $revision = clone $currentRevision;
     $revision->setNew(true);
     $revision->setRevisionId(null);
     $revision->resetFlags();
     if ($sourceChanged) {
         $revision->setFlagText(true);
     }
     if ($titleChanged) {
         $revision->setFlagTitle(true);
         $revision->setMetadataId($metadata->getMetadataId());
         $page->setTitle($toMeta->getTitle());
     }
     $revision->setComments(_("Reverted to page revision number") . " " . $toRevision->getRevisionNumber());
     if ($userId) {
         $revision->setUserId($userId);
         $page->setLastEditUserId($userId);
     } else {
         $revision->setUserId(0);
         $page->setLastEditUserId(0);
         $revision->setUserString($userString);
         $page->setLastEditUserString($userString);
     }
     if ($sourceChanged) {
         $fullSource = false;
         // first check if store new source as a diff or as a full-source.
         if (true || $currentRevision->getSinceFullSource() > 9) {
             $fullSource = true;
         } else {
             // also compare size of diff against size of new source.
             // must be less than %50 to qualify
             $differ = new ODiff();
             $diff = $differ->diffString($oSource, $nSource);
             if (strlen($diff) > 0.5 * strlen($nSource)) {
                 $fullSource = true;
             }
         }
         $pageSource = new DB_PageSource();
         if ($fullSource) {
             $pageSource->setText($nSource);
             $revision->setSinceFullSource(0);
             $revision->setDiffSource(false);
         } else {
             $pageSource->setText($diff);
             $revision->setDiffSource(true);
             $revision->setSinceFullSource($currentRevision->getSinceFullSource() + 1);
         }
         $pageSource->save();
         $revision->setSourceId($pageSource->getSourceId());
     } else {
         // copy source id i.e. do nothing
     }
     $revision->setRevisionNumber($revision->getRevisionNumber() + 1);
     $now = new ODate();
     $revision->setDateLastEdited($now);
     $revision->save();
     $page->setRevisionId($revision->getRevisionId());
     $page->setDateLastEdited($now);
     $page->setRevisionNumber($revision->getRevisionNumber());
     $page->save();
     // outdate party!
     $outdater = new Outdater();
     if ($sourceChanged) {
         $outdater->pageEvent("source_changed", $page);
     }
     if ($titleChanged) {
         $outdater->pageEvent("title_changed", $page);
     }
     // index page
     EventLogger::instance()->logSavePage($page);
     $db->commit();
     if (GlobalProperties::$UI_SLEEP) {
         sleep(1);
     }
 }
Beispiel #3
0
 public function build($runData)
 {
     $pl = $runData->getParameterList();
     $site = $runData->getTemp("site");
     $pageId = $pl->getParameterValue("page_id");
     $mode = $pl->getParameterValue("mode");
     $runData->ajaxResponseAdd("mode", $mode);
     $user = $runData->getUser();
     $userId = $runData->getUserId();
     if ($userId == null) {
         $userString = $runData->createIpString();
         $runData->contextAdd("anonymousString", $userString);
     }
     $db = Database::connection();
     $db->begin();
     if ($pageId === null || $pageId === '') {
         // means probably creating a new page
         // no context is needed
         $runData->sessionStart();
         $mode = "page";
         $runData->contextAdd("mode", $mode);
         $runData->contextAdd("newPage", true);
         // first create if a page not already exists!
         $unixName = $pl->getParameterValue("wiki_page");
         $unixName = WDStringUtils::toUnixName($unixName);
         // purify! (for sure)
         if (!$unixName) {
             throw new ProcessException(_("The page can not be found or does not exist."), "no_page");
         }
         $page = DB_PagePeer::instance()->selectByName($site->getSiteId(), $unixName);
         if ($page != null) {
             // page exists!!! error!
             throw new ProcessException(_("The page you want to create already exists. Please refresh the page in your browser to see it."));
             /*	$runData->ajaxResponseAdd("pageExists", true);
             			$runData->ajaxResponseAdd("locked", true); //well, it is somehow locked...
             			$runData->setModuleTemplate("edit/NewPageExistsWinModule");
             			$db->commit();
             			return;	*/
         }
         // extract category name
         if (strpos($unixName, ':') != false) {
             // ok, there is category!
             $exp = explode(':', $unixName);
             $categoryName = $exp[0];
             $suggestedTitle = ucwords(str_replace("-", " ", $exp[1]));
         } else {
             // no category name, "_default" assumed
             $categoryName = "_default";
             $suggestedTitle = ucwords(str_replace("-", " ", $unixName));
         }
         $stitle = $pl->getParameterValue("title");
         if ($stitle) {
             $suggestedTitle = $stitle;
         }
         $category = DB_CategoryPeer::instance()->selectByName($categoryName, $site->getSiteId());
         if ($category == null) {
             // get the default!
             //$category = DB_CategoryPeer::instance()->selectByName('_default', $site->getSiteId());
             $category = $this->createTempCategory($categoryName, $site);
         }
         // now check for permissions!!!
         WDPermissionManager::instance()->hasPagePermission('create', $user, $category);
         $autoincrement = false;
         if (preg_match(';^([a-z0-9]+:)?' . self::$AUTOINCREMENT_PAGE . '$;', $unixName)) {
             $autoincrement = true;
         }
         if (!$autoincrement) {
             $lock = new DB_PageEditLock();
             $lock->setPageUnixName($unixName);
             $lock->setSiteId($site->getSiteId());
             $lock->setUserId($runData->getUserId());
             $lock->setUserString($runData->getSession()->getIpAddress());
             $lock->setDateStarted(new ODate());
             $lock->setDateLastAccessed(new ODate());
             $lock->setMode("page");
             if ($pl->getParameterValue("force_lock") != null) {
                 $lock->deleteConflicts();
             } else {
                 // check for conflicts
                 $conflicts = $lock->getConflicts();
                 if ($conflicts != null) {
                     $runData->ajaxResponseAdd("locked", true);
                     $runData->setModuleTemplate("edit/NewPageLockedWinModule");
                     $runData->contextAdd("locks", $conflicts);
                     return;
                 }
             }
             $secret = md5(time() . rand(1000, 9999));
             $lock->setSecret($secret);
             $lock->setSessionId($runData->getSession()->getSessionId());
             $lock->save();
             $runData->ajaxResponseAdd('lock_id', $lock->getLockId());
             $runData->ajaxResponseAdd('lock_secret', $secret);
         } else {
             $runData->contextAdd('disableLocks', true);
             $runData->ajaxResponseAdd('disableLocks', true);
         }
         $runData->contextAdd("title", $suggestedTitle);
         /* Select available templates, but only if the category does not have a live template. */
         $templatePage = $category->getTemplatePage();
         if ($templatePage && ($form = Wikidot_Form::fromSource($templatePage->getSource()))) {
             $runData->contextAdd("form", new Wikidot_Form_Renderer($form));
         } elseif (!$templatePage || !preg_match(';^={4,}$;sm', $templatePage->getSource())) {
             $templatesCategory = DB_CategoryPeer::instance()->selectByName("template", $site->getSiteId());
             if ($templatesCategory != null) {
                 $c = new Criteria();
                 $c->add("category_id", $templatesCategory->getCategoryId());
                 $c->addOrderAscending("title");
                 $templates = DB_PagePeer::instance()->select($c);
                 $runData->contextAdd("templates", $templates);
             }
             // check if there is a default template...
             if ($category != null) {
                 if ($category->getTemplateId() != null) {
                     $runData->contextAdd("templateId", $category->getTemplateId());
                 }
             }
         } else {
             /* Has default template, try to populate the edit box with initial content. */
             $templateSource = $templatePage->getSource();
             $split = preg_split(';^={4,}$;sm', $templateSource);
             if (count($split) >= 2) {
                 /* Fine, there is some initial content. */
                 $templateSource = trim(preg_replace(";^.*?\n={4,};s", '', $templateSource));
             } else {
                 $templateSource = '';
             }
             $runData->contextAdd('source', $templateSource);
         }
         $db->commit();
         return;
     }
     // now if editing an existing page...
     if (!$pageId || !is_numeric($pageId)) {
         throw new ProcessException(_("The page can not be found or does not exist."), "no_page");
     }
     $page = DB_PagePeer::instance()->selectByPrimaryKey($pageId);
     if (!$page || $page->getSiteId() !== $site->getSiteId()) {
         throw new ProcessException(_("The page can not be found or does not exist."), "no_page");
     }
     $category = $page->getCategory();
     if ($category == null) {
         throw new ProcessException(_("Internal error - page category does not exist!!!"));
     }
     // now check for permissions!
     WDPermissionManager::instance()->hasPagePermission('edit', $user, $category, $page);
     // now check if form is defined
     $templatePage = $category->getTemplatePage();
     if (preg_match('/^[^:]*:[^_]|^[^_:][^:]*$/', $page->getUnixName()) && $templatePage && ($form = Wikidot_Form::fromSource($templatePage->getSource()))) {
         $form->setDataFromYaml($page->getSource());
         $runData->contextAdd("form", new Wikidot_Form_Renderer($form));
         // check if mode is sections if page is editable in this mode
     } elseif ($mode == "section") {
         $compiledContent = $page->getCompiled()->getText();
         $editable = WDEditUtils::sectionsEditable($compiledContent);
         if ($editable == false) {
             throw new ProcessException(_("Sections are not editable due to unclear section structure. This sometimes happen when nested headings are used (inside other page elements) or the page include other pages."), "sections_uneditable");
         }
         // ok, get ranges for edit now.
         $pageSource = $page->getSource();
         $rangeMap = WDEditUtils::sectionMapping($pageSource);
         $sectionId = $pl->getParameterValue("section");
         if (!isset($rangeMap[$sectionId])) {
             throw new ProcessException(_("Sections are not editable due to unclear section structure. This sometimes happen when nested headings are used (inside other page elements) or the page include other pages."), "sections_uneditable");
         }
         $rangeStart = $rangeMap[$sectionId]['start'];
         $rangeEnd = $rangeMap[$sectionId]['end'];
         $runData->ajaxResponseAdd('section', $sectionId);
         $runData->ajaxResponseAdd('rangeStart', $rangeStart);
         $runData->ajaxResponseAdd('rangeEnd', $rangeEnd);
     }
     // if we have not returned yet it means that the lock does not exist or is expired
     // if session is not started - start it!
     $runData->sessionStart();
     // create new page lock
     $lock = new DB_PageEditLock();
     $lock->setPageId($page->getPageId());
     $lock->setPageUnixName($page->getUnixName());
     $lock->setSiteId($site->getSiteId());
     $lock->setUserId($runData->getUserId());
     $lock->setUserString($runData->getSession()->getIpAddress());
     $lock->setDateStarted(new ODate());
     $lock->setDateLastAccessed(new ODate());
     $lock->setMode($mode);
     if ($mode == "section") {
         $lock->setRangeStart($rangeStart);
         $lock->setRangeEnd($rangeEnd);
     }
     // delete outdated...
     DB_PageEditLockPeer::instance()->deleteOutdated($pageId);
     // check for conflicts
     if ($pl->getParameterValue("force_lock") != null) {
         $lock->deleteConflicts();
     } else {
         $blocklocks = $lock->getConflicts();
         if ($blocklocks != null) {
             // conflicting locks exist.
             $runData->setModuleTemplate("edit/LockExistsWinModule");
             $runData->ajaxResponseAdd("locked", true);
             $runData->contextAdd("locks", $blocklocks);
             return;
         }
     }
     $secret = md5(time() . rand(1000, 9999));
     $lock->setSecret($secret);
     $lock->setSessionId($runData->getSession()->getSessionId());
     $lock->save();
     $runData->ajaxResponseAdd('lock_id', $lock->getLockId());
     $runData->ajaxResponseAdd('lock_secret', $secret);
     // also put current page revision in case one wants to regain lock after expired.
     $runData->ajaxResponseAdd('page_revision_id', $page->getRevisionId());
     // keep the session - i.e. put an object into session storage not to delete it!!!
     $runData->sessionAdd("keep", true);
     if ($mode == "page") {
         $pageSource = $page->getSource();
         $runData->contextAdd("source", $pageSource);
     }
     if ($mode == "append") {
         $runData->contextAdd("source", "");
         // source not required...
     }
     if ($mode == "section") {
         // slice the source...
         $sliced = explode("\n", $pageSource);
         $s = array_slice($sliced, $rangeStart, $rangeEnd - $rangeStart + 1);
         $runData->contextAdd("source", trim(implode("\n", $s)));
     }
     $runData->contextAdd("title", $page->getTitleRaw());
     $runData->contextAdd("pageId", $page->getPageId());
     $runData->contextAdd("mode", $mode);
     $runData->ajaxResponseAdd("timeLeft", 15 * 60);
     $db->commit();
 }
Beispiel #4
0
 public function deleteConflicts()
 {
     $c = $this->getConflictsCriteria();
     DB_PageEditLockPeer::instance()->delete($c);
 }