package.php 18.9 KB
Newer Older
jlopez's avatar
jlopez committed
1
2
3
4
<?php

require_once(__DIR__.'/model/Package.php');

jlopez's avatar
jlopez committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * Package tool function
 * 
 * @file package.php
 */

/**
 * Replace special character with empty string
 * 
 * @param string $value The value to clean
 * 
 * @return string
 * 
 */
jlopez's avatar
jlopez committed
19
20
21
22
23
24
25
26
function cleanValue($value) {
    $value = str_replace("\n", "", $value);
    $value = str_replace("\r", "", $value);
    $value = str_replace("\t", "", $value);
    $value = str_replace("  ", "", $value);
    return $value;
}

jlopez's avatar
jlopez committed
27
28
29
30
31
32
33
/**
 * Load HTML page from url and return a DOMDocument object
 * 
 * @param string url The url of the page
 * 
 * @return DOMDocument
 */
jlopez's avatar
jlopez committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function getDocument($url) {
    $opts = array(
        'http'=>array(
          'method'=>"GET",
          'header'=>"Accept-language: en\r\n"
        )
    );
    $context = stream_context_create($opts);
    $content = file_get_contents($url, false, $context);

    $doc = new DOMDocument();
    $doc->loadHtml($content);
    return $doc;
}

jlopez's avatar
jlopez committed
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Find all packages from distribution
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
61
62
63
64
65
66
67
68
69
70
71
72
function findDistributionPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {
    if($os_name == "ubuntu" || $os_name == "debian") {
        return findUbuntuDebianPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname);
    }
    else if($os_name == "fedora" || $os_name == "centos") {
        return findFedoraCentosPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname);
    }
    else if($os_name == "alpine") {
        return findAlpinePackages($os_name, $version_num, $version_name, $keyword, $limit, $onlyname);
    }
}

jlopez's avatar
jlopez committed
73
74
75
76
77
78
79
80
81
82
83
84
85

