MediaWiki REL1_34
CategoryTreePage.php
Go to the documentation of this file.
1<?php
26
32 public $target = '';
33
37 public $tree = null;
38
39 public function __construct() {
40 parent::__construct( 'CategoryTree' );
41 }
42
47 private function getOption( $name ) {
48 global $wgCategoryTreeDefaultOptions;
49
50 if ( $this->tree ) {
51 return $this->tree->getOption( $name );
52 } else {
53 return $wgCategoryTreeDefaultOptions[$name];
54 }
55 }
56
61 public function execute( $par ) {
62 global $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions;
63
64 $this->setHeaders();
65 $this->addHelpLink( 'Extension:CategoryTree' );
66 $request = $this->getRequest();
67 if ( $par ) {
68 $this->target = $par;
69 } else {
70 $this->target = $request->getVal( 'target' );
71 if ( $this->target === null ) {
72 $rootcategory = $this->msg( 'rootcategory' );
73 if ( $rootcategory->exists() ) {
74 $this->target = $rootcategory->text();
75 }
76 }
77 }
78
79 $this->target = trim( $this->target );
80
81 $options = [];
82
83 # grab all known options from the request. Normalization is done by the CategoryTree class
84 foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
85 if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) ) {
86 $default = $wgCategoryTreeSpecialPageOptions[$option];
87 }
88
89 $options[$option] = $request->getVal( $option, $default );
90 }
91
92 $this->tree = new CategoryTree( $options );
93
94 $output = $this->getOutput();
95 $output->addWikiMsg( 'categorytree-header' );
96
97 $this->executeInputForm();
98
99 if ( $this->target !== '' && $this->target !== null ) {
101 CategoryTree::setHeaders( $output );
102 }
103
104 $title = CategoryTree::makeTitle( $this->target );
105
106 if ( $title && $title->getArticleID() ) {
107 $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeParents' ] ) );
108 $output->addHTML( $this->msg( 'categorytree-parents' )->parse() );
109 $output->addHTML( $this->msg( 'colon-separator' )->escaped() );
110
111 $parents = $this->tree->renderParents( $title );
112
113 if ( $parents == '' ) {
114 $output->addHTML( $this->msg( 'categorytree-no-parent-categories' )->parse() );
115 } else {
116 $output->addHTML( $parents );
117 }
118
119 $output->addHTML( Xml::closeElement( 'div' ) );
120
121 $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeResult' ] ) );
122 $output->addHTML( $this->tree->renderNode( $title, 1 ) );
123 $output->addHTML( Xml::closeElement( 'div' ) );
124 } else {
125 $output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeNotice' ] ) );
126 $output->addHTML( $this->msg( 'categorytree-not-found', $this->target )->parse() );
127 $output->addHTML( Xml::closeElement( 'div' ) );
128 }
129 }
130 }
131
135 private function executeInputForm() {
136 $namespaces = $this->getRequest()->getVal( 'namespaces', '' );
137 // mode may be overriden by namespaces option
138 $mode = ( $namespaces == '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
139 if ( $mode == CategoryTreeMode::CATEGORIES ) {
140 $modeDefault = 'categories';
141 } elseif ( $mode == CategoryTreeMode::PAGES ) {
142 $modeDefault = 'pages';
143 } else {
144 $modeDefault = 'all';
145 }
146
147 $formDescriptor = [
148 'category' => [
149 'type' => 'title',
150 'name' => 'target',
151 'label-message' => 'categorytree-category',
152 'namespace' => NS_CATEGORY,
153 ],
154
155 'mode' => [
156 'type' => 'select',
157 'name' => 'mode',
158 'label-message' => 'categorytree-mode-label',
159 'options-messages' => [
160 'categorytree-mode-categories' => 'categories',
161 'categorytree-mode-pages' => 'pages',
162 'categorytree-mode-all' => 'all',
163 ],
164 'default' => $modeDefault,
165 'nodata' => true,
166 ],
167
168 'namespace' => [
169 'type' => 'namespaceselect',
170 'name' => 'namespaces',
171 'label-message' => 'namespace',
172 'all' => '',
173 ],
174 ];
175
176 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
177 ->addHiddenFields( [ 'title' => $this->getPageTitle()->getPrefixedDbKey() ] )
178 ->setWrapperLegendMsg( 'categorytree-legend' )
179 ->setSubmitTextMsg( 'categorytree-go' )
180 ->setMethod( 'get' )
181 ->prepareForm()
182 ->displayForm( false );
183 }
184
193 public function prefixSearchSubpages( $search, $limit, $offset ) {
194 $title = Title::newFromText( $search, NS_CATEGORY );
195 if ( $title && $title->getNamespace() !== NS_CATEGORY ) {
196 // Someone searching for something like "Wikipedia:Foo"
197 $title = Title::makeTitleSafe( NS_CATEGORY, $search );
198 }
199 if ( !$title ) {
200 // No prefix suggestion outside of category namespace
201 return [];
202 }
203 $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
204 $searchEngine->setLimitOffset( $limit, $offset );
205 // Autocomplete subpage the same as a normal search, but just for categories
206 $searchEngine->setNamespaces( [ NS_CATEGORY ] );
207 $result = $searchEngine->defaultPrefixSearch( $search );
208
209 return array_map( function ( Title $t ) {
210 // Remove namespace in search suggestion
211 return $t->getText();
212 }, $result );
213 }
214
218 protected function getGroupName() {
219 return 'pages';
220 }
221
222}
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 setHeaders(OutputPage $outputPage)
Add ResourceLoader modules to the OutputPage object.
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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,... $params)
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.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Represents a title within MediaWiki.
Definition Title.php:42
const NS_CATEGORY
Definition Defines.php:83