MediaWiki  1.33.0
ApiCategoryTree.php
Go to the documentation of this file.
1 <?php
19 class ApiCategoryTree extends ApiBase {
23  public function execute() {
24  $params = $this->extractRequestParams();
25  $options = [];
26  if ( isset( $params['options'] ) ) {
27  $options = FormatJson::decode( $params['options'] );
28  if ( !is_object( $options ) ) {
29  $this->dieWithError( 'apierror-categorytree-invalidjson', 'invalidjson' );
30  }
31  $options = get_object_vars( $options );
32  }
33 
34  $title = CategoryTree::makeTitle( $params['category'] );
35  if ( !$title || $title->isExternal() ) {
36  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['category'] ) ] );
37  }
38 
39  $depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1;
40 
41  $ct = new CategoryTree( $options );
42  $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depth );
43  $ctConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'categorytree' );
44  $html = $this->getHTML( $ct, $title, $depth, $ctConfig );
45 
46  $this->getMain()->setCacheMode( 'public' );
47 
48  $this->getResult()->addContentValue( $this->getModuleName(), 'html', $html );
49  }
50 
56  public function getConditionalRequestData( $condition ) {
57  if ( $condition === 'last-modified' ) {
58  $params = $this->extractRequestParams();
59  $title = CategoryTree::makeTitle( $params['category'] );
60  return wfGetDB( DB_REPLICA )->selectField( 'page', 'page_touched',
61  [
62  'page_namespace' => NS_CATEGORY,
63  'page_title' => $title->getDBkey(),
64  ],
65  __METHOD__
66  );
67  }
68  }
69 
79  private function getHTML( CategoryTree $ct, Title $title, $depth, Config $ctConfig ) {
80  global $wgContLang, $wgMemc;
81 
82  $mckey = wfMemcKey(
83  'ajax-categorytree',
84  md5( $title->getDBkey() ),
85  md5( $ct->getOptionsAsCacheKey( $depth ) ),
86  $this->getLanguage()->getCode(),
87  $wgContLang->getExtraHashOptions(),
88  $ctConfig->get( 'RenderHashAppend' )
89  );
90 
91  $touched = $this->getConditionalRequestData( 'last-modified' );
92  if ( $touched ) {
93  $mcvalue = $wgMemc->get( $mckey );
94  if ( $mcvalue && $touched <= $mcvalue['timestamp'] ) {
95  $html = $mcvalue['value'];
96  }
97  }
98 
99  if ( !isset( $html ) ) {
100  $html = $ct->renderChildren( $title, $depth );
101 
102  $wgMemc->set(
103  $mckey,
104  [
105  'timestamp' => wfTimestampNow(),
106  'value' => $html
107  ],
108  86400
109  );
110  }
111  return trim( $html );
112  }
113 
117  public function getAllowedParams() {
118  return [
119  'category' => [
120  ApiBase::PARAM_TYPE => 'string',
121  ApiBase::PARAM_REQUIRED => true,
122  ],
123  'options' => [
124  ApiBase::PARAM_TYPE => 'string',
125  ],
126  ];
127  }
128 
132  public function isInternal() {
133  return true;
134  }
135 }
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 @suppress PhanPluginDuplicateConditionalNullCoalescing until PHP7 is r...
Definition: CategoryTree.php:802
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1990
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:632
$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 $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
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:44
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
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
$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:1985
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
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:2636
ConfigFactory\getDefaultInstance
static getDefaultInstance()
Definition: ConfigFactory.php:51
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:743
ApiCategoryTree\isInternal
isInternal()
@inheritDoc
Definition: ApiCategoryTree.php:132
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1941
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
ApiCategoryTree\execute
execute()
@inheritDoc
Definition: ApiCategoryTree.php:23
CategoryTree\renderChildren
renderChildren(Title $title, $depth=1)
Returns a string with an HTML representation of the children of the given category.
Definition: CategoryTree.php:397
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1577
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
Title
Represents a title within MediaWiki.
Definition: Title.php:40
CategoryTree\getOptionsAsCacheKey
getOptionsAsCacheKey( $depth=null)
Definition: CategoryTree.php:282
$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:1985
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:512
CategoryTree\makeTitle
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
Definition: CategoryTree.php:776
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:528
ApiCategoryTree\getAllowedParams
getAllowedParams()
@inheritDoc
Definition: ApiCategoryTree.php:117
wfMemcKey
wfMemcKey(... $args)
Make a cache key for the local wiki.
Definition: GlobalFunctions.php:2561
$wgContLang
$wgContLang
Definition: Setup.php:790
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:79
ApiCategoryTree\getConditionalRequestData
getConditionalRequestData( $condition)
Definition: ApiCategoryTree.php:56