Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 105 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CategoryTreePage | |
0.00% |
0 / 105 |
|
0.00% |
0 / 7 |
552 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOption | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| execute | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
| executeInputForm | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
20 | |||
| executeCategoryTree | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
20 | |||
| prefixSearchSubpages | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * © 2006 Daniel Kinzler |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | * @ingroup Extensions |
| 22 | * @author Daniel Kinzler, brightbyte.de |
| 23 | */ |
| 24 | |
| 25 | namespace MediaWiki\Extension\CategoryTree; |
| 26 | |
| 27 | use MediaWiki\Html\Html; |
| 28 | use MediaWiki\HTMLForm\HTMLForm; |
| 29 | use MediaWiki\Search\SearchEngineFactory; |
| 30 | use MediaWiki\SpecialPage\SpecialPage; |
| 31 | use MediaWiki\Title\Title; |
| 32 | |
| 33 | /** |
| 34 | * Special page for the CategoryTree extension, an AJAX based gadget |
| 35 | * to display the category structure of a wiki |
| 36 | */ |
| 37 | class CategoryTreePage extends SpecialPage { |
| 38 | public string $target = ''; |
| 39 | public ?CategoryTree $tree = null; |
| 40 | |
| 41 | public function __construct( |
| 42 | private readonly CategoryTreeFactory $categoryTreeFactory, |
| 43 | private readonly SearchEngineFactory $searchEngineFactory, |
| 44 | ) { |
| 45 | parent::__construct( 'CategoryTree' ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param string $name |
| 50 | * @return mixed |
| 51 | */ |
| 52 | private function getOption( string $name ) { |
| 53 | if ( $this->tree ) { |
| 54 | return $this->tree->optionManager->getOption( $name ); |
| 55 | } else { |
| 56 | return $this->getConfig()->get( 'CategoryTreeDefaultOptions' )[$name]; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Main execution function |
| 62 | * @param string|null $par Parameters passed to the page |
| 63 | */ |
| 64 | public function execute( $par ) { |
| 65 | $this->setHeaders(); |
| 66 | $this->addHelpLink( 'Extension:CategoryTree' ); |
| 67 | $request = $this->getRequest(); |
| 68 | if ( $par ) { |
| 69 | $this->target = trim( $par ); |
| 70 | } else { |
| 71 | $this->target = trim( $request->getText( 'target' ) ); |
| 72 | if ( $this->target === '' ) { |
| 73 | $rootcategory = $this->msg( 'rootcategory' ); |
| 74 | if ( $rootcategory->exists() ) { |
| 75 | $this->target = $rootcategory->text(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | $options = []; |
| 81 | $config = $this->getConfig(); |
| 82 | |
| 83 | # grab all known options from the request. Normalization is done by the CategoryTree class |
| 84 | $categoryTreeDefaultOptions = $config->get( 'CategoryTreeDefaultOptions' ); |
| 85 | $categoryTreeSpecialPageOptions = $config->get( 'CategoryTreeSpecialPageOptions' ); |
| 86 | foreach ( $categoryTreeDefaultOptions as $option => $default ) { |
| 87 | if ( isset( $categoryTreeSpecialPageOptions[$option] ) ) { |
| 88 | $default = $categoryTreeSpecialPageOptions[$option]; |
| 89 | } |
| 90 | |
| 91 | $options[$option] = $request->getVal( $option, $default ); |
| 92 | } |
| 93 | |
| 94 | $this->tree = $this->categoryTreeFactory->newCategoryTree( $options ); |
| 95 | |
| 96 | $this->getOutput()->addWikiMsg( 'categorytree-header' ); |
| 97 | |
| 98 | $this->executeInputForm(); |
| 99 | |
| 100 | if ( $this->target !== '' ) { |
| 101 | $this->executeCategoryTree(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Input form for entering a category |
| 107 | */ |
| 108 | private function executeInputForm() { |
| 109 | $namespaces = $this->getRequest()->getRawVal( 'namespaces' ) ?? ''; |
| 110 | // mode may be overriden by namespaces option |
| 111 | $mode = ( $namespaces === '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL ); |
| 112 | if ( $mode === CategoryTreeMode::CATEGORIES ) { |
| 113 | $modeDefault = 'categories'; |
| 114 | } elseif ( $mode === CategoryTreeMode::PAGES ) { |
| 115 | $modeDefault = 'pages'; |
| 116 | } else { |
| 117 | $modeDefault = 'all'; |
| 118 | } |
| 119 | |
| 120 | $formDescriptor = [ |
| 121 | 'category' => [ |
| 122 | 'type' => 'title', |
| 123 | 'name' => 'target', |
| 124 | 'label-message' => 'categorytree-category', |
| 125 | 'namespace' => NS_CATEGORY, |
| 126 | 'default' => str_replace( '_', ' ', $this->target ), |
| 127 | ], |
| 128 | |
| 129 | 'mode' => [ |
| 130 | 'type' => 'select', |
| 131 | 'name' => 'mode', |
| 132 | 'label-message' => 'categorytree-mode-label', |
| 133 | 'options-messages' => [ |
| 134 | 'categorytree-mode-categories' => 'categories', |
| 135 | 'categorytree-mode-pages' => 'pages', |
| 136 | 'categorytree-mode-all' => 'all', |
| 137 | ], |
| 138 | 'default' => $modeDefault, |
| 139 | 'nodata' => true, |
| 140 | ], |
| 141 | |
| 142 | 'namespace' => [ |
| 143 | 'type' => 'namespaceselect', |
| 144 | 'name' => 'namespaces', |
| 145 | 'label-message' => 'namespace', |
| 146 | 'all' => '', |
| 147 | ], |
| 148 | ]; |
| 149 | |
| 150 | HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
| 151 | ->setWrapperLegendMsg( 'categorytree-legend' ) |
| 152 | ->setSubmitTextMsg( 'categorytree-go' ) |
| 153 | ->setMethod( 'get' ) |
| 154 | // Strip subpage |
| 155 | ->setTitle( $this->getPageTitle() ) |
| 156 | ->prepareForm() |
| 157 | ->displayForm( false ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Show the category tree |
| 162 | */ |
| 163 | private function executeCategoryTree() { |
| 164 | $output = $this->getOutput(); |
| 165 | CategoryTree::setHeaders( $output ); |
| 166 | |
| 167 | $title = CategoryTree::makeTitle( $this->target ); |
| 168 | if ( !$title || !$title->getArticleID() ) { |
| 169 | $output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeNotice' ], |
| 170 | $this->msg( 'categorytree-not-found' ) |
| 171 | ->plaintextParams( $this->target ) |
| 172 | ->parse() |
| 173 | ) ); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | $parents = $this->tree->renderParents( $title ); |
| 178 | if ( $parents === '' ) { |
| 179 | $parents = $this->msg( 'categorytree-no-parent-categories' )->parse(); |
| 180 | } |
| 181 | |
| 182 | $output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeParents' ], |
| 183 | $this->msg( 'categorytree-parents' )->parse() . |
| 184 | $this->msg( 'colon-separator' )->escaped() . |
| 185 | $parents |
| 186 | ) ); |
| 187 | |
| 188 | $output->addHTML( Html::rawElement( 'div', |
| 189 | [ |
| 190 | 'class' => 'CategoryTreeResult CategoryTreeTag', |
| 191 | 'data-ct-options' => $this->tree->optionManager->getOptionsAsJsStructure(), |
| 192 | ], |
| 193 | $this->tree->renderNode( $title, 1 ) |
| 194 | ) ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Return an array of subpages beginning with $search that this special page will accept. |
| 199 | * |
| 200 | * @param string $search Prefix to search for |
| 201 | * @param int $limit Maximum number of results to return (usually 10) |
| 202 | * @param int $offset Number of results to skip (usually 0) |
| 203 | * @return string[] Matching subpages |
| 204 | */ |
| 205 | public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 206 | $title = Title::newFromText( $search, NS_CATEGORY ); |
| 207 | if ( $title && !$title->inNamespace( NS_CATEGORY ) ) { |
| 208 | // Someone searching for something like "Wikipedia:Foo" |
| 209 | $title = Title::makeTitleSafe( NS_CATEGORY, $search ); |
| 210 | } |
| 211 | if ( !$title ) { |
| 212 | // No prefix suggestion outside of category namespace |
| 213 | return []; |
| 214 | } |
| 215 | $searchEngine = $this->searchEngineFactory->create(); |
| 216 | $searchEngine->setLimitOffset( $limit, $offset ); |
| 217 | // Autocomplete subpage the same as a normal search, but just for categories |
| 218 | $searchEngine->setNamespaces( [ NS_CATEGORY ] ); |
| 219 | $result = $searchEngine->defaultPrefixSearch( $search ); |
| 220 | |
| 221 | // Remove namespace in search suggestion |
| 222 | return array_map( static fn ( Title $t ) => $t->getText(), $result ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @inheritDoc |
| 227 | */ |
| 228 | protected function getGroupName() { |
| 229 | return 'pages'; |
| 230 | } |
| 231 | |
| 232 | } |