| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\CategoryTree; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Json\FormatJson; |
| 8 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 9 |
use MediaWiki\Linker\LinkRenderer; |
| 10 |
use MediaWiki\MainConfigNames; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 13 |
use Wikimedia\ParamValidator\ParamValidator; |
| 14 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ApiCategoryTree extends ApiBase { |
| 34 |
private LanguageConverterFactory $languageConverterFactory; |
| 35 |
private LinkRenderer $linkRenderer; |
| 36 |
private IConnectionProvider $dbProvider; |
| 37 |
private WANObjectCache $wanCache; |
| 38 |
|
| 39 |
public function __construct( |
| 40 |
ApiMain $main, |
| 41 |
string $action, |
| 42 |
IConnectionProvider $dbProvider, |
| 43 |
LanguageConverterFactory $languageConverterFactory, |
| 44 |
LinkRenderer $linkRenderer, |
| 45 |
WANObjectCache $wanCache |
| 46 |
) { |
| 47 |
parent::__construct( $main, $action ); |
| 48 |
$this->languageConverterFactory = $languageConverterFactory; |
| 49 |
$this->linkRenderer = $linkRenderer; |
| 50 |
$this->dbProvider = $dbProvider; |
| 51 |
$this->wanCache = $wanCache; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function execute() { |
| 58 |
$params = $this->extractRequestParams(); |
| 59 |
|
| 60 |
$options = $this->extractOptions( $params ); |
| 61 |
|
| 62 |
$title = CategoryTree::makeTitle( $params['category'] ); |
| 63 |
if ( !$title || $title->isExternal() ) { |
| 64 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['category'] ) ] ); |
| 65 |
} |
| 66 |
|
| 67 |
$depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1; |
| 68 |
|
| 69 |
$ct = new CategoryTree( $options, $this->getConfig(), $this->dbProvider, $this->linkRenderer ); |
| 70 |
$depth = $ct->optionManager->capDepth( $depth ); |
| 71 |
$html = $this->getHTML( $ct, $title, $depth ); |
| 72 |
|
| 73 |
$this->getMain()->setCacheMode( 'public' ); |
| 74 |
|
| 75 |
$this->getResult()->addContentValue( $this->getModuleName(), 'html', $html ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private function extractOptions( array $params ): array { |
| 83 |
if ( !isset( $params['options'] ) ) { |
| 84 |
return []; |
| 85 |
} |
| 86 |
|
| 87 |
$options = FormatJson::decode( $params['options'] ); |
| 88 |
if ( !is_object( $options ) ) { |
| 89 |
$this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' ); |
| 90 |
} |
| 91 |
|
| 92 |
foreach ( $options as $option => $value ) { |
| 93 |
if ( is_scalar( $value ) || $value === null ) { |
| 94 |
continue; |
| 95 |
} |
| 96 |
if ( $option === 'namespaces' && is_array( $value ) ) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
$this->dieWithError( |
| 100 |
[ 'apierror-categorytree-invalidjson-option', $option ], 'invalidjson-option' |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
return get_object_vars( $options ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getConditionalRequestData( $condition ) { |
| 113 |
if ( $condition === 'last-modified' ) { |
| 114 |
$params = $this->extractRequestParams(); |
| 115 |
$title = CategoryTree::makeTitle( $params['category'] ); |
| 116 |
return $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 117 |
->select( 'page_touched' ) |
| 118 |
->from( 'page' ) |
| 119 |
->where( [ |
| 120 |
'page_namespace' => NS_CATEGORY, |
| 121 |
'page_title' => $title->getDBkey(), |
| 122 |
] ) |
| 123 |
->caller( __METHOD__ ) |
| 124 |
->fetchField(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private function getHTML( CategoryTree $ct, Title $title, int $depth ): string { |
| 137 |
$langConv = $this->languageConverterFactory->getLanguageConverter(); |
| 138 |
|
| 139 |
return $this->wanCache->getWithSetCallback( |
| 140 |
$this->wanCache->makeKey( |
| 141 |
'categorytree-html-ajax', |
| 142 |
md5( $title->getDBkey() ), |
| 143 |
md5( $ct->optionManager->getOptionsAsCacheKey( $depth ) ), |
| 144 |
$this->getLanguage()->getCode(), |
| 145 |
$langConv->getExtraHashOptions(), |
| 146 |
$this->getConfig()->get( MainConfigNames::RenderHashAppend ) |
| 147 |
), |
| 148 |
$this->wanCache::TTL_DAY, |
| 149 |
static function () use ( $ct, $title, $depth ) { |
| 150 |
return $ct->renderChildren( $title, $depth ); |
| 151 |
}, |
| 152 |
[ |
| 153 |
'touchedCallback' => function () { |
| 154 |
$timestamp = $this->getConditionalRequestData( 'last-modified' ); |
| 155 |
|
| 156 |
return $timestamp ? wfTimestamp( TS_UNIX, $timestamp ) : null; |
| 157 |
} |
| 158 |
] |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function getAllowedParams() { |
| 166 |
return [ |
| 167 |
'category' => [ |
| 168 |
ParamValidator::PARAM_TYPE => 'string', |
| 169 |
ParamValidator::PARAM_REQUIRED => true, |
| 170 |
], |
| 171 |
'options' => [ |
| 172 |
ParamValidator::PARAM_TYPE => 'string', |
| 173 |
], |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function isInternal() { |
| 181 |
return true; |
| 182 |
} |
| 183 |
} |