summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Legler <alex@a3li.li>2016-01-30 20:54:17 +0100
committerAlex Legler <alex@a3li.li>2016-01-30 20:54:17 +0100
commit1970a12ee87a9aaa03c1bad804c114720cb7545b (patch)
treeaf7989495a7dd64226124e32e4f0f18c4cca57a4 /GentooPackages
parentTranslate: Backport message API deprecation patch (diff)
downloadextensions-1970a12ee87a9aaa03c1bad804c114720cb7545b.tar.gz
extensions-1970a12ee87a9aaa03c1bad804c114720cb7545b.tar.bz2
extensions-1970a12ee87a9aaa03c1bad804c114720cb7545b.zip
Add new package info extension
Diffstat (limited to 'GentooPackages')
-rw-r--r--GentooPackages/GentooPackages.php137
-rw-r--r--GentooPackages/extension.json35
-rw-r--r--GentooPackages/resources/css/gentooPackages.css39
3 files changed, 211 insertions, 0 deletions
diff --git a/GentooPackages/GentooPackages.php b/GentooPackages/GentooPackages.php
new file mode 100644
index 00000000..c75dcf6b
--- /dev/null
+++ b/GentooPackages/GentooPackages.php
@@ -0,0 +1,137 @@
+<?php
+
+class GentooPackages {
+ static function packageInfo($input, array $args, Parser $parser, PPFrame $frame) {
+ $atom = $args['atom'];
+ $type = $args['type'];
+
+ if ($atom === NULL) {
+ return "Package name missing";
+ } else {
+ return array(self::fetchOrError($atom, $type), 'markerType' => 'nowiki');
+ }
+ }
+
+ static function fetchOrError($atom, $type) {
+ $json_str = Http::get("https://packages.gentoo.org/packages/" . $atom . ".json");
+
+ if ($json_str === false) {
+ return '<div class="alert alert-danger">Cannot load package information. Is the atom correct?</div>';
+ } else {
+ $json = json_decode($json_str, true);
+
+ if ($type === 'use') {
+ return self::render($json);
+ } else {
+ return '<div class="alert alert-danger">Unknown type parameter value.</div>';
+ }
+ }
+ }
+
+ 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 <<<HTML
+ <div class="panel panel-default gp-panel">
+ <div class="panel-heading gp-panel-heading">
+ <h3 class="panel-title">
+ <span class="text-muted">USE flags for</span>
+ <a href="${json['href']}">${json['atom']}</a>
+ <small><span class="fa fa-external-link-square"></span></small>
+ <small class="gp-pkg-desc">${desc}</small>
+ </h3>
+ </div>
+ <div class="table-responsive gp-useflag-table-container">
+ ${use_flags}
+ </div>
+ <div class="panel-footer gp-panel-footer">
+ <small class="pull-right">
+ Data provided by the <a href="https://packages.gentoo.org">Gentoo Package Database</a>
+ &middot;
+ Last update: ${updated_at}
+ </small>
+ <small>
+ <a href="/wiki/Handbook:AMD64/Working/USE">More information about USE flags</a>
+ </small>
+ </div>
+ </div>
+HTML;
+ }
+
+ static function renderFlags($json) {
+ $flags = self::sortFlags($json);
+
+ $html = <<<HTML
+ <table class="table gp-useflag-table">
+HTML;
+
+ foreach ($flags as $flag) {
+ $name = htmlspecialchars($flag['name']);
+ $desc = htmlspecialchars($flag['description']);
+
+ $html .= <<<HTML
+ <tr>
+ <td>
+ <code><a href="https://packages.gentoo.org/useflags/${name}">${name}</a></code>
+ </td>
+ <td>
+ ${desc}
+ </td>
+ <td class="gp-useflag-type">
+ ${flag['type']}
+ </td>
+ </tr>
+HTML;
+ }
+
+ $html .= <<< HTML
+ </table>
+HTML;
+
+ return $html;
+ }
+
+ static function sortFlags($json) {
+ $merged_flags = self::mapByFlagName(array_merge($json['use']['global'], $json['use']['local']));
+ $local_flags = self::getFlagNames($json['use']['local']);
+ $all_flags = self::getFlagNames($merged_flags);
+ sort($all_flags);
+
+ $result = array();
+ foreach($all_flags as $flag_name) {
+ $result[$flag_name] = $merged_flags[$flag_name];
+ $result[$flag_name]['type'] = in_array($flag_name, $local_flags) ? 'local' : 'global';
+ }
+
+ return $result;
+ }
+
+ static function mapByFlagName($flags) {
+ $result = array();
+
+ foreach($flags as $flag) {
+ $result[$flag['name']] = $flag;
+ }
+
+ return $result;
+ }
+
+ static function getFlagNames($flag_map) {
+ $result = array();
+
+ foreach($flag_map as $flag) {
+ $result[] = $flag['name'];
+ }
+
+ return $result;
+ }
+
+ static function initHooks($parser) {
+ global $wgOut;
+
+ $parser->setHook('package-info', 'GentooPackages::packageInfo');
+ $wgOut->addModules('ext.gentooPackages');
+ }
+}
diff --git a/GentooPackages/extension.json b/GentooPackages/extension.json
new file mode 100644
index 00000000..eb25f416
--- /dev/null
+++ b/GentooPackages/extension.json
@@ -0,0 +1,35 @@
+{
+ "name": "GentooPackages",
+ "version": "0.0.1",
+ "author": [
+ "Alex Legler"
+ ],
+ "url": "https://wiki.gentoo.org/",
+ "descriptionmsg": "boilerplate-desc",
+ "license-name": "AGPL",
+ "type": "parserhook",
+ "AutoloadClasses": {
+ "GentooPackages": "GentooPackages.php"
+ },
+ "config": {
+ "BoilerPlateEnableFoo": true
+ },
+ "Hooks": {
+ "ParserFirstCallInit": [
+ "GentooPackages::initHooks"
+ ]
+ },
+ "ResourceModules": {
+ "ext.gentooPackages": {
+ "styles": [
+ "resources/css/gentooPackages.css"
+ ],
+ "position": "top"
+ }
+ },
+ "ResourceFileModulePaths": {
+ "localBasePath": "",
+ "remoteExtPath": "GentooPackages"
+ },
+ "manifest_version": 1
+}
diff --git a/GentooPackages/resources/css/gentooPackages.css b/GentooPackages/resources/css/gentooPackages.css
new file mode 100644
index 00000000..8b1d47aa
--- /dev/null
+++ b/GentooPackages/resources/css/gentooPackages.css
@@ -0,0 +1,39 @@
+div.gp-panel {
+
+}
+
+div.gp-panel-heading h3 {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+div.gp-panel-heading h3 small.gp-pkg-desc {
+ font-size: 90%;
+ margin-left: 1em;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+div.gp-panel-footer {
+ color: #666;
+ padding-top: 3px;
+ padding-bottom: 3px;
+}
+
+.panel table.table tr th:first-child,
+.panel table.table tr td:first-child {
+ padding-left: 1em;
+}
+
+.gp-useflag-table-container {
+ max-height: 300px;
+ overflow-y: scroll;
+}
+
+.gp-useflag-type {
+ font-size: 85%;
+ color: #888;
+ text-align: right;
+}