/**
 * Find all packages from fedora or centos
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
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
function findFedoraCentosPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $keyword_first = strtolower($keyword[0]);

    $url_packages = "";

    if($os_name == "fedora") {
        $url_packages = "https://dl.fedoraproject.org/pub/fedora/linux/releases/".$version_num."/Everything/source/tree/Packages/".$keyword_first."/";
    }
    else if($os_name == "centos") {
        $url_packages = "http://mirror.centos.org/centos/".$version_num."/os/x86_64/Packages/";
    }

    $doc = getDocument($url_packages);

    $as = $doc->getElementsByTagName('a');

    $size = $as->length;

    $index = 0;

    for ($i = 5; $i <= $size; $i++) {
        
        $value = $as[$i]->nodeValue;

        if($value != null) {
            $subject = $value;
            $pattern = "#(.*)-(.*)-(.*?)\.(.*)(\.rpm)#";
            preg_match($pattern, $subject, $matches);
            $name = $matches[1];
            $version = $matches[2];

            $pos = strpos($name, $keyword);

            if($pos !== false) {
                $link = $url_packages.$value;
                $other = $os_name.":".$version_num;
                $description = "";
                $package = new Package($name, $version, $description, $link, $other);
                $results[] = $package;

                $index++;

                if($index == $limit) {
                    break;
                }
            }
        }
    }

    return $results;
}

jlopez's avatar
jlopez committed
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * Find all packages from alpine
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function findAlpinePackages($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $keyword = "*".$keyword."*";

    $basic_url = "https://pkgs.alpinelinux.org/packages";

    $url_packages = $basic_url."?name=".$keyword."&branch=v".$version_num."&repo=main&arch=x86_64";

    $doc = getDocument($url_packages);

    $tr = $doc->getElementsByTagName('tr');

    $size = $tr->length;

    $index = 0;

    for ($i = 1; $i <= $size; $i++) {
jlopez's avatar
jlopez committed
172

root's avatar
root committed
173
        $check = $tr[$i]->nodeValue;
jlopez's avatar
jlopez committed
174

root's avatar
root committed
175
176
177
178
179
180
	    if($check == null) {
           continue;
        }

        $name = cleanValue($tr[$i]->getElementsByTagName('td')[0]->nodeValue);
        $version = $tr[$i]->getElementsByTagName('td')[1]->nodeValue;
jlopez's avatar
jlopez committed
181
182
183
184

        if($name != null && $name != "") {

            $description = "";
root's avatar
root committed
185
            $link = "https://pkgs.alpinelinux.org/package/v".$version_num."/main/x86_64/".$name;
jlopez's avatar
jlopez committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
            $other = $os_name.":".$version_num;
            $package = new Package($name, $version, $description, $link, $other);
            $results[] = $package;
            $index++;

            if($index == $limit) {
                break;
            }
        }
    }

    return $results;
}

jlopez's avatar
jlopez committed
200
201
202
203
204
205
206
207
208
209
210
211
/**
 * Find all packages from ubuntu or debian
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
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
function findUbuntuDebianPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $filter = "all";

    if($onlyname) {
        $filter = "names";
    }

    $basic_url = "";

    if($os_name == "ubuntu") {
        $basic_url = "https://packages.ubuntu.com";
    }
    else if($os_name == "debian") {
        $basic_url = "https://packages.debian.org";
    }

    $url_packages = $basic_url."/search?suite=".$version_name."&section=all&arch=any&keywords=".$keyword."&searchon=".$filter;

    $doc = getDocument($url_packages);

    $list = $doc->getElementById('psearchres');

    $names = $list->getElementsByTagName('h3');
    $uls = $list->getElementsByTagName('ul');


    $size = $names->length;

    if($names->length != $uls->length) {
        $size = min($names->length, $uls->length);
    }

    $size = min($size, $limit);

    for ($i = 0; $i <= $size; $i++) {
        $name = substr($names[$i]->nodeValue, 8);

        if($name) {

            $description = $uls[$i]->childNodes[1]->childNodes[1]->nodeValue;
            $description = cleanValue(substr($description, 1, -2));    
            $version = $uls[$i]->childNodes[1]->childNodes[5]->nodeValue;

            if($version == null) {
                $version = $uls[$i]->childNodes[1]->childNodes[4]->nodeValue;
            }

            if($version == null) {
                $version = $uls[$i]->childNodes[1]->childNodes[3]->nodeValue;
            }
            
            if($version == null) {
                $version = "";
            } else {
                $version = explode (":", $version)[0];
            }

            $link = $basic_url."/".$version_name."/".$name;

            $other = $os_name.":".$version_num;

            $package = new Package($name, $version, $description, $link, $other);

            $results[] = $package;
    
        }

    }

    return $results;
} 

jlopez's avatar
jlopez committed
287
288
289
290
291
292
293
294
295
296
297
298
299

/**
 * Find all packages from python
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
function findPythonPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $basic_url = "https://pypi.org/search/?q=";
    
    $url_packages = $basic_url.$keyword;

    $doc = getDocument($url_packages);

    $tr = $doc->getElementsByTagName('li');

    $size = $tr->length;

    $index = 0;

jlopez's avatar
jlopez committed
316
    for ($i = 0; $i <= $size-1; $i++) {
jlopez's avatar
jlopez committed
317
318
319

        $vv = $tr[$i]->getElementsByTagName('span');

jlopez's avatar
jlopez committed
320
        if($vv->length != 0) {
jlopez's avatar
jlopez committed
321

jlopez's avatar
jlopez committed
322
            $name = $vv[0];
jlopez's avatar
jlopez committed
323

jlopez's avatar
jlopez committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
            $description = $tr[$i]->getElementsByTagName('p');

            if($name != null) {

                $name = $name->nodeValue;
                $version = $vv[1]->nodeValue;
                $description = $description[0]->nodeValue;
                $link = "https://pypi.org/project/$name";
                $other = "python";

                $package = new Package($name, $version, $description, $link, $other);
                $results[] = $package;

                $index++;
            }

            if($index == $limit) {
                break;
            }
        }
    }
jlopez's avatar
jlopez committed
345
346
347
348

    return $results;
}

jlopez's avatar
jlopez committed
349
350
351
352
353
354
355
356
357
358
359
360
/**
 * Find all R packages from CRAN, Bioconductor, Rforge or Github
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
function findRPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $repos = "";

    if($os_name == "CRAN") {
        $repos = "cran";
    } 
    else if($os_name == "Bioconductor") {
        $repos = "bioc";
    }
    else if($os_name == "Rforge") {
        $repos = "rforge";
    }
    else if($os_name == "Github") {
        $repos = "github";
    }

jlopez's avatar
jlopez committed
378
    $results = [];
jlopez's avatar
jlopez committed
379

jlopez's avatar
jlopez committed
380
    if($repos == "cran") {
jlopez's avatar
jlopez committed
381

jlopez's avatar
Fix #16    
jlopez committed
382
383
384
385
386
387
        $json = file_get_contents("http://seer.r-pkg.org:9200/cran-devel/package/_search?size=$limit&q=Package:$keyword*");
        $json = json_decode($json, true);

        if($json["hits"]) {

            $packages = $json["hits"]["hits"];
jlopez's avatar
jlopez committed
388

jlopez's avatar
Fix #16    
jlopez committed
389
            foreach ($packages as $package){
jlopez's avatar
jlopez committed
390

jlopez's avatar
Fix #16    
jlopez committed
391
                $source = $package["_source"];
jlopez's avatar
jlopez committed
392

jlopez's avatar
Fix #16    
jlopez committed
393
                $name = $source["Package"];
jlopez's avatar
jlopez committed
394
                $version = $source["Version"];
jlopez's avatar
jlopez committed
395
                $description = $source["Description"];
jlopez's avatar
Fix #16    
jlopez committed
396
                $link = "https://cran.r-project.org/web/packages/$name/index.html";
jlopez's avatar
jlopez committed
397
                $other = "Depends: ".$source["Depends"]."<br/>Imports : ".$source["Imports"];
jlopez's avatar
jlopez committed
398

jlopez's avatar
jlopez committed
399
400
401
402
                $package = new Package($name, $version, $description, $link, $other);
                $results[] = $package;
            }
        }
jlopez's avatar
jlopez committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    }
    else if($repos == "bioc") {

        $url_packages = "https://www.bioconductor.org/packages/release/bioc/";

        $doc = getDocument($url_packages);

        $trs = $doc->getElementsByTagName('tr');

        $size = $trs->length - 1;

        $index = 0;

        for ($i = 1; $i <= $size; $i++) {
            $name = $trs[$i]->childNodes[1]->nodeValue;

            if(strpos($name, $keyword) !== false) {

                $description = $trs[$i]->childNodes[5]->nodeValue;

                $version = "";

                $link = "https://www.bioconductor.org/packages/release/bioc/html/".$name.".html";

                $other =  $trs[$i]->childNodes[3]->nodeValue;

                $package = new Package($name, $version, $description, $link, $other);
                $results[] = $package;
                
                $index++;

            }

            if($index == $limit) {
                break;
            }
        }

jlopez's avatar
jlopez committed
441
442
443
444
445
446
    }

    return $results;

}

jlopez's avatar
jlopez committed
447
448
449
450
451
452
453
454
455
456
457
458
/**
 * Find all packages from CPAN
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
function findCPANPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $basic_url = "https://metacpan.org/search?size=".$limit."&q=";
    $url_packages = $basic_url.$keyword;

    $doc = getDocument($url_packages);
    $bigs = $doc->getElementsByTagName('big');
    $spans = $doc->getElementsByTagName('span');
    $finder = new DomXPath($doc);
    $classname="relatize";
    $spans = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");

    $size = $bigs->length - 1;

    for ($i = 0; $i <= $size; $i++) {
        $splt = explode (" - ", $bigs[$i]->nodeValue);

        $name = cleanValue($splt[0]);
        $name = str_replace(" ", "", $name);
        $description = cleanValue($splt[1]);
        $link = "https://metacpan.org/pod/".$name;
        $version = "";
        $other = $spans[$i]->childNodes[0]->nodeValue;

        $package = new Package($name, $version, $description, $link, $other);
        $results[] = $package;
    }

    return $results;
}

jlopez's avatar
jlopez committed
492
493
494
495
496
497
498
499
500
501
502
503
/**
 * Find all packages from NPM
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
function findNPMPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $basic_url = "https://www.npmjs.com/search?q=";
    $url_packages = $basic_url.$keyword;
    $doc = getDocument($url_packages);
    $section = $doc->getElementsByTagName('section');
    $size = $section->length;

    $index = 0;

    for ($i = 0; $i <= $size; $i++) {

        $vv = $section[$i]->childNodes[1];
        $name = $vv->childNodes[0]->childNodes[0]->nodeValue;
        $description = $vv->childNodes[2]->childNodes[0]->nodeValue;
        $version == "";

        if($vv->childNodes->length == 5) {
            $version = $vv->childNodes[4]->childNodes[1]->nodeValue;
        } else if($vv->childNodes->length == 7) {
            $version = $vv->childNodes[6]->childNodes[1]->nodeValue;
        }

        $other = explode (" ", $version);
        $version = explode (" ", $version)[1];
        
        $other = $other[3]." ".$other[4]." ".$other[5];

        $link = "https://www.npmjs.com/package/".$name;
    
        $package = new Package($name, $version, $description, $link, $other);
        $results[] = $package;
        
        $index++;
    
        if($index == $limit) {
            break;
        }

    }

    return $results;
}

jlopez's avatar
jlopez committed
550
551
552
553
554
555
556
557
558
559
560
561
/**
 * Find all packages from Ruby
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
function findRubyPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $basic_url = "https://rubygems.org/search?page=1&query=";
    
    $url_packages = $basic_url.$keyword;

    $doc = getDocument($url_packages);

    $tr = $doc->getElementsByTagName('a');

    $size = $tr->length;

    $index = 0;

    for ($i = 9; $i <= $size; $i++) {

        $vv = $tr[$i]->childNodes[1];

        $name = cleanValue($vv->childNodes[1]->childNodes[0]->nodeValue);

        if($name != "" && $name != null) {
            $version = $vv->childNodes[1]->childNodes[1]->nodeValue;

            $description = $vv->childNodes[3]->nodeValue;
    
            $other = $tr[$i]->childNodes[3]->childNodes[0]->nodeValue;
    
            $link = "https://rubygems.org/gems/".$name;
    
            $package = new Package($name, $version, $description, $link, $other);
            $results[] = $package;
    
            $index++;
    
            if($index == $limit) {
                break;
            }
        }
    }

    $doc = getDocument($url_packages);

    return $results;

    
}

jlopez's avatar
jlopez committed
611
612
613
614
615
616
617
618
619
620
621
622
/**
 * Find all packages from Conda
 * 
 * @param string $os_name The name of the distribution
 * @param string $version_num The version of the distribution
 * @param string $version_name The version name of the distribution
 * @param string $keyword The keyword to search for in all packages of the distribution
 * @param int $limit The limit of package found
 * @param bool $onlyname If your search only on the name of the package. If the api of the distribution have this possibility.
 * 
 * @return array
 */
