MediaWiki REL1_34
ApiCategoryTree.php
Go to the documentation of this file.
1<?php
2
4
22class ApiCategoryTree extends ApiBase {
26 public function execute() {
27 $params = $this->extractRequestParams();
28 $options = [];
29 if ( isset( $params['options'] ) ) {
30 $options = FormatJson::decode( $params['options'] );
31 if ( !is_object( $options ) ) {
32 $this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' );
33 }
34 $options = get_object_vars( $options );
35 }
36
37 $title = CategoryTree::makeTitle( $params['category'] );
38 if ( !$title || $title->isExternal() ) {
39 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['category'] ) ] );
40 }
41
42 $depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1;
43
44 $ct = new CategoryTree( $options );
45 $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depth );
46 $ctConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'categorytree' );
47 $html = $this->getHTML( $ct, $title, $depth, $ctConfig );
48
49 $this->getMain()->setCacheMode( 'public' );
50
51 $this->getResult()->addContentValue( $this->getModuleName(), 'html', $html );
52 }
53
59 public function getConditionalRequestData( $condition ) {
60 if ( $condition === 'last-modified' ) {
61 $params = $this->extractRequestParams();
62 $title = CategoryTree::makeTitle( $params['category'] );
63 return wfGetDB( DB_REPLICA )->selectField( 'page', 'page_touched',
64 [
65 'page_namespace' => NS_CATEGORY,
66 'page_title' => $title->getDBkey(),
67 ],
68 __METHOD__
69 );
70 }
71 }
72
82 private function getHTML( CategoryTree $ct, Title $title, $depth, Config $ctConfig ) {
83 global $wgMemc;
84
85 $mckey = ObjectCache::getLocalClusterInstance()->makeKey(
86 'ajax-categorytree',
87 md5( $title->getDBkey() ),
88 md5( $ct->getOptionsAsCacheKey( $depth ) ),
89 $this->getLanguage()->getCode(),
90 MediaWikiServices::getInstance()->getContentLanguage()->getExtraHashOptions(),
91 $ctConfig->get( 'RenderHashAppend' )
92 );
93
94 $touched = $this->getConditionalRequestData( 'last-modified' );
95 if ( $touched ) {
96 $mcvalue = $wgMemc->get( $mckey );
97 if ( $mcvalue && $touched <= $mcvalue['timestamp'] ) {
98 $html = $mcvalue['value'];
99 }
100 }
101
102 if ( !isset( $html ) ) {
103 $html = $ct->renderChildren( $title, $depth );
104
105 $wgMemc->set(
106 $mckey,
107 [
108 'timestamp' => wfTimestampNow(),
109 'value' => $html
110 ],
111 86400
112 );
113 }
114 return trim( $html );
115 }
116
120 public function getAllowedParams() {
121 return [
122 'category' => [
123 ApiBase::PARAM_TYPE => 'string',
125 ],
126 'options' => [
127 ApiBase::PARAM_TYPE => 'string',
128 ],
129 ];
130 }
131
135 public function isInternal() {
136 return true;
137 }
138}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
$wgMemc
Definition Setup.php:790
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:118
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
getMain()
Get the main module.
Definition ApiBase.php:536
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
getConditionalRequestData( $condition)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHTML(CategoryTree $ct, Title $title, $depth, Config $ctConfig)
Get category tree HTML for the given tree, title, depth and config.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.Concrete implementatio...
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
Core functions for the CategoryTree extension, an AJAX based gadget to display the category structure...
renderChildren(Title $title, $depth=1)
Returns a string with an HTML representation of the children of the given category.
getOptionsAsCacheKey( $depth=null)
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
static capDepth( $mode, $depth)
Internal function to cap depth PhanPluginDuplicateConditionalNullCoalescing until PHP7 is required.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:42
const NS_CATEGORY
Definition Defines.php:83
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
const DB_REPLICA
Definition defines.php:25