summaryrefslogtreecommitdiffstats
path: root/examples/librpm.php
blob: 16fdc78b4ecc4b34b343ba8f08b7f37b56efab66 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
 * Minimal userland OO library
 **/
namespace Remi\RPM;

abstract class Common {
	protected $info = NULL;

	protected function _dep(Array $names, Array $flags, Array $vers) {
		$ret = [];
		$signs = [
			RPMSENSE_EQUAL                    => ' = ',
			RPMSENSE_LESS                     => ' < ',
			RPMSENSE_GREATER                  => ' > ',
			RPMSENSE_LESS    | RPMSENSE_EQUAL => ' <= ',
			RPMSENSE_GREATER | RPMSENSE_EQUAL => ' >= ',
		];

		if (count($names) == count($vers) && count($vers) == count($flags)) {
			for ($i=0 ; $i<count($names) ; $i++) {
				if (isset($signs[$flags[$i] & 15])) {
					$ret[] = $names[$i] . $signs[$flags[$i] & 15] . $vers[$i];
				} else {
					$ret[] = $names[$i] . $vers[$i];
				}
			}
		}
		return $ret;
	}

	protected function _files() {
		$ret = [];
		for ($i=0 ; $i<count($this->info['Basenames']) ; $i++) {
			$ret[] = $this->info['Dirnames'][$this->info['Dirindexes'][$i]] . $this->info['Basenames'][$i];
		}
		return $ret;
	}

	public function __get($name) {
		switch ($name) {
			case 'EVR':
			case 'NEVR':
			case 'NEVRA':
				$ret = '';
				if (substr($name, 0, 1) === 'N') {
					$ret = $this->info['Name'] . '-';
				}
				if (isset($this->info['Epoch'])) {
					$ret .= $this->info['Epoch'] . ':';
				}
				$ret .= $this->info['Version'] . '-' . $this->info['Release'];

				if (substr($name, -1) === 'A') {
					$ret .= '.' . $this->info['Arch'];
				}
				return $ret;
			case 'Requires':
				if (isset($this->info['Requirename'])) {
					return $this->_dep($this->info['Requirename'], $this->info['Requireflags'], $this->info['Requireversion']);
				}
			case 'Conflicts':
				if (isset($this->info['Conflictname'])) {
					return $this->_dep($this->info['Conflictname'], $this->info['Conflictflags'], $this->info['Conflictversion']);
				}
			case 'Obsoletes':
				if (isset($this->info['Obsoletename'])) {
					return $this->_dep($this->info['Obsoletename'], $this->info['Obsoleteflags'], $this->info['Obsoleteversion']);
				}
			case 'Provides':
				if (isset($this->info['Providename'])) {
					return $this->_dep($this->info['Providename'], $this->info['Provideflags'], $this->info['Provideversion']);
				}
			case 'Files':
				if (isset($this->info['Basenames'])) {
					return $this->_files();
				}
			default:
				if (isset($this->info[$name])) {
					return $this->info[$name];
				}
				return NULL;
		}
	}
}

/**
 * From a RPM file
 *
 * $a = new \Remi\RPM\File(dirname(__DIR__) . '/tests/bidon.rpm');
 * var_dump($a->Vendor);
 * var_dump($a->NEVRA);
 **/
class File extends Common {

	public function __construct($path) {
		$info = \rpminfo($path, true, $error);

		if ($error) {
			throw new \RuntimeException($error);
		} else {
			$this->info = $info;
		}
	}
}

/**
 * From an installed RPM
 *
 * $a = new \Remi\RPM\Package('php');
 * var_dump($a->License);
 * var_dump($a->NEVRA);
 **/
class Package extends Common {

	public function __construct($name, $index=0) {
		$info = \rpmdbinfo($name, true);

		if (!$info) {
			throw new \RuntimeException("$name not found");
		} else if ($index < 0 || $index >= count($info)) {
			throw new \OutOfBoundsException("$index not in range");
		} else {
			$this->info = $info[$index];
		}
	}
}

/**
 * Find packages which provides something or own a file
 *
 * $a = \Remi\RPM\WhatProvides("php-redis");
 * print_r($a[0]->NEVR);
 *
 * $a = \Remi\RPM\WhatProvides("/usr/bin/php");
 * print_r($a[0]->NEVR);
 **/
function WhatProvides($crit) {
	if (file_exists($crit)) {
		$a = \rpmdbsearch($crit, RPM_TAG_INSTFILENAMES);
	} else {
		$a = \rpmdbsearch($crit, RPM_TAG_PROVIDENAME);
	}
	$r = [];
	if (is_array($a)) {
		foreach($a as $i) {
			$r[] = new Package($i['Name']);
		}
	}
	return $r;
}

/**
 * Find packages which requires something
 *
 * $a = \Remi\RPM\WhatRequires("php-common");
 * print_r($a[0]->NEVRA);
 **/
function WhatRequires($crit) {
	$a = \rpmdbsearch($crit, RPM_TAG_REQUIRENAME);

	$r = [];
	if (is_array($a)) {
		foreach($a as $i) {
			$r[] = new Package($i['Name']);
		}
	}
	return $r;
}
/*
$a = new File(dirname(__DIR__).'/tests/bidon.rpm');
$a = new Package('phpunit7');
print_r($a->Requires);
*/