summaryrefslogtreecommitdiff
blob: f8ecfd790d88d9a7d6f93a4fbe08ea31940ddf7e (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
<?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 [self::fetchOrError($atom, $type), 'markerType' => 'nowiki'];
    }
  }

  static function fetchOrError($atom, $type) {
    global $wgVersion;
    $url = "https://packages.gentoo.org/packages/${atom}.json";
    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) {
      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>
        </tr>
HTML;
    }

    $html .= <<< HTML
      </table>
HTML;

    return $html;
  }

  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;
  }

  static function initHooks($parser) {
    global $wgOut;

    $parser->setHook('package-info', 'GentooPackages::packageInfo');
    $wgOut->addModules('ext.gentooPackages');
  }
}