jenkins-bot has submitted this change and it was merged.
Change subject: Refactor babel boxes into classes with common interface
......................................................................
Refactor babel boxes into classes with common interface
Change-Id: Ibed6e8952617f2d7c12e08dfb9aa79a8a927b14f
---
M Babel.class.php
A BabelBox/BabelBox.php
A BabelBox/LanguageBabelBox.php
A BabelBox/NotBabelBox.php
A BabelBox/NullBabelBox.php
M extension.json
6 files changed, 339 insertions(+), 184 deletions(-)
Approvals:
Nikerabbit: Checked; Looks good to me, approved
jenkins-bot: Verified
diff --git a/Babel.class.php b/Babel.class.php
index c681978..ca86574 100644
--- a/Babel.class.php
+++ b/Babel.class.php
@@ -130,40 +130,51 @@
$template = wfMessage( 'babel-template', $name
)->inContentLanguage()->text();
if ( $name === '' ) {
- return '';
+ $box = new NullBabelBox();
} elseif ( $components !== false ) {
// Valid parameter syntax (with lowercase language
code), babel box
- return self::mGenerateBox( $components['code'],
$components['level'] )
- . self::mGenerateCategories(
- $components['code'],
- $components['level'],
- $createCategories
- );
+ $box = new LanguageBabelBox(
+ self::$title,
+ $components['code'],
+ $components['level'],
+ $createCategories
+ );
} elseif ( self::mPageExists( $template ) ) {
// Check for an existing template
$templateParameters[0] = $template;
$template = implode( '|', $templateParameters );
- return self::mGenerateNotaBox(
$parser->replaceVariables( "{{{$template}}}" ) );
+ $box = new NotBabelBox(
+ self::$title->getPageLanguage()->getDir(),
+ $parser->replaceVariables( "{{{$template}}}" )
+ );
} elseif ( self::mValidTitle( $template ) ) {
// Non-existing page, so try again as a babel box,
// with converting the code to lowercase
$components2 = self::mParseParameter( $name, /* code to
lowercase */
true );
if ( $components2 !== false ) {
- return self::mGenerateBox(
$components2['code'], $components2['level'] )
- . self::mGenerateCategories(
- $components2['code'],
- $components2['level'],
- $createCategories
- );
+ $box = new LanguageBabelBox(
+ self::$title,
+ $components2['code'],
+ $components2['level'],
+ $createCategories
+ );
} else {
// Non-existent page and invalid parameter
syntax, red link.
- return self::mGenerateNotaBox( '[[' . $template
. ']]' );
+ $box = new NotBabelBox(
+
self::$title->getPageLanguage()->getDir(),
+ '[[' . $template . ']]'
+ );
}
} else {
// Invalid title, output raw.
- return self::mGenerateNotaBox( $template );
+ $box = new NotBabelBox(
+ self::$title->getPageLanguage()->getDir(),
+ $template
+ );
}
+
+ return $box->render();
}
/**
@@ -253,172 +264,6 @@
}
/**
- * Generate an inner item which is not a babel box.
- *
- * @param string $content What's inside the box, in wikitext format.
- * @return string A single non-babel box, in wikitext format.
- */
- protected static function mGenerateNotaBox( $content ) {
- $dir_head = self::$title->getPageLanguage()->getDir();
- $notabox = <<<EOT
-<div class="mw-babel-notabox" dir="$dir_head">$content</div>
-EOT;
-
- return $notabox;
- }
-
- /**
- * Generate a babel box for the given language and level.
- *
- * @param string $code Language code to use.
- * @param string|int $level Level of ability to use.
- * @return string A single babel box, in wikitext format.
- */
- protected static function mGenerateBox( $code, $level ) {
- $lang = wfBCP47( $code );
- $portal = wfMessage( 'babel-portal', $code
)->inContentLanguage()->plain();
- if ( $portal !== '' ) {
- $portal = "[[$portal|$lang]]";
- } else {
- $portal = $lang;
- }
- $header = "$portal<span
class=\"mw-babel-box-level-$level\">-$level</span>";
-
- $code = strtolower( $code );
- $name = BabelLanguageCodes::getName( $code );
- $code = BabelLanguageCodes::getCode( $code );
- $text = self::mGetText( $name, $code, $level );
-
- $dir_current = Language::factory( $code )->getDir();
-
- $spacing = Babel::mCssAttrib( 'border-spacing',
'babel-cellspacing', true );
- $padding = Babel::mCssAttrib( 'padding', 'babel-cellpadding',
true );
-
- if ( $spacing === '' ) {
- $style = ( $padding === '' ) ? '' : ( 'style="' .
$padding . '"' );
- } else {
- $style = ( $padding === '' ) ?
- 'style="' . $spacing . '"' :
- 'style="' . $padding . ' ' . $spacing . '"';
- }
-
- $dir_head = self::$title->getPageLanguage()->getDir();
-
- $box = <<<EOT
-<div class="mw-babel-box mw-babel-box-$level" dir="$dir_head">
-{|$style
-! dir="$dir_head" | $header
-| dir="$dir_current" lang="$lang" | $text
-|}
-</div>
-EOT;
-
- return $box;
- }
-
- /**
- * Get the text to display in the language box for specific language and
- * level.
- *
- * @param string $name
- * @param string $language Language code of language to use.
- * @param string $level Level to use.
- * @return string Text for display, in wikitext format.
- */
- protected static function mGetText( $name, $language, $level ) {
- global $wgBabelMainCategory, $wgBabelCategoryNames;
-
- if ( $wgBabelCategoryNames[$level] === false ) {
- $categoryLevel = self::$title->getFullText();
- } else {
- $categoryLevel = ':Category:' .
- self::mReplaceCategoryVariables(
$wgBabelCategoryNames[$level], $language );
- }
-
- if ( $wgBabelMainCategory === false ) {
- $categoryMain = self::$title->getFullText();
- } else {
- $categoryMain = ':Category:' .
- self::mReplaceCategoryVariables(
$wgBabelMainCategory, $language );
- }
-
- // Give grep a chance to find the usages:
- // babel-0-n, babel-1-n, babel-2-n, babel-3-n, babel-4-n,
babel-5-n, babel-N-n
- $text = wfMessage( "babel-$level-n",
- $categoryLevel, $categoryMain, '',
self::$title->getDBkey()
- )->inLanguage( $language )->text();
-
- $fallbackLanguage = Language::getFallbackfor( $language );
- $fallback = wfMessage( "babel-$level-n",
- $categoryLevel, $categoryMain, '',
self::$title->getDBkey()
- )->inLanguage( $fallbackLanguage ? $fallbackLanguage :
$language )->text();
-
- // Give grep a chance to find the usages:
- // babel-0, babel-1, babel-2, babel-3, babel-4, babel-5, babel-N
- if ( $text == $fallback ) {
- $text = wfMessage( "babel-$level",
- $categoryLevel, $categoryMain, $name,
self::$title->getDBkey()
- )->inLanguage( $language )->text();
- }
-
- return $text;
- }
-
- /**
- * Generate categories for the given language and level.
- *
- * @param string $code Language code to use.
- * @param string|int $level Level of ability to use.
- * @param bool $createCategories If true, creates non existing
categories;
- * otherwise, doesn't create them.
- * @return string Wikitext to add categories.
- */
- protected static function mGenerateCategories( $code, $level,
$createCategories = true ) {
- global $wgBabelMainCategory, $wgBabelCategoryNames;
-
- $r = '';
-
- # Add main category
- if ( $wgBabelMainCategory !== false ) {
- $category = self::mReplaceCategoryVariables(
$wgBabelMainCategory, $code );
- $r .= "[[Category:$category|$level]]";
- if ( $createCategories ) {
- BabelAutoCreate::create( $category, $code );
- }
- }
-
- # Add level category
- if ( $wgBabelCategoryNames[$level] !== false ) {
- $category = self::mReplaceCategoryVariables(
$wgBabelCategoryNames[$level], $code );
- $r .= "[[Category:$category]]";
- if ( $createCategories ) {
- BabelAutoCreate::create( $category, $code,
$level );
- }
- }
-
- return $r;
- }
-
- /**
- * Replace the placeholder variables from the category names
configurtion
- * array with actual values.
- *
- * @param string $category Category name (containing variables).
- * @param string $code Language code of category.
- * @return string Category name with variables replaced.
- */
- protected static function mReplaceCategoryVariables( $category, $code )
{
- global $wgLanguageCode;
- $category = strtr( $category, [
- '%code%' => $code,
- '%wikiname%' => BabelLanguageCodes::getName( $code,
$wgLanguageCode ),
- '%nativename%' => BabelLanguageCodes::getName( $code )
- ] );
-
- return $category;
- }
-
- /**
* Determine a CSS attribute, such as "border-spacing", from a
localizeable message.
*
* @param string $name Name of CSS attribute.
@@ -428,7 +273,7 @@
* @todo Move this function to a more appropriate place, likely outside
the class.
* @return Message|string
*/
- protected static function mCssAttrib( $name, $key,
$assumeNumbersArePixels = false ) {
+ public static function mCssAttrib( $name, $key, $assumeNumbersArePixels
= false ) {
$value = wfMessage( $key )->inContentLanguage();
if ( $value->isDisabled() ) {
$value = '';
diff --git a/BabelBox/BabelBox.php b/BabelBox/BabelBox.php
new file mode 100644
index 0000000..474310a
--- /dev/null
+++ b/BabelBox/BabelBox.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Contains interface code.
+ *
+ * @file
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
+ */
+
+/**
+ * Interface for babel boxes.
+ */
+interface BabelBox {
+
+ /**
+ * Return the babel box code.
+ *
+ * @return string HTML
+ */
+ public function render();
+
+}
diff --git a/BabelBox/LanguageBabelBox.php b/BabelBox/LanguageBabelBox.php
new file mode 100644
index 0000000..5394985
--- /dev/null
+++ b/BabelBox/LanguageBabelBox.php
@@ -0,0 +1,207 @@
+<?php
+/**
+ * Contains code for language boxes.
+ *
+ * @file
+ * @author Robert Leverington
+ * @author Robin Pepermans
+ * @author Niklas Laxström
+ * @author Brian Wolff
+ * @author Purodha Blissenbach
+ * @author Sam Reed
+ * @author Siebrand Mazeland
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
+ */
+
+/**
+ * Class for babel language boxes.
+ */
+class LanguageBabelBox implements BabelBox {
+
+ /**
+ * @var Title
+ */
+ private $title;
+
+ /**
+ * @var string
+ */
+ private $code;
+
+ /**
+ * @var string
+ */
+ private $level;
+
+ /**
+ * @var bool
+ */
+ private $createCategories;
+
+ /**
+ * Construct a babel box for the given language and level.
+ *
+ * @param Title $title
+ * @param string $code Language code to use.
+ * @param string|int $level Level of ability to use.
+ * @param bool $createCategories If true, creates non existing
categories;
+ * otherwise, doesn't create them.
+ */
+ public function __construct( Title $title, $code, $level,
$createCategories = true ) {
+ $this->title = $title;
+ $this->code = $code;
+ $this->level = $level;
+ $this->createCategories = $createCategories;
+ }
+
+ /**
+ * Return the babel box code.
+ *
+ * @return string A babel box for the given language and level.
+ */
+ public function render() {
+ $code = $this->code;
+
+ $lang = wfBCP47( $code );
+ $portal = wfMessage( 'babel-portal', $code
)->inContentLanguage()->plain();
+ if ( $portal !== '' ) {
+ $portal = "[[$portal|$lang]]";
+ } else {
+ $portal = $lang;
+ }
+ $header = "$portal<span
class=\"mw-babel-box-level-{$this->level}\">-{$this->level}</span>";
+
+ $code = strtolower( $code );
+ $name = BabelLanguageCodes::getName( $code );
+ $code = BabelLanguageCodes::getCode( $code );
+ $text = self::getText( $this->title, $name, $code, $this->level
);
+
+ $dir_current = Language::factory( $code )->getDir();
+
+ $spacing = Babel::mCssAttrib( 'border-spacing',
'babel-cellspacing', true );
+ $padding = Babel::mCssAttrib( 'padding', 'babel-cellpadding',
true );
+
+ if ( $spacing === '' ) {
+ $style = ( $padding === '' ) ? '' : ( 'style="' .
$padding . '"' );
+ } else {
+ $style = ( $padding === '' ) ?
+ 'style="' . $spacing . '"' :
+ 'style="' . $padding . ' ' . $spacing . '"';
+ }
+
+ $dir_head = $this->title->getPageLanguage()->getDir();
+
+ $box = <<<EOT
+<div class="mw-babel-box mw-babel-box-{$this->level}" dir="$dir_head">
+{|$style
+! dir="$dir_head" | $header
+| dir="$dir_current" lang="$lang" | $text
+|}
+</div>
+EOT;
+
+ $box .= $this->generateCategories();
+
+ return $box;
+ }
+
+ /**
+ * Get the text to display in the language box for specific language and
+ * level.
+ *
+ * @param Title $title
+ * @param string $name
+ * @param string $language Language code of language to use.
+ * @param string $level Level to use.
+ * @return string Text for display, in wikitext format.
+ */
+ private static function getText( Title $title, $name, $language, $level
) {
+ global $wgBabelMainCategory, $wgBabelCategoryNames;
+
+ if ( $wgBabelCategoryNames[$level] === false ) {
+ $categoryLevel = $title->getFullText();
+ } else {
+ $categoryLevel = ':Category:' .
+ self::replaceCategoryVariables(
$wgBabelCategoryNames[$level], $language );
+ }
+
+ if ( $wgBabelMainCategory === false ) {
+ $categoryMain = $title->getFullText();
+ } else {
+ $categoryMain = ':Category:' .
+ self::replaceCategoryVariables(
$wgBabelMainCategory, $language );
+ }
+
+ // Give grep a chance to find the usages:
+ // babel-0-n, babel-1-n, babel-2-n, babel-3-n, babel-4-n,
babel-5-n, babel-N-n
+ $text = wfMessage( "babel-$level-n",
+ $categoryLevel, $categoryMain, '', $title->getDBkey()
+ )->inLanguage( $language )->text();
+
+ $fallbackLanguage = Language::getFallbackfor( $language );
+ $fallback = wfMessage( "babel-$level-n",
+ $categoryLevel, $categoryMain, '', $title->getDBkey()
+ )->inLanguage( $fallbackLanguage ? $fallbackLanguage :
$language )->text();
+
+ // Give grep a chance to find the usages:
+ // babel-0, babel-1, babel-2, babel-3, babel-4, babel-5, babel-N
+ if ( $text == $fallback ) {
+ $text = wfMessage( "babel-$level",
+ $categoryLevel, $categoryMain, $name,
$title->getDBkey()
+ )->inLanguage( $language )->text();
+ }
+
+ return $text;
+ }
+
+ /**
+ * Generate categories for the language box.
+ *
+ * @return string Wikitext to add categories.
+ */
+ private function generateCategories() {
+ global $wgBabelMainCategory, $wgBabelCategoryNames;
+
+ $r = '';
+
+ # Add main category
+ if ( $wgBabelMainCategory !== false ) {
+ $category = self::replaceCategoryVariables(
$wgBabelMainCategory, $this->code );
+ $r .= "[[Category:$category|{$this->level}]]";
+ if ( $this->createCategories ) {
+ BabelAutoCreate::create( $category, $this->code
);
+ }
+ }
+
+ # Add level category
+ if ( $wgBabelCategoryNames[$this->level] !== false ) {
+ $category = self::replaceCategoryVariables(
$wgBabelCategoryNames[$this->level], $this->code );
+ $r .= "[[Category:$category]]";
+ if ( $this->createCategories ) {
+ BabelAutoCreate::create( $category,
$this->code, $this->level );
+ }
+ }
+
+ return $r;
+ }
+
+ /**
+ * Replace the placeholder variables from the category names
configurtion
+ * array with actual values.
+ *
+ * @param string $category Category name (containing variables).
+ * @param string $code Language code of category.
+ * @return string Category name with variables replaced.
+ */
+ private static function replaceCategoryVariables( $category, $code ) {
+ global $wgLanguageCode;
+ $category = strtr( $category, [
+ '%code%' => $code,
+ '%wikiname%' => BabelLanguageCodes::getName( $code,
$wgLanguageCode ),
+ '%nativename%' => BabelLanguageCodes::getName( $code )
+ ] );
+
+ return $category;
+ }
+
+}
diff --git a/BabelBox/NotBabelBox.php b/BabelBox/NotBabelBox.php
new file mode 100644
index 0000000..f251c22
--- /dev/null
+++ b/BabelBox/NotBabelBox.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Contains code for inner items which are not babel boxes.
+ *
+ * @file
+ * @author Robert Leverington
+ * @author Robin Pepermans
+ * @author Niklas Laxström
+ * @author Brian Wolff
+ * @author Purodha Blissenbach
+ * @author Sam Reed
+ * @author Siebrand Mazeland
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
+ */
+
+/**
+ * Class for inner items which are not babel boxes.
+ */
+class NotBabelBox implements BabelBox {
+
+ /**
+ * @var string
+ */
+ private $dir;
+
+ /**
+ * @var string
+ */
+ private $content;
+
+ /**
+ * Construct a non-babel box.
+ *
+ * @param string $dir HTML 'dir' attribute
+ * @param string $content What's inside the box, in wikitext format.
+ */
+ public function __construct( $dir, $content ) {
+ $this->dir = $dir;
+ $this->content = $content;
+ }
+
+ /**
+ * Return the babel box code.
+ *
+ * @return string A single non-babel box, in wikitext format.
+ */
+ public function render() {
+ $notabox = <<<EOT
+<div class="mw-babel-notabox" dir="{$this->dir}">{$this->content}</div>
+EOT;
+
+ return $notabox;
+ }
+
+}
diff --git a/BabelBox/NullBabelBox.php b/BabelBox/NullBabelBox.php
new file mode 100644
index 0000000..ca67d44
--- /dev/null
+++ b/BabelBox/NullBabelBox.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Contains code for inner items which render as empty strings.
+ *
+ * @file
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
+ */
+
+/**
+ * Class for inner items which render as empty strings.
+ */
+class NullBabelBox implements BabelBox {
+
+ /**
+ * Return the babel box code.
+ *
+ * @return string Empty string
+ */
+ public function render() {
+ return '';
+ }
+
+}
diff --git a/extension.json b/extension.json
index c17e7bf..57ecd40 100644
--- a/extension.json
+++ b/extension.json
@@ -18,7 +18,11 @@
"Babel": "Babel.class.php",
"BabelLanguageCodes": "BabelLanguageCodes.class.php",
"BabelStatic": "BabelStatic.class.php",
- "BabelAutoCreate": "BabelAutoCreate.class.php"
+ "BabelAutoCreate": "BabelAutoCreate.class.php",
+ "BabelBox": "BabelBox/BabelBox.php",
+ "LanguageBabelBox": "BabelBox/LanguageBabelBox.php",
+ "NotBabelBox": "BabelBox/NotBabelBox.php",
+ "NullBabelBox": "BabelBox/NullBabelBox.php"
},
"ResourceModules": {
"ext.babel": {
--
To view, visit https://gerrit.wikimedia.org/r/279341
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ibed6e8952617f2d7c12e08dfb9aa79a8a927b14f
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/Babel
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits