3 include_once("pkgbasefuncs.inc.php");
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) {
18 $q = "SELECT UsersID FROM PackageComments ";
19 $q.= "WHERE ID = " . intval($comment_id);
20 $result = $dbh->query($q);
26 $uid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
28 return has_credential(CRED_COMMENT_DELETE
, array($uid));
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']));
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) {
58 $q = "SELECT UsersID FROM PackageComments ";
59 $q.= "WHERE ID = " . intval($comment_id);
60 $result = $dbh->query($q);
66 $uid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
68 return has_credential(CRED_COMMENT_EDIT
, array($uid));
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']));
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;}
95 $q = "SELECT ID FROM Packages ";
96 $q.= "WHERE Name = " . $dbh->quote($name);
97 $result = $dbh->query($q);
101 $row = $result->fetch(PDO
::FETCH_NUM
);
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) {
114 $pkgid = intval($pkgid);
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);
124 while ($row = $result->fetch(PDO
::FETCH_COLUMN
, 0)) {
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) {
140 $pkgid = intval($pkgid);
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);
150 while ($row = $result->fetch(PDO
::FETCH_COLUMN
, 0)) {
158 * Get providers for a specific package
160 * @param string $name The name of the "package" to get providers for
162 * @return array The IDs and names of all providers of the package
164 function pkg_providers($name) {
165 $dbh = DB
::connect();
166 $q = "SELECT p.ID, p.Name FROM Packages p ";
167 $q.= "INNER JOIN PackageRelations pr ON pr.PackageID = p.ID ";
168 $q.= "INNER JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
169 $q.= "WHERE rt.Name = 'provides' ";
170 $q.= "AND pr.RelName = " . $dbh->quote($name);
171 $result = $dbh->query($q);
177 $providers = array();
178 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
185 * Get package dependencies for a specific package
187 * @param int $pkgid The package to get dependencies for
189 * @return array All package dependencies for the package
191 function pkg_dependencies($pkgid) {
193 $pkgid = intval($pkgid);
195 $dbh = DB
::connect();
196 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
197 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
198 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = p.Name ";
199 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
200 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
201 $q.= "ORDER BY pd.DepName";
202 $result = $dbh->query($q);
206 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
214 * Get package relations for a specific package
216 * @param int $pkgid The package to get relations for
218 * @return array All package relations for the package
220 function pkg_relations($pkgid) {
222 $pkgid = intval($pkgid);
224 $dbh = DB
::connect();
225 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, pr.RelArch, p.ID FROM PackageRelations pr ";
226 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
227 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
228 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
229 $q.= "ORDER BY pr.RelName";
230 $result = $dbh->query($q);
234 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
242 * Get the HTML code to display a package dependency link
244 * @param string $name The name of the dependency
245 * @param string $type The name of the dependency type
246 * @param string $cond The package dependency condition string
247 * @param string $arch The package dependency architecture
248 * @param int $pkg_id The package of the package to display the dependency for
249 * @param bool $show_desc Whether the description of optdepends is shown
251 * @return string The HTML code of the label to display
253 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id, $show_desc=true) {
254 if ($type == 'optdepends' && strpos($name, ':') !== false) {
255 $tokens = explode(':', $name, 2);
262 $providers = array();
263 if (is_null($pkg_id)) {
265 * TODO: We currently perform one SQL query per nonexistent
266 * package dependency. It would be much better if we could
267 * annotate dependency data with providers so that we already
268 * know whether a dependency is a "provision name" or a package
269 * from the official repositories at this point.
271 $providers = pkg_providers($name);
274 if (count($providers) > 0) {
275 $link = htmlspecialchars($name) . ' ';
276 $link .= '<span class="virtual-dep">(';
277 foreach ($providers as $provider) {
278 $name = $provider[1];
279 $link .= '<a href="';
280 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
281 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
282 $link .= htmlspecialchars($name) . '</a>, ';
284 $link = substr($link, 0, -2);
288 if (is_null($pkg_id)) {
289 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
291 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
293 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
294 $link .= htmlspecialchars($name) . '</a>';
295 $link .= htmlspecialchars($cond);
298 if ($type != 'depends' ||
$arch) {
301 if ($type == 'makedepends') {
303 } elseif ($type == 'checkdepends') {
305 } elseif ($type == 'optdepends') {
309 if ($type != 'depends' && $arch) {
314 $link .= htmlspecialchars($arch);
318 if ($show_desc && $type == 'optdepends') {
319 $link .= ' – ' . htmlspecialchars($desc) . ' </em>';
328 * Get the HTML code to display a package relation
330 * @param string $name The name of the relation
331 * @param string $cond The package relation condition string
332 * @param string $arch The package relation architecture
334 * @return string The HTML code of the label to display
336 function pkg_rel_html($name, $cond, $arch) {
337 $html = htmlspecialchars($name) . htmlspecialchars($cond);
340 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
347 * Get the HTML code to display a source link
349 * @param string $url The URL of the source
350 * @param string $arch The source architecture
352 * @return string The HTML code of the label to display
354 function pkg_source_link($url, $arch) {
355 $url = explode('::', $url);
356 $parsed_url = parse_url($url[0]);
358 if (isset($parsed_url['scheme']) ||
isset($url[1])) {
359 $link = '<a href="' . htmlspecialchars((isset($url[1]) ?
$url[1] : $url[0]), ENT_QUOTES
) . '">' . htmlspecialchars($url[0]) . '</a>';
361 $link = htmlspecialchars($url[0]);
365 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
372 * Determine packages that depend on a package
374 * @param string $name The package name for the dependency search
376 * @return array All packages that depend on the specified package name
378 function pkg_required($name="") {
381 $dbh = DB
::connect();
382 $q = "SELECT p.Name, dt.Name, '' AS DepCondition, pd.DepArch, p.ID FROM PackageDepends pd ";
383 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
384 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
385 $q.= "WHERE pd.DepName = " . $dbh->quote($name) . " ";
386 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) = " . $dbh->quote($name) . " ";
387 $q.= "ORDER BY p.Name";
388 $result = $dbh->query($q);
389 if (!$result) {return array();}
390 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
398 * Get all package sources for a specific package
400 * @param string $pkgid The package ID to get the sources for
402 * @return array All sources associated with a specific package
404 function pkg_sources($pkgid) {
406 $pkgid = intval($pkgid);
408 $dbh = DB
::connect();
409 $q = "SELECT Source, SourceArch FROM PackageSources ";
410 $q.= "WHERE PackageID = " . $pkgid;
411 $q.= " ORDER BY Source";
412 $result = $dbh->query($q);
416 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
424 * Determine package names from package IDs
426 * @param string|array $pkgids The package IDs to get names for
428 * @return array|string All names if multiple package IDs, otherwise package name
430 function pkg_name_from_id($pkgids) {
431 if (is_array($pkgids)) {
432 $pkgids = sanitize_ids($pkgids);
434 $dbh = DB
::connect();
435 $q = "SELECT Name FROM Packages WHERE ID IN (";
436 $q.= implode(",", $pkgids) . ")";
437 $result = $dbh->query($q);
439 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {
440 $names[] = $row['Name'];
445 elseif ($pkgids > 0) {
446 $dbh = DB
::connect();
447 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
448 $result = $dbh->query($q);
450 $name = $result->fetch(PDO
::FETCH_NUM
);
460 * Determine if a package name is on the database blacklist
462 * @param string $name The package name to check
464 * @return bool True if the name is blacklisted, otherwise false
466 function pkg_name_is_blacklisted($name) {
467 $dbh = DB
::connect();
468 $q = "SELECT COUNT(*) FROM PackageBlacklist ";
469 $q.= "WHERE Name = " . $dbh->quote($name);
470 $result = $dbh->query($q);
472 if (!$result) return false;
473 return ($result->fetchColumn() > 0);
477 * Get the package details
479 * @param string $id The package ID to get description for
481 * @return array The package's details OR error message
483 function pkg_get_details($id=0) {
484 $dbh = DB
::connect();
486 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
487 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
488 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
489 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
490 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
491 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
492 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
493 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
494 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
495 $q.= "FROM Packages, PackageBases ";
496 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
497 $q.= "AND Packages.ID = " . intval($id);
498 $result = $dbh->query($q);
503 $row['error'] = __("Error retrieving package details.");
506 $row = $result->fetch(PDO
::FETCH_ASSOC
);
508 $row['error'] = __("Package details could not be found.");
516 * Display the package details page
518 * @param string $id The package ID to get details page for
519 * @param array $row Package details retrieved by pkg_get_details()
520 * @param string $SID The session ID of the visitor
524 function pkg_display_details($id=0, $row, $SID="") {
525 $dbh = DB
::connect();
527 if (isset($row['error'])) {
528 print "<p>" . $row['error'] . "</p>\n";
531 $base_id = pkgbase_from_pkgid($id);
532 $pkgbase_name = pkgbase_name_from_id($base_id);
534 include('pkg_details.php');
537 include('pkg_comment_box.php');
540 $limit = isset($_GET['comments']) ?
0 : 10;
541 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED
);
542 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
543 if (!empty($comments)) {
544 include('pkg_comments.php');
549 /* pkg_search_page(SID)
550 * outputs the body of search/search results page
553 * SID - current Session ID
555 * package search page has been accessed
556 * request variables have not been sanitized
559 * O - starting result number
560 * PP - number of search hits per page
561 * K - package search string
562 * SO - search hit sort order:
563 * values: a - ascending
565 * SB - sort search hits by:
566 * values: n - package name
567 * v - number of votes
568 * m - maintainer username
569 * SeB- property that search string (K) represents
570 * values: n - package name
571 * nd - package name & description
572 * x - package name (exact match)
573 * m - package maintainer's username
574 * s - package submitter's username
575 * do_Orphans - boolean. whether to search packages
576 * without a maintainer
579 * These two are actually handled in packages.php.
581 * IDs- integer array of ticked packages' IDs
582 * action - action to be taken on ticked packages
583 * values: do_Flag - Flag out-of-date
584 * do_UnFlag - Remove out-of-date flag
588 * do_Notify - Enable notification
589 * do_UnNotify - Disable notification
591 function pkg_search_page($SID="") {
592 $dbh = DB
::connect();
595 * Get commonly used variables.
596 * TODO: Reduce the number of database queries!
599 $myuid = uid_from_sid($SID);
601 /* Sanitize paging variables. */
602 if (isset($_GET['O'])) {
603 $_GET['O'] = max(intval($_GET['O']), 0);
608 if (isset($_GET["PP"])) {
609 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
615 * FIXME: Pull out DB-related code. All of it! This one's worth a
616 * choco-chip cookie, one of those nice big soft ones.
619 /* Build the package search query. */
620 $q_select = "SELECT ";
622 $q_select .= "CommentNotify.UserID AS Notify,
623 PackageVotes.UsersID AS Voted, ";
625 $q_select .= "Users.Username AS Maintainer,
626 Packages.Name, Packages.Version, Packages.Description,
627 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
628 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
630 $q_from = "FROM Packages
631 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
632 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
634 /* This is not needed for the total row count query. */
635 $q_from_extra = "LEFT JOIN PackageVotes
636 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
637 LEFT JOIN CommentNotify
638 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
643 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
645 if (isset($_GET['K'])) {
646 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
647 /* Search by maintainer. */
648 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
650 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
651 /* Search by submitter. */
652 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
654 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
655 /* Search by name. */
656 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
657 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
659 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
660 /* Search by package base name. */
661 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
662 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
664 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
665 /* Search by name (exact match). */
666 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
668 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
669 /* Search by package base name (exact match). */
670 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
673 /* Keyword search (default). */
678 foreach (str_getcsv($_GET['K'], ' ') as $term) {
682 if ($count > 0 && strtolower($term) == "and") {
686 if ($count > 0 && strtolower($term) == "or") {
690 if ($count > 0 && strtolower($term) == "not") {
695 $term = "%" . addcslashes($term, '%_') . "%";
696 $q_keywords .= $op . " (Packages.Name LIKE " . $dbh->quote($term) . " OR ";
697 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
698 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
699 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
700 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
709 if (!empty($q_keywords)) {
710 $q_where .= "AND (" . $q_keywords . ") ";
715 if (isset($_GET["do_Orphans"])) {
716 $q_where .= "AND MaintainerUID IS NULL ";
719 if (isset($_GET['outdated'])) {
720 if ($_GET['outdated'] == 'on') {
721 $q_where .= "AND OutOfDateTS IS NOT NULL ";
723 elseif ($_GET['outdated'] == 'off') {
724 $q_where .= "AND OutOfDateTS IS NULL ";
728 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ?
'DESC' : 'ASC';
730 $q_sort = "ORDER BY ";
731 $sort_by = isset($_GET["SB"]) ?
$_GET["SB"] : '';
734 $q_sort .= "NumVotes " . $order . ", ";
737 $q_sort .= "Popularity " . $order . ", ";
741 $q_sort .= "Voted " . $order . ", ";
746 $q_sort .= "Notify " . $order . ", ";
750 $q_sort .= "Maintainer " . $order . ", ";
753 $q_sort .= "-ModifiedTS " . $order . ", ";
758 $q_sort .= " Packages.Name " . $order . " ";
760 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
762 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
763 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
765 $result = $dbh->query($q);
766 $result_t = $dbh->query($q_total);
768 $row = $result_t->fetch(PDO
::FETCH_NUM
);
775 if ($result && $total > 0) {
776 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
784 /* Calculate the results to use. */
785 $first = $_GET['O'] +
1;
787 /* Calculation of pagination links. */
788 $per_page = ($_GET['PP'] > 0) ?
$_GET['PP'] : 50;
789 $current = ceil($first / $per_page);
790 $pages = ceil($total / $per_page);
791 $templ_pages = array();
794 $templ_pages['« ' . __('First')] = 0;
795 $templ_pages['‹ ' . __('Previous')] = ($current - 2) * $per_page;
798 if ($current - 5 > 1)
799 $templ_pages["..."] = false;
801 for ($i = max($current - 5, 1); $i <= min($pages, $current +
5); $i++
) {
802 $templ_pages[$i] = ($i - 1) * $per_page;
805 if ($current +
5 < $pages)
806 $templ_pages["... "] = false;
808 if ($current < $pages) {
809 $templ_pages[__('Next') . ' ›'] = $current * $per_page;
810 $templ_pages[__('Last') . ' »'] = ($pages - 1) * $per_page;
813 include('pkg_search_form.php');
816 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {
817 $searchresults[] = $row;
821 include('pkg_search_results.php');
827 * Determine if a POST string has been sent by a visitor
829 * @param string $action String to check has been sent via POST
831 * @return bool True if the POST string was used, otherwise false
833 function current_action($action) {
834 return (isset($_POST['action']) && $_POST['action'] == $action) ||
835 isset($_POST[$action]);
839 * Determine if sent IDs are valid integers
841 * @param array $ids IDs to validate
843 * @return array All sent IDs that are valid integers
845 function sanitize_ids($ids) {
847 foreach ($ids as $id) {
857 * Add package information to the database for a specific package
859 * @param int $base_id ID of the package base
860 * @param string $pkgname Name of the new package
861 * @param string $pkgver Version of the new package
862 * @param string $pkgdesc Description of the new package
863 * @param string $pkgurl Upstream URL for the new package
865 * @return int ID of the new package
867 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
868 $dbh = DB
::connect();
869 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
870 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
871 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
872 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
874 return $dbh->lastInsertId();
878 * Add a dependency for a specific package to the database
880 * @param int $pkgid The package ID to add the dependency for
881 * @param string $type The type of dependency to add
882 * @param string $depname The name of the dependency to add
883 * @param string $depcondition The type of dependency for the package
884 * @param string $deparch The architecture of the dependency to add
888 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
889 $dbh = DB
::connect();
890 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
892 pkg_dependency_type_id_from_name($type),
893 $dbh->quote($depname),
894 $dbh->quote($depcondition),
895 $deparch ?
$dbh->quote($deparch) : 'NULL'
901 * Add a relation for a specific package to the database
903 * @param int $pkgid The package ID to add the relation for
904 * @param string $type The type of relation to add
905 * @param string $relname The name of the relation to add
906 * @param string $relcondition The version requirement of the relation
907 * @param string $relarch The architecture of the relation to add
911 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
912 $dbh = DB
::connect();
913 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
915 pkg_relation_type_id_from_name($type),
916 $dbh->quote($relname),
917 $dbh->quote($relcondition),
918 $relarch ?
$dbh->quote($relarch) : 'NULL'
924 * Add a source for a specific package to the database
926 * @param int $pkgid The package ID to add the source for
927 * @param string $pkgsrc The package source to add to the database
928 * @param string $srcarch The architecture of the source to add
932 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
933 $dbh = DB
::connect();
934 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
936 $dbh->quote($pkgsrc),
937 $srcarch ?
$dbh->quote($srcarch) : 'NULL'
943 * Creates a new group and returns its ID
945 * If the groups already exists, the ID of the already existing group is
948 * @param string $name The name of the group to create
950 * @return int The ID of the group
952 function pkg_create_group($name) {
953 $dbh = DB
::connect();
954 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
955 $result = $dbh->query($q);
957 $grpid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
963 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
965 return $dbh->lastInsertId();
969 * Add a package to a group
971 * @param int $pkgid The package ID of the package to add
972 * @param int $grpid The group ID of the group to add the package to
976 function pkg_add_grp($pkgid, $grpid) {
977 $dbh = DB
::connect();
978 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
986 * Creates a new license and returns its ID
988 * If the license already exists, the ID of the already existing license is
991 * @param string $name The name of the license to create
993 * @return int The ID of the license
995 function pkg_create_license($name) {
996 $dbh = DB
::connect();
997 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
998 $result = $dbh->query($q);
1000 $licid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
1006 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
1008 return $dbh->lastInsertId();
1012 * Add a license to a package
1014 * @param int $pkgid The package ID of the package
1015 * @param int $grpid The ID of the license to add
1019 function pkg_add_lic($pkgid, $licid) {
1020 $dbh = DB
::connect();
1021 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
1029 * Determine package information for latest package
1031 * @param int $numpkgs Number of packages to get information on
1033 * @return array $packages Package info for the specified number of recent packages
1035 function latest_pkgs($numpkgs) {
1036 $dbh = DB
::connect();
1038 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
1039 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
1040 $q.= "PackageBases.ID = Packages.PackageBaseID ";
1041 $q.= "ORDER BY SubmittedTS DESC ";
1042 $q.= "LIMIT " . intval($numpkgs);
1043 $result = $dbh->query($q);
1045 $packages = array();
1047 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {