2015-05-20 17:35:20 +00:00
|
|
|
<?php
|
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 {
|
2019-02-05 14:57:56 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2015-05-20 17:35:20 +00:00
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
2016-10-14 16:11:52 +00:00
|
|
|
$options = [];
|
2015-05-20 17:35:20 +00:00
|
|
|
if ( isset( $params['options'] ) ) {
|
|
|
|
$options = FormatJson::decode( $params['options'] );
|
|
|
|
if ( !is_object( $options ) ) {
|
2018-10-15 21:34:16 +00:00
|
|
|
$this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' );
|
2015-05-20 17:35:20 +00:00
|
|
|
}
|
|
|
|
$options = get_object_vars( $options );
|
|
|
|
}
|
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;
|
|
|
|
|
2016-08-29 04:55:46 +00:00
|
|
|
$ct = new CategoryTree( $options );
|
2016-02-05 05:31:34 +00:00
|
|
|
$depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depth );
|
2015-05-20 17:35:20 +00:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$ctConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'categorytree' );
|
|
|
|
$html = $this->getHTML( $ct, $title, $depth, $ctConfig );
|
|
|
|
|
|
|
|
if (
|
|
|
|
$ctConfig->get( 'CategoryTreeHTTPCache' ) &&
|
|
|
|
$config->get( 'SquidMaxage' ) &&
|
|
|
|
$config->get( 'UseSquid' )
|
|
|
|
) {
|
|
|
|
if ( $config->get( 'UseESI' ) ) {
|
|
|
|
$this->getRequest()->response()->header(
|
|
|
|
'Surrogate-Control: max-age=' . $config->get( 'SquidMaxage' ) . ', content="ESI/1.0"'
|
|
|
|
);
|
|
|
|
$this->getMain()->setCacheMaxAge( 0 );
|
|
|
|
} else {
|
|
|
|
$this->getMain()->setCacheMaxAge( $config->get( 'SquidMaxage' ) );
|
|
|
|
}
|
2017-05-30 18:21:54 +00:00
|
|
|
// cache for anons only
|
|
|
|
$this->getRequest()->response()->header( 'Vary: Accept-Encoding, Cookie' );
|
|
|
|
// TODO: purge the squid cache when a category page is invalidated
|
2015-05-20 17:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addContentValue( $this->getModuleName(), 'html', $html );
|
|
|
|
}
|
|
|
|
|
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'] );
|
2017-09-02 00:03:53 +00:00
|
|
|
return wfGetDB( DB_REPLICA )->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 ) {
|
2015-05-20 17:35:20 +00:00
|
|
|
global $wgContLang, $wgMemc;
|
|
|
|
|
|
|
|
$mckey = wfMemcKey(
|
|
|
|
'ajax-categorytree',
|
|
|
|
md5( $title->getDBkey() ),
|
|
|
|
md5( $ct->getOptionsAsCacheKey( $depth ) ),
|
|
|
|
$this->getLanguage()->getCode(),
|
|
|
|
$wgContLang->getExtraHashOptions(),
|
|
|
|
$ctConfig->get( 'RenderHashAppend' )
|
|
|
|
);
|
|
|
|
|
|
|
|
$touched = $this->getConditionalRequestData( 'last-modified' );
|
|
|
|
if ( $touched ) {
|
|
|
|
$mcvalue = $wgMemc->get( $mckey );
|
|
|
|
if ( $mcvalue && $touched <= $mcvalue['timestamp'] ) {
|
|
|
|
$html = $mcvalue['value'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isset( $html ) ) {
|
|
|
|
$html = $ct->renderChildren( $title, $depth );
|
|
|
|
|
|
|
|
$wgMemc->set(
|
|
|
|
$mckey,
|
2016-10-14 16:11:52 +00:00
|
|
|
[
|
2015-05-20 17:35:20 +00:00
|
|
|
'timestamp' => wfTimestampNow(),
|
|
|
|
'value' => $html
|
2016-10-14 16:11:52 +00:00
|
|
|
],
|
2015-05-20 17:35:20 +00:00
|
|
|
86400
|
|
|
|
);
|
|
|
|
}
|
2016-01-31 20:44:58 +00:00
|
|
|
return trim( $html );
|
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' => [
|
2015-05-20 17:35:20 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-10-14 16:11:52 +00:00
|
|
|
],
|
|
|
|
'options' => [
|
2015-05-20 17:35:20 +00:00
|
|
|
ApiBase::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
|
|
|
}
|