Suppress page translations and render only the default page(s)

This feature add an option `notranslations` that allows the category
tree only to show the default pages and not include their translations
equivalents on the tree. For example let's say we have a page (Test_page)
that was created in a default language "en" in a category TestCat, and has
translations listed: Test_page/es, Test_page/de, Test_page/ar etc.

Doing `<categorytree notranslations="on">TestCat</categorytree> will render
only the Test_page page with it's link to the page on the tree and nothing
more.

Note: If page "Foo/hu" exist but "Foo" doesn't, it should include "Foo/hu"
      in the category tree. In most case though, a base page will exist.
      Also, the feature is only available if Extension:Translate is made
      available on that wiki.

TODO: If "notranslations" is used along side with "showcount", consider
      subtracting the number of pages suppressed from the total page count
      in the category tree for consistency.

Bug: T229265
Change-Id: Ib4e7ab6ad98c05857cda1cd98bbc19e0504677bb
This commit is contained in:
Derick Alangi 2019-07-29 20:19:36 +01:00
parent eb1d495a25
commit 87ddb7f735
2 changed files with 33 additions and 1 deletions

View file

@ -116,7 +116,8 @@
"mode": null,
"hideprefix": null,
"showcount": false,
"namespaces": false
"namespaces": false,
"notranslations": false
},
"CategoryTreeCategoryPageMode": 0,
"CategoryTreeCategoryPageOptions": {

View file

@ -390,6 +390,7 @@ class CategoryTree {
* Returns a string with an HTML representation of the children of the given category.
* @param Title $title
* @param int $depth
* @suppress PhanUndeclaredClassMethod,PhanUndeclaredClassInstanceof
* @return string
*/
public function renderChildren( Title $title, $depth = 1 ) {
@ -456,8 +457,38 @@ class CategoryTree {
# collect categories separately from other pages
$categories = '';
$other = '';
$suppressTranslations = self::decodeBoolean(
$this->getOption( 'notranslations' )
) && ExtensionRegistry::getInstance()->isLoaded( 'Translate' );
if ( $suppressTranslations ) {
$lb = new LinkBatch();
foreach ( $res as $row ) {
$title = Title::newFromText( $row->page_title, $row->page_namespace );
// Page name could have slashes, check the subpage for valid language built-in codes
$isValidLangCode = $title->getSubpageText();
if ( $title !== null && $isValidLangCode ) {
$lb->addObj( $title->getBaseTitle() );
}
}
$lb->execute();
}
foreach ( $res as $row ) {
if ( $suppressTranslations ) {
$title = Title::newFromRow( $row );
$baseTitle = $title->getBaseTitle();
$page = \TranslatablePage::isTranslationPage( $title );
if ( ( $page instanceof \TranslatablePage ) && $baseTitle->exists() ) {
// T229265: Render only the default pages created and ignore their
// translations.
continue;
}
}
# NOTE: in inverse mode, the page record may be null, because we use a right join.
# happens for categories with no category page (red cat links)
if ( $inverse && $row->page_title === null ) {