Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ParsoidUtils | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
createLangFragment | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
createLangAttribute | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
addAttributesToNode | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
addCategory | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Kartographer; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use MediaWiki\Page\PageReferenceValue; |
7 | use Wikimedia\Bcp47Code\Bcp47CodeValue; |
8 | use Wikimedia\Parsoid\DOM\DocumentFragment; |
9 | use Wikimedia\Parsoid\DOM\Element; |
10 | use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
11 | |
12 | /** |
13 | * @license MIT |
14 | */ |
15 | class ParsoidUtils { |
16 | |
17 | /** |
18 | * Creates an internationalized Parsoid fragment. If $language is not provided, the returned |
19 | * fragment is generated in the page language. |
20 | * @param string $msgKey |
21 | * @param array $params |
22 | * @param ParsoidExtensionAPI $extApi |
23 | * @param string|null $language |
24 | * @return DocumentFragment |
25 | */ |
26 | public static function createLangFragment( |
27 | string $msgKey, array $params, ParsoidExtensionAPI $extApi, ?string $language |
28 | ): DocumentFragment { |
29 | if ( $language === null ) { |
30 | return $extApi->createPageContentI18nFragment( $msgKey, $params ); |
31 | } |
32 | return $extApi->createLangI18nFragment( new Bcp47CodeValue( $language ), $msgKey, $params ); |
33 | } |
34 | |
35 | /** |
36 | * Creates an internationalized Parsoid attribute on the provided element. If $language is |
37 | * not provided, the created attribute is generated in the page language. |
38 | * @param Element $element |
39 | * @param string $name |
40 | * @param string $msgKey |
41 | * @param array $params |
42 | * @param ParsoidExtensionAPI $extApi |
43 | * @param string|null $language |
44 | * @return void |
45 | */ |
46 | public static function createLangAttribute( |
47 | Element $element, string $name, string $msgKey, array $params, ParsoidExtensionAPI $extApi, ?string $language |
48 | ): void { |
49 | if ( $language === null ) { |
50 | $extApi->addPageContentI18nAttribute( $element, $name, $msgKey, $params ); |
51 | } else { |
52 | $extApi->addLangI18nAttribute( $element, new Bcp47CodeValue( $language ), $name, $msgKey, $params ); |
53 | } |
54 | } |
55 | |
56 | public static function addAttributesToNode( array $attrs, Element $node ): void { |
57 | foreach ( $attrs as $k => $v ) { |
58 | if ( $v === null ) { |
59 | continue; |
60 | } |
61 | if ( $k === 'class' && is_array( $v ) ) { |
62 | $node->setAttribute( $k, implode( ' ', $v ) ); |
63 | } else { |
64 | $node->setAttribute( $k, $v ); |
65 | } |
66 | } |
67 | } |
68 | |
69 | /** |
70 | * Add category to the page, from its key |
71 | * @param ParsoidExtensionAPI $extApi |
72 | * @param string $category |
73 | * @return void |
74 | */ |
75 | public static function addCategory( ParsoidExtensionAPI $extApi, string $category ) { |
76 | $catService = MediaWikiServices::getInstance()->getTrackingCategories(); |
77 | $linkTarget = $extApi->getPageConfig()->getLinkTarget(); |
78 | $pageRef = PageReferenceValue::localReference( |
79 | $linkTarget->getNamespace(), $linkTarget->getDBkey() |
80 | ); |
81 | $cat = $catService->resolveTrackingCategory( $category, $pageRef ); |
82 | if ( $cat ) { |
83 | $extApi->getMetadata()->addCategory( $cat ); |
84 | } |
85 | } |
86 | } |