MediaWiki  1.34.0
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 ) {
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"
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 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:672
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
CategoryTreePage\getOption
getOption( $name)
Definition: CategoryTreePage.php:47
CategoryTreePage\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: CategoryTreePage.php:193
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
CategoryTreePage\__construct
__construct()
Definition: CategoryTreePage.php:39
CategoryTree
Core functions for the CategoryTree extension, an AJAX based gadget to display the category structure...
Definition: CategoryTree.php:31
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
CategoryTreeMode\PAGES
const PAGES
Definition: CategoryTreeMode.php:32
CategoryTreePage\execute
execute( $par)
Main execution function.
Definition: CategoryTreePage.php:61
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
CategoryTreeHooks\shouldForceHeaders
static shouldForceHeaders()
Definition: CategoryTreeHooks.php:35
$t
$t
Definition: make-normalization-table.php:143
$title
$title
Definition: testCompression.php:34
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
$output
$output
Definition: SyntaxHighlight.php:335
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
CategoryTreePage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: CategoryTreePage.php:218
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:37
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
CategoryTreePage
Special page for the CategoryTree extension, an AJAX based gadget to display the category structure o...
Definition: CategoryTreePage.php:31
CategoryTreePage\$target
$target
Definition: CategoryTreePage.php:32
CategoryTreeMode\ALL
const ALL
Definition: CategoryTreeMode.php:34
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:117
CategoryTree\makeTitle
static makeTitle( $title)
Creates a Title object from a user provided (and thus unsafe) string.
Definition: CategoryTree.php:745
CategoryTreePage\executeInputForm
executeInputForm()
Input form for entering a category.
Definition: CategoryTreePage.php:135
CategoryTreeMode\CATEGORIES
const CATEGORIES
Definition: CategoryTreeMode.php:30
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303
CategoryTree\setHeaders
static setHeaders(OutputPage $outputPage)
Add ResourceLoader modules to the OutputPage object.
Definition: CategoryTree.php:260
CategoryTreePage\$tree
CategoryTree $tree
Definition: CategoryTreePage.php:37