Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| LanguagesHelper | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doesTitleHasLanguagesOrVariants | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getLanguagesAndVariants | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | namespace MediaWiki\Minerva; |
| 21 | |
| 22 | use MediaWiki\Language\LanguageConverterFactory; |
| 23 | use MediaWiki\Output\OutputPage; |
| 24 | use MediaWiki\Title\Title; |
| 25 | |
| 26 | /** |
| 27 | * Helper class to encapsulate logic responsible for checking languages and variants for given title |
| 28 | * @package MediaWiki\Minerva |
| 29 | */ |
| 30 | class LanguagesHelper { |
| 31 | |
| 32 | public function __construct( |
| 33 | private readonly LanguageConverterFactory $languageConverterFactory, |
| 34 | ) { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Whether the Title is also available in other languages or variants |
| 39 | * @param OutputPage $out Output page to fetch language links |
| 40 | * @param Title $title |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function doesTitleHasLanguagesOrVariants( |
| 44 | OutputPage $out, |
| 45 | Title $title |
| 46 | ): bool { |
| 47 | if ( $out->getLanguageLinks() !== [] ) { |
| 48 | return true; |
| 49 | } |
| 50 | $langConv = $this->languageConverterFactory->getLanguageConverter( $title->getPageLanguage() ); |
| 51 | return $langConv->hasVariants(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param OutputPage $out Output page to fetch language links |
| 56 | * @param Title $title |
| 57 | * @return string[] Array of language codes |
| 58 | */ |
| 59 | public function getLanguagesAndVariants( |
| 60 | OutputPage $out, |
| 61 | Title $title |
| 62 | ): array { |
| 63 | $langlinks = array_map( |
| 64 | static fn ( string $langlink ) => explode( ':', $langlink, 2 )[ 0 ], |
| 65 | $out->getLanguageLinks(), |
| 66 | ); |
| 67 | |
| 68 | $langConv = $this->languageConverterFactory->getLanguageConverter( $title->getPageLanguage() ); |
| 69 | $variants = $langConv->hasVariants() ? $langConv->getVariants() : []; |
| 70 | |
| 71 | return array_unique( array_merge( |
| 72 | $langlinks, |
| 73 | $variants |
| 74 | ) ); |
| 75 | } |
| 76 | } |