Debian Codegraph @

extensions/CategoryTree/includes/ApiCategoryTree.php

raw symbols SCIP
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 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * http://www.gnu.org/copyleft/gpl.html
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 * @inheritDoc
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 * @param array $params
80 * @return string[]
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 * @param string $condition
109 *
110 * @return bool|null|string
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 * Get category tree HTML for the given tree, title and depth
130 *
131 * @param CategoryTree $ct
132 * @param Title $title
133 * @param int $depth
134 * @return string HTML
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 * @inheritDoc
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 * @inheritDoc
179 */
180 public function isInternal() {
181 return true;
182 }
183 }