Remember user ID when flagging package bases
[aur.git] / web / lib / pkgfuncs.inc.php
blobf40347187a13b1396730803ac7badd01a783a61d
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 user can edit a specific package comment
48 * Only the comment submitter, Trusted Users, and Developers can edit
49 * comments. This function is used for the backend side of comment editing.
51 * @param string $comment_id The comment ID in the database
53 * @return bool True if the user can edit the comment, otherwise false
55 function can_edit_comment($comment_id=0) {
56 $dbh = DB::connect();
58 $q = "SELECT UsersID FROM PackageComments ";
59 $q.= "WHERE ID = " . intval($comment_id);
60 $result = $dbh->query($q);
62 if (!$result) {
63 return false;
66 $uid = $result->fetch(PDO::FETCH_COLUMN, 0);
68 return has_credential(CRED_COMMENT_EDIT, array($uid));
71 /**
72 * Determine if the user can edit a specific package comment using an array
74 * Only the comment submitter, Trusted Users, and Developers can edit
75 * comments. This function is used for the frontend side of comment editing.
77 * @param array $comment All database information relating a specific comment
79 * @return bool True if the user can edit the comment, otherwise false
81 function can_edit_comment_array($comment) {
82 return has_credential(CRED_COMMENT_EDIT, array($comment['UsersID']));
85 /**
86 * Check to see if the package name already exists in the database
88 * @param string $name The package name to check
90 * @return string|void Package name if it already exists
92 function pkg_from_name($name="") {
93 if (!$name) {return NULL;}
94 $dbh = DB::connect();
95 $q = "SELECT ID FROM Packages ";
96 $q.= "WHERE Name = " . $dbh->quote($name);
97 $result = $dbh->query($q);
98 if (!$result) {
99 return;
101 $row = $result->fetch(PDO::FETCH_NUM);
102 return $row[0];
106 * Get licenses for a specific package
108 * @param int $pkgid The package to get licenses for
110 * @return array All licenses for the package
112 function pkg_licenses($pkgid) {
113 $lics = array();
114 $pkgid = intval($pkgid);
115 if ($pkgid > 0) {
116 $dbh = DB::connect();
117 $q = "SELECT l.Name FROM Licenses l ";
118 $q.= "INNER JOIN PackageLicenses pl ON pl.LicenseID = l.ID ";
119 $q.= "WHERE pl.PackageID = ". $pkgid;
120 $result = $dbh->query($q);
121 if (!$result) {
122 return array();
124 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
125 $lics[] = $row;
128 return $lics;
132 * Get package groups for a specific package
134 * @param int $pkgid The package to get groups for
136 * @return array All package groups for the package
138 function pkg_groups($pkgid) {
139 $grps = array();
140 $pkgid = intval($pkgid);
141 if ($pkgid > 0) {
142 $dbh = DB::connect();
143 $q = "SELECT g.Name FROM Groups g ";
144 $q.= "INNER JOIN PackageGroups pg ON pg.GroupID = g.ID ";
145 $q.= "WHERE pg.PackageID = ". $pkgid;
146 $result = $dbh->query($q);
147 if (!$result) {
148 return array();
150 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
151 $grps[] = $row;
154 return $grps;
158 * Get package dependencies for a specific package
160 * @param int $pkgid The package to get dependencies for
162 * @return array All package dependencies for the package
164 function pkg_dependencies($pkgid) {
165 $deps = array();
166 $pkgid = intval($pkgid);
167 if ($pkgid > 0) {
168 $dbh = DB::connect();
169 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
170 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
171 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = p.Name ";
172 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
173 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
174 $q.= "ORDER BY pd.DepName";
175 $result = $dbh->query($q);
176 if (!$result) {
177 return array();
179 while ($row = $result->fetch(PDO::FETCH_NUM)) {
180 $deps[] = $row;
183 return $deps;
187 * Get package relations for a specific package
189 * @param int $pkgid The package to get relations for
191 * @return array All package relations for the package
193 function pkg_relations($pkgid) {
194 $rels = array();
195 $pkgid = intval($pkgid);
196 if ($pkgid > 0) {
197 $dbh = DB::connect();
198 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, pr.RelArch, p.ID FROM PackageRelations pr ";
199 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
200 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
201 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
202 $q.= "ORDER BY pr.RelName";
203 $result = $dbh->query($q);
204 if (!$result) {
205 return array();
207 while ($row = $result->fetch(PDO::FETCH_NUM)) {
208 $rels[] = $row;
211 return $rels;
215 * Get the HTML code to display a package dependency link
217 * @param string $name The name of the dependency
218 * @param string $type The name of the dependency type
219 * @param string $cond The package dependency condition string
220 * @param string $arch The package dependency architecture
221 * @param int $pkg_id The package of the package to display the dependency for
222 * @param bool $show_desc Whether the description of optdepends is shown
224 * @return string The HTML code of the label to display
226 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id, $show_desc=true) {
227 if ($type == 'optdepends' && strpos($name, ':') !== false) {
228 $tokens = explode(':', $name, 2);
229 $name = $tokens[0];
230 $desc = $tokens[1];
231 } else {
232 $desc = '(unknown)';
235 $link = '<a href="';
236 if (is_null($pkg_id)) {
237 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
238 } else {
239 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
241 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
242 $link .= htmlspecialchars($name) . '</a>';
243 $link .= htmlspecialchars($cond);
245 if ($type != 'depends' || $arch) {
246 $link .= ' <em>(';
248 if ($type == 'makedepends') {
249 $link .= 'make';
250 } elseif ($type == 'checkdepends') {
251 $link .= 'check';
252 } elseif ($type == 'optdepends') {
253 $link .= 'optional';
256 if ($type != 'depends' && $arch) {
257 $link .= ', ';
260 if ($arch) {
261 $link .= htmlspecialchars($arch);
264 $link .= ')';
265 if ($show_desc && $type == 'optdepends') {
266 $link .= ' &ndash; ' . htmlspecialchars($desc) . ' </em>';
268 $link .= '</em>';
271 return $link;
275 * Get the HTML code to display a package relation
277 * @param string $name The name of the relation
278 * @param string $cond The package relation condition string
279 * @param string $arch The package relation architecture
281 * @return string The HTML code of the label to display
283 function pkg_rel_html($name, $cond, $arch) {
284 $html = htmlspecialchars($name) . htmlspecialchars($cond);
286 if ($arch) {
287 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
290 return $html;
294 * Get the HTML code to display a source link
296 * @param string $url The URL of the source
297 * @param string $arch The source architecture
299 * @return string The HTML code of the label to display
301 function pkg_source_link($url, $arch) {
302 $url = explode('::', $url);
303 $parsed_url = parse_url($url[0]);
305 if (isset($parsed_url['scheme']) || isset($url[1])) {
306 $link = '<a href="' . htmlspecialchars((isset($url[1]) ? $url[1] : $url[0]), ENT_QUOTES) . '">' . htmlspecialchars($url[0]) . '</a>';
307 } else {
308 $link = htmlspecialchars($url[0]);
311 if ($arch) {
312 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
315 return $link;
319 * Determine packages that depend on a package
321 * @param string $name The package name for the dependency search
323 * @return array All packages that depend on the specified package name
325 function pkg_required($name="") {
326 $deps = array();
327 if ($name != "") {
328 $dbh = DB::connect();
329 $q = "SELECT p.Name, dt.Name, '' AS DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
330 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
331 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
332 $q.= "WHERE pd.DepName = " . $dbh->quote($name) . " ";
333 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = " . $dbh->quote($name) . " ";
334 $q.= "ORDER BY p.Name";
335 $result = $dbh->query($q);
336 if (!$result) {return array();}
337 while ($row = $result->fetch(PDO::FETCH_NUM)) {
338 $deps[] = $row;
341 return $deps;
345 * Get all package sources for a specific package
347 * @param string $pkgid The package ID to get the sources for
349 * @return array All sources associated with a specific package
351 function pkg_sources($pkgid) {
352 $sources = array();
353 $pkgid = intval($pkgid);
354 if ($pkgid > 0) {
355 $dbh = DB::connect();
356 $q = "SELECT Source, SourceArch FROM PackageSources ";
357 $q.= "WHERE PackageID = " . $pkgid;
358 $q.= " ORDER BY Source";
359 $result = $dbh->query($q);
360 if (!$result) {
361 return array();
363 while ($row = $result->fetch(PDO::FETCH_NUM)) {
364 $sources[] = $row;
367 return $sources;
371 * Determine package names from package IDs
373 * @param string|array $pkgids The package IDs to get names for
375 * @return array|string All names if multiple package IDs, otherwise package name
377 function pkg_name_from_id($pkgids) {
378 if (is_array($pkgids)) {
379 $pkgids = sanitize_ids($pkgids);
380 $names = array();
381 $dbh = DB::connect();
382 $q = "SELECT Name FROM Packages WHERE ID IN (";
383 $q.= implode(",", $pkgids) . ")";
384 $result = $dbh->query($q);
385 if ($result) {
386 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
387 $names[] = $row['Name'];
390 return $names;
392 elseif ($pkgids > 0) {
393 $dbh = DB::connect();
394 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
395 $result = $dbh->query($q);
396 if ($result) {
397 $name = $result->fetch(PDO::FETCH_NUM);
399 return $name[0];
401 else {
402 return NULL;
407 * Determine if a package name is on the database blacklist
409 * @param string $name The package name to check
411 * @return bool True if the name is blacklisted, otherwise false
413 function pkg_name_is_blacklisted($name) {
414 $dbh = DB::connect();
415 $q = "SELECT COUNT(*) FROM PackageBlacklist ";
416 $q.= "WHERE Name = " . $dbh->quote($name);
417 $result = $dbh->query($q);
419 if (!$result) return false;
420 return ($result->fetchColumn() > 0);
424 * Get the package details
426 * @param string $id The package ID to get description for
428 * @return array The package's details OR error message
430 function pkg_get_details($id=0) {
431 $dbh = DB::connect();
433 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
434 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
435 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
436 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
437 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
438 $q.= "PackageBases.PackagerUID, ";
439 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
440 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
441 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
442 $q.= "FROM Packages, PackageBases ";
443 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
444 $q.= "AND Packages.ID = " . intval($id);
445 $result = $dbh->query($q);
447 $row = array();
449 if (!$result) {
450 $row['error'] = __("Error retrieving package details.");
452 else {
453 $row = $result->fetch(PDO::FETCH_ASSOC);
454 if (empty($row)) {
455 $row['error'] = __("Package details could not be found.");
459 return $row;
463 * Display the package details page
465 * @param string $id The package ID to get details page for
466 * @param array $row Package details retrieved by pkg_get_details()
467 * @param string $SID The session ID of the visitor
469 * @return void
471 function pkg_display_details($id=0, $row, $SID="") {
472 $dbh = DB::connect();
474 if (isset($row['error'])) {
475 print "<p>" . $row['error'] . "</p>\n";
477 else {
478 $base_id = pkgbase_from_pkgid($id);
479 $pkgbase_name = pkgbase_name_from_id($base_id);
481 include('pkg_details.php');
483 if ($SID) {
484 include('pkg_comment_box.php');
487 $limit = isset($_GET['comments']) ? 0 : 10;
488 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
489 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
490 if (!empty($comments)) {
491 include('pkg_comments.php');
496 /* pkg_search_page(SID)
497 * outputs the body of search/search results page
499 * parameters:
500 * SID - current Session ID
501 * preconditions:
502 * package search page has been accessed
503 * request variables have not been sanitized
505 * request vars:
506 * O - starting result number
507 * PP - number of search hits per page
508 * K - package search string
509 * SO - search hit sort order:
510 * values: a - ascending
511 * d - descending
512 * SB - sort search hits by:
513 * values: n - package name
514 * v - number of votes
515 * m - maintainer username
516 * SeB- property that search string (K) represents
517 * values: n - package name
518 * nd - package name & description
519 * x - package name (exact match)
520 * m - package maintainer's username
521 * s - package submitter's username
522 * do_Orphans - boolean. whether to search packages
523 * without a maintainer
526 * These two are actually handled in packages.php.
528 * IDs- integer array of ticked packages' IDs
529 * action - action to be taken on ticked packages
530 * values: do_Flag - Flag out-of-date
531 * do_UnFlag - Remove out-of-date flag
532 * do_Adopt - Adopt
533 * do_Disown - Disown
534 * do_Delete - Delete
535 * do_Notify - Enable notification
536 * do_UnNotify - Disable notification
538 function pkg_search_page($SID="") {
539 $dbh = DB::connect();
542 * Get commonly used variables.
543 * TODO: Reduce the number of database queries!
545 if ($SID)
546 $myuid = uid_from_sid($SID);
548 /* Sanitize paging variables. */
549 if (isset($_GET['O'])) {
550 $_GET['O'] = max(intval($_GET['O']), 0);
551 } else {
552 $_GET['O'] = 0;
555 if (isset($_GET["PP"])) {
556 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
557 } else {
558 $_GET["PP"] = 50;
562 * FIXME: Pull out DB-related code. All of it! This one's worth a
563 * choco-chip cookie, one of those nice big soft ones.
566 /* Build the package search query. */
567 $q_select = "SELECT ";
568 if ($SID) {
569 $q_select .= "CommentNotify.UserID AS Notify,
570 PackageVotes.UsersID AS Voted, ";
572 $q_select .= "Users.Username AS Maintainer,
573 Packages.Name, Packages.Version, Packages.Description,
574 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
575 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
577 $q_from = "FROM Packages
578 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
579 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
580 if ($SID) {
581 /* This is not needed for the total row count query. */
582 $q_from_extra = "LEFT JOIN PackageVotes
583 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
584 LEFT JOIN CommentNotify
585 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
586 } else {
587 $q_from_extra = "";
590 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
592 if (isset($_GET['K'])) {
593 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
594 /* Search by maintainer. */
595 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
597 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
598 /* Search by submitter. */
599 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
601 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
602 /* Search by name. */
603 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
604 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
606 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
607 /* Search by package base name. */
608 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
609 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
611 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
612 /* Search by name (exact match). */
613 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
615 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
616 /* Search by package base name (exact match). */
617 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
619 else {
620 /* Keyword search (default). */
621 $count = 0;
622 $q_keywords = "";
623 $op = "";
625 foreach (str_getcsv($_GET['K'], ' ') as $term) {
626 if ($term == "") {
627 continue;
629 if ($count > 0 && strtolower($term) == "and") {
630 $op = "AND ";
631 continue;
633 if ($count > 0 && strtolower($term) == "or") {
634 $op = "OR ";
635 continue;
637 if ($count > 0 && strtolower($term) == "not") {
638 $op .= "NOT ";
639 continue;
642 $term = "%" . addcslashes($term, '%_') . "%";
643 $q_keywords .= $op . " (Packages.Name LIKE " . $dbh->quote($term) . " OR ";
644 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
645 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
646 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
647 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
649 $count++;
650 if ($count >= 20) {
651 break;
653 $op = "AND ";
656 if (!empty($q_keywords)) {
657 $q_where .= "AND (" . $q_keywords . ") ";
662 if (isset($_GET["do_Orphans"])) {
663 $q_where .= "AND MaintainerUID IS NULL ";
666 if (isset($_GET['outdated'])) {
667 if ($_GET['outdated'] == 'on') {
668 $q_where .= "AND OutOfDateTS IS NOT NULL ";
670 elseif ($_GET['outdated'] == 'off') {
671 $q_where .= "AND OutOfDateTS IS NULL ";
675 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
677 $q_sort = "ORDER BY ";
678 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
679 switch ($sort_by) {
680 case 'v':
681 $q_sort .= "NumVotes " . $order . ", ";
682 break;
683 case 'p':
684 $q_sort .= "Popularity " . $order . ", ";
685 break;
686 case 'w':
687 if ($SID) {
688 $q_sort .= "Voted " . $order . ", ";
690 break;
691 case 'o':
692 if ($SID) {
693 $q_sort .= "Notify " . $order . ", ";
695 break;
696 case 'm':
697 $q_sort .= "Maintainer " . $order . ", ";
698 break;
699 case 'a':
700 $q_sort .= "-ModifiedTS " . $order . ", ";
701 break;
702 default:
703 break;
705 $q_sort .= " Packages.Name " . $order . " ";
707 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
709 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
710 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
712 $result = $dbh->query($q);
713 $result_t = $dbh->query($q_total);
714 if ($result_t) {
715 $row = $result_t->fetch(PDO::FETCH_NUM);
716 $total = $row[0];
718 else {
719 $total = 0;
722 if ($result && $total > 0) {
723 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
724 $SO_next = "a";
726 else {
727 $SO_next = "d";
731 /* Calculate the results to use. */
732 $first = $_GET['O'] + 1;
734 /* Calculation of pagination links. */
735 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
736 $current = ceil($first / $per_page);
737 $pages = ceil($total / $per_page);
738 $templ_pages = array();
740 if ($current > 1) {
741 $templ_pages['&laquo; ' . __('First')] = 0;
742 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
745 if ($current - 5 > 1)
746 $templ_pages["..."] = false;
748 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
749 $templ_pages[$i] = ($i - 1) * $per_page;
752 if ($current + 5 < $pages)
753 $templ_pages["... "] = false;
755 if ($current < $pages) {
756 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
757 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
760 include('pkg_search_form.php');
762 if ($result) {
763 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
764 $searchresults[] = $row;
768 include('pkg_search_results.php');
770 return;
774 * Determine if a POST string has been sent by a visitor
776 * @param string $action String to check has been sent via POST
778 * @return bool True if the POST string was used, otherwise false
780 function current_action($action) {
781 return (isset($_POST['action']) && $_POST['action'] == $action) ||
782 isset($_POST[$action]);
786 * Determine if sent IDs are valid integers
788 * @param array $ids IDs to validate
790 * @return array All sent IDs that are valid integers
792 function sanitize_ids($ids) {
793 $new_ids = array();
794 foreach ($ids as $id) {
795 $id = intval($id);
796 if ($id > 0) {
797 $new_ids[] = $id;
800 return $new_ids;
804 * Add package information to the database for a specific package
806 * @param int $base_id ID of the package base
807 * @param string $pkgname Name of the new package
808 * @param string $pkgver Version of the new package
809 * @param string $pkgdesc Description of the new package
810 * @param string $pkgurl Upstream URL for the new package
812 * @return int ID of the new package
814 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
815 $dbh = DB::connect();
816 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
817 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
818 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
819 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
820 $dbh->exec($q);
821 return $dbh->lastInsertId();
825 * Add a dependency for a specific package to the database
827 * @param int $pkgid The package ID to add the dependency for
828 * @param string $type The type of dependency to add
829 * @param string $depname The name of the dependency to add
830 * @param string $depcondition The type of dependency for the package
831 * @param string $deparch The architecture of the dependency to add
833 * @return void
835 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
836 $dbh = DB::connect();
837 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
838 $pkgid,
839 pkg_dependency_type_id_from_name($type),
840 $dbh->quote($depname),
841 $dbh->quote($depcondition),
842 $deparch ? $dbh->quote($deparch) : 'NULL'
844 $dbh->exec($q);
848 * Add a relation for a specific package to the database
850 * @param int $pkgid The package ID to add the relation for
851 * @param string $type The type of relation to add
852 * @param string $relname The name of the relation to add
853 * @param string $relcondition The version requirement of the relation
854 * @param string $relarch The architecture of the relation to add
856 * @return void
858 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
859 $dbh = DB::connect();
860 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
861 $pkgid,
862 pkg_relation_type_id_from_name($type),
863 $dbh->quote($relname),
864 $dbh->quote($relcondition),
865 $relarch ? $dbh->quote($relarch) : 'NULL'
867 $dbh->exec($q);
871 * Add a source for a specific package to the database
873 * @param int $pkgid The package ID to add the source for
874 * @param string $pkgsrc The package source to add to the database
875 * @param string $srcarch The architecture of the source to add
877 * @return void
879 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
880 $dbh = DB::connect();
881 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
882 $pkgid,
883 $dbh->quote($pkgsrc),
884 $srcarch ? $dbh->quote($srcarch) : 'NULL'
886 $dbh->exec($q);
890 * Creates a new group and returns its ID
892 * If the groups already exists, the ID of the already existing group is
893 * returned.
895 * @param string $name The name of the group to create
897 * @return int The ID of the group
899 function pkg_create_group($name) {
900 $dbh = DB::connect();
901 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
902 $result = $dbh->query($q);
903 if ($result) {
904 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
905 if ($grpid > 0) {
906 return $grpid;
910 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
911 $dbh->exec($q);
912 return $dbh->lastInsertId();
916 * Add a package to a group
918 * @param int $pkgid The package ID of the package to add
919 * @param int $grpid The group ID of the group to add the package to
921 * @return void
923 function pkg_add_grp($pkgid, $grpid) {
924 $dbh = DB::connect();
925 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
926 $pkgid,
927 $grpid
929 $dbh->exec($q);
933 * Creates a new license and returns its ID
935 * If the license already exists, the ID of the already existing license is
936 * returned.
938 * @param string $name The name of the license to create
940 * @return int The ID of the license
942 function pkg_create_license($name) {
943 $dbh = DB::connect();
944 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
945 $result = $dbh->query($q);
946 if ($result) {
947 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
948 if ($licid > 0) {
949 return $licid;
953 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
954 $dbh->exec($q);
955 return $dbh->lastInsertId();
959 * Add a license to a package
961 * @param int $pkgid The package ID of the package
962 * @param int $grpid The ID of the license to add
964 * @return void
966 function pkg_add_lic($pkgid, $licid) {
967 $dbh = DB::connect();
968 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
969 $pkgid,
970 $licid
972 $dbh->exec($q);
976 * Determine package information for latest package
978 * @param int $numpkgs Number of packages to get information on
980 * @return array $packages Package info for the specified number of recent packages
982 function latest_pkgs($numpkgs) {
983 $dbh = DB::connect();
985 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
986 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
987 $q.= "PackageBases.ID = Packages.PackageBaseID ";
988 $q.= "ORDER BY SubmittedTS DESC ";
989 $q.= "LIMIT " . intval($numpkgs);
990 $result = $dbh->query($q);
992 $packages = array();
993 if ($result) {
994 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
995 $packages[] = $row;
999 return $packages;