2006-07-26 17:12:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-04-17 13:11:25 +00:00
|
|
|
* Special page for the CategoryTree extension, an AJAX based gadget
|
2006-07-26 17:12:30 +00:00
|
|
|
* to display the category structure of a wiki
|
|
|
|
*
|
2010-06-06 15:12:22 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2007-03-13 11:39:12 +00:00
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
2006-07-26 17:12:30 +00:00
|
|
|
* @copyright © 2006 Daniel Kinzler
|
2018-04-13 22:19:35 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2006-07-26 17:12:30 +00:00
|
|
|
*/
|
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
class CategoryTreePage extends SpecialPage {
|
2014-08-17 21:03:48 +00:00
|
|
|
public $target = '';
|
2012-11-21 08:31:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var CategoryTree
|
|
|
|
*/
|
2014-08-17 21:03:48 +00:00
|
|
|
public $tree = null;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2018-04-11 03:47:26 +00:00
|
|
|
public function __construct() {
|
2018-04-11 03:39:03 +00:00
|
|
|
parent::__construct( 'CategoryTree' );
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2012-02-09 01:23:31 +00:00
|
|
|
/**
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param string $name
|
2012-02-09 01:23:31 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-04-11 03:47:26 +00:00
|
|
|
private function getOption( $name ) {
|
2008-06-28 20:13:20 +00:00
|
|
|
global $wgCategoryTreeDefaultOptions;
|
|
|
|
|
2010-11-26 19:09:35 +00:00
|
|
|
if ( $this->tree ) {
|
|
|
|
return $this->tree->getOption( $name );
|
|
|
|
} else {
|
|
|
|
return $wgCategoryTreeDefaultOptions[$name];
|
|
|
|
}
|
2008-06-28 20:13:20 +00:00
|
|
|
}
|
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
/**
|
|
|
|
* Main execution function
|
2017-10-11 18:25:13 +00:00
|
|
|
* @param string|null $par Parameters passed to the page
|
2006-07-26 17:12:30 +00:00
|
|
|
*/
|
2018-04-11 03:47:26 +00:00
|
|
|
public function execute( $par ) {
|
2017-05-30 18:21:54 +00:00
|
|
|
global $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions,
|
|
|
|
$wgCategoryTreeForceHeaders;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
$this->setHeaders();
|
2012-02-09 01:23:31 +00:00
|
|
|
$request = $this->getRequest();
|
|
|
|
if ( $par ) {
|
|
|
|
$this->target = $par;
|
|
|
|
} else {
|
2017-09-27 19:27:00 +00:00
|
|
|
$this->target = $request->getVal( 'target' );
|
|
|
|
if ( $this->target === null ) {
|
|
|
|
$rootcategory = $this->msg( 'rootcategory' );
|
|
|
|
if ( $rootcategory->exists() ) {
|
|
|
|
$this->target = $rootcategory->text();
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 01:23:31 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-11 23:36:53 +00:00
|
|
|
$this->target = trim( $this->target );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2016-10-14 16:11:52 +00:00
|
|
|
$options = [];
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2008-06-28 20:13:20 +00:00
|
|
|
# grab all known options from the request. Normalization is done by the CategoryTree class
|
|
|
|
foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
|
2012-02-09 01:23:31 +00:00
|
|
|
if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) ) {
|
2008-06-30 14:09:47 +00:00
|
|
|
$default = $wgCategoryTreeSpecialPageOptions[$option];
|
2012-02-09 01:23:31 +00:00
|
|
|
}
|
2008-06-30 14:09:47 +00:00
|
|
|
|
2012-02-09 01:23:31 +00:00
|
|
|
$options[$option] = $request->getVal( $option, $default );
|
2008-06-28 20:13:20 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2008-06-28 20:13:20 +00:00
|
|
|
$this->tree = new CategoryTree( $options );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2012-02-09 01:23:31 +00:00
|
|
|
$output = $this->getOutput();
|
|
|
|
$output->addWikiMsg( 'categorytree-header' );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2008-07-01 13:36:50 +00:00
|
|
|
$this->executeInputForm();
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2010-04-20 22:00:34 +00:00
|
|
|
if ( $this->target !== '' && $this->target !== null ) {
|
2012-02-09 01:23:31 +00:00
|
|
|
if ( !$wgCategoryTreeForceHeaders ) {
|
|
|
|
CategoryTree::setHeaders( $output );
|
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
$title = CategoryTree::makeTitle( $this->target );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-11 23:36:53 +00:00
|
|
|
if ( $title && $title->getArticleID() ) {
|
2016-10-14 16:11:52 +00:00
|
|
|
$output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeParents' ] ) );
|
2016-10-13 22:05:03 +00:00
|
|
|
$output->addHTML( $this->msg( 'categorytree-parents' )->parse() );
|
|
|
|
$output->addHTML( $this->msg( 'colon-separator' )->escaped() );
|
2006-08-24 17:12:13 +00:00
|
|
|
|
2008-06-28 20:13:20 +00:00
|
|
|
$parents = $this->tree->renderParents( $title );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2008-03-18 17:38:32 +00:00
|
|
|
if ( $parents == '' ) {
|
2016-10-13 22:05:03 +00:00
|
|
|
$output->addHTML( $this->msg( 'categorytree-no-parent-categories' )->parse() );
|
2008-03-18 17:38:32 +00:00
|
|
|
} else {
|
2012-02-09 01:23:31 +00:00
|
|
|
$output->addHTML( $parents );
|
2008-03-18 17:38:32 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2012-02-09 01:23:31 +00:00
|
|
|
$output->addHTML( Xml::closeElement( 'div' ) );
|
2008-06-28 20:13:20 +00:00
|
|
|
|
2016-10-14 16:11:52 +00:00
|
|
|
$output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeResult' ] ) );
|
2012-02-09 01:23:31 +00:00
|
|
|
$output->addHTML( $this->tree->renderNode( $title, 1 ) );
|
|
|
|
$output->addHTML( Xml::closeElement( 'div' ) );
|
|
|
|
} else {
|
2016-10-14 16:11:52 +00:00
|
|
|
$output->addHTML( Xml::openElement( 'div', [ 'class' => 'CategoryTreeNotice' ] ) );
|
2016-10-13 22:05:03 +00:00
|
|
|
$output->addHTML( $this->msg( 'categorytree-not-found', $this->target )->parse() );
|
2012-02-09 01:23:31 +00:00
|
|
|
$output->addHTML( Xml::closeElement( 'div' ) );
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
/**
|
|
|
|
* Input form for entering a category
|
|
|
|
*/
|
2018-04-11 03:47:26 +00:00
|
|
|
private function executeInputForm() {
|
2012-09-25 14:11:32 +00:00
|
|
|
$namespaces = $this->getRequest()->getVal( 'namespaces', '' );
|
2017-05-30 18:21:54 +00:00
|
|
|
// mode may be overriden by namespaces option
|
2016-02-05 06:45:46 +00:00
|
|
|
$mode = ( $namespaces == '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
|
2016-05-18 16:05:14 +00:00
|
|
|
if ( $mode == CategoryTreeMode::CATEGORIES ) {
|
|
|
|
$modeDefault = 'categories';
|
2017-05-30 18:21:54 +00:00
|
|
|
} elseif ( $mode == CategoryTreeMode::PAGES ) {
|
2016-05-18 16:05:14 +00:00
|
|
|
$modeDefault = 'pages';
|
|
|
|
} else {
|
|
|
|
$modeDefault = 'all';
|
|
|
|
}
|
|
|
|
|
|
|
|
$formDescriptor = [
|
|
|
|
'category' => [
|
|
|
|
'type' => 'title',
|
|
|
|
'name' => 'target',
|
|
|
|
'label-message' => 'categorytree-category',
|
|
|
|
'namespace' => NS_CATEGORY,
|
|
|
|
],
|
|
|
|
|
|
|
|
'mode' => [
|
|
|
|
'type' => 'select',
|
|
|
|
'name' => 'mode',
|
|
|
|
'label-message' => 'categorytree-mode-label',
|
|
|
|
'options-messages' => [
|
|
|
|
'categorytree-mode-categories' => 'categories',
|
|
|
|
'categorytree-mode-pages' => 'pages',
|
|
|
|
'categorytree-mode-all' => 'all',
|
|
|
|
],
|
2016-06-24 16:18:39 +00:00
|
|
|
'default' => $modeDefault,
|
|
|
|
'nodata' => true,
|
2016-05-18 16:05:14 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
'namespace' => [
|
|
|
|
'type' => 'namespaceselect',
|
|
|
|
'name' => 'namespaces',
|
|
|
|
'label-message' => 'namespace',
|
|
|
|
'all' => '',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2018-04-09 01:50:31 +00:00
|
|
|
HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
|
2016-05-18 16:05:14 +00:00
|
|
|
->addHiddenFields( [ 'title' => $this->getPageTitle()->getPrefixedDbKey() ] )
|
|
|
|
->setWrapperLegendMsg( 'categorytree-legend' )
|
|
|
|
->setSubmitTextMsg( 'categorytree-go' )
|
|
|
|
->setMethod( 'get' )
|
|
|
|
->prepareForm()
|
|
|
|
->displayForm( false );
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
2015-05-16 11:20:47 +00:00
|
|
|
|
2015-12-28 22:03:36 +00:00
|
|
|
/**
|
|
|
|
* Return an array of subpages beginning with $search that this special page will accept.
|
|
|
|
*
|
|
|
|
* @param string $search Prefix to search for
|
|
|
|
* @param int $limit Maximum number of results to return (usually 10)
|
|
|
|
* @param int $offset Number of results to skip (usually 0)
|
|
|
|
* @return string[] Matching subpages
|
|
|
|
*/
|
|
|
|
public function prefixSearchSubpages( $search, $limit, $offset ) {
|
|
|
|
$title = Title::newFromText( $search, NS_CATEGORY );
|
|
|
|
if ( $title && $title->getNamespace() !== NS_CATEGORY ) {
|
|
|
|
// Someone searching for something like "Wikipedia:Foo"
|
|
|
|
$title = Title::makeTitleSafe( NS_CATEGORY, $search );
|
|
|
|
}
|
|
|
|
if ( !$title ) {
|
|
|
|
// No prefix suggestion outside of category namespace
|
2016-10-14 16:11:52 +00:00
|
|
|
return [];
|
2015-12-28 22:03:36 +00:00
|
|
|
}
|
|
|
|
// Autocomplete subpage the same as a normal search, but just for categories
|
|
|
|
$prefixSearcher = new TitlePrefixSearch;
|
2016-10-14 16:11:52 +00:00
|
|
|
$result = $prefixSearcher->search( $title->getPrefixedText(), $limit, [ NS_CATEGORY ], $offset );
|
2015-12-28 22:03:36 +00:00
|
|
|
|
|
|
|
return array_map( function ( Title $t ) {
|
|
|
|
// Remove namespace in search suggestion
|
|
|
|
return $t->getText();
|
|
|
|
}, $result );
|
|
|
|
}
|
|
|
|
|
2015-05-16 11:20:47 +00:00
|
|
|
protected function getGroupName() {
|
|
|
|
return 'pages';
|
|
|
|
}
|
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|