增加PageListPreview的Fallback模式

master
落雨楓 1 year ago
parent 6e162edd86
commit f294122708

@ -1,7 +1,12 @@
<?php <?php
namespace Isekai\Widgets; namespace Isekai\Widgets;
use ApiMain;
use Exception;
use FauxRequest;
use Html; use Html;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\MediaWikiServices;
use Title; use Title;
class PreviewPageListWidget { class PreviewPageListWidget {
@ -36,15 +41,30 @@ class PreviewPageListWidget {
return trim($page); return trim($page);
}, $pageList); }, $pageList);
$pageListHtml = [];
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
foreach ($pageList as $pageTitle) {
$title = Title::newFromText($pageTitle);
$pageListHtml[] = Html::rawElement('li', [], $linkRenderer->makeLink($title));
}
return Html::openElement('div', [ return Html::openElement('div', [
'class' => self::CONTAINER_CLASS_NAME, 'class' => self::CONTAINER_CLASS_NAME,
'data-loader' => $loader, 'data-loader' => $loader,
'data-autofocus' => $autoFocus, 'data-autofocus' => $autoFocus,
'data-lazyload' => $lazyLoad, 'data-lazyload' => $lazyLoad,
]) . Html::element('script', [ ]) .
Html::element('script', [
'type' => 'application/json', 'type' => 'application/json',
'data-pages' => true, 'data-pages' => true,
], json_encode($pageList)) . Html::closeElement('div'); ], json_encode($pageList)) .
Html::openElement('div', [
'class' => 'fallback-page-list'
]) . Html::openElement('ul', []) .
implode("\n", $pageListHtml) .
Html::closeElement('ul') .
Html::closeElement('div') .
Html::closeElement('div');
case 'category': case 'category':
$category = $params['category'] ?? null; $category = $params['category'] ?? null;
if (!$category) { if (!$category) {
@ -53,15 +73,56 @@ class PreviewPageListWidget {
], wfMessage('isekai-preview-page-list-error-invalid-category')->parse()); ], wfMessage('isekai-preview-page-list-error-invalid-category')->parse());
} }
$categoryTitle = new Title($category, NS_CATEGORY); $categoryTitle = Title::newFromText($category, NS_CATEGORY);
$categoryTitleText = $categoryTitle->getPrefixedText(); $categoryTitleText = $categoryTitle->getPrefixedText();
return Html::element('div', [ $pageList = [];
$request = [
'action' => 'query',
'list' => 'categorymembers',
'cmtitle' => $categoryTitleText,
'cmtype' => 'page',
'cmnamespace' => 0,
'cmlimit' => 100,
'cmsort' => 'sortkey',
];
$api = new ApiMain(new FauxRequest($request));
try {
$api->execute();
$categoryMembers = $api->getResult()->getResultData(['query', 'categorymembers']);
foreach ($categoryMembers as $categoryMember) {
if (is_array($categoryMember) && isset($categoryMember['title'])) {
$pageList[] = $categoryMember['title'];
}
}
} catch (Exception $e) {
wfDebugLog('exception', $e->getMessage(), 'all', [
'exception' => $e
]);
}
$pageListHtml = [];
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
foreach ($pageList as $pageTitle) {
$title = Title::newFromText($pageTitle);
$pageListHtml[] = Html::rawElement('li', [], $linkRenderer->makeLink($title));
}
return Html::openElement('div', [
'class' => self::CONTAINER_CLASS_NAME, 'class' => self::CONTAINER_CLASS_NAME,
'data-loader' => $loader,
'data-category' => $categoryTitleText, 'data-category' => $categoryTitleText,
'data-autofocus' => $autoFocus, 'data-autofocus' => $autoFocus,
'data-lazyload' => $lazyLoad, 'data-lazyload' => $lazyLoad,
]); ]) . Html::openElement('div', [
'class' => 'fallback-page-list'
]) . Html::openElement('ul', []) .
implode("\n", $pageListHtml) .
Html::closeElement('ul') .
Html::closeElement('div') .
Html::closeElement('div');
} }
return Html::element('span', [ return Html::element('span', [

@ -116,7 +116,7 @@ class CategoryPageListLoader extends BasePageListLoader {
action: 'query', action: 'query',
list: 'categorymembers', list: 'categorymembers',
cmtitle: this.category, cmtitle: this.category,
cmtype: page, cmtype: 'page',
cmnamespace: 0, cmnamespace: 0,
cmlimit: 200, cmlimit: 200,
cmsort: 'sortkey', cmsort: 'sortkey',
@ -154,7 +154,7 @@ if (previewPageList.length > 0) {
let pagesJsonEl = document.querySelector('script[type="application/json"][data-pages]'); let pagesJsonEl = document.querySelector('script[type="application/json"][data-pages]');
let pageList = []; let pageList = [];
if (pagesJsonEl) { if (pagesJsonEl) {
pageList = JSON.parse(pagesJsonEl.innerHTML); pageList = JSON.parse(pagesJsonEl.innerText);
} }
props.pageListLoader = new PresetPageListLoader(pageList); props.pageListLoader = new PresetPageListLoader(pageList);
break; break;

@ -12,6 +12,12 @@
height: 100%; height: 100%;
margin: 0; margin: 0;
.fallback-page-list {
padding: 12px 20px;
height: @preview-page-list-height;
overflow-y: auto;
}
.loading { .loading {
width: 100%; width: 100%;
height: 99.5%; height: 99.5%;

Loading…
Cancel
Save