summaryrefslogtreecommitdiff
blob: 3dbedd38379ca98b1cb34970cb12454a8ffe65a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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 <em>' . htmlspecialchars($atom) . '</em> 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');
  }
}