增加PageListPreview的Fallback模式

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

@ -1,7 +1,12 @@
<?php
namespace Isekai\Widgets;
use ApiMain;
use Exception;
use FauxRequest;
use Html;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\MediaWikiServices;
use Title;
class PreviewPageListWidget {
@ -36,15 +41,30 @@ class PreviewPageListWidget {
return trim($page);
}, $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', [
'class' => self::CONTAINER_CLASS_NAME,
'data-loader' => $loader,
'data-autofocus' => $autoFocus,
'data-lazyload' => $lazyLoad,
]) . Html::element('script', [
]) .
Html::element('script', [
'type' => 'application/json',
'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':
$category = $params['category'] ?? null;
if (!$category) {
@ -53,15 +73,56 @@ class PreviewPageListWidget {
], wfMessage('isekai-preview-page-list-error-invalid-category')->parse());
}
$categoryTitle = new Title($category, NS_CATEGORY);
$categoryTitle = Title::newFromText($category, NS_CATEGORY);
$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,
'data-loader' => $loader,
'data-category' => $categoryTitleText,
'data-autofocus' => $autoFocus,
'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', [

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

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

Loading…
Cancel
Save