MediaWiki REL1_31
CategoryTreePage.php
Go to the documentation of this file.
1<?php
30 public $target = '';
31
35 public $tree = null;
36
37 public function __construct() {
38 parent::__construct( 'CategoryTree' );
39 }
40
45 private function getOption( $name ) {
46 global $wgCategoryTreeDefaultOptions;
47
48 if ( $this->tree ) {
49 return $this->tree->getOption( $name );
50 } else {
51 return $wgCategoryTreeDefaultOptions[$name];
52 }
53 }
54
59 public function execute( $par ) {
60 global $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions;
61
62 $this->setHeaders();
63 $request = $this->getRequest();
64 if ( $par ) {
65 $this->target = $par;
66 } else {
67 $this->target = $request->getVal( 'target' );
68 if ( $this->target === null ) {
69 $rootcategory = $this->msg( 'rootcategory' );
70 if ( $rootcategory->exists() ) {
71 $this->target = $rootcategory->text();
72 }
73 }
74 }
75
76 $this->target = trim( $this->target );
77
78 $options = [];
79
80 # grab all known options from the request. Normalization is done by the CategoryTree class
81 foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
82 if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) ) {
83 $default = $wgCategoryTreeSpecialPageOptions[$option];
84 }
85
86 $options[$option] = $request->getVal( $option, $default );
87 }
88
89 $this->tree = new CategoryTree( $options );
90
91 $output = $this->getOutput();
92 $output->addWikiMsg( 'categorytree-header' );
93
94 $this->executeInputForm();
95
96 if ( $this->target !== '' && $this->target !== null ) {
99 }
100
101 $title = CategoryTree::makeTitle( $this->target );
102
103 if ( $title && $title->getArticleID() ) {
104 $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeParents' ] ) );
105 $output->addHTML( $this->msg( 'categorytree-parents' )->parse() );
106 $output->addHTML( $this->msg( 'colon-separator' )->escaped() );
107
108 $parents = $this->tree->renderParents( $title );
109
110 if ( $parents == '' ) {
111 $output->addHTML( $this->msg( 'categorytree-no-parent-categories' )->parse() );
112 } else {
113 $output->addHTML( $parents );
114 }
115
116 $output->addHTML( Xml::closeElement( 'div' ) );
117
118 $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeResult' ] ) );
119 $output->addHTML( $this->tree->renderNode( $title, 1 ) );
120 $output->addHTML( Xml::closeElement( 'div' ) );
121 } else {
122 $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeNotice' ] ) );
123 $output->addHTML( $this->msg( 'categorytree-not-found', $this->target )->parse() );
124 $output->addHTML( Xml::closeElement( 'div' ) );
125 }
126 }
127 }
128
132 private function executeInputForm() {
133 $namespaces = $this->getRequest()->getVal( 'namespaces', '' );
134 // mode may be overriden by namespaces option
135 $mode = ( $namespaces == '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
136 if ( $mode == CategoryTreeMode::CATEGORIES ) {
137 $modeDefault = 'categories';
138 } elseif ( $mode == CategoryTreeMode::PAGES ) {
139 $modeDefault = 'pages';
140 } else {
141 $modeDefault = 'all';
142 }
143
144 $formDescriptor = [
145 'category' => [
146 'type' => 'title',
147 'name' => 'target',
148 'label-message' => 'categorytree-category',
149 'namespace' => NS_CATEGORY,
150 ],
151
152 'mode' => [
153 'type' => 'select',
154 'name' => 'mode',
155 'label-message' => 'categorytree-mode-label',
156 'options-messages' => [
157 'categorytree-mode-categories' => 'categories',
158 'categorytree-mode-pages' => 'pages',
159 'categorytree-mode-all' => 'all',
160 ],
161 'default' => $modeDefault,
162 'nodata' => true,
163 ],
164
165 'namespace' => [
166 'type' => 'namespaceselect',
167 'name' => 'namespaces',
168 'label-message' => 'namespace',
169 'all' => '',
170 ],
171 ];
172
173 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
174 ->addHiddenFields( [ 'title' => $this->getPageTitle()->getPrefixedDbKey() ] )
175 ->setWrapperLegendMsg( 'categorytree-legend' )
176 ->setSubmitTextMsg( 'categorytree-go' )
177 ->setMethod( 'get' )
178 ->prepareForm()
179 ->displayForm( false );
180 }
181
190 public function prefixSearchSubpages( $search, $limit, $offset ) {
191 $title = Title::newFromText( $search, NS_CATEGORY );
192 if ( $title && $title->getNamespace() !== NS_CATEGORY ) {
193 // Someone searching for something like "Wikipedia:Foo"
194 $title = Title::makeTitleSafe( NS_CATEGORY, $search );
195 }
196 if ( !$title ) {
197 // No prefix suggestion outside of category namespace
198 return [];
199 }
200 // Autocomplete subpage the same as a normal search, but just for categories
201 $prefixSearcher = new TitlePrefixSearch;
202 $result = $prefixSearcher->search( $title->getPrefixedText(), $limit, [ NS_CATEGORY ], $offset );
203
204 return array_map( function ( Title $t ) {
205 // Remove namespace in search suggestion
206 return $t->getText();
207 }, $result );
208 }
209
210 protected function getGroupName() {
211 return 'pages';
212 }
213
214}
Special page for the CategoryTree extension, an AJAX based gadget to display the category structure o...
execute( $par)
Main execution function.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
executeInputForm()
Input form for entering a category.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
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 setHeaders( $outputPage)
Add ResourceLoader modules to the OutputPage object.
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
Performs prefix search, returning Title objects.
Represents a title within MediaWiki.
Definition Title.php:39
namespace being checked & $result
Definition hooks.txt:2323
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2255
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:934
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:2001
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
const NS_CATEGORY
Definition Defines.php:88