Skip to content

Commit

Permalink
Merge pull request #40 from prajapati-kaushik/8338-wiki-bundle-git-pu…
Browse files Browse the repository at this point in the history
…sh-pull-integration

git push and pull integration #8338
  • Loading branch information
prajapati-kaushik committed Feb 21, 2024
2 parents 90fea4f + 3dd95c2 commit 8c3278d
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ yarn-error.log
/.php_cs
/.php_cs.cache
###< friendsofphp/php-cs-fixer ###
.php-cs-fixer.cache
43 changes: 41 additions & 2 deletions Controller/WikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use LinkORB\Bundle\WikiBundle\Services\WikiEventService;
use LinkORB\Bundle\WikiBundle\Services\WikiPageService;
use LinkORB\Bundle\WikiBundle\Services\WikiService;
use RuntimeException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -98,7 +97,7 @@ public function serchAction(Request $request): Response

if (!empty($formData['wikiName'])) {
if (!$wiki = $this->wikiService->getWikiByName($formData['wikiName'])) {
throw new RuntimeException('Wiki '.$formData['wikiName'].'not found', Response::HTTP_NOT_FOUND);
throw new \RuntimeException('Wiki '.$formData['wikiName'].'not found', Response::HTTP_NOT_FOUND);
}
if (!$wikiRoles = $this->wikiService->getWikiPermission($wiki)) {
throw new AccessDeniedException('Access denied!');
Expand All @@ -124,8 +123,28 @@ public function serchAction(Request $request): Response
]);
}

/**
* @Route("/{wikiName}/publish", name="wiki_publish", methods="GET")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function publishAction(Request $request, Wiki $wiki): Response
{
if (!$wikiRoles = $this->wikiService->getWikiPermission($wiki)) {
throw new AccessDeniedException('Access denied!');
}
if (!$wikiRoles['writeRole']) {
throw new AccessDeniedException('Access denied!');
}

$this->wikiService->publishWiki($wiki, $this->getUser()->getUsername(), $this->getUser()->getEmail());

return $this->redirectToRoute('wiki_view', ['wikiName' => $wiki->getName()]);
}

/**
* @Route("/{wikiName}/edit", name="wiki_edit", methods="GET|POST")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function editAction(Request $request, Wiki $wiki, WikiEventService $wikiEventService): Response
Expand All @@ -142,6 +161,7 @@ public function editAction(Request $request, Wiki $wiki, WikiEventService $wikiE

/**
* @Route("/{wikiName}/delete", name="wiki_delete", methods="GET")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function deleteAction(Request $request, Wiki $wiki, WikiEventService $wikiEventService): Response
Expand Down Expand Up @@ -230,9 +250,11 @@ public function viewAction(WikiPageService $wikiPageService, $wikiName): Respons
['wikiName' => $wikiName]
);
}

if (!$wikiRoles = $this->wikiService->getWikiPermission($wiki)) {
throw new AccessDeniedException('Access denied!');
}

$data = $wikiRoles;
// $wikiPageRepository = $this->get('LinkORB\Bundle\WikiBundle\Repository\WikiPageRepository');

Expand All @@ -250,11 +272,14 @@ public function viewAction(WikiPageService $wikiPageService, $wikiName): Respons
$data['wikiPages'] = $wikiPages;
$data['wiki'] = $wiki;

$this->wikiService->autoPull($wiki);

return $this->render('@Wiki/wiki_page/index.html.twig', $data);
}

/**
* @Route("/{wikiName}/export", name="wiki_export", methods="GET")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function exportAction(Wiki $wiki, WikiService $wikiService): Response
Expand All @@ -279,6 +304,7 @@ public function exportAction(Wiki $wiki, WikiService $wikiService): Response

/**
* @Route("/{wikiName}/export-single-markdown", name="wiki_export_single_markdown", methods="GET")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function exportSingleMarkdownAction(Wiki $wiki, WikiService $wikiService): Response
Expand All @@ -305,6 +331,7 @@ public function exportSingleMarkdownAction(Wiki $wiki, WikiService $wikiService)

/**
* @Route("/{wikiName}/export-single-html", name="wiki_export_single_html", methods="GET")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function exportSingleHtmlAction(Wiki $wiki, WikiService $wikiService): Response
Expand Down Expand Up @@ -335,4 +362,16 @@ public function exportSingleHtmlAction(Wiki $wiki, WikiService $wikiService): Re

return $response;
}

/**
* @Route("/{wikiName}/pull", name="wiki_pull", methods="GET")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
public function gitPullAction(Wiki $wiki): Response
{
$this->wikiService->pull($wiki);

return $this->redirectToRoute('wiki_view', ['wikiName' => $wiki->getName()]);
}
}
47 changes: 47 additions & 0 deletions Controller/WikiPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

/**
* @Route("/wiki/{wikiName}")
*
* @ParamConverter("wiki", options={"mapping"={"wikiName"="name"}})
*/
class WikiPageController extends AbstractController
Expand Down Expand Up @@ -52,6 +53,16 @@ public function indexAction(Wiki $wiki): Response
return $this->render('@Wiki/wiki_page/index.html.twig', $data);
}

