summaryrefslogtreecommitdiffstats
path: root/class/Parser.php
blob: 311371884def318dba3861ab54f62680b6e200ef (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Class for "repo" Table management
 *
 * PHP version 5
 *
 * Copyright © 2010 Remi Collet
 *
 * This file is part of rpmphp.
 *
 * rpmphp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * rpmphp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with rpmphp.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Main
 * @package   RPMPHP
 *
 * @author    Remi Collet <unknown@unknwown.com>
 * @author    Johan Cwiklinski <johan@x-tnd.be>
 * @copyright 2010 Remi Collet
 * @license   http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version
 * @link      http://github.com/remicollet/rpmphp/
 * @since     The begining of times.
*/
class Parser
{
    /**
     * Display a message
     */
    static private function log($msg)
    {
        echo date("r : ") . $msg ."\n";
    }

    /**
     * Parse the Bugzilla ACL list from pkgdb
     *
     * @param TableAcl $acls the table to write to
     * @param string   $url  the file to read from
     *
     * @return integer number of parsed line
     */
    static public function readAcls(TableAcls $acls, $url)
    {
        $tot = 0;

        self::log("Read pkgdb/owner");
        $fic=fopen($url, 'r');
        if (!$fic) {
            self::log("ERROR reading '$url'");
        } else {
            $nb = $acls->getCount();
            $acls->truncate();
            self::log("Delete $nb owners");

            for ($tot=0 ; $line=fgetcsv($fic, 1024, '|'); ) {
                if (count($line)>5 && substr($line[0], 0, 1)!='#') {
                    for ($i=0; $i<6; $i++) {
                        $line[$i]=trim($line[$i]);
                    }
                    $input = array(
                        'collection'    => $line[0],
                        'name'          => $line[1],
                        'summary'       => $line[2],
                    );
                    if (!empty($line[3])) {
                        $input['owner'] = $line[3];
                    }
                    if (!empty($line[4])) {
                        $input['qa'] = $line[3];
                    }
                    if (!empty($line[5])) {
                        $input['cc'] = $line[3];
                    }
                    if ($acls->add($input)) {
                        $tot++;
                    }
                }
            }
            fclose($fic);
            self::log("wrote $tot package's owner");
        }
        return $tot;
    }

    /**
     * Parse the content of a R repository
     *
     * @param TableUpstream $uptable the table to write to
     * @param hastable      $repo    the repo to read from
     *
     * @return integer number of parsed line
     */
    static private function readOneR(TableUpstream $uptable, Array $repo)
    {
        $tot = 0;

        self::log("Reading " . $repo["name"] . " (" .
            $repo["state"] . ")");
        $index = @file_get_contents($repo["url"]);
        if (!$index) {
            self::log("Can't read [" . $repo["url"] . "], skip this channel");
            return 0;
        }
        if ($repo["state"]=="stable") {
            $crit = array('type'=>'R', 'channel'=>$repo['name']);
            $nb = $uptable->delete($crit);
            self::log("Delete $nb packages");
        }
        $results=array();
        $pat = '/Package: *(.*)\nVersion: *(.*)\n/i';
        if (preg_match_all($pat, $index, $results, PREG_SET_ORDER)) {
            foreach ($results as $result) {
                $rpmname = "R-".$result[1];
                $ver = str_replace('-', '.', $result[2]);
                $add = $uptable->record(
                    "R",
                    $repo["name"],
                    $rpmname,
                    $ver,
                    $repo["state"]=='stable',
                    ($repo["state"]=="stable"?"":"devel")
                );

                if ($add) {
                    $tot++;
                }
            }
            self::log("Write $tot packages in this channel");
        } else {
            self::log("No package in this channel");
        }
        return $tot;
    }

    /**
     * Parse the content of all R repository
     *
     * @param TableUpstream $uptable the table to write to
     * @param TableRRepo    $rrepo   the table to read from
     *
     * @return integer number of parsed line
     */
    static public function readR(TableUpstream $uptable, TableRRepo $rrepo)
    {
        $tot = 0;

        foreach ($rrepo->request() as $repo) {
            $tot += self::readOneR($uptable, $repo);
        }
        self::log("Write $tot packages in all channels");
        return $tot;
    }

    /**
     * Parse the content of all RPM repository
     *
     * @param TableRpm      $rpmtable   the table to write to
     * @param TableRpmRepo  $rpmrepo    the table to store timestamp
     * @param hastable      $row        the repo to read from
     *
     * @return integer number of parsed line
     */
    static public function readOneRpm(TableRpm $rpmtable, TableRpmRepo $rpmrepo, Array $row)
    {
        $tot = 0;

        self::log("REPOSITORY " . $row['main'] . " " . $row['sub']);
        $TimRemote = 0;
        $repomd = @simplexml_load_file($row['url'] . "repodata/repomd.xml");
        if ($repomd) {
            foreach ($repomd->data as $data) {
                if ($data->attributes()=="primary") {
                    $TimRemote = $data->timestamp;
                    $UrlRemote = $row['url'] . $data->location->attributes();
                }
            }
        }
        if (!$TimRemote) {
            self::log("Can't read $UrlRemote");

        } else if ($TimRemote > $row['stamp']) {
            self::log("Loading $UrlRemote");

            //$fic=gzopen("primary.xml.gz", "r");
            $fic=gzopen($UrlRemote, "r");
            if ($fic) {
                $txt="";
                while ($buf=gzread($fic, 8196)) {
                    $txt .= $buf;
                }
                self::log("Read " . strlen($txt) . " bytes");
                gzclose($fic);

                $primary = simplexml_load_string($txt);
                self::log("Read " . $primary->attributes() . " packages");
                unset($txt);

                $crit = array(
                    'repo_main' => $row['main'],
                    'repo_sub'  => $row['sub']
                );
                $nb = $rpmtable->delete($crit);
                self::log("Delete $nb packages");

                foreach ($primary->package as $package) {
                    if ($package->attributes()=='rpm') {
                        $ver = $package->version->attributes();

                        $input = array(
                            'repo_main' => $row['main'],
                            'repo_sub'  => $row['sub'],
                            'name'      => $package->name,
                            'epoch'     => $ver['epoch'],
                            'ver'       => $ver['ver'],
                            'rel'       => $ver['rel'],
                            'summary'   => $package->summary,
                            'url'       => $package->url
                        );
                        if ($rpmtable->add($input)) {
                            $tot++;
                        }
                    }
                }
                self::log("Write $tot packages");

                $rpmrepo->update($row['id'], array('stamp' =>$TimRemote));

                unset($primary);
            } else {
                self::log("ERROR : can't read $UrlRemote");
            }
        } else {
            self::log("no update needed : $TimRemote / " . $row['stamp']);
        }

        return $tot;
    }

    /**
     * Parse the content of all RPM repository
     *
     * @param TableRpm      $rpmtable   the table to write to
     * @param TableRpmRepo  $rpmrepo    the table to read from
     * @param Array         $crit       array for repo selection
     *
     * @return integer number of parsed line
     */
    static public function readRpm(TableRpm $rpmtable, TableRpmRepo $rpmrepo, Array $crit)
    {
        $tot = 0;

        foreach ($rpmrepo->request($crit) as $row) {
            $tot += self::ReadOneRpm($rpmtable, $rpmrepo, $row);
        }
        return $tot;
    }

    /**
     * Parse the PECL webservices
     *
     * @param TableUpstream $uptable the table to write to
     * @param string        $url     the URL to read from
     *
     * @return integer number of parsed line
     */
    static public function readPecl(TableUpstream $uptable, $url)
    {
        self::log("PECL listLatestReleases - stable");

        $request = xmlrpc_encode_request("package.listLatestReleases", "stable");
        $context = stream_context_create(
            array(
                'http' => array(
                    'method' => "POST",
                    'header' => "Content-Type: text/xml",
                    'content' => $request
                )
            )
        );
        $file = file_get_contents($url, false, $context);
        if (!$file) {
            self::log("Can't file_get_contents($url)");
            return 0;
        }

        $stable = xmlrpc_decode($file);
        if (xmlrpc_is_fault($stable)) {
            self::log("ERROR xmlrpc: $stable[faultString] ($stable[faultCode])");
        } else {
            $nb = $uptable->delete(array('type'=>'pecl', 'channel'=>'pecl'));
            self::log("Delete $nb packages");

            $nb=0;
            foreach ($stable as $name => $info) {
                $rpmname="php-pecl-".str_replace("_", "-", $name);

                $id = $uptable->record(
                    'pecl',
                    'pecl',
                    $rpmname,
                    $info["version"],
                    true
                );
                if ($id) {
                    $nb++;
                }
            }
            self::log("Write $nb packages");
        }

        // -------------------------------------------------------------------
        self::log("PECL listLatestReleases - unstable");

        $request = xmlrpc_encode_request("package.listLatestReleases", array());
        $context = stream_context_create(
            array(
                'http' => array(
                    'method' => "POST",
                    'header' => "Content-Type: text/xml",
                    'content' => $request
                )
            )
        );
        $file = file_get_contents($url, false, $context);
        if (!$file) {
            self::log("Can't file_get_contents($url)");
            return 0;
        }
        $unstable = xmlrpc_decode($file);
        if (xmlrpc_is_fault($unstable)) {
            self::log("ERROR xmlrpc: $stable[faultString] ($stable[faultCode])");
        } else {
            $nb=0;
            foreach ($unstable as $name => $info) {
                $rpmname="php-pecl-".str_replace("_", "-", $name);

                $id = $uptable->record(
                    'pecl',
                    'pecl',
                    $rpmname,
                    $info["version"],
                    true,
                    $info["state"]
                );
                if ($id) {
                    $nb++;
                }
            }
            self::log("Write $nb packages");
        }
        return $nb;
    }
}
?>