Drop suffixes from confirm parameters
[aur.git] / web / lib / pkgfuncs.inc.php
blobb467a3d3351f796fe311d0c619f5bfb0b6ee9da4
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
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 PackageBases.PackagerUID IS NOT NULL ';
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 $count = 0;
605 $q_keywords = "";
606 $op = "";
608 foreach (str_getcsv($_GET['K'], ' ') as $term) {
609 if ($term == "") {
610 continue;
612 if ($count > 0 && strtolower($term) == "and") {
613 $op = "AND ";
614 continue;
616 if ($count > 0 && strtolower($term) == "or") {
617 $op = "OR ";
618 continue;
620 if ($count > 0 && strtolower($term) == "not") {
621 $op .= "NOT ";
622 continue;
625 $term = "%" . addcslashes($term, '%_') . "%";
626 $q_keywords .= $op . " (Packages.Name LIKE " . $dbh->quote($term) . " OR ";
627 $q_keywords .= "Description LIKE " . $dbh->quote($term) . ") ";
629 $count++;
630 if ($count >= 20) {
631 break;
633 $op = "AND ";
636 if (!empty($q_keywords)) {
637 $q_where .= "AND (" . $q_keywords . ") ";
642 if (isset($_GET["do_Orphans"])) {
643 $q_where .= "AND MaintainerUID IS NULL ";
646 if (isset($_GET['outdated'])) {
647 if ($_GET['outdated'] == 'on') {
648 $q_where .= "AND OutOfDateTS IS NOT NULL ";
650 elseif ($_GET['outdated'] == 'off') {
651 $q_where .= "AND OutOfDateTS IS NULL ";
655 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
657 $q_sort = "ORDER BY ";
658 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
659 switch ($sort_by) {
660 case 'c':
661 $q_sort .= "CategoryID " . $order . ", ";
662 break;
663 case 'v':
664 $q_sort .= "NumVotes " . $order . ", ";
665 break;
666 case 'w':
667 if ($SID) {
668 $q_sort .= "Voted " . $order . ", ";
670 break;
671 case 'o':
672 if ($SID) {
673 $q_sort .= "Notify " . $order . ", ";
675 break;
676 case 'm':
677 $q_sort .= "Maintainer " . $order . ", ";
678 break;
679 case 'a':
680 $q_sort .= "ModifiedTS " . $order . ", ";
681 break;
682 default:
683 break;
685 $q_sort .= " Packages.Name " . $order . " ";
687 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
689 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
690 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
692 $result = $dbh->query($q);
693 $result_t = $dbh->query($q_total);
694 if ($result_t) {
695 $row = $result_t->fetch(PDO::FETCH_NUM);
696 $total = $row[0];
698 else {
699 $total = 0;
702 if ($result && $total > 0) {
703 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
704 $SO_next = "a";
706 else {
707 $SO_next = "d";
711 /* Calculate the results to use. */
712 $first = $_GET['O'] + 1;
714 /* Calculation of pagination links. */
715 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
716 $current = ceil($first / $per_page);
717 $pages = ceil($total / $per_page);
718 $templ_pages = array();
720 if ($current > 1) {
721 $templ_pages['&laquo; ' . __('First')] = 0;
722 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
725 if ($current - 5 > 1)
726 $templ_pages["..."] = false;
728 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
729 $templ_pages[$i] = ($i - 1) * $per_page;
732 if ($current + 5 < $pages)
733 $templ_pages["... "] = false;
735 if ($current < $pages) {
736 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
737 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
740 include('pkg_search_form.php');
742 if ($result) {
743 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
744 $searchresults[] = $row;
748 include('pkg_search_results.php');
750 return;
754 * Determine if a POST string has been sent by a visitor
756 * @param string $action String to check has been sent via POST
758 * @return bool True if the POST string was used, otherwise false
760 function current_action($action) {
761 return (isset($_POST['action']) && $_POST['action'] == $action) ||
762 isset($_POST[$action]);
766 * Determine if sent IDs are valid integers
768 * @param array $ids IDs to validate
770 * @return array All sent IDs that are valid integers
772 function sanitize_ids($ids) {
773 $new_ids = array();
774 foreach ($ids as $id) {
775 $id = intval($id);
776 if ($id > 0) {
777 $new_ids[] = $id;
780 return $new_ids;
784 * Add package information to the database for a specific package
786 * @param int $base_id ID of the package base
787 * @param string $pkgname Name of the new package
788 * @param string $pkgver Version of the new package
789 * @param string $pkgdesc Description of the new package
790 * @param string $pkgurl Upstream URL for the new package
792 * @return int ID of the new package
794 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
795 $dbh = DB::connect();
796 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
797 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
798 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
799 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
800 $dbh->exec($q);
801 return $dbh->lastInsertId();
805 * Add a dependency for a specific package to the database
807 * @param int $pkgid The package ID to add the dependency for
808 * @param string $type The type of dependency to add
809 * @param string $depname The name of the dependency to add
810 * @param string $depcondition The type of dependency for the package
811 * @param string $deparch The architecture of the dependency to add
813 * @return void
815 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
816 $dbh = DB::connect();
817 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
818 $pkgid,
819 pkg_dependency_type_id_from_name($type),
820 $dbh->quote($depname),
821 $dbh->quote($depcondition),
822 $deparch ? $dbh->quote($deparch) : 'NULL'
824 $dbh->exec($q);
828 * Add a relation for a specific package to the database
830 * @param int $pkgid The package ID to add the relation for
831 * @param string $type The type of relation to add
832 * @param string $relname The name of the relation to add
833 * @param string $relcondition The version requirement of the relation
834 * @param string $relarch The architecture of the relation to add
836 * @return void
838 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
839 $dbh = DB::connect();
840 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
841 $pkgid,
842 pkg_relation_type_id_from_name($type),
843 $dbh->quote($relname),
844 $dbh->quote($relcondition),
845 $relarch ? $dbh->quote($relarch) : 'NULL'
847 $dbh->exec($q);
851 * Add a source for a specific package to the database
853 * @param int $pkgid The package ID to add the source for
854 * @param string $pkgsrc The package source to add to the database
855 * @param string $srcarch The architecture of the source to add
857 * @return void
859 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
860 $dbh = DB::connect();
861 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
862 $pkgid,
863 $dbh->quote($pkgsrc),
864 $srcarch ? $dbh->quote($srcarch) : 'NULL'
866 $dbh->exec($q);
870 * Creates a new group and returns its ID
872 * If the groups already exists, the ID of the already existing group is
873 * returned.
875 * @param string $name The name of the group to create
877 * @return int The ID of the group
879 function pkg_create_group($name) {
880 $dbh = DB::connect();
881 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
882 $result = $dbh->query($q);
883 if ($result) {
884 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
885 if ($grpid > 0) {
886 return $grpid;
890 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
891 $dbh->exec($q);
892 return $dbh->lastInsertId();
896 * Add a package to a group
898 * @param int $pkgid The package ID of the package to add
899 * @param int $grpid The group ID of the group to add the package to
901 * @return void
903 function pkg_add_grp($pkgid, $grpid) {
904 $dbh = DB::connect();
905 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
906 $pkgid,
907 $grpid
909 $dbh->exec($q);
913 * Creates a new license and returns its ID
915 * If the license already exists, the ID of the already existing license is
916 * returned.
918 * @param string $name The name of the license to create
920 * @return int The ID of the license
922 function pkg_create_license($name) {
923 $dbh = DB::connect();
924 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
925 $result = $dbh->query($q);
926 if ($result) {
927 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
928 if ($licid > 0) {
929 return $licid;
933 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
934 $dbh->exec($q);
935 return $dbh->lastInsertId();
939 * Add a license to a package
941 * @param int $pkgid The package ID of the package
942 * @param int $grpid The ID of the license to add
944 * @return void
946 function pkg_add_lic($pkgid, $licid) {
947 $dbh = DB::connect();
948 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
949 $pkgid,
950 $licid
952 $dbh->exec($q);
956 * Determine package information for latest package
958 * @param int $numpkgs Number of packages to get information on
960 * @return array $packages Package info for the specified number of recent packages
962 function latest_pkgs($numpkgs) {
963 $dbh = DB::connect();
965 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
966 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
967 $q.= "PackageBases.ID = Packages.PackageBaseID ";
968 $q.= "ORDER BY SubmittedTS DESC ";
969 $q.= "LIMIT " . intval($numpkgs);
970 $result = $dbh->query($q);
972 $packages = array();
973 if ($result) {
974 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
975 $packages[] = $row;
979 return $packages;