/**
* @Route("/pages/read-only", name="wiki_page_read_only", methods="GET")
*/
public function readOnlyAction(Wiki $wiki): Response
{
return $this->render('@Wiki/wiki_page/read_only.html.twig', [
'wiki' => $wiki,
]);
}

/**
* @Route("/pages/add", name="wiki_page_add", methods="GET|POST")
*/
Expand All @@ -60,6 +71,12 @@ public function addAction(Request $request, Wiki $wiki, WikiEventService $wikiEv
$wikiPage = new WikiPage();
$wikiPage->setWiki($wiki);

if ($wiki->isReadOnly()) {
return $this->redirectToRoute('wiki_page_read_only', [
'wikiName' => $wiki->getName(),
]);
}

if ($pageName = $request->query->get('pageName')) {
$wikiPage->setName($pageName);
}
Expand Down Expand Up @@ -106,6 +123,8 @@ public function viewAction(Request $request, Wiki $wiki, string $pageName): Resp
$data['wikiPage'] = $wikiPage;
$data['wiki'] = $wiki;

$this->wikiService->autoPull($wiki);

return $this->render('@Wiki/wiki_page/view.html.twig', $data);
}

Expand All @@ -114,6 +133,12 @@ public function viewAction(Request $request, Wiki $wiki, string $pageName): Resp
*/
public function editAdvanceAction(Request $request, Wiki $wiki, WikiEventService $wikiEventService, $pageName): Response
{
if ($wiki->isReadOnly()) {
return $this->redirectToRoute('wiki_page_read_only', [
'wikiName' => $wiki->getName(),
]);
}

$wikiPage = $this->wikiPageRepository->findOneByWikiIdAndName($wiki->getId(), $pageName);

return $this->getEditForm($request, $wikiPage, $wikiEventService);
Expand All @@ -124,6 +149,12 @@ public function editAdvanceAction(Request $request, Wiki $wiki, WikiEventService
*/
public function editAction(Request $request, Wiki $wiki, WikiEventService $wikiEventService, $pageName): Response
{
if ($wiki->isReadOnly()) {
return $this->redirectToRoute('wiki_page_read_only', [
'wikiName' => $wiki->getName(),
]);
}

$wikiPage = $this->wikiPageRepository->findOneByWikiIdAndName($wiki->getId(), $pageName);

if (!$wikiRoles = $this->wikiService->getWikiPermission($wikiPage->getWiki())) {
Expand All @@ -143,6 +174,8 @@ public function editAction(Request $request, Wiki $wiki, WikiEventService $wikiE
$this->em->persist($wikiPage);
$this->em->flush();

$this->publishPage($wikiPage);

return new JsonResponse(['status' => 'success']);
}

Expand All @@ -160,6 +193,8 @@ public function editAction(Request $request, Wiki $wiki, WikiEventService $wikiE
$wikiPage->getId()
);

$this->publishPage($wikiPage);

return $this->redirectToRoute('wiki_page_view', [
'wikiName' => $wikiPage->getWiki()->getName(),
'pageName' => $wikiPage->getName(),
Expand Down Expand Up @@ -261,6 +296,8 @@ protected function getEditForm($request, $wikiPage, WikiEventService $wikiEventS
);
}

$this->publishPage($wikiPage);

if ($add) {
return $this->redirectToRoute('wiki_page_edit', [
'wikiName' => $wikiPage->getWiki()->getName(),
Expand Down Expand Up @@ -289,4 +326,14 @@ protected function getEditForm($request, $wikiPage, WikiEventService $wikiEventS

return $this->render('@Wiki/wiki_page/advanced.html.twig', $data);
}

private function publishPage(WikiPage $wikiPage)
{
return $this->wikiService->publishWikiPage(
$wikiPage->getWiki(),
$wikiPage,
$this->getUser()->getUsername(),
$this->getUser()->getEmail()
);
}
}
31 changes: 31 additions & 0 deletions Entity/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Yaml\Yaml;

/**
* @ORM\Entity(repositoryClass="LinkORB\Bundle\WikiBundle\Repository\WikiRepository")
Expand All @@ -13,7 +14,9 @@ class Wiki
{
/**
* @ORM\Id()
*
* @ORM\GeneratedValue()
*
* @ORM\Column(type="integer")
*/
private $id;
Expand All @@ -30,6 +33,7 @@ class Wiki

/**
* @ORM\OneToMany(targetEntity="LinkORB\Bundle\WikiBundle\Entity\WikiPage", mappedBy="wiki")
*
* @ORM\OrderBy({"name" = "ASC"})
*/
private $wikiPages;
Expand All @@ -49,6 +53,11 @@ class Wiki
*/
private $config;

/**
* @ORM\Column(type="integer", nullable=true)
*/
private $lastPullAt;

public function __construct()
{
$this->wikiPages = new ArrayCollection();
Expand Down Expand Up @@ -149,4 +158,26 @@ public function setConfig(?string $config): self

return $this;
}

public function getConfigArray()
{
return Yaml::parse($this->config ?? '') ?? [];
}

public function getLastPullAt(): ?int
{
return $this->lastPullAt;
}

public function setLastPullAt(?int $lastPullAt): self
{
$this->lastPullAt = $lastPullAt;

return $this;
}

public function isReadOnly(): bool
{
return (bool) !empty($this->getConfigArray()['read-only']) ? $this->getConfigArray()['read-only'] : false;
}
}
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,43 @@ wiki_bundle:
```

### Step 4: Enjoy :)

---
### Store/sync wiki's page content on git/GitHub.

The repository this feature supports storing and retrieving wiki page content on git/Github. For that, configure git/Github details into the wiki config field. Currently, support git/GitHub web URL(HTTPS) for pull and push.


Example config in the wiki.config

```
push:
- type: git
url: https://github.com/gitHub-username/wiki-git.git
secret: `ENV:WIKI_GIT_TOKEN` # defines which env to use as a secret
pull:
- type: git
url: https://github.com/gitHub-username/wiki-git.git
secret: `ENV:WIKI_GIT_TOKEN` # defines which env to use as a secret
```

- `type`: push target type.
- `url`: GitHub Clone URL(HTTPS) where pull/push content.
- `secret:` Personal access tokens for authentication.


Git publish and pull links are in the wiki page admin dropdown.

`Note:` If the GitHub repository is not empty, Pull first to sync the repository.

---

### Read-only wikis

This feature supports preventing users from editing wiki page content that is being managed in git/Github.
For that, set config option into wiki config field.
```
read-only: true
```
`read-only`: boolen true/false value. If this is set to true, the edit and add features are prohibited for the user.
6 changes: 6 additions & 0 deletions Resources/views/base.wiki.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
<a href="{{ path('wiki_edit', {'wikiName': wiki.name}) }}" class="dropdown-item">
<i class="fa fa-edit"></i> Wiki settings
</a>
<a href="{{ path('wiki_publish', {'wikiName': wiki.name}) }}" class="dropdown-item">
<i class="fa fa-github"></i> Publish
</a>
<a href="{{ path('wiki_pull', {'wikiName': wiki.name}) }}" class="dropdown-item">
<i class="fa fa-github"></i> pull
</a>
<a href="{{ path('wiki_event_index', {'wikiName': wiki.name}) }}" class="dropdown-item">
<i class="fa fa-calendar" aria-hidden="true"></i> Events
</a>
Expand Down
17 changes: 17 additions & 0 deletions Resources/views/wiki_page/read_only.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends '@Wiki/base.wiki.html.twig' %}

{% block body %}
<fieldset class="wiki-page-content">
<legend><i class="fa fa-lock"></i> Read Only</legend>
<div class="mt-2 wiki-content">
{{ "
## Wiki is Read-only
The content of this wiki page is set to read-only, and it is probably managed through an external CMS.
You can't `add/edit` page(s) here.
The configuration for the CMS can be found in the Wiki config input field.
The system currently supports git/GitHub as an external CMS.
"|markdown_to_html }}
</div>
</fieldset>
{% endblock %}
Loading

0 comments on commit 8c3278d

Please sign in to comment.