jlopez's avatar
jlopez committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
function findCondaPackage($os_name, $version_num, $version_name, $keyword, $limit, $onlyname) {

    $results = array();

    $basic_url = "https://anaconda.org/search?q=access%3Apublic+platform%3Alinux-64+type%3Aconda+";

    $url_packages = $basic_url.$keyword;

    $doc = getDocument($url_packages);

    $tr = $doc->getElementsByTagName('tr');

    $size = $tr->length;

    $index = 0;

    for ($i = 1; $i <= $size; $i++) {

jlopez's avatar
jlopez committed
641
        $check = $tr[$i]->nodeValue;
jlopez's avatar
jlopez committed
642

jlopez's avatar
jlopez committed
643
        if($check == null) {          
jlopez's avatar
jlopez committed
644
           continue;
root's avatar
root committed
645
         }
jlopez's avatar
jlopez committed
646
647

        $vv = $tr[$i]->getElementsByTagName('a')[1];
jlopez's avatar
jlopez committed
648
649
650
651

        $author = cleanValue($tr[$i]->getElementsByTagName('a')[0]->nodeValue);

        $name = $author . "/" . $vv->childNodes[1]->nodeValue;
jlopez's avatar
jlopez committed
652
        $version = $vv->childNodes[3]->nodeValue;
jlopez's avatar
jlopez committed
653

jlopez's avatar
jlopez committed
654
        $description = cleanValue($vv->parentNode->parentNode->childNodes[2]->nodeValue);
jlopez's avatar
jlopez committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
        $other = $tr[$i]->childNodes[3]->nodeValue;

        $link = "https://anaconda.org/conda-forge/".$name;

        $package = new Package($name, $version, $description, $link, $other);
        array_push($results, $package);
        $index++;

        if($index == $limit) {
            return $results;
        }
    }
    
    return $results;
root's avatar
root committed
669
}