summaryrefslogtreecommitdiff
blob: 950d5d674288efe2b41918dadd97856b3c920d00 (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
<?php

class GentooPackages { // implements MediaWiki\Hook\ParserFirstCallInitHook {
  public static function packageInfo($input, array $args, Parser $parser, PPFrame $frame) {
    $parser->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('<div class="alert alert-danger">Unknown type parameter value.</div>');
    }

    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('<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);
      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 <<<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;
  }

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

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