2015-05-20 17:35:20 +00:00
|
|
|
<?php
|
2019-05-16 19:46:57 +00:00
|
|
|
|
2021-04-07 22:35:52 +00:00
|
|
|
namespace MediaWiki\Extension\CategoryTree;
|
|
|
|
|
|
|
|
use ApiBase;
|
2021-11-02 06:03:15 +00:00
|
|
|
use ApiMain;
|
2021-04-07 22:35:52 +00:00
|
|
|
use FormatJson;
|
2023-10-03 09:30:38 +00:00
|
|
|
use MediaWiki\Config\Config;
|
|
|
|
use MediaWiki\Config\ConfigFactory;
|
2021-11-02 06:03:15 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2023-10-26 20:22:10 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2023-08-16 01:54:29 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-11-02 06:03:15 +00:00
|
|
|
use WANObjectCache;
|
2022-04-03 23:24:23 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2023-09-20 20:36:37 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2019-05-16 19:46:57 +00:00
|
|
|
|
2018-04-16 06:50:28 +00:00
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
2015-05-20 17:35:20 +00:00
|
|
|
|
|
|
|
class ApiCategoryTree extends ApiBase {
|
2021-11-02 06:03:15 +00:00
|
|
|
/** @var ConfigFactory */
|
|
|
|
private $configFactory;
|
|
|
|
|
|
|
|
/** @var LanguageConverterFactory */
|
|
|
|
private $languageConverterFactory;
|
|
|
|
|
2023-10-26 20:22:10 +00:00
|
|
|
/** @var LinkRenderer */
|
|
|
|
private $linkRenderer;
|
|
|
|
|
2023-09-20 20:36:37 +00:00
|
|
|
/** @var IConnectionProvider */
|
|
|
|
private $dbProvider;
|
|
|
|
|
2021-11-02 06:03:15 +00:00
|
|
|
/** @var WANObjectCache */
|
|
|
|
private $wanCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ApiMain $main
|
|
|
|
* @param string $action
|
|
|
|
* @param ConfigFactory $configFactory
|
2023-09-20 20:36:37 +00:00
|
|
|
* @param IConnectionProvider $dbProvider
|
2021-11-02 06:03:15 +00:00
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
2023-10-26 20:22:10 +00:00
|
|
|
* @param LinkRenderer $linkRenderer
|
2021-11-02 06:03:15 +00:00
|
|
|
* @param WANObjectCache $wanCache
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
$action,
|
|
|
|
ConfigFactory $configFactory,
|
2023-09-20 20:36:37 +00:00
|
|
|
IConnectionProvider $dbProvider,
|
2021-11-02 06:03:15 +00:00
|
|
|
LanguageConverterFactory $languageConverterFactory,
|
2023-10-26 20:22:10 +00:00
|
|
|
LinkRenderer $linkRenderer,
|
2021-11-02 06:03:15 +00:00
|
|
|
WANObjectCache $wanCache
|
|
|
|
) {
|
|
|
|
parent::__construct( $main, $action );
|
|
|
|
$this->configFactory = $configFactory;
|
|
|
|
$this->languageConverterFactory = $languageConverterFactory;
|
2023-10-26 20:22:10 +00:00
|
|
|
$this->linkRenderer = $linkRenderer;
|
2023-09-20 20:36:37 +00:00
|
|
|
$this->dbProvider = $dbProvider;
|
2021-11-02 06:03:15 +00:00
|
|
|
$this->wanCache = $wanCache;
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:57:56 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2015-05-20 17:35:20 +00:00
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
2023-04-20 21:14:41 +00:00
|
|
|
|
|
|
|
$options = $this->extractOptions( $params );
|
2018-08-17 16:57:49 +00:00
|
|
|
|
|
|
|
$title = CategoryTree::makeTitle( $params['category'] );
|
|
|
|
if ( !$title || $title->isExternal() ) {
|
2018-10-15 21:34:16 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['category'] ) ] );
|
2018-08-17 16:57:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 17:35:20 +00:00
|
|
|
$depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1;
|
|
|
|
|
2023-10-28 19:40:51 +00:00
|
|
|
$ct = new CategoryTree( $options, $this->getConfig(), $this->linkRenderer );
|
|
|
|
$depth = $ct->optionManager->capDepth( $depth );
|
2021-11-02 06:03:15 +00:00
|
|
|
$ctConfig = $this->configFactory->makeConfig( 'categorytree' );
|
2015-05-20 17:35:20 +00:00
|
|
|
$html = $this->getHTML( $ct, $title, $depth, $ctConfig );
|
|
|
|
|
2019-02-27 05:40:33 +00:00
|
|
|
$this->getMain()->setCacheMode( 'public' );
|
2015-05-20 17:35:20 +00:00
|
|
|
|
|
|
|
$this->getResult()->addContentValue( $this->getModuleName(), 'html', $html );
|
|
|
|
}
|
|
|
|
|
2023-04-20 21:14:41 +00:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function extractOptions( $params ): array {
|
|
|
|
if ( !isset( $params['options'] ) ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = FormatJson::decode( $params['options'] );
|
|
|
|
if ( !is_object( $options ) ) {
|
|
|
|
$this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $options as $option => $value ) {
|
2023-04-25 18:06:08 +00:00
|
|
|
if ( is_scalar( $value ) || $value === null ) {
|
|
|
|
continue;
|
2023-04-20 21:14:41 +00:00
|
|
|
}
|
2023-04-25 18:06:08 +00:00
|
|
|
if ( $option === 'namespaces' && is_array( $value ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-categorytree-invalidjson-option', $option ], 'invalidjson-option'
|
|
|
|
);
|
2023-04-20 21:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return get_object_vars( $options );
|
|
|
|
}
|
|
|
|
|
2018-04-09 01:50:31 +00:00
|
|
|
/**
|
|
|
|
* @param string $condition
|
|
|
|
*
|
|
|
|
* @return bool|null|string
|
|
|
|
*/
|
2015-05-20 17:35:20 +00:00
|
|
|
public function getConditionalRequestData( $condition ) {
|
|
|
|
if ( $condition === 'last-modified' ) {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$title = CategoryTree::makeTitle( $params['category'] );
|
2023-09-20 20:36:37 +00:00
|
|
|
return $this->dbProvider->getReplicaDatabase()->selectField( 'page', 'page_touched',
|
2016-10-14 16:11:52 +00:00
|
|
|
[
|
2015-05-20 17:35:20 +00:00
|
|
|
'page_namespace' => NS_CATEGORY,
|
|
|
|
'page_title' => $title->getDBkey(),
|
2016-10-14 16:11:52 +00:00
|
|
|
],
|
2015-05-20 17:35:20 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get category tree HTML for the given tree, title, depth and config
|
|
|
|
*
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param CategoryTree $ct
|
|
|
|
* @param Title $title
|
|
|
|
* @param int $depth
|
|
|
|
* @param Config $ctConfig Config for CategoryTree
|
2015-05-20 17:35:20 +00:00
|
|
|
* @return string HTML
|
|
|
|
*/
|
2018-12-16 09:47:07 +00:00
|
|
|
private function getHTML( CategoryTree $ct, Title $title, $depth, Config $ctConfig ) {
|
2021-11-02 06:03:15 +00:00
|
|
|
$langConv = $this->languageConverterFactory->getLanguageConverter();
|
2020-02-06 19:36:40 +00:00
|
|
|
|
2021-11-02 06:03:15 +00:00
|
|
|
return $this->wanCache->getWithSetCallback(
|
|
|
|
$this->wanCache->makeKey(
|
2020-02-06 19:36:40 +00:00
|
|
|
'categorytree-html-ajax',
|
|
|
|
md5( $title->getDBkey() ),
|
2023-10-28 18:11:36 +00:00
|
|
|
md5( $ct->optionManager->getOptionsAsCacheKey( $depth ) ),
|
2020-02-06 19:36:40 +00:00
|
|
|
$this->getLanguage()->getCode(),
|
2021-02-27 13:58:51 +00:00
|
|
|
$langConv->getExtraHashOptions(),
|
2020-02-06 19:36:40 +00:00
|
|
|
$ctConfig->get( 'RenderHashAppend' )
|
|
|
|
),
|
2021-11-02 06:03:15 +00:00
|
|
|
$this->wanCache::TTL_DAY,
|
2021-05-03 06:57:15 +00:00
|
|
|
static function () use ( $ct, $title, $depth ) {
|
2020-02-06 19:36:40 +00:00
|
|
|
return trim( $ct->renderChildren( $title, $depth ) );
|
|
|
|
},
|
|
|
|
[
|
|
|
|
'touchedCallback' => function () {
|
|
|
|
$timestamp = $this->getConditionalRequestData( 'last-modified' );
|
|
|
|
|
|
|
|
return $timestamp ? wfTimestamp( TS_UNIX, $timestamp ) : null;
|
|
|
|
}
|
|
|
|
]
|
2015-05-20 17:35:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:57:56 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2015-05-20 17:35:20 +00:00
|
|
|
public function getAllowedParams() {
|
2016-10-14 16:11:52 +00:00
|
|
|
return [
|
|
|
|
'category' => [
|
2022-04-03 23:24:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2016-10-14 16:11:52 +00:00
|
|
|
],
|
|
|
|
'options' => [
|
2022-04-03 23:24:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2016-10-14 16:11:52 +00:00
|
|
|
],
|
|
|
|
];
|
2015-05-20 17:35:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 14:57:56 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2015-05-20 17:35:20 +00:00
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-29 04:55:46 +00:00
|
|
|
}
|