Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
},
"groups": {
"bluelink": {
"image": "resources/lib/ooui/themes/wikimediaui/images/icons/article-rtl-progressive.svg"
"image": "cdxIconArticle"
},
"redlink": {
"image": "resources/lib/ooui/themes/wikimediaui/images/icons/articleNotFound-ltr.svg",
"image": "cdxIconArticleNotFound",
"color": {
"border": "#ba0000",
"highlight": {
Expand All @@ -63,7 +63,7 @@
}
},
"externallink": {
"image": "resources/lib/ooui/themes/wikimediaui/images/icons/linkExternal-ltr-progressive.svg",
"image": "cdxIconLinkExternal",
"color": {
"border": "grey",
"highlight": {
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ parameters:
- ../../vendor
bootstrapFiles:
- ../../includes/AutoLoader.php
ignoreErrors:
# MW_INSTALL_PATH is defined by MediaWiki's entry-point bootstrap
# (index.php, api.php, maintenance scripts), not by AutoLoader.php.
# MW core itself uses this constant directly (see CodexModule.php);
# there is no equivalent on the Config service in MW 1.44+.
-
message: '#Constant MW_INSTALL_PATH not found\.#'
path: src/Extension.php
8 changes: 8 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@
<file name="src/EntryPoints/SpecialNetwork.php" />
</errorLevel>
</UndefinedDocblockClass>
<UndefinedConstant>
<errorLevel type="suppress">
<!-- MW_INSTALL_PATH is set by MediaWiki's runtime bootstrap.
MW core uses this constant directly (CodexModule.php);
there is no equivalent on the Config service in MW 1.44+. -->
<file name="src/Extension.php" />
</errorLevel>
</UndefinedConstant>
</issueHandlers>
</psalm>
3 changes: 2 additions & 1 deletion resources/js/ApiPageConnectionRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ module.ApiPageConnectionRepo = ( function ( mw, ApiConnectionsBuilder ) {
for (let index in icons) {
let icon = icons[index]
if (icon.type === 'ooui') {
images[page.title] = 'resources/lib/ooui/themes/wikimediaui/images/icons/' + icon.icon;
images[page.title] = (mw.config.get('wgScriptPath') || '')
+ '/resources/lib/ooui/themes/wikimediaui/images/icons/' + icon.icon;
break
} else if (icon.type === 'file') {
fileIcons[page.title] = icon.icon;
Expand Down
18 changes: 16 additions & 2 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@

namespace MediaWiki\Extension\Network;

use MediaWiki\Extension\Network\NetworkFunction\IconResolver;
use MediaWiki\Extension\Network\NetworkFunction\NetworkConfig;
use MediaWiki\Extension\Network\NetworkFunction\NetworkPresenter;
use MediaWiki\Extension\Network\NetworkFunction\NetworkUseCase;
use MediaWiki\Extension\Network\NetworkFunction\ParserFunctionNetworkPresenter;
use MediaWiki\Extension\Network\NetworkFunction\SpecialNetworkPresenter;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;

class Extension {

public function __construct(
private readonly IconResolver $iconResolver
) {
}

public static function getFactory(): self {
return new self();
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
return new self(
new IconResolver(
MW_INSTALL_PATH . '/resources/lib/codex-icons/codex-icons.json',
$mainConfig->get( MainConfigNames::ScriptPath )
)
);
}

public function newNetworkFunction( NetworkPresenter $presenter, NetworkConfig $config ): NetworkUseCase {
return new NetworkUseCase( $presenter, $config->getOptions() );
return new NetworkUseCase( $presenter, $config->getOptions(), $this->iconResolver );
}

public function newParserFunctionNetworkPresenter(): ParserFunctionNetworkPresenter {
Expand Down
108 changes: 108 additions & 0 deletions src/NetworkFunction/IconResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare( strict_types = 1 );

namespace MediaWiki\Extension\Network\NetworkFunction;

use MediaWiki\Logger\LoggerFactory;

class IconResolver {

private ?array $iconData = null;

public function __construct(
private readonly string $codexIconsJsonPath,
private readonly string $scriptPath
) {
}

public function resolve( array $visJsOptions ): array {
if ( !isset( $visJsOptions['groups'] ) || !is_array( $visJsOptions['groups'] ) ) {
return $visJsOptions;
}
foreach ( $visJsOptions['groups'] as $name => $group ) {
if ( is_array( $group ) && isset( $group['image'] ) && is_string( $group['image'] ) ) {
$visJsOptions['groups'][$name]['image'] = $this->resolveImage( $group['image'] );
}
}
return $visJsOptions;
}

private function resolveImage( string $value ): string {
if ( preg_match( '/^cdxIcon[A-Z]/', $value ) === 1 ) {
$resolved = $this->codexIconToDataUri( $value );
if ( $resolved === null ) {
LoggerFactory::getInstance( 'Network' )->warning(
'Unknown or unsupported Codex icon "{name}" in PageNetworkOptions; '
. 'falling back to literal value (will likely fail to load).',
[ 'name' => $value ]
);
return $value;
}
return $resolved;
}
if ( $this->isAbsoluteUrl( $value ) ) {
return $value;
}
return $this->scriptPath . '/' . $value;
}

private function isAbsoluteUrl( string $value ): bool {
return str_starts_with( $value, 'http://' )
|| str_starts_with( $value, 'https://' )
|| str_starts_with( $value, '//' )
|| str_starts_with( $value, '/' )
|| str_starts_with( $value, 'data:' );
}

private function codexIconToDataUri( string $iconName ): ?string {
$path = $this->extractPath( $this->loadIcons()[$iconName] ?? null );
if ( $path === null ) {
return null;
}
// Codex icons are authored on a 20x20 grid.
// See https://doc.wikimedia.org/codex/main/style-guide/icons.html
$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20">'
. $path
. '</svg>';
return 'data:image/svg+xml;base64,' . base64_encode( $svg );
}

/**
* codex-icons.json stores entries in three shapes:
* - a plain string of SVG path markup (e.g. cdxIconAdd)
* - an associative array with an 'ltr' key (e.g. cdxIconArticle)
* - an associative array with a 'default' key plus a 'langCodeMap' (e.g. cdxIconBold)
*
* Returns the SVG path markup for any of these, preferring 'ltr' when available
* and falling back to 'default'. Returns null for shapes we don't understand.
*/
private function extractPath( mixed $icon ): ?string {
if ( is_string( $icon ) ) {
return $icon;
}
if ( !is_array( $icon ) ) {
return null;
}
if ( isset( $icon['ltr'] ) && is_string( $icon['ltr'] ) ) {
return $icon['ltr'];
}
if ( isset( $icon['default'] ) && is_string( $icon['default'] ) ) {
return $icon['default'];
}
return null;
}

private function loadIcons(): array {
if ( $this->iconData === null ) {
if ( is_readable( $this->codexIconsJsonPath ) ) {
$json = file_get_contents( $this->codexIconsJsonPath );
$this->iconData = ( $json !== false ? json_decode( $json, true ) : null ) ?? [];
} else {
$this->iconData = [];
}
}
return $this->iconData;
}

}
5 changes: 3 additions & 2 deletions src/NetworkFunction/NetworkUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class NetworkUseCase {

public function __construct(
private readonly NetworkPresenter $presenter,
private readonly array $visJsOptions
private readonly array $visJsOptions,
private readonly IconResolver $iconResolver
) {
}

Expand Down Expand Up @@ -65,7 +66,7 @@ private function getVisJsOptions( array $arguments ): array {
$this->visJsOptions,
json_decode( $arguments['options'] ?? '{}', true ) ?? []
);
return $visJsOptions;
return $this->iconResolver->resolve( $visJsOptions );
}

/**
Expand Down
Loading
Loading