Skip to content

Commit

Permalink
refactor: migrated sitemap page to Twig (#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jun 28, 2024
1 parent f2a431c commit 2af247a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
20 changes: 20 additions & 0 deletions phpmyfaq/assets/templates/sitemap.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<section class="col-12">
<h1>{{ pageHeader }}</h1>

<div class="navbar navbar-expand-lg">
<ul class="nav">
{% for letter in letters %}
<a class="nav-link" href="{{ letter.url }}">{{ letter.letter }}</a>
{% endfor %}
</ul>
</div>

<ul>
{% for faq in faqs %}
<li>
<a title="{{ faq.question }}" href="{{ faq.url }}">{{ faq.question }}</a>
<br>{{ faq.answer }}
</li>
{% endfor %}
</ul>
</section>
6 changes: 0 additions & 6 deletions phpmyfaq/assets/themes/default/templates/sitemap.html

This file was deleted.

30 changes: 21 additions & 9 deletions phpmyfaq/sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,49 @@
* @since 2005-08-21
*/

use phpMyFAQ\Configuration;
use phpMyFAQ\Filter;
use phpMyFAQ\Sitemap;
use phpMyFAQ\Strings;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\Request;
use Twig\Extension\DebugExtension;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
exit();
}

$faqConfig = Configuration::getConfigurationInstance();

$faqSession->userTracking('sitemap', 0);

$request = Request::createFromGlobals();
$letter = Filter::filterVar($request->query->get('letter'), FILTER_SANITIZE_SPECIAL_CHARS);
if (!is_null($letter) && (1 == Strings::strlen($letter))) {
$currentLetter = strtoupper(Strings::substr($letter, 0, 1));
$currLetter = strtoupper(Strings::substr($letter, 0, 1));
} else {
$currentLetter = '';
$currLetter = '';
}

$siteMap = new Sitemap($faqConfig);
$siteMap->setUser($currentUser);
$siteMap->setGroups($currentGroups);

