mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CategoryTree
synced 2024-11-27 17:51:00 +00:00
Optimize PHP code
* Use ' instead of ". * Use === instead of ==. * Use !== instead of !=. * Use explicit type cast. * Combine array assignment. * Use null coalescing operator. Change-Id: Ic2fe4f62556df77262915b5bcbe1b11a1d907e6d
This commit is contained in:
parent
031e5bf358
commit
ed87adce49
|
@ -61,16 +61,12 @@ class CategoryTree {
|
||||||
// ensure default values and order of options.
|
// ensure default values and order of options.
|
||||||
// Order may become important, it may influence the cache key!
|
// Order may become important, it may influence the cache key!
|
||||||
foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
|
foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
|
||||||
if ( isset( $options[$option] ) ) {
|
$this->mOptions[$option] = $options[$option] ?? $default;
|
||||||
$this->mOptions[$option] = $options[$option];
|
|
||||||
} else {
|
|
||||||
$this->mOptions[$option] = $default;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->mOptions['mode'] = self::decodeMode( $this->mOptions['mode'] );
|
$this->mOptions['mode'] = self::decodeMode( $this->mOptions['mode'] );
|
||||||
|
|
||||||
if ( $this->mOptions['mode'] == CategoryTreeMode::PARENTS ) {
|
if ( $this->mOptions['mode'] === CategoryTreeMode::PARENTS ) {
|
||||||
// namespace filter makes no sense with CategoryTreeMode::PARENTS
|
// namespace filter makes no sense with CategoryTreeMode::PARENTS
|
||||||
$this->mOptions['namespaces'] = false;
|
$this->mOptions['namespaces'] = false;
|
||||||
}
|
}
|
||||||
|
@ -82,7 +78,7 @@ class CategoryTree {
|
||||||
if ( $this->mOptions['namespaces'] ) {
|
if ( $this->mOptions['namespaces'] ) {
|
||||||
# automatically adjust mode to match namespace filter
|
# automatically adjust mode to match namespace filter
|
||||||
if ( count( $this->mOptions['namespaces'] ) === 1
|
if ( count( $this->mOptions['namespaces'] ) === 1
|
||||||
&& $this->mOptions['namespaces'][0] == NS_CATEGORY ) {
|
&& $this->mOptions['namespaces'][0] === NS_CATEGORY ) {
|
||||||
$this->mOptions['mode'] = CategoryTreeMode::CATEGORIES;
|
$this->mOptions['mode'] = CategoryTreeMode::CATEGORIES;
|
||||||
} elseif ( !in_array( NS_FILE, $this->mOptions['namespaces'] ) ) {
|
} elseif ( !in_array( NS_FILE, $this->mOptions['namespaces'] ) ) {
|
||||||
$this->mOptions['mode'] = CategoryTreeMode::PAGES;
|
$this->mOptions['mode'] = CategoryTreeMode::PAGES;
|
||||||
|
@ -104,7 +100,7 @@ class CategoryTree {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isInverse() {
|
private function isInverse() {
|
||||||
return $this->getOption( 'mode' ) == CategoryTreeMode::PARENTS;
|
return $this->getOption( 'mode' ) === CategoryTreeMode::PARENTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,7 +131,7 @@ class CategoryTree {
|
||||||
|
|
||||||
if ( is_numeric( $n ) ) {
|
if ( is_numeric( $n ) ) {
|
||||||
$ns = (int)$n;
|
$ns = (int)$n;
|
||||||
} elseif ( $n == '-' || $n == '_' || $n == '*' || $lower == 'main' ) {
|
} elseif ( $n === '-' || $n === '_' || $n === '*' || $lower === 'main' ) {
|
||||||
$ns = NS_MAIN;
|
$ns = NS_MAIN;
|
||||||
} else {
|
} else {
|
||||||
$ns = $contLang->getNsIndex( $n );
|
$ns = $contLang->getNsIndex( $n );
|
||||||
|
@ -172,15 +168,15 @@ class CategoryTree {
|
||||||
return (int)$mode;
|
return (int)$mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $mode == 'all' ) {
|
if ( $mode === 'all' ) {
|
||||||
$mode = CategoryTreeMode::ALL;
|
$mode = CategoryTreeMode::ALL;
|
||||||
} elseif ( $mode == 'pages' ) {
|
} elseif ( $mode === 'pages' ) {
|
||||||
$mode = CategoryTreeMode::PAGES;
|
$mode = CategoryTreeMode::PAGES;
|
||||||
} elseif ( $mode == 'categories' || $mode == 'sub' ) {
|
} elseif ( $mode === 'categories' || $mode === 'sub' ) {
|
||||||
$mode = CategoryTreeMode::CATEGORIES;
|
$mode = CategoryTreeMode::CATEGORIES;
|
||||||
} elseif ( $mode == 'parents' || $mode == 'super' || $mode == 'inverse' ) {
|
} elseif ( $mode === 'parents' || $mode === 'super' || $mode === 'inverse' ) {
|
||||||
$mode = CategoryTreeMode::PARENTS;
|
$mode = CategoryTreeMode::PARENTS;
|
||||||
} elseif ( $mode == 'default' ) {
|
} elseif ( $mode === 'default' ) {
|
||||||
$mode = $wgCategoryTreeDefaultOptions['mode'];
|
$mode = $wgCategoryTreeDefaultOptions['mode'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,15 +205,15 @@ class CategoryTree {
|
||||||
return ( (int)$value > 0 );
|
return ( (int)$value > 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $value == 'yes' || $value == 'y'
|
if ( $value === 'yes' || $value === 'y'
|
||||||
|| $value == 'true' || $value == 't' || $value == 'on'
|
|| $value === 'true' || $value === 't' || $value === 'on'
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
} elseif ( $value == 'no' || $value == 'n'
|
} elseif ( $value === 'no' || $value === 'n'
|
||||||
|| $value == 'false' || $value == 'f' || $value == 'off'
|
|| $value === 'false' || $value === 'f' || $value === 'off'
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
} elseif ( $value == 'null' || $value == 'default' || $value == 'none' || $value == 'x' ) {
|
} elseif ( $value === 'null' || $value === 'default' || $value === 'none' || $value === 'x' ) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -246,21 +242,21 @@ class CategoryTree {
|
||||||
|
|
||||||
$value = trim( strtolower( $value ) );
|
$value = trim( strtolower( $value ) );
|
||||||
|
|
||||||
if ( $value == 'yes' || $value == 'y'
|
if ( $value === 'yes' || $value === 'y'
|
||||||
|| $value == 'true' || $value == 't' || $value == 'on'
|
|| $value === 'true' || $value === 't' || $value === 'on'
|
||||||
) {
|
) {
|
||||||
return CategoryTreeHidePrefix::ALWAYS;
|
return CategoryTreeHidePrefix::ALWAYS;
|
||||||
} elseif ( $value == 'no' || $value == 'n'
|
} elseif ( $value === 'no' || $value === 'n'
|
||||||
|| $value == 'false' || $value == 'f' || $value == 'off'
|
|| $value === 'false' || $value === 'f' || $value === 'off'
|
||||||
) {
|
) {
|
||||||
return CategoryTreeHidePrefix::NEVER;
|
return CategoryTreeHidePrefix::NEVER;
|
||||||
} elseif ( $value == 'always' ) {
|
} elseif ( $value === 'always' ) {
|
||||||
return CategoryTreeHidePrefix::ALWAYS;
|
return CategoryTreeHidePrefix::ALWAYS;
|
||||||
} elseif ( $value == 'never' ) {
|
} elseif ( $value === 'never' ) {
|
||||||
return CategoryTreeHidePrefix::NEVER;
|
return CategoryTreeHidePrefix::NEVER;
|
||||||
} elseif ( $value == 'auto' ) {
|
} elseif ( $value === 'auto' ) {
|
||||||
return CategoryTreeHidePrefix::AUTO;
|
return CategoryTreeHidePrefix::AUTO;
|
||||||
} elseif ( $value == 'categories' || $value == 'category' || $value == 'smart' ) {
|
} elseif ( $value === 'categories' || $value === 'category' || $value === 'smart' ) {
|
||||||
return CategoryTreeHidePrefix::CATEGORIES;
|
return CategoryTreeHidePrefix::CATEGORIES;
|
||||||
} else {
|
} else {
|
||||||
return $wgCategoryTreeDefaultOptions['hideprefix'];
|
return $wgCategoryTreeDefaultOptions['hideprefix'];
|
||||||
|
@ -284,9 +280,9 @@ class CategoryTree {
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected static function encodeOptions( array $options, $enc ) {
|
protected static function encodeOptions( array $options, $enc ) {
|
||||||
if ( $enc == 'mode' || $enc == '' ) {
|
if ( $enc === 'mode' || $enc === '' ) {
|
||||||
$opt = $options['mode'];
|
$opt = $options['mode'];
|
||||||
} elseif ( $enc == 'json' ) {
|
} elseif ( $enc === 'json' ) {
|
||||||
$opt = FormatJson::encode( $options );
|
$opt = FormatJson::encode( $options );
|
||||||
} else {
|
} else {
|
||||||
throw new Exception( 'Unknown encoding for CategoryTree options: ' . $enc );
|
throw new Exception( 'Unknown encoding for CategoryTree options: ' . $enc );
|
||||||
|
@ -300,7 +296,7 @@ class CategoryTree {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getOptionsAsCacheKey( $depth = null ) {
|
public function getOptionsAsCacheKey( $depth = null ) {
|
||||||
$key = "";
|
$key = '';
|
||||||
|
|
||||||
foreach ( $this->mOptions as $k => $v ) {
|
foreach ( $this->mOptions as $k => $v ) {
|
||||||
if ( is_array( $v ) ) {
|
if ( is_array( $v ) ) {
|
||||||
|
@ -310,7 +306,7 @@ class CategoryTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $depth !== null ) {
|
if ( $depth !== null ) {
|
||||||
$key .= ";depth=" . $depth;
|
$key .= ';depth=' . $depth;
|
||||||
}
|
}
|
||||||
return $key;
|
return $key;
|
||||||
}
|
}
|
||||||
|
@ -402,7 +398,7 @@ class CategoryTree {
|
||||||
public function renderChildren( Title $title, $depth = 1 ) {
|
public function renderChildren( Title $title, $depth = 1 ) {
|
||||||
global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable;
|
global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable;
|
||||||
|
|
||||||
if ( $title->getNamespace() != NS_CATEGORY ) {
|
if ( $title->getNamespace() !== NS_CATEGORY ) {
|
||||||
// Non-categories can't have children. :)
|
// Non-categories can't have children. :)
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
@ -436,8 +432,8 @@ class CategoryTree {
|
||||||
// NOTE: we assume that the $namespaces array contains only integers!
|
// NOTE: we assume that the $namespaces array contains only integers!
|
||||||
// decodeNamepsaces makes it so.
|
// decodeNamepsaces makes it so.
|
||||||
$where['page_namespace'] = $namespaces;
|
$where['page_namespace'] = $namespaces;
|
||||||
} elseif ( $mode != CategoryTreeMode::ALL ) {
|
} elseif ( $mode !== CategoryTreeMode::ALL ) {
|
||||||
if ( $mode == CategoryTreeMode::PAGES ) {
|
if ( $mode === CategoryTreeMode::PAGES ) {
|
||||||
$where['cl_type'] = [ 'page', 'subcat' ];
|
$where['cl_type'] = [ 'page', 'subcat' ];
|
||||||
} else {
|
} else {
|
||||||
$where['cl_type'] = 'subcat';
|
$where['cl_type'] = 'subcat';
|
||||||
|
@ -506,13 +502,13 @@ class CategoryTree {
|
||||||
|
|
||||||
$cat = null;
|
$cat = null;
|
||||||
|
|
||||||
if ( $doCount && $row->page_namespace == NS_CATEGORY ) {
|
if ( $doCount && (int)$row->page_namespace === NS_CATEGORY ) {
|
||||||
$cat = Category::newFromRow( $row, $t );
|
$cat = Category::newFromRow( $row, $t );
|
||||||
}
|
}
|
||||||
|
|
||||||
$s = $this->renderNodeInfo( $t, $cat, $depth - 1 );
|
$s = $this->renderNodeInfo( $t, $cat, $depth - 1 );
|
||||||
|
|
||||||
if ( $row->page_namespace == NS_CATEGORY ) {
|
if ( (int)$row->page_namespace === NS_CATEGORY ) {
|
||||||
$categories .= $s;
|
$categories .= $s;
|
||||||
} else {
|
} else {
|
||||||
$other .= $s;
|
$other .= $s;
|
||||||
|
@ -572,7 +568,7 @@ class CategoryTree {
|
||||||
public function renderNode( Title $title, $children = 0 ) {
|
public function renderNode( Title $title, $children = 0 ) {
|
||||||
global $wgCategoryTreeUseCategoryTable;
|
global $wgCategoryTreeUseCategoryTable;
|
||||||
|
|
||||||
if ( $wgCategoryTreeUseCategoryTable && $title->getNamespace() == NS_CATEGORY
|
if ( $wgCategoryTreeUseCategoryTable && $title->getNamespace() === NS_CATEGORY
|
||||||
&& !$this->isInverse()
|
&& !$this->isInverse()
|
||||||
) {
|
) {
|
||||||
$cat = Category::newFromTitle( $title );
|
$cat = Category::newFromTitle( $title );
|
||||||
|
@ -599,12 +595,12 @@ class CategoryTree {
|
||||||
|
|
||||||
$hideprefix = $this->getOption( 'hideprefix' );
|
$hideprefix = $this->getOption( 'hideprefix' );
|
||||||
|
|
||||||
if ( $hideprefix == CategoryTreeHidePrefix::ALWAYS ) {
|
if ( $hideprefix === CategoryTreeHidePrefix::ALWAYS ) {
|
||||||
$hideprefix = true;
|
$hideprefix = true;
|
||||||
} elseif ( $hideprefix == CategoryTreeHidePrefix::AUTO ) {
|
} elseif ( $hideprefix === CategoryTreeHidePrefix::AUTO ) {
|
||||||
$hideprefix = ( $mode == CategoryTreeMode::CATEGORIES );
|
$hideprefix = ( $mode === CategoryTreeMode::CATEGORIES );
|
||||||
} elseif ( $hideprefix == CategoryTreeHidePrefix::CATEGORIES ) {
|
} elseif ( $hideprefix === CategoryTreeHidePrefix::CATEGORIES ) {
|
||||||
$hideprefix = ( $ns == NS_CATEGORY );
|
$hideprefix = ( $ns === NS_CATEGORY );
|
||||||
} else {
|
} else {
|
||||||
$hideprefix = true;
|
$hideprefix = true;
|
||||||
}
|
}
|
||||||
|
@ -632,11 +628,11 @@ class CategoryTree {
|
||||||
|
|
||||||
$attr = [ 'class' => 'CategoryTreeBullet' ];
|
$attr = [ 'class' => 'CategoryTreeBullet' ];
|
||||||
|
|
||||||
if ( $ns == NS_CATEGORY ) {
|
if ( $ns === NS_CATEGORY ) {
|
||||||
if ( $cat ) {
|
if ( $cat ) {
|
||||||
if ( $mode == CategoryTreeMode::CATEGORIES ) {
|
if ( $mode === CategoryTreeMode::CATEGORIES ) {
|
||||||
$count = intval( $cat->getSubcatCount() );
|
$count = intval( $cat->getSubcatCount() );
|
||||||
} elseif ( $mode == CategoryTreeMode::PAGES ) {
|
} elseif ( $mode === CategoryTreeMode::PAGES ) {
|
||||||
$count = intval( $cat->getPageCount() ) - intval( $cat->getFileCount() );
|
$count = intval( $cat->getPageCount() ) - intval( $cat->getFileCount() );
|
||||||
} else {
|
} else {
|
||||||
$count = intval( $cat->getPageCount() );
|
$count = intval( $cat->getPageCount() );
|
||||||
|
@ -646,16 +642,16 @@ class CategoryTree {
|
||||||
$bullet = '';
|
$bullet = '';
|
||||||
$attr['class'] = 'CategoryTreeEmptyBullet';
|
$attr['class'] = 'CategoryTreeEmptyBullet';
|
||||||
} else {
|
} else {
|
||||||
$linkattr = [];
|
$linkattr = [
|
||||||
|
'class' => 'CategoryTreeToggle',
|
||||||
|
'data-ct-title' => $key,
|
||||||
|
];
|
||||||
|
|
||||||
$linkattr[ 'class' ] = "CategoryTreeToggle";
|
if ( $children === 0 ) {
|
||||||
$linkattr['data-ct-title'] = $key;
|
$linkattr['data-ct-state'] = 'collapsed';
|
||||||
|
|
||||||
if ( $children == 0 ) {
|
|
||||||
$linkattr[ 'data-ct-state' ] = 'collapsed';
|
|
||||||
} else {
|
} else {
|
||||||
$linkattr[ 'data-ct-loaded' ] = true;
|
$linkattr['data-ct-loaded'] = true;
|
||||||
$linkattr[ 'data-ct-state' ] = 'expanded';
|
$linkattr['data-ct-state'] = 'expanded';
|
||||||
}
|
}
|
||||||
|
|
||||||
$bullet = Html::element( 'span', $linkattr ) . ' ';
|
$bullet = Html::element( 'span', $linkattr ) . ' ';
|
||||||
|
@ -677,13 +673,13 @@ class CategoryTree {
|
||||||
'div',
|
'div',
|
||||||
[
|
[
|
||||||
'class' => 'CategoryTreeChildren',
|
'class' => 'CategoryTreeChildren',
|
||||||
'style' => $children > 0 ? "display:block" : "display:none"
|
'style' => $children > 0 ? 'display:block' : 'display:none'
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( $ns == NS_CATEGORY && $children > 0 ) {
|
if ( $ns === NS_CATEGORY && $children > 0 ) {
|
||||||
$children = $this->renderChildren( $title, $children );
|
$children = $this->renderChildren( $title, $children );
|
||||||
if ( $children == '' ) {
|
if ( $children === '' ) {
|
||||||
switch ( $mode ) {
|
switch ( $mode ) {
|
||||||
case CategoryTreeMode::CATEGORIES:
|
case CategoryTreeMode::CATEGORIES:
|
||||||
$msg = 'categorytree-no-subcategories';
|
$msg = 'categorytree-no-subcategories';
|
||||||
|
@ -783,7 +779,7 @@ class CategoryTree {
|
||||||
# The title must be in the category namespace
|
# The title must be in the category namespace
|
||||||
# Ignore a leading Category: if there is one
|
# Ignore a leading Category: if there is one
|
||||||
$t = Title::newFromText( $title, NS_CATEGORY );
|
$t = Title::newFromText( $title, NS_CATEGORY );
|
||||||
if ( !$t || $t->getNamespace() != NS_CATEGORY || $t->getInterwiki() != '' ) {
|
if ( !$t || $t->getNamespace() !== NS_CATEGORY || $t->getInterwiki() !== '' ) {
|
||||||
// If we were given something like "Wikipedia:Foo" or "Template:",
|
// If we were given something like "Wikipedia:Foo" or "Template:",
|
||||||
// try it again but forced.
|
// try it again but forced.
|
||||||
$title = "Category:$title";
|
$title = "Category:$title";
|
||||||
|
|
|
@ -118,10 +118,10 @@ class CategoryTreePage extends SpecialPage {
|
||||||
private function executeInputForm() {
|
private function executeInputForm() {
|
||||||
$namespaces = $this->getRequest()->getVal( 'namespaces', '' );
|
$namespaces = $this->getRequest()->getVal( 'namespaces', '' );
|
||||||
// mode may be overriden by namespaces option
|
// mode may be overriden by namespaces option
|
||||||
$mode = ( $namespaces == '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
|
$mode = ( $namespaces === '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
|
||||||
if ( $mode == CategoryTreeMode::CATEGORIES ) {
|
if ( $mode === CategoryTreeMode::CATEGORIES ) {
|
||||||
$modeDefault = 'categories';
|
$modeDefault = 'categories';
|
||||||
} elseif ( $mode == CategoryTreeMode::PAGES ) {
|
} elseif ( $mode === CategoryTreeMode::PAGES ) {
|
||||||
$modeDefault = 'pages';
|
$modeDefault = 'pages';
|
||||||
} else {
|
} else {
|
||||||
$modeDefault = 'all';
|
$modeDefault = 'all';
|
||||||
|
@ -183,7 +183,7 @@ class CategoryTreePage extends SpecialPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
$parents = $this->tree->renderParents( $title );
|
$parents = $this->tree->renderParents( $title );
|
||||||
if ( $parents == '' ) {
|
if ( $parents === '' ) {
|
||||||
$parents = $this->msg( 'categorytree-no-parent-categories' )->parse();
|
$parents = $this->msg( 'categorytree-no-parent-categories' )->parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -235,7 +235,7 @@ class Hooks implements
|
||||||
* @return bool|void True or no return value to continue or false to abort
|
* @return bool|void True or no return value to continue or false to abort
|
||||||
*/
|
*/
|
||||||
public function onArticleFromTitle( $title, &$article, $context ) {
|
public function onArticleFromTitle( $title, &$article, $context ) {
|
||||||
if ( $title->getNamespace() == NS_CATEGORY ) {
|
if ( $title->getNamespace() === NS_CATEGORY ) {
|
||||||
$article = new CategoryTreeCategoryPage( $title );
|
$article = new CategoryTreeCategoryPage( $title );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -327,10 +327,7 @@ class Hooks implements
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cat = null;
|
$cat = $specialPage->categoryTreeCategories[$catTitle->getDBkey()] ?? null;
|
||||||
if ( isset( $specialPage->categoryTreeCategories[$catTitle->getDBkey()] ) ) {
|
|
||||||
$cat = $specialPage->categoryTreeCategories[$catTitle->getDBkey()];
|
|
||||||
}
|
|
||||||
|
|
||||||
$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
|
$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue