MediaWiki  1.34.0
ApiCategoryTree.php
Go to the documentation of this file.
1 <?php
2 
4 
22 class 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',
124  ApiBase::PARAM_REQUIRED => true,
125  ],
126  'options' => [
127  ApiBase::PARAM_TYPE => 'string',
128  ],
129  ];
130  }
131 
135  public function isInternal() {
136  return true;
137  }
138 }
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:342
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
CategoryTree\capDepth
static capDepth( $mode, $depth)
Internal function to cap depth PhanPluginDuplicateConditionalNullCoalescing until PHP7 is required.
Definition: CategoryTree.php:771
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
CategoryTree
Core functions for the CategoryTree extension, an AJAX based gadget to display the category structure...
Definition: CategoryTree.php:31
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
$wgMemc
$wgMemc
Definition: Setup.php:791
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
Config
Interface for configuration instances.
Definition: Config.php:28
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:174
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiCategoryTree\isInternal
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
Definition: ApiCategoryTree.php:135
$title
$title
Definition: testCompression.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1898
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
ApiCategoryTree\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.Concrete implementatio...
Definition: ApiCategoryTree.php:26
CategoryTree\renderChildren
renderChildren(Title $title, $depth=1)
Returns a string with an HTML representation of the children of the given category.
Definition: CategoryTree.php:396
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
ApiCategoryTree
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: ApiCategoryTree.php:22
Title
Represents a title within MediaWiki.
Definition: Title.php:42
CategoryTree\getOptionsAsCacheKey
getOptionsAsCacheKey( $depth=null)
Definition: CategoryTree.php:288
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
CategoryTree\makeTitle
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
Definition: CategoryTree.php:745
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:536
ApiCategoryTree\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiCategoryTree.php:120
ApiCategoryTree\getHTML
getHTML(CategoryTree $ct, Title $title, $depth, Config $ctConfig)
Get category tree HTML for the given tree, title, depth and config.
Definition: ApiCategoryTree.php:82
ApiCategoryTree\getConditionalRequestData
getConditionalRequestData( $condition)
Definition: ApiCategoryTree.php:59