public function __construct($repositoryPath) { $pathString = $repositoryPath; if (!StringUtils::startsWith($pathString, '/')) { throw new \Exception('Repository Path must be absolute'); } if (!StringUtils::endsWith($pathString, '/')) { $pathString .= '/'; } $this->repositoryPath = DirectoryPath::parse($pathString); }
/** * @param Request $request * @param FilePath $path * * @return Response * @throws \Exception * @throws GitException * @throws HttpException */ public function editPageAction(Request $request, FilePath $path) { $this->assertRole('ROLE_COMMITTER'); if (!StringUtils::endsWith($path->getName(), '.md')) { throw new HttpException(500, 'Only editing of markdown files is supported'); } $user = $this->getUser(); try { $this->getWikiService()->createLock($user, $path); } catch (PageLockedException $e) { $renderedView = $this->renderView('DdrGitkiBaseBundle:Wiki:page.locked.html.twig', array('path' => $path, 'lockedBy' => $e->getLockedBy())); return new Response($renderedView, 409); } $form = $this->createFormBuilder()->add('content', 'textarea')->add('commitMessage', 'text', array('label' => 'Commit Message', 'required' => true))->add('actions', 'form_actions', array('buttons' => array('save' => array('type' => 'submit', 'options' => array('label' => 'Save')), 'cancel' => array('type' => 'submit', 'options' => array('label' => 'Cancel', 'button_class' => 'default')))))->getForm(); $form->handleRequest($request); /** @var SubmitButton $cancelButton */ $cancelButton = $form->get('actions')->get('cancel'); if ($cancelButton->isClicked()) { $this->getWikiService()->removeLock($user, $path); return $this->redirect($this->generateUrl('ddr_gitki_wiki_file', array('path' => $path))); } if ($form->isValid()) { $content = $form->get('content')->getData(); $commitMessage = $form->get('commitMessage')->getData(); try { $this->getWikiService()->savePage($user, $path, $content, $commitMessage); $this->getWikiService()->removeLock($user, $path); return $this->redirect($this->generateUrl('ddr_gitki_wiki_file', array('path' => $path))); } catch (GitException $e) { throw $e; } } else { $content = null; if ($this->getWikiService()->exists($path)) { $content = $this->getWikiService()->getContent($path); } else { $title = $request->query->get('title'); if (!empty($title)) { $content = $title . "\n"; for ($i = 0; $i < strlen($title); $i++) { $content .= '='; } $content .= "\n\n"; } } if (!$form->isSubmitted()) { $form->setData(array('content' => $content, 'commitMessage' => 'Editing ' . $path->toAbsoluteUrlString())); } } return $this->render('DdrGitkiBaseBundle:Wiki:page.edit.html.twig', array('form' => $form->createView(), 'path' => $path)); }
/** * @param User $user * @param FilePath $relativeOldFilePath * @param FilePath $relativeNewFilePath * @param string $commitMessage * * @throws FileExistsException * @throws \Exception */ public function renameFile(User $user, FilePath $relativeOldFilePath, FilePath $relativeNewFilePath, $commitMessage) { if ($this->gitRepository->exists($relativeNewFilePath)) { throw new FileExistsException('File ' . $relativeNewFilePath->toRelativeFileString() . ' already exists'); } if (empty($commitMessage)) { throw new \Exception('Commit message must not be empty'); } $oldLockPath = $this->getLockPath($relativeOldFilePath); $this->assertHasLock($user, $oldLockPath); $this->createLock($user, $relativeNewFilePath); $this->gitRepository->moveAndCommit($this->getAuthor($user), $commitMessage, $relativeOldFilePath, $relativeNewFilePath); $this->removeLock($user, $relativeOldFilePath); $this->removeLock($user, $relativeNewFilePath); if (StringUtils::endsWith($relativeOldFilePath->getName(), '.md')) { $this->eventDispatcher->dispatch('ddr.gitki.wiki.markdown_document.deleted', new MarkdownDocumentDeletedEvent($relativeOldFilePath, $user->getRealName(), time(), $commitMessage)); } if (StringUtils::endsWith($relativeNewFilePath->getName(), '.md')) { $content = $this->getContent($relativeNewFilePath); $parsedMarkdownDocument = $this->markdownService->parse($relativeNewFilePath, $content); $this->eventDispatcher->dispatch('ddr.gitki.wiki.markdown_document.saved', new MarkdownDocumentSavedEvent($relativeNewFilePath, $user->getEmail(), time(), $parsedMarkdownDocument, $commitMessage)); } }