MediaWiki REL1_32
ApiCategoryTree.php
Go to the documentation of this file.
1<?php
19class ApiCategoryTree extends ApiBase {
20 public function execute() {
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
36 $title = CategoryTree::makeTitle( $params['category'] );
37 if ( !$title || $title->isExternal() ) {
38 if ( is_callable( [ $this, 'dieWithError' ] ) ) {
39 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['category'] ) ] );
40 } else {
41 $this->dieUsageMsg( [ 'invalidtitle', wfEscapeWikiText( $params['category'] ) ] );
42 }
43 }
44
45 $depth = isset( $options['depth'] ) ? (int)$options['depth'] : 1;
46
47 $ct = new CategoryTree( $options );
48 $depth = CategoryTree::capDepth( $ct->getOption( 'mode' ), $depth );
49 $config = $this->getConfig();
50 $ctConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'categorytree' );
51 $html = $this->getHTML( $ct, $title, $depth, $ctConfig );
52
53 if (
54 $ctConfig->get( 'CategoryTreeHTTPCache' ) &&
55 $config->get( 'SquidMaxage' ) &&
56 $config->get( 'UseSquid' )
57 ) {
58 if ( $config->get( 'UseESI' ) ) {
59 $this->getRequest()->response()->header(
60 'Surrogate-Control: max-age=' . $config->get( 'SquidMaxage' ) . ', content="ESI/1.0"'
61 );
62 $this->getMain()->setCacheMaxAge( 0 );
63 } else {
64 $this->getMain()->setCacheMaxAge( $config->get( 'SquidMaxage' ) );
65 }
66 // cache for anons only
67 $this->getRequest()->response()->header( 'Vary: Accept-Encoding, Cookie' );
68 // TODO: purge the squid cache when a category page is invalidated
69 }
70
71 $this->getResult()->addContentValue( $this->getModuleName(), 'html', $html );
72 }
73
79 public function getConditionalRequestData( $condition ) {
80 if ( $condition === 'last-modified' ) {
82 $title = CategoryTree::makeTitle( $params['category'] );
83 return wfGetDB( DB_REPLICA )->selectField( 'page', 'page_touched',
84 [
85 'page_namespace' => NS_CATEGORY,
86 'page_title' => $title->getDBkey(),
87 ],
88 __METHOD__
89 );
90 }
91 }
92
102 private function getHTML( $ct, $title, $depth, $ctConfig ) {
103 global $wgContLang, $wgMemc;
104
105 $mckey = wfMemcKey(
106 'ajax-categorytree',
107 md5( $title->getDBkey() ),
108 md5( $ct->getOptionsAsCacheKey( $depth ) ),
109 $this->getLanguage()->getCode(),
110 $wgContLang->getExtraHashOptions(),
111 $ctConfig->get( 'RenderHashAppend' )
112 );
113
114 $touched = $this->getConditionalRequestData( 'last-modified' );
115 if ( $touched ) {
116 $mcvalue = $wgMemc->get( $mckey );
117 if ( $mcvalue && $touched <= $mcvalue['timestamp'] ) {
118 $html = $mcvalue['value'];
119 }
120 }
121
122 if ( !isset( $html ) ) {
123 $html = $ct->renderChildren( $title, $depth );
124
125 $wgMemc->set(
126 $mckey,
127 [
128 'timestamp' => wfTimestampNow(),
129 'value' => $html
130 ],
131 86400
132 );
133 }
134 return trim( $html );
135 }
136
137 public function getAllowedParams() {
138 return [
139 'category' => [
140 ApiBase::PARAM_TYPE => 'string',
142 ],
143 'options' => [
144 ApiBase::PARAM_TYPE => 'string',
145 ],
146 ];
147 }
148
149 public function isInternal() {
150 return true;
151 }
152}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMemcKey(... $args)
Make a cache key for the local wiki.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
$wgContLang
Definition Setup.php:809
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:37
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:111
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:1987
getMain()
Get the main module.
Definition ApiBase.php:555
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
getResult()
Get the result object.
Definition ApiBase.php:659
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:770
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:539
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
getConditionalRequestData( $condition)
getHTML( $ct, $title, $depth, $ctConfig)
Get category tree HTML for the given tree, title, depth and config.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
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...
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
static capDepth( $mode, $depth)
Internal function to cap depth.
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:2050
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:2062
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:64
const NS_CATEGORY
Definition Defines.php:78
const DB_REPLICA
Definition defines.php:25
$params