Use Git repositories to store packages
[aur.git] / web / lib / pkgfuncs.inc.php
blob0c7f2d9e4fa08baad38cd1e87f69a15d9dd8b105
1 <?php
3 include_once("pkgbasefuncs.inc.php");
5 /**
6 * Determine if the user can delete a specific package comment
8 * Only the comment submitter, Trusted Users, and Developers can delete
9 * comments. This function is used for the backend side of comment deletion.
11 * @param string $comment_id The comment ID in the database
13 * @return bool True if the user can delete the comment, otherwise false
15 function can_delete_comment($comment_id=0) {
16 $dbh = DB::connect();
18 $q = "SELECT UsersID FROM PackageComments ";
19 $q.= "WHERE ID = " . intval($comment_id);
20 $result = $dbh->query($q);
22 if (!$result) {
23 return false;
26 $uid = $result->fetch(PDO::FETCH_COLUMN, 0);
28 return has_credential(CRED_COMMENT_DELETE, array($uid));
31 /**
32 * Determine if the user can delete a specific package comment using an array
34 * Only the comment submitter, Trusted Users, and Developers can delete
35 * comments. This function is used for the frontend side of comment deletion.
37 * @param array $comment All database information relating a specific comment
39 * @return bool True if the user can delete the comment, otherwise false
41 function can_delete_comment_array($comment) {
42 return has_credential(CRED_COMMENT_DELETE, array($comment['UsersID']));
45 /**
46 * Determine if the visitor can submit blacklisted packages.
48 * Only Trusted Users and Developers can delete blacklisted packages. Packages
49 * are blacklisted if they are include in the official repositories.
51 * @return bool True if the user can submit blacklisted packages, otherwise false
53 function can_submit_blacklisted() {
54 return has_credential(CRED_PKGBASE_SUBMIT_BLACKLISTED);
57 /**
58 * Check to see if the package name already exists in the database
60 * @param string $name The package name to check
62 * @return string|void Package name if it already exists
64 function pkg_from_name($name="") {
65 if (!$name) {return NULL;}
66 $dbh = DB::connect();
67 $q = "SELECT ID FROM Packages ";
68 $q.= "WHERE Name = " . $dbh->quote($name);
69 $result = $dbh->query($q);
70 if (!$result) {
71 return;
73 $row = $result->fetch(PDO::FETCH_NUM);
74 return $row[0];
77 /**
78 * Get licenses for a specific package
80 * @param int $pkgid The package to get licenses for
82 * @return array All licenses for the package
84 function pkg_licenses($pkgid) {
85 $lics = array();
86 $pkgid = intval($pkgid);
87 if ($pkgid > 0) {
88 $dbh = DB::connect();
89 $q = "SELECT l.Name FROM Licenses l ";
90 $q.= "INNER JOIN PackageLicenses pl ON pl.LicenseID = l.ID ";
91 $q.= "WHERE pl.PackageID = ". $pkgid;
92 $result = $dbh->query($q);
93 if (!$result) {
94 return array();
96 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
97 $lics[] = $row;
100 return $lics;
104 * Get package groups for a specific package
106 * @param int $pkgid The package to get groups for
108 * @return array All package groups for the package
110 function pkg_groups($pkgid) {
111 $grps = array();
112 $pkgid = intval($pkgid);
113 if ($pkgid > 0) {
114 $dbh = DB::connect();
115 $q = "SELECT g.Name FROM Groups g ";
116 $q.= "INNER JOIN PackageGroups pg ON pg.GroupID = g.ID ";
117 $q.= "WHERE pg.PackageID = ". $pkgid;
118 $result = $dbh->query($q);
119 if (!$result) {
120 return array();
122 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
123 $grps[] = $row;
126 return $grps;
130 * Get package dependencies for a specific package
132 * @param int $pkgid The package to get dependencies for
134 * @return array All package dependencies for the package
136 function pkg_dependencies($pkgid) {
137 $deps = array();
138 $pkgid = intval($pkgid);
139 if ($pkgid > 0) {
140 $dbh = DB::connect();
141 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
142 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
143 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = p.Name ";
144 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
145 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
146 $q.= "ORDER BY pd.DepName";
147 $result = $dbh->query($q);
148 if (!$result) {
149 return array();
151 while ($row = $result->fetch(PDO::FETCH_NUM)) {
152 $deps[] = $row;
155 return $deps;
159 * Get package relations for a specific package
161 * @param int $pkgid The package to get relations for
163 * @return array All package relations for the package
165 function pkg_relations($pkgid) {
166 $rels = array();
167 $pkgid = intval($pkgid);
168 if ($pkgid > 0) {
169 $dbh = DB::connect();
170 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, pr.RelArch, p.ID FROM PackageRelations pr ";
171 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
172 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
173 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
174 $q.= "ORDER BY pr.RelName";
175 $result = $dbh->query($q);
176 if (!$result) {
177 return array();
179 while ($row = $result->fetch(PDO::FETCH_NUM)) {
180 $rels[] = $row;
183 return $rels;
187 * Get the HTML code to display a package dependency link
189 * @param string $name The name of the dependency
190 * @param string $type The name of the dependency type
191 * @param string $cond The package dependency condition string
192 * @param string $arch The package dependency architecture
193 * @param int $pkg_id The package of the package to display the dependency for
195 * @return string The HTML code of the label to display
197 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id) {
198 if ($type == 'optdepends' && strpos($name, ':') !== false) {
199 $tokens = explode(':', $name, 2);
200 $name = $tokens[0];
201 $desc = $tokens[1];
202 } else {
203 $desc = '(unknown)';
206 $link = '<a href="';
207 if (is_null($pkg_id)) {
208 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
209 } else {
210 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
212 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
213 $link .= htmlspecialchars($name) . '</a>';
214 $link .= htmlspecialchars($cond);
216 if ($type != 'depends' || $arch) {
217 $link .= ' <em>(';
219 if ($type == 'makedepends') {
220 $link .= 'make';
221 } elseif ($type == 'checkdepends') {
222 $link .= 'check';
223 } elseif ($type == 'optdepends') {
224 $link .= 'optional';
227 if ($type != 'depends' && $arch) {
228 $link .= ', ';
231 if ($arch) {
232 $link .= htmlspecialchars($arch);
235 $link .= ')';
236 if ($type == 'optdepends') {
237 $link .= ' &ndash; ' . htmlspecialchars($desc) . ' </em>';
239 $link .= '</em>';
242 return $link;
246 * Get the HTML code to display a package relation
248 * @param string $name The name of the relation
249 * @param string $cond The package relation condition string
250 * @param string $arch The package relation architecture
252 * @return string The HTML code of the label to display
254 function pkg_rel_html($name, $cond, $arch) {
255 $html = htmlspecialchars($name) . htmlspecialchars($cond);
257 if ($arch) {
258 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
261 return $html;
265 * Get the HTML code to display a source link
267 * @param string $url The URL of the source
268 * @param string $arch The source architecture
270 * @return string The HTML code of the label to display
272 function pkg_source_link($url, $arch) {
273 $url = explode('::', $url);
274 $parsed_url = parse_url($url[0]);
276 if (isset($parsed_url['scheme']) || isset($url[1])) {
277 $link = '<a href="' . htmlspecialchars((isset($url[1]) ? $url[1] : $url[0]), ENT_QUOTES) . '">' . htmlspecialchars($url[0]) . '</a>';
278 } else {
279 $link = htmlspecialchars($url[0]);
282 if ($arch) {
283 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
286 return $link;
290 * Determine packages that depend on a package
292 * @param string $name The package name for the dependency search
294 * @return array All packages that depend on the specified package name
296 function pkg_required($name="") {
297 $deps = array();
298 if ($name != "") {
299 $dbh = DB::connect();
300 $q = "SELECT DISTINCT p.Name, PackageID FROM PackageDepends pd ";
301 $q.= "JOIN Packages p ON pd.PackageID = p.ID ";
302 $q.= "WHERE DepName = " . $dbh->quote($name) . " ";
303 $q.= "ORDER BY p.Name";
304 $result = $dbh->query($q);
305 if (!$result) {return array();}
306 while ($row = $result->fetch(PDO::FETCH_NUM)) {
307 $deps[] = $row;
310 return $deps;
314 * Get all package sources for a specific package
316 * @param string $pkgid The package ID to get the sources for
318 * @return array All sources associated with a specific package
320 function pkg_sources($pkgid) {
321 $sources = array();
322 $pkgid = intval($pkgid);
323 if ($pkgid > 0) {
324 $dbh = DB::connect();
325 $q = "SELECT Source, SourceArch FROM PackageSources ";
326 $q.= "WHERE PackageID = " . $pkgid;
327 $q.= " ORDER BY Source";
328 $result = $dbh->query($q);
329 if (!$result) {
330 return array();
332 while ($row = $result->fetch(PDO::FETCH_NUM)) {
333 $sources[] = $row;
336 return $sources;
340 * Determine package names from package IDs
342 * @param string|array $pkgids The package IDs to get names for
344 * @return array|string All names if multiple package IDs, otherwise package name
346 function pkg_name_from_id($pkgids) {
347 if (is_array($pkgids)) {
348 $pkgids = sanitize_ids($pkgids);
349 $names = array();
350 $dbh = DB::connect();
351 $q = "SELECT Name FROM Packages WHERE ID IN (";
352 $q.= implode(",", $pkgids) . ")";
353 $result = $dbh->query($q);
354 if ($result) {
355 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
356 $names[] = $row['Name'];
359 return $names;
361 elseif ($pkgids > 0) {
362 $dbh = DB::connect();
363 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
364 $result = $dbh->query($q);
365 if ($result) {
366 $name = $result->fetch(PDO::FETCH_NUM);
368 return $name[0];
370 else {
371 return NULL;
376 * Determine if a package name is on the database blacklist
378 * @param string $name The package name to check
380 * @return bool True if the name is blacklisted, otherwise false
382 function pkg_name_is_blacklisted($name) {
383 $dbh = DB::connect();
384 $q = "SELECT COUNT(*) FROM PackageBlacklist ";
385 $q.= "WHERE Name = " . $dbh->quote($name);
386 $result = $dbh->query($q);
388 if (!$result) return false;
389 return ($result->fetchColumn() > 0);
393 * Get the package details
395 * @param string $id The package ID to get description for
397 * @return array The package's details OR error message
399 function pkg_get_details($id=0) {
400 $dbh = DB::connect();
402 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
403 $q.= "PackageBases.Name AS BaseName, PackageBases.CategoryID, ";
404 $q.= "PackageBases.NumVotes, PackageBases.OutOfDateTS, ";
405 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
406 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
407 $q.= "PackageBases.PackagerUID, PackageCategories.Category, ";
408 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
409 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
410 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
411 $q.= "FROM Packages, PackageBases, PackageCategories ";
412 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
413 $q.= "AND PackageBases.CategoryID = PackageCategories.ID ";
414 $q.= "AND Packages.ID = " . intval($id);
415 $result = $dbh->query($q);
417 $row = array();
419 if (!$result) {
420 $row['error'] = __("Error retrieving package details.");
422 else {
423 $row = $result->fetch(PDO::FETCH_ASSOC);
424 if (empty($row)) {
425 $row['error'] = __("Package details could not be found.");
429 return $row;
433 * Display the package details page
435 * @param string $id The package ID to get details page for
436 * @param array $row Package details retrieved by pkg_get_details()
437 * @param string $SID The session ID of the visitor
439 * @return void
441 function pkg_display_details($id=0, $row, $SID="") {
442 $dbh = DB::connect();
444 if (isset($row['error'])) {
445 print "<p>" . $row['error'] . "</p>\n";
447 else {
448 $base_id = pkgbase_from_pkgid($id);
449 $pkgbase_name = pkgbase_name_from_id($base_id);
451 include('pkg_details.php');
453 if ($SID) {
454 include('pkg_comment_form.php');
457 $limit = isset($_GET['comments']) ? 0 : 10;
458 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
459 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
460 if (!empty($comments)) {
461 include('pkg_comments.php');
466 /* pkg_search_page(SID)
467 * outputs the body of search/search results page
469 * parameters:
470 * SID - current Session ID
471 * preconditions:
472 * package search page has been accessed
473 * request variables have not been sanitized
475 * request vars:
476 * O - starting result number
477 * PP - number of search hits per page
478 * C - package category ID number
479 * K - package search string
480 * SO - search hit sort order:
481 * values: a - ascending
482 * d - descending
483 * SB - sort search hits by:
484 * values: c - package category
485 * n - package name
486 * v - number of votes
487 * m - maintainer username
488 * SeB- property that search string (K) represents
489 * values: n - package name
490 * nd - package name & description
491 * x - package name (exact match)
492 * m - package maintainer's username
493 * s - package submitter's username
494 * do_Orphans - boolean. whether to search packages
495 * without a maintainer
498 * These two are actually handled in packages.php.
500 * IDs- integer array of ticked packages' IDs
501 * action - action to be taken on ticked packages
502 * values: do_Flag - Flag out-of-date
503 * do_UnFlag - Remove out-of-date flag
504 * do_Adopt - Adopt
505 * do_Disown - Disown
506 * do_Delete - Delete (requires confirm_Delete to be set)
507 * do_Notify - Enable notification
508 * do_UnNotify - Disable notification
510 function pkg_search_page($SID="") {
511 $dbh = DB::connect();
514 * Get commonly used variables.
515 * TODO: Reduce the number of database queries!
517 if ($SID)
518 $myuid = uid_from_sid($SID);
519 $cats = pkgbase_categories($dbh);
521 /* Sanitize paging variables. */
522 if (isset($_GET['O'])) {
523 $_GET['O'] = max(intval($_GET['O']), 0);
524 } else {
525 $_GET['O'] = 0;
528 if (isset($_GET["PP"])) {
529 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
530 } else {
531 $_GET["PP"] = 50;
535 * FIXME: Pull out DB-related code. All of it! This one's worth a
536 * choco-chip cookie, one of those nice big soft ones.
539 /* Build the package search query. */
540 $q_select = "SELECT ";
541 if ($SID) {
542 $q_select .= "CommentNotify.UserID AS Notify,
543 PackageVotes.UsersID AS Voted, ";
545 $q_select .= "Users.Username AS Maintainer,
546 PackageCategories.Category,
547 Packages.Name, Packages.Version, Packages.Description,
548 PackageBases.NumVotes, Packages.ID, Packages.PackageBaseID,
549 PackageBases.OutOfDateTS ";
551 $q_from = "FROM Packages
552 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
553 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID)
554 LEFT JOIN PackageCategories
555 ON (PackageBases.CategoryID = PackageCategories.ID) ";
556 if ($SID) {
557 /* This is not needed for the total row count query. */
558 $q_from_extra = "LEFT JOIN PackageVotes
559 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
560 LEFT JOIN CommentNotify
561 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
562 } else {
563 $q_from_extra = "";
566 $q_where = "WHERE 1 = 1 ";
568 * TODO: Possibly do string matching on category to make request
569 * variable values more sensible.
571 if (isset($_GET["C"]) && intval($_GET["C"])) {
572 $q_where .= "AND PackageBases.CategoryID = ".intval($_GET["C"])." ";
575 if (isset($_GET['K'])) {
576 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
577 /* Search by maintainer. */
578 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
580 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
581 /* Search by submitter. */
582 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
584 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
585 /* Search by name. */
586 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
587 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
589 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
590 /* Search by package base name. */
591 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
592 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
594 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
595 /* Search by name (exact match). */
596 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
598 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
599 /* Search by package base name (exact match). */
600 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
602 else {
603 /* Search by name and description (default). */
604 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
605 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . " OR ";
606 $q_where .= "Description LIKE " . $dbh->quote($K) . ") ";
610 if (isset($_GET["do_Orphans"])) {
611 $q_where .= "AND MaintainerUID IS NULL ";
614 if (isset($_GET['outdated'])) {
615 if ($_GET['outdated'] == 'on') {
616 $q_where .= "AND OutOfDateTS IS NOT NULL ";
618 elseif ($_GET['outdated'] == 'off') {
619 $q_where .= "AND OutOfDateTS IS NULL ";
623 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
625 $q_sort = "ORDER BY ";
626 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
627 switch ($sort_by) {
628 case 'c':
629 $q_sort .= "CategoryID " . $order . ", ";
630 break;
631 case 'v':
632 $q_sort .= "NumVotes " . $order . ", ";
633 break;
634 case 'w':
635 if ($SID) {
636 $q_sort .= "Voted " . $order . ", ";
638 break;
639 case 'o':
640 if ($SID) {
641 $q_sort .= "Notify " . $order . ", ";
643 break;
644 case 'm':
645 $q_sort .= "Maintainer " . $order . ", ";
646 break;
647 case 'a':
648 $q_sort .= "ModifiedTS " . $order . ", ";
649 break;
650 default:
651 break;
653 $q_sort .= " Packages.Name " . $order . " ";
655 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
657 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
658 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
660 $result = $dbh->query($q);
661 $result_t = $dbh->query($q_total);
662 if ($result_t) {
663 $row = $result_t->fetch(PDO::FETCH_NUM);
664 $total = $row[0];
666 else {
667 $total = 0;
670 if ($result && $total > 0) {
671 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
672 $SO_next = "a";
674 else {
675 $SO_next = "d";
679 /* Calculate the results to use. */
680 $first = $_GET['O'] + 1;
682 /* Calculation of pagination links. */
683 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
684 $current = ceil($first / $per_page);
685 $pages = ceil($total / $per_page);
686 $templ_pages = array();
688 if ($current > 1) {
689 $templ_pages['&laquo; ' . __('First')] = 0;
690 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
693 if ($current - 5 > 1)
694 $templ_pages["..."] = false;
696 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
697 $templ_pages[$i] = ($i - 1) * $per_page;
700 if ($current + 5 < $pages)
701 $templ_pages["... "] = false;
703 if ($current < $pages) {
704 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
705 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
708 include('pkg_search_form.php');
710 if ($result) {
711 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
712 $searchresults[] = $row;
716 include('pkg_search_results.php');
718 return;
722 * Determine if a POST string has been sent by a visitor
724 * @param string $action String to check has been sent via POST
726 * @return bool True if the POST string was used, otherwise false
728 function current_action($action) {
729 return (isset($_POST['action']) && $_POST['action'] == $action) ||
730 isset($_POST[$action]);
734 * Determine if sent IDs are valid integers
736 * @param array $ids IDs to validate
738 * @return array All sent IDs that are valid integers
740 function sanitize_ids($ids) {
741 $new_ids = array();
742 foreach ($ids as $id) {
743 $id = intval($id);
744 if ($id > 0) {
745 $new_ids[] = $id;
748 return $new_ids;
752 <<<<<<< HEAD
753 * Add package information to the database for a specific package
755 * @param int $base_id ID of the package base
756 * @param string $pkgname Name of the new package
757 * @param string $pkgver Version of the new package
758 * @param string $pkgdesc Description of the new package
759 * @param string $pkgurl Upstream URL for the new package
761 * @return int ID of the new package
763 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
764 $dbh = DB::connect();
765 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
766 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
767 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
768 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
769 $dbh->exec($q);
770 return $dbh->lastInsertId();
774 * Add a dependency for a specific package to the database
776 * @param int $pkgid The package ID to add the dependency for
777 * @param string $type The type of dependency to add
778 * @param string $depname The name of the dependency to add
779 * @param string $depcondition The type of dependency for the package
780 * @param string $deparch The architecture of the dependency to add
782 * @return void
784 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
785 $dbh = DB::connect();
786 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
787 $pkgid,
788 pkg_dependency_type_id_from_name($type),
789 $dbh->quote($depname),
790 $dbh->quote($depcondition),
791 $deparch ? $dbh->quote($deparch) : 'NULL'
793 $dbh->exec($q);
797 * Add a relation for a specific package to the database
799 * @param int $pkgid The package ID to add the relation for
800 * @param string $type The type of relation to add
801 * @param string $relname The name of the relation to add
802 * @param string $relcondition The version requirement of the relation
803 * @param string $relarch The architecture of the relation to add
805 * @return void
807 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
808 $dbh = DB::connect();
809 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
810 $pkgid,
811 pkg_relation_type_id_from_name($type),
812 $dbh->quote($relname),
813 $dbh->quote($relcondition),
814 $relarch ? $dbh->quote($relarch) : 'NULL'
816 $dbh->exec($q);
820 * Add a source for a specific package to the database
822 * @param int $pkgid The package ID to add the source for
823 * @param string $pkgsrc The package source to add to the database
824 * @param string $srcarch The architecture of the source to add
826 * @return void
828 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
829 $dbh = DB::connect();
830 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
831 $pkgid,
832 $dbh->quote($pkgsrc),
833 $srcarch ? $dbh->quote($srcarch) : 'NULL'
835 $dbh->exec($q);
839 * Creates a new group and returns its ID
841 * If the groups already exists, the ID of the already existing group is
842 * returned.
844 * @param string $name The name of the group to create
846 * @return int The ID of the group
848 function pkg_create_group($name) {
849 $dbh = DB::connect();
850 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
851 $result = $dbh->query($q);
852 if ($result) {
853 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
854 if ($grpid > 0) {
855 return $grpid;
859 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
860 $dbh->exec($q);
861 return $dbh->lastInsertId();
865 * Add a package to a group
867 * @param int $pkgid The package ID of the package to add
868 * @param int $grpid The group ID of the group to add the package to
870 * @return void
872 function pkg_add_grp($pkgid, $grpid) {
873 $dbh = DB::connect();
874 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
875 $pkgid,
876 $grpid
878 $dbh->exec($q);
882 * Creates a new license and returns its ID
884 * If the license already exists, the ID of the already existing license is
885 * returned.
887 * @param string $name The name of the license to create
889 * @return int The ID of the license
891 function pkg_create_license($name) {
892 $dbh = DB::connect();
893 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
894 $result = $dbh->query($q);
895 if ($result) {
896 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
897 if ($licid > 0) {
898 return $licid;
902 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
903 $dbh->exec($q);
904 return $dbh->lastInsertId();
908 * Add a license to a package
910 * @param int $pkgid The package ID of the package
911 * @param int $grpid The ID of the license to add
913 * @return void
915 function pkg_add_lic($pkgid, $licid) {
916 $dbh = DB::connect();
917 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
918 $pkgid,
919 $licid
921 $dbh->exec($q);
925 * Determine package information for latest package
927 * @param int $numpkgs Number of packages to get information on
929 * @return array $packages Package info for the specified number of recent packages
931 function latest_pkgs($numpkgs) {
932 $dbh = DB::connect();
934 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
935 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
936 $q.= "PackageBases.ID = Packages.PackageBaseID ";
937 $q.= "ORDER BY SubmittedTS DESC ";
938 $q.= "LIMIT " . intval($numpkgs);
939 $result = $dbh->query($q);
941 $packages = array();
942 if ($result) {
943 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
944 $packages[] = $row;
948 return $packages;