getOutput()->addModules('ext.gentooPackages');
$atom = $args['atom'];
$type = $args['type'];
if ($atom === NULL) {
return "Package name missing";
}
try {
$objCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$cacheKeyString = $objCache->makeKey('packageInfo', $atom, $type);
$packageInfo = $objCache->getWithSetCallback($cacheKeyString, $objCache::TTL_DAY,
function( $oldValue, &$ttl, array &$setOpts ) use ( $atom, $type ) {
return self::fetchOrError($atom, $type);
}
);
return [$packageInfo, 'markerType' => 'nowiki'];
} catch (Exception $ex) {
return [$ex->getMessage(), 'markerType' => 'nowiki'];
}
}
public static function fetchOrError($atom, $type) {
global $wgVersion;
$url = "https://packages.gentoo.org/packages/${atom}.json";
if ($type !== 'use') {
throw new UnexpectedValueException('
Unknown type parameter value.
');
}
if(version_compare( $wgVersion, '1.33', '<=' ))
$json_str = Http::get($url);
else {
$httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()
->create($url, [ 'method' => 'GET' ], __METHOD__ );
$status = $httpRequest->execute();
if ($status->isOK())
$json_str = $httpRequest->getContent();
else
$json_str = false;
}
if ($json_str === false) {
throw new ErrorException('Cannot load package information. Is the atom ' . htmlspecialchars($atom) . ' correct?
');
} else {
$json = json_decode($json_str, true);
return self::render($json);
}
}
private static function render($json) {
$use_flags = self::renderFlags($json);
$updated_at = strftime('%Y-%m-%d %H:%M', strtotime($json['updated_at']));
$desc = htmlspecialchars($json['description']);
return <<
${use_flags}
HTML;
}
private static function renderFlags($json) {
$flags = self::sortFlags($json);
$html = <<
HTML;
foreach ($flags as $flag) {
$name = htmlspecialchars($flag['name']);
$desc = htmlspecialchars($flag['description']);
$html .= <<
${name}
|
${desc}
|
HTML;
}
$html .= <<< HTML
HTML;
return $html;
}
private static function sortFlags($json) {
$merged_flags = [];
foreach(array_merge($json['use']['global'], $json['use']['local']) as $flag)
$merged_flags[$flag['name']] = $flag;
ksort($merged_flags);
return $merged_flags;
}
public static function onParserFirstCallInit($parser) {
$parser->setHook('package-info', 'GentooPackages::packageInfo');
}
}