| 1 |
<?php |
| 2 |
|
| 3 |
r |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace MediaWiki\Extension\CategoryTree; |
| 26 |
|
| 27 |
use MediaWiki\Html\Html; |
| 28 |
use MediaWiki\HTMLForm\HTMLForm; |
| 29 |
use MediaWiki\SpecialPage\SpecialPage; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use SearchEngineFactory; |
| 32 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class CategoryTreePage extends SpecialPage { |
| 39 |
public string $target = ''; |
| 40 |
private IConnectionProvider $dbProvider; |
| 41 |
private SearchEngineFactory $searchEngineFactory; |
| 42 |
public ?CategoryTree $tree = null; |
| 43 |
|
| 44 |
public function __construct( |
| 45 |
IConnectionProvider $dbProvider, |
| 46 |
SearchEngineFactory $searchEngineFactory |
| 47 |
) { |
| 48 |
parent::__construct( 'CategoryTree' ); |
| 49 |
$this->dbProvider = $dbProvider; |
| 50 |
$this->searchEngineFactory = $searchEngineFactory; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private function getOption( string $name ) { |
| 58 |
if ( $this->tree ) { |
| 59 |
return $this->tree->optionManager->getOption( $name ); |
| 60 |
} else { |
| 61 |
return $this->getConfig()->get( 'CategoryTreeDefaultOptions' )[$name]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function execute( $par ) { |
| 70 |
$this->setHeaders(); |
| 71 |
$this->addHelpLink( 'Extension:CategoryTree' ); |
| 72 |
$request = $this->getRequest(); |
| 73 |
if ( $par ) { |
| 74 |
$this->target = trim( $par ); |
| 75 |
} else { |
| 76 |
$this->target = trim( $request->getText( 'target' ) ); |
| 77 |
if ( $this->target === '' ) { |
| 78 |
$rootcategory = $this->msg( 'rootcategory' ); |
| 79 |
if ( $rootcategory->exists() ) { |
| 80 |
$this->target = $rootcategory->text(); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$options = []; |
| 86 |
$config = $this->getConfig(); |
| 87 |
|
| 88 |
|
| 89 |
$categoryTreeDefaultOptions = $config->get( 'CategoryTreeDefaultOptions' ); |
| 90 |
$categoryTreeSpecialPageOptions = $config->get( 'CategoryTreeSpecialPageOptions' ); |
| 91 |
foreach ( $categoryTreeDefaultOptions as $option => $default ) { |
| 92 |
if ( isset( $categoryTreeSpecialPageOptions[$option] ) ) { |
| 93 |
$default = $categoryTreeSpecialPageOptions[$option]; |
| 94 |
} |
| 95 |
|
| 96 |
$options[$option] = $request->getVal( $option, $default ); |
| 97 |
} |
| 98 |
|
| 99 |
$this->tree = new CategoryTree( $options, $config, $this->dbProvider, $this->getLinkRenderer() ); |
| 100 |
|
| 101 |
$this->getOutput()->addWikiMsg( 'categorytree-header' ); |
| 102 |
|
| 103 |
$this->executeInputForm(); |
| 104 |
|
| 105 |
if ( $this->target !== '' ) { |
| 106 |
$this->executeCategoryTree(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private function executeInputForm() { |
| 114 |
$namespaces = $this->getRequest()->getRawVal( 'namespaces' ) ?? ''; |
| 115 |
|
| 116 |
$mode = ( $namespaces === '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL ); |
| 117 |
if ( $mode === CategoryTreeMode::CATEGORIES ) { |
| 118 |
$modeDefault = 'categories'; |
| 119 |
} elseif ( $mode === CategoryTreeMode::PAGES ) { |
| 120 |
$modeDefault = 'pages'; |
| 121 |
} else { |
| 122 |
$modeDefault = 'all'; |
| 123 |
} |
| 124 |
|
| 125 |
$formDescriptor = [ |
| 126 |
'category' => [ |
| 127 |
'type' => 'title', |
| 128 |
'name' => 'target', |
| 129 |
'label-message' => 'categorytree-category', |
| 130 |
'namespace' => NS_CATEGORY, |
| 131 |
'default' => str_replace( '_', ' ', $this->target ), |
| 132 |
], |
| 133 |
|
| 134 |
'mode' => [ |
| 135 |
'type' => 'select', |
| 136 |
'name' => 'mode', |
| 137 |
'label-message' => 'categorytree-mode-label', |
| 138 |
'options-messages' => [ |
| 139 |
'categorytree-mode-categories' => 'categories', |
| 140 |
'categorytree-mode-pages' => 'pages', |
| 141 |
'categorytree-mode-all' => 'all', |
| 142 |
], |
| 143 |
'default' => $modeDefault, |
| 144 |
'nodata' => true, |
| 145 |
], |
| 146 |
|
| 147 |
'namespace' => [ |
| 148 |
'type' => 'namespaceselect', |
| 149 |
'name' => 'namespaces', |
| 150 |
'label-message' => 'namespace', |
| 151 |
'all' => '', |
| 152 |
], |
| 153 |
]; |
| 154 |
|
| 155 |
HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
| 156 |
->setWrapperLegendMsg( 'categorytree-legend' ) |
| 157 |
->setSubmitTextMsg( 'categorytree-go' ) |
| 158 |
->setMethod( 'get' ) |
| 159 |
|
| 160 |
->setTitle( $this->getPageTitle() ) |
| 161 |
->prepareForm() |
| 162 |
->displayForm( false ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function executeCategoryTree() { |
| 169 |
$output = $this->getOutput(); |
| 170 |
CategoryTree::setHeaders( $output ); |
| 171 |
|
| 172 |
$title = CategoryTree::makeTitle( $this->target ); |
| 173 |
if ( !$title || !$title->getArticleID() ) { |
| 174 |
$output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeNotice' ], |
| 175 |
$this->msg( 'categorytree-not-found' ) |
| 176 |
->plaintextParams( $this->target ) |
| 177 |
->parse() |
| 178 |
) ); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
$parents = $this->tree->renderParents( $title ); |
| 183 |
if ( $parents === '' ) { |
| 184 |
$parents = $this->msg( 'categorytree-no-parent-categories' )->parse(); |
| 185 |
} |
| 186 |
|
| 187 |
$output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeParents' ], |
| 188 |
$this->msg( 'categorytree-parents' )->parse() . |
| 189 |
$this->msg( 'colon-separator' )->escaped() . |
| 190 |
$parents |
| 191 |
) ); |
| 192 |
|
| 193 |
$output->addHTML( Html::rawElement( 'div', |
| 194 |
[ |
| 195 |
'class' => 'CategoryTreeResult CategoryTreeTag', |
| 196 |
'data-ct-mode' => $this->tree->optionManager->getOption( 'mode' ), |
| 197 |
'data-ct-options' => $this->tree->optionManager->getOptionsAsJsStructure(), |
| 198 |
], |
| 199 |
$this->tree->renderNode( $title, 1 ) |
| 200 |
) ); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 212 |
$title = Title::newFromText( $search, NS_CATEGORY ); |
| 213 |
if ( $title && !$title->inNamespace( NS_CATEGORY ) ) { |
| 214 |
|
| 215 |
$title = Title::makeTitleSafe( NS_CATEGORY, $search ); |
| 216 |
} |
| 217 |
if ( !$title ) { |
| 218 |
|
| 219 |
return []; |
| 220 |
} |
| 221 |
$searchEngine = $this->searchEngineFactory->create(); |
| 222 |
$searchEngine->setLimitOffset( $limit, $offset ); |
| 223 |
|
| 224 |
$searchEngine->setNamespaces( [ NS_CATEGORY ] ); |
| 225 |
$result = $searchEngine->defaultPrefixSearch( $search ); |
| 226 |
|
| 227 |
return array_map( static function ( Title $t ) { |
| 228 |
|
| 229 |
return $t->getText(); |
| 230 |
}, $result ); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
protected function getGroupName() { |
| 237 |
return 'pages'; |
| 238 |
} |
| 239 |
|
| 240 |
} |