$template->parse(
$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twig->addExtension(new DebugExtension());
$twigTemplate = $twig->loadTemplate('./sitemap.twig');

// Twig template variables
$templateVars = [
'pageHeader' => $currLetter === '' || $currLetter === '0' ? Translation::get('msgSitemap') : $currLetter,
'letters' => $siteMap->getAllFirstLetters(),
'faqs' => $siteMap->getFaqsFromLetter($currLetter),
'writeCurrentLetter' => $currLetter === '' || $currLetter === '0' ? Translation::get('msgSitemap') : $currLetter,
];

$template->addRenderedTwigOutput(
'mainPageContent',
[
'pageHeader' => $currentLetter === '' || $currentLetter === '0' ? Translation::get('msgSitemap') : $currentLetter,
'renderLetters' => $siteMap->getAllFirstLetters(),
'renderSiteMap' => $siteMap->getRecordsFromLetter($currentLetter),
'writeCurrentLetter' => $currentLetter === '' || $currentLetter === '0' ? Translation::get('msgSitemap') : $currentLetter,
]
$twigTemplate->render($templateVars)
);
62 changes: 29 additions & 33 deletions phpmyfaq/src/phpMyFAQ/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

namespace phpMyFAQ;

use Exception;
use ParsedownExtra;
use phpMyFAQ\Database\Sqlite3;
use stdClass;

/**
* Class Sitemap
Expand Down Expand Up @@ -69,11 +71,10 @@ public function setGroups(array $groups): void

/**
* Returns all available first letters.
* @return stdClass[]
*/
public function getAllFirstLetters(): string
public function getAllFirstLetters(): array
{
global $sids;

if ($this->groupSupport) {
$permPart = sprintf(
'( fdg.group_id IN (%s)
Expand All @@ -90,7 +91,7 @@ public function getAllFirstLetters(): string
);
}

$renderLetters = '<ul class="nav">';
$letters = [];

$query = sprintf(
"
Expand Down Expand Up @@ -118,42 +119,41 @@ public function getAllFirstLetters(): string
Database::getTablePrefix(),
Database::getTablePrefix(),
Database::getTablePrefix(),
$this->configuration->getLanguage()->getLanguage(),
$this->configuration->getDb()->escape($this->configuration->getLanguage()->getLanguage()),
$permPart
);

$result = $this->configuration->getDb()->query($query);
while ($row = $this->configuration->getDb()->fetchObject($result)) {
$letters = Strings::strtoupper($row->letters);
$letter = new stdClass();
$letter->letter = Strings::strtoupper($row->letters);

if (Strings::preg_match("/^\w+/iu", $letters) !== 0) {
if (Strings::preg_match("/^\w+/iu", $letter->letter) !== 0) {
$url = sprintf(
'%sindex.php?%saction=sitemap&amp;letter=%s&amp;lang=%s',
'%sindex.php?action=sitemap&letter=%s&lang=%s',
$this->configuration->getDefaultUrl(),
$sids,
$letters,
$letter->letter,
$this->configuration->getLanguage()->getLanguage()
);
$oLink = new Link($url, $this->configuration);
$oLink->text = $letters;
$oLink->class = 'nav-link';
$renderLetters .= '<li class="nav-item">' . $oLink->toHtmlAnchor() . '</li>';
$link = new Link($url, $this->configuration);
$letter->url = $link->toString();
}

$letters[] = $letter;
}

return $renderLetters . '</ul>';
return $letters;
}

/**
* Returns all records from the current first letter.
*
* @param string $letter Letter
* @throws \Exception
* @return stdClass[]
* @throws Exception
*/
public function getRecordsFromLetter(string $letter = 'A'): string
public function getFaqsFromLetter(string $letter = 'A'): array
{
global $sids;

if ($this->groupSupport) {
$permPart = sprintf(
'( fdg.group_id IN (%s)
Expand All @@ -172,7 +172,7 @@ public function getRecordsFromLetter(string $letter = 'A'): string

$letter = Strings::strtoupper($this->configuration->getDb()->escape(Strings::substr($letter, 0, 1)));

$renderSiteMap = '';
$faqs = [];

$query = sprintf(
"
Expand Down Expand Up @@ -218,36 +218,32 @@ public function getRecordsFromLetter(string $letter = 'A'): string
$parseDownExtra = new ParsedownExtra();

while ($row = $this->configuration->getDb()->fetchObject($result)) {
if ($oldId != $row->id) {
$title = Strings::htmlspecialchars($row->thema, ENT_QUOTES);
if ($oldId !== $row->id) {
$faq = new stdClass();
$faq->question = $row->thema;
$url = sprintf(
'%sindex.php?%saction=faq&amp;cat=%d&amp;id=%d&amp;artlang=%s',
'%sindex.php?action=faq&cat=%d&id=%d&artlang=%s',
$this->configuration->getDefaultUrl(),
$sids,
$row->category_id,
$row->id,
$row->lang
);

$link = new Link($url, $this->configuration);
$link->itemTitle = $row->thema;
$link->text = $title;
$link->tooltip = $title;

$renderSiteMap .= sprintf('<li>%s<br>', $link->toHtmlAnchor());
$faq->url = $link->toString();

if ($this->configuration->get('main.enableMarkdownEditor')) {
$renderSiteMap .= Utils::chopString(strip_tags((string) $parseDownExtra->text($row->snap)), 25) .
" ...</li>\n";
$faq->answer = Utils::chopString(strip_tags((string) $parseDownExtra->text($row->snap)), 25);
} else {
$renderSiteMap .= Utils::chopString(strip_tags((string) $row->snap), 25) .
" ...</li>\n";
$faq->answer = Utils::chopString(strip_tags((string) $row->snap), 25);
}
$faqs[] = $faq;
}

$oldId = $row->id;
}

return $renderSiteMap === '' || $renderSiteMap === '0' ? '' : '<ul>' . $renderSiteMap . '</ul>';
return $faqs;
}
}
11 changes: 6 additions & 5 deletions tests/phpMyFAQ/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use phpMyFAQ\Configuration\DatabaseConfiguration;
use phpMyFAQ\Database\Sqlite3;
use PHPUnit\Framework\TestCase;
use stdClass;

class SitemapTest extends TestCase
{
Expand Down Expand Up @@ -56,10 +57,10 @@ protected function tearDown(): void
public function testGetAllFirstLetters(): void
{
$letters = $this->sitemap->getAllFirstLetters();
$this->assertIsString($letters);
$this->assertEquals(
'<ul class="nav"><li class="nav-item"><a class="nav-link" href="https://faq.example.org/sitemap/S/en.html">S</a></li></ul>',
$letters
);
$expected = new stdClass();
$expected->letter = 'S';
$expected->url = 'https://faq.example.org/sitemap/S/en.html';
$this->assertIsArray($letters);
$this->assertEquals([$expected], $letters);
}
}

0 comments on commit 2af247a

Please sign in to comment.