MediaWiki  1.31.0
ApiCategoryTree.php
Go to the documentation of this file.
1 <?php
19 class ApiCategoryTree extends ApiBase {
20  public function execute() {
21  $params = $this->extractRequestParams();
22  $options = [];
23  if ( isset( $params['options'] ) ) {
24  $options = FormatJson::decode( $params['options'] );
25  if ( !is_object( $options ) ) {
26  if ( is_callable( [ $this, 'dieWithError' ] ) ) {
27  $this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' );
28  } else {
29  $this->dieUsage( 'Options must be valid a JSON object', 'invalidjson' );
30  }
31  return;
32  }
33  $options = get_object_vars( $options );
34  }
35  $depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1;
36 
37  $ct = new CategoryTree( $options );
38  $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depth );
39  $title = CategoryTree::makeTitle( $params['category'] );
40  $config = $this->getConfig();
41  $ctConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'categorytree' );
42  $html = $this->getHTML( $ct, $title, $depth, $ctConfig );
43 
44  if (
45  $ctConfig->get( 'CategoryTreeHTTPCache' ) &&
46  $config->get( 'SquidMaxage' ) &&
47  $config->get( 'UseSquid' )
48  ) {
49  if ( $config->get( 'UseESI' ) ) {
50  $this->getRequest()->response()->header(
51  'Surrogate-Control: max-age=' . $config->get( 'SquidMaxage' ) . ', content="ESI/1.0"'
52  );
53  $this->getMain()->setCacheMaxAge( 0 );
54  } else {
55  $this->getMain()->setCacheMaxAge( $config->get( 'SquidMaxage' ) );
56  }
57  // cache for anons only
58  $this->getRequest()->response()->header( 'Vary: Accept-Encoding, Cookie' );
59  // TODO: purge the squid cache when a category page is invalidated
60  }
61 
62  $this->getResult()->addContentValue( $this->getModuleName(), 'html', $html );
63  }
64 
70  public function getConditionalRequestData( $condition ) {
71  if ( $condition === 'last-modified' ) {
72  $params = $this->extractRequestParams();
73  $title = CategoryTree::makeTitle( $params['category'] );
74  return wfGetDB( DB_REPLICA )->selectField( 'page', 'page_touched',
75  [
76  'page_namespace' => NS_CATEGORY,
77  'page_title' => $title->getDBkey(),
78  ],
79  __METHOD__
80  );
81  }
82  }
83 
93  private function getHTML( $ct, $title, $depth, $ctConfig ) {
95 
96  $mckey = wfMemcKey(
97  'ajax-categorytree',
98  md5( $title->getDBkey() ),
99  md5( $ct->getOptionsAsCacheKey( $depth ) ),
100  $this->getLanguage()->getCode(),
101  $wgContLang->getExtraHashOptions(),
102  $ctConfig->get( 'RenderHashAppend' )
103  );
104 
105  $touched = $this->getConditionalRequestData( 'last-modified' );
106  if ( $touched ) {
107  $mcvalue = $wgMemc->get( $mckey );
108  if ( $mcvalue && $touched <= $mcvalue['timestamp'] ) {
109  $html = $mcvalue['value'];
110  }
111  }
112 
113  if ( !isset( $html ) ) {
114  $html = $ct->renderChildren( $title, $depth );
115 
116  $wgMemc->set(
117  $mckey,
118  [
119  'timestamp' => wfTimestampNow(),
120  'value' => $html
121  ],
122  86400
123  );
124  }
125  return trim( $html );
126  }
127 
128  public function getAllowedParams() {
129  return [
130  'category' => [
131  ApiBase::PARAM_TYPE => 'string',
132  ApiBase::PARAM_REQUIRED => true,
133  ],
134  'options' => [
135  ApiBase::PARAM_TYPE => 'string',
136  ],
137  ];
138  }
139 
140  public function isInternal() {
141  return true;
142  }
143 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:111
CategoryTree\capDepth
static capDepth( $mode, $depth)
Internal function to cap depth.
Definition: CategoryTree.php:793
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1895
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:87
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:641
CategoryTree
Core functions for the CategoryTree extension, an AJAX based gadget to display the category structure...
Definition: CategoryTree.php:29
$params
$params
Definition: styleTest.css.php:40
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:37
ApiCategoryTree\getHTML
getHTML( $ct, $title, $depth, $ctConfig)
Get category tree HTML for the given tree, title, depth and config.
Definition: ApiCategoryTree.php:93
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1987
wfMemcKey
wfMemcKey()
Make a cache key for the local wiki.
Definition: GlobalFunctions.php:2700
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2800
ConfigFactory\getDefaultInstance
static getDefaultInstance()
Definition: ConfigFactory.php:51
ApiCategoryTree\isInternal
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
Definition: ApiCategoryTree.php:140
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1997
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:79
ApiCategoryTree\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiCategoryTree.php:20
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:749
ApiBase\dieUsage
dieUsage( $description, $errorCode, $httpRespCode=0, $extradata=null)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition: ApiBase.php:2672
ApiCategoryTree
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: ApiCategoryTree.php:19
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1987
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:521
CategoryTree\makeTitle
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
Definition: CategoryTree.php:768
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:537
ApiCategoryTree\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiCategoryTree.php:128
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
ApiCategoryTree\getConditionalRequestData
getConditionalRequestData( $condition)
Definition: ApiCategoryTree.php:70