Show package popularity in search results
[aur.git] / web / lib / pkgfuncs.inc.php
blobf52757888655c6d90910d1d3aff18280129b8ab3
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, PackageBases.Popularity, Packages.ID,
549 Packages.PackageBaseID, 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 'p':
667 $q_sort .= "Popularity " . $order . ", ";
668 break;
669 case 'w':
670 if ($SID) {
671 $q_sort .= "Voted " . $order . ", ";
673 break;
674 case 'o':
675 if ($SID) {
676 $q_sort .= "Notify " . $order . ", ";
678 break;
679 case 'm':
680 $q_sort .= "Maintainer " . $order . ", ";
681 break;
682 case 'a':
683 $q_sort .= "-ModifiedTS " . $order . ", ";
684 break;
685 default:
686 break;
688 $q_sort .= " Packages.Name " . $order . " ";
690 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
692 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
693 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
695 $result = $dbh->query($q);
696 $result_t = $dbh->query($q_total);
697 if ($result_t) {
698 $row = $result_t->fetch(PDO::FETCH_NUM);
699 $total = $row[0];
701 else {
702 $total = 0;
705 if ($result && $total > 0) {
706 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
707 $SO_next = "a";
709 else {
710 $SO_next = "d";
714 /* Calculate the results to use. */
715 $first = $_GET['O'] + 1;
717 /* Calculation of pagination links. */
718 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
719 $current = ceil($first / $per_page);
720 $pages = ceil($total / $per_page);
721 $templ_pages = array();
723 if ($current > 1) {
724 $templ_pages['&laquo; ' . __('First')] = 0;
725 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
728 if ($current - 5 > 1)
729 $templ_pages["..."] = false;
731 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
732 $templ_pages[$i] = ($i - 1) * $per_page;
735 if ($current + 5 < $pages)
736 $templ_pages["... "] = false;
738 if ($current < $pages) {
739 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
740 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
743 include('pkg_search_form.php');
745 if ($result) {
746 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
747 $searchresults[] = $row;
751 include('pkg_search_results.php');
753 return;
757 * Determine if a POST string has been sent by a visitor
759 * @param string $action String to check has been sent via POST
761 * @return bool True if the POST string was used, otherwise false
763 function current_action($action) {
764 return (isset($_POST['action']) && $_POST['action'] == $action) ||
765 isset($_POST[$action]);
769 * Determine if sent IDs are valid integers
771 * @param array $ids IDs to validate
773 * @return array All sent IDs that are valid integers
775 function sanitize_ids($ids) {
776 $new_ids = array();
777 foreach ($ids as $id) {
778 $id = intval($id);
779 if ($id > 0) {
780 $new_ids[] = $id;
783 return $new_ids;
787 * Add package information to the database for a specific package
789 * @param int $base_id ID of the package base
790 * @param string $pkgname Name of the new package
791 * @param string $pkgver Version of the new package
792 * @param string $pkgdesc Description of the new package
793 * @param string $pkgurl Upstream URL for the new package
795 * @return int ID of the new package
797 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
798 $dbh = DB::connect();
799 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
800 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
801 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
802 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
803 $dbh->exec($q);
804 return $dbh->lastInsertId();
808 * Add a dependency for a specific package to the database
810 * @param int $pkgid The package ID to add the dependency for
811 * @param string $type The type of dependency to add
812 * @param string $depname The name of the dependency to add
813 * @param string $depcondition The type of dependency for the package
814 * @param string $deparch The architecture of the dependency to add
816 * @return void
818 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
819 $dbh = DB::connect();
820 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
821 $pkgid,
822 pkg_dependency_type_id_from_name($type),
823 $dbh->quote($depname),
824 $dbh->quote($depcondition),
825 $deparch ? $dbh->quote($deparch) : 'NULL'
827 $dbh->exec($q);
831 * Add a relation for a specific package to the database
833 * @param int $pkgid The package ID to add the relation for
834 * @param string $type The type of relation to add
835 * @param string $relname The name of the relation to add
836 * @param string $relcondition The version requirement of the relation
837 * @param string $relarch The architecture of the relation to add
839 * @return void
841 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
842 $dbh = DB::connect();
843 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
844 $pkgid,
845 pkg_relation_type_id_from_name($type),
846 $dbh->quote($relname),
847 $dbh->quote($relcondition),
848 $relarch ? $dbh->quote($relarch) : 'NULL'
850 $dbh->exec($q);
854 * Add a source for a specific package to the database
856 * @param int $pkgid The package ID to add the source for
857 * @param string $pkgsrc The package source to add to the database
858 * @param string $srcarch The architecture of the source to add
860 * @return void
862 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
863 $dbh = DB::connect();
864 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
865 $pkgid,
866 $dbh->quote($pkgsrc),
867 $srcarch ? $dbh->quote($srcarch) : 'NULL'
869 $dbh->exec($q);
873 * Creates a new group and returns its ID
875 * If the groups already exists, the ID of the already existing group is
876 * returned.
878 * @param string $name The name of the group to create
880 * @return int The ID of the group
882 function pkg_create_group($name) {
883 $dbh = DB::connect();
884 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
885 $result = $dbh->query($q);
886 if ($result) {
887 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
888 if ($grpid > 0) {
889 return $grpid;
893 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
894 $dbh->exec($q);
895 return $dbh->lastInsertId();
899 * Add a package to a group
901 * @param int $pkgid The package ID of the package to add
902 * @param int $grpid The group ID of the group to add the package to
904 * @return void
906 function pkg_add_grp($pkgid, $grpid) {
907 $dbh = DB::connect();
908 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
909 $pkgid,
910 $grpid
912 $dbh->exec($q);
916 * Creates a new license and returns its ID
918 * If the license already exists, the ID of the already existing license is
919 * returned.
921 * @param string $name The name of the license to create
923 * @return int The ID of the license
925 function pkg_create_license($name) {
926 $dbh = DB::connect();
927 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
928 $result = $dbh->query($q);
929 if ($result) {
930 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
931 if ($licid > 0) {
932 return $licid;
936 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
937 $dbh->exec($q);
938 return $dbh->lastInsertId();
942 * Add a license to a package
944 * @param int $pkgid The package ID of the package
945 * @param int $grpid The ID of the license to add
947 * @return void
949 function pkg_add_lic($pkgid, $licid) {
950 $dbh = DB::connect();
951 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
952 $pkgid,
953 $licid
955 $dbh->exec($q);
959 * Determine package information for latest package
961 * @param int $numpkgs Number of packages to get information on
963 * @return array $packages Package info for the specified number of recent packages
965 function latest_pkgs($numpkgs) {
966 $dbh = DB::connect();
968 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
969 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
970 $q.= "PackageBases.ID = Packages.PackageBaseID ";
971 $q.= "ORDER BY SubmittedTS DESC ";
972 $q.= "LIMIT " . intval($numpkgs);
973 $result = $dbh->query($q);
975 $packages = array();
976 if ($result) {
977 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
978 $packages[] = $row;
982 return $packages;