setExpiry(60 * 60 * 24); // 1 day
$cache->setCacheKey(['packageInfo', $atom, $type]);
try {
$packageInfo = $cache->getCachedValue('self::fetchOrError', [$atom, $type]);
$cache->saveCache();
return [$packageInfo, 'markerType' => 'nowiki'];
} catch (Exception $ex) {
return [$ex->message, 'markerType' => 'nowiki'];
}
}
}
private 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 = \MediaWiki\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 function onParserFirstCallInit($parser) {
$parser->setHook('package-info', 'GentooPackages::packageInfo');
$output = $parser->getOutput();
$output->addModules('ext.gentooPackages');
}
}