Skip to content

Commit

Permalink
replace old action with rpc api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanashi committed Aug 16, 2024
1 parent b0e174b commit c28e3a2
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 109 deletions.
6 changes: 6 additions & 0 deletions fileDelete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="http://www.woltlab.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.woltlab.com http://www.woltlab.com/XSD/6.0/fileDelete.xsd">
<delete>
<file>lib/action/FaqSearchAction</file>
</delete>
</data>
29 changes: 29 additions & 0 deletions files/js/Hanashi/Faq/Api/Questions/Search.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 8 additions & 16 deletions files/js/Hanashi/Faq/BBCode.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions files/js/Hanashi/Faq/Question.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 0 additions & 68 deletions files/lib/action/FaqSearchAction.class.php

This file was deleted.

11 changes: 11 additions & 0 deletions files/lib/bootstrap/dev.tkirch.wsc.faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use wcf\acp\page\FaqCategoryListPage;
use wcf\acp\page\FaqQuestionListPage;
use wcf\event\acp\menu\item\ItemCollecting;
use wcf\event\endpoint\ControllerCollecting;
use wcf\event\worker\RebuildWorkerCollecting;
use wcf\system\endpoint\controller\faq\questions\search\GetSearch;
use wcf\system\endpoint\controller\faq\questions\search\RenderSearch;
use wcf\system\event\EventHandler;
use wcf\system\menu\acp\AcpMenuItem;
use wcf\system\request\LinkHandler;
Expand Down Expand Up @@ -76,4 +79,12 @@ static function (RebuildWorkerCollecting $event) {
$event->register(FaqQuestionSearchIndexRebuildDataWorker::class, 200);
}
);

EventHandler::getInstance()->register(
ControllerCollecting::class,
static function (ControllerCollecting $event) {
$event->register(new RenderSearch());
$event->register(new GetSearch());
}
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace wcf\system\endpoint\controller\faq\questions\search;

use Laminas\Diactoros\Response\JsonResponse;
use Override;
use PDO;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use wcf\data\faq\Question;
use wcf\data\faq\QuestionList;
use wcf\http\Helper;
use wcf\system\endpoint\GetRequest;
use wcf\system\endpoint\IController;
use wcf\system\exception\UserInputException;
use wcf\system\WCF;

#[GetRequest('/faq/questions/search')]
final class GetSearch implements IController
{
#[Override]
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
{
$parameters = Helper::mapApiParameters($request, GetSearchParameters::class);
if (\mb_strlen($parameters->query) < 3) {
throw new UserInputException('query', 'tooShort');
}

$questionIDs = $this->getQuestionsIDs($parameters->query);

return new JsonResponse([
'template' => WCF::getTPL()->fetch('faqQuestionSearchResult', 'wcf', [
'questions' => $this->getQuestions($questionIDs),
]),
]);
}

/**
* @return list<int>
*/
private function getQuestionsIDs(string $query): array
{
$sql = "
SELECT faq_questions.questionID
FROM wcf1_faq_questions faq_questions
LEFT JOIN wcf1_language_item language_item
ON language_item.languageItem = faq_questions.question
WHERE faq_questions.question LIKE ?
OR (
language_item.languageItemValue LIKE ?
AND language_item.languageID = ?
)
ORDER BY faq_questions.question
";
$statement = WCF::getDB()->prepare($sql, 5);
$statement->execute([
'%' . WCF::getDB()->escapeLikeValue($query) . '%',
'%' . WCF::getDB()->escapeLikeValue($query) . '%',
WCF::getLanguage()->languageID,
]);

return $statement->fetchAll(PDO::FETCH_COLUMN, 0);
}

/**
* @return list<Question>
*/
private function getQuestions(array $questionIDs): array
{
if ($questionIDs === []) {
return [];
}

$questionList = new QuestionList();
$questionList->setObjectIDs($questionIDs);
$questionList->readObjects();

return $questionList->getObjects();
}
}

/** @internal */
final class GetSearchParameters // phpcs:ignore
{
public function __construct(
/** @var non-empty-string */
public readonly string $query,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace wcf\system\endpoint\controller\faq\questions\search;

use Laminas\Diactoros\Response\JsonResponse;
use Override;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use wcf\system\endpoint\GetRequest;
use wcf\system\endpoint\IController;
use wcf\system\WCF;

#[GetRequest('/faq/questions/search/render')]
final class RenderSearch implements IController
{
#[Override]
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
{
return new JsonResponse([
'template' => WCF::getTPL()->fetch('faqQuestionSearchDialog', 'wcf'),
]);
}
}
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
<instruction type="acpMenu"/>
<instruction type="option"/>
<instruction type="box"/>
<instruction type="fileDelete"/>
</instructions>
</package>
3 changes: 1 addition & 2 deletions templates/__faqWysiwygJavaScript.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{jsphrase name='wcf.faq.question.search.error.tooShort'}
{jsphrase name='wcf.faq.bbcode.faqEntry'}
{capture assign='faqEndpoints'}{link controller='FaqSearch' application='wcf'}{/link}{/capture}
new FaqBBCode('{$wysiwygSelector|encodeJS}', '{$faqEndpoints|encodeJS}');
new FaqBBCode('{unsafe:$wysiwygSelector|encodeJS}');
})
</script>
33 changes: 33 additions & 0 deletions ts/Hanashi/Faq/Api/Questions/Search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend";
import { ApiResult, apiResultFromError, apiResultFromValue } from "WoltLabSuite/Core/Api/Result";

type Response = {
template: string;
};

export async function renderSearch(): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/faq/questions/search/render`);

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}

export async function searchQuestions(query: string): Promise<ApiResult<Response>> {
const url = new URL(`${window.WSC_API_URL}index.php?api/rpc/faq/questions/search`);
url.searchParams.set("query", query);

let response: Response;
try {
response = (await prepareRequest(url).get().fetchAsJson()) as Response;
} catch (e) {
return apiResultFromError(e);
}

return apiResultFromValue(response);
}
Loading

0 comments on commit c28e3a2

Please sign in to comment.