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 annotation
243 * (dependency type, architecture, ...)
245 * @param string $type The name of the dependency type
246 * @param string $arch The package dependency architecture
247 * @param string $desc An optdepends description
249 * @return string The HTML code of the label to display
251 function pkg_deplink_annotation($type, $arch, $desc=false) {
252 if ($type == 'depends' && !$arch) {
258 if ($type == 'makedepends') {
260 } elseif ($type == 'checkdepends') {
262 } elseif ($type == 'optdepends') {
266 if ($type != 'depends' && $arch) {
271 $link .= htmlspecialchars($arch);
275 if ($type == 'optdepends' && $desc) {
276 $link .= ' – ' . htmlspecialchars($desc) . ' </em>';
284 * Get the HTML code to display a package dependency link
286 * @param string $name The name of the dependency
287 * @param string $type The name of the dependency type
288 * @param string $cond The package dependency condition string
289 * @param string $arch The package dependency architecture
290 * @param int $pkg_id The package of the package to display the dependency for
292 * @return string The HTML code of the label to display
294 function pkg_depend_link($name, $type, $cond, $arch, $pkg_id) {
295 if ($type == 'optdepends' && strpos($name, ':') !== false) {
296 $tokens = explode(':', $name, 2);
303 $providers = array();
304 if (is_null($pkg_id)) {
306 * TODO: We currently perform one SQL query per nonexistent
307 * package dependency. It would be much better if we could
308 * annotate dependency data with providers so that we already
309 * know whether a dependency is a "provision name" or a package
310 * from the official repositories at this point.
312 $providers = pkg_providers($name);
315 if (count($providers) > 0) {
316 $link = htmlspecialchars($name) . ' ';
317 $link .= '<span class="virtual-dep">(';
318 foreach ($providers as $provider) {
319 $name = $provider[1];
320 $link .= '<a href="';
321 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
322 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
323 $link .= htmlspecialchars($name) . '</a>, ';
325 $link = substr($link, 0, -2);
329 if (is_null($pkg_id)) {
330 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
332 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
334 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
335 $link .= htmlspecialchars($name) . '</a>';
336 $link .= htmlspecialchars($cond);
339 return $link . pkg_deplink_annotation($type, $arch, $desc);
343 * Get the HTML code to display a package requirement link
345 * @param string $name The name of the requirement
346 * @param string $depends The (literal) name of the dependency of $name
347 * @param string $type The name of the dependency type
348 * @param string $arch The package dependency architecture
349 * @param string $pkgname The name of dependant package
351 * @return string The HTML code of the link to display
353 function pkg_requiredby_link($name, $depends, $type, $arch, $pkgname) {
354 if ($type == 'optdepends' && strpos($name, ':') !== false) {
355 $tokens = explode(':', $name, 2);
360 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES
);
361 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
362 $link .= htmlspecialchars($name) . '</a>';
364 if ($depends != $pkgname) {
366 if (strpos($depends, ':') !== false) {
367 $tokens = explode(':', $depname, 2);
368 $depname = $tokens[0];
371 $link .= ' <span class="virtual-dep">(';
372 $link .= __('requires %s', htmlspecialchars($depname));
376 return $link . pkg_deplink_annotation($type, $arch);
380 * Get the HTML code to display a package relation
382 * @param string $name The name of the relation
383 * @param string $cond The package relation condition string
384 * @param string $arch The package relation architecture
386 * @return string The HTML code of the label to display
388 function pkg_rel_html($name, $cond, $arch) {
389 $html = htmlspecialchars($name) . htmlspecialchars($cond);
392 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
399 * Get the HTML code to display a source link
401 * @param string $url The URL of the source
402 * @param string $arch The source architecture
404 * @return string The HTML code of the label to display
406 function pkg_source_link($url, $arch) {
407 $url = explode('::', $url);
408 $parsed_url = parse_url($url[0]);
410 if (isset($parsed_url['scheme']) ||
isset($url[1])) {
411 $link = '<a href="' . htmlspecialchars((isset($url[1]) ?
$url[1] : $url[0]), ENT_QUOTES
) . '">' . htmlspecialchars($url[0]) . '</a>';
413 $link = htmlspecialchars($url[0]);
417 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
424 * Determine packages that depend on a package
426 * @param string $name The package name for the dependency search
427 * @param array $provides A list of virtual provisions of the package
429 * @return array All packages that depend on the specified package name
431 function pkg_required($name="", $provides) {
434 $dbh = DB
::connect();
436 $name_list = $dbh->quote($name);
437 foreach ($provides as $p) {
438 $name_list .= ',' . $dbh->quote($p[0]);
441 $q = "SELECT p.Name, pd.DepName, dt.Name, pd.DepArch FROM PackageDepends pd ";
442 $q.= "LEFT JOIN Packages p ON p.ID = pd.PackageID ";
443 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
444 $q.= "WHERE pd.DepName IN (" . $name_list . ") ";
445 $q.= "OR SUBSTRING(pd.DepName FROM 1 FOR POSITION(': ' IN pd.DepName) - 1) IN (" . $name_list . ") ";
446 $q.= "ORDER BY p.Name";
447 $result = $dbh->query($q);
448 if (!$result) {return array();}
449 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
457 * Get all package sources for a specific package
459 * @param string $pkgid The package ID to get the sources for
461 * @return array All sources associated with a specific package
463 function pkg_sources($pkgid) {
465 $pkgid = intval($pkgid);
467 $dbh = DB
::connect();
468 $q = "SELECT Source, SourceArch FROM PackageSources ";
469 $q.= "WHERE PackageID = " . $pkgid;
470 $q.= " ORDER BY Source";
471 $result = $dbh->query($q);
475 while ($row = $result->fetch(PDO
::FETCH_NUM
)) {
483 * Determine package names from package IDs
485 * @param string|array $pkgids The package IDs to get names for
487 * @return array|string All names if multiple package IDs, otherwise package name
489 function pkg_name_from_id($pkgids) {
490 if (is_array($pkgids)) {
491 $pkgids = sanitize_ids($pkgids);
493 $dbh = DB
::connect();
494 $q = "SELECT Name FROM Packages WHERE ID IN (";
495 $q.= implode(",", $pkgids) . ")";
496 $result = $dbh->query($q);
498 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {
499 $names[] = $row['Name'];
504 elseif ($pkgids > 0) {
505 $dbh = DB
::connect();
506 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
507 $result = $dbh->query($q);
509 $name = $result->fetch(PDO
::FETCH_NUM
);
519 * Determine if a package name is on the database blacklist
521 * @param string $name The package name to check
523 * @return bool True if the name is blacklisted, otherwise false
525 function pkg_name_is_blacklisted($name) {
526 $dbh = DB
::connect();
527 $q = "SELECT COUNT(*) FROM PackageBlacklist ";
528 $q.= "WHERE Name = " . $dbh->quote($name);
529 $result = $dbh->query($q);
531 if (!$result) return false;
532 return ($result->fetchColumn() > 0);
536 * Get the package details
538 * @param string $id The package ID to get description for
540 * @return array The package's details OR error message
542 function pkg_get_details($id=0) {
543 $dbh = DB
::connect();
545 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
546 $q.= "PackageBases.Name AS BaseName, PackageBases.NumVotes, ";
547 $q.= "PackageBases.Popularity, PackageBases.OutOfDateTS, ";
548 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
549 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
550 $q.= "PackageBases.PackagerUID, PackageBases.FlaggerUID, ";
551 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
552 $q.= " WHERE PackageRequests.PackageBaseID = Packages.PackageBaseID ";
553 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
554 $q.= "FROM Packages, PackageBases ";
555 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
556 $q.= "AND Packages.ID = " . intval($id);
557 $result = $dbh->query($q);
562 $row['error'] = __("Error retrieving package details.");
565 $row = $result->fetch(PDO
::FETCH_ASSOC
);
567 $row['error'] = __("Package details could not be found.");
575 * Display the package details page
577 * @param string $id The package ID to get details page for
578 * @param array $row Package details retrieved by pkg_get_details()
579 * @param string $SID The session ID of the visitor
583 function pkg_display_details($id=0, $row, $SID="") {
584 $dbh = DB
::connect();
586 if (isset($row['error'])) {
587 print "<p>" . $row['error'] . "</p>\n";
590 $base_id = pkgbase_from_pkgid($id);
591 $pkgbase_name = pkgbase_name_from_id($base_id);
593 include('pkg_details.php');
596 include('pkg_comment_box.php');
599 $limit = isset($_GET['comments']) ?
0 : 10;
600 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED
);
601 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
602 if (!empty($comments)) {
603 include('pkg_comments.php');
608 /* pkg_search_page(SID)
609 * outputs the body of search/search results page
612 * SID - current Session ID
614 * package search page has been accessed
615 * request variables have not been sanitized
618 * O - starting result number
619 * PP - number of search hits per page
620 * K - package search string
621 * SO - search hit sort order:
622 * values: a - ascending
624 * SB - sort search hits by:
625 * values: n - package name
626 * v - number of votes
627 * m - maintainer username
628 * SeB- property that search string (K) represents
629 * values: n - package name
630 * nd - package name & description
631 * x - package name (exact match)
632 * m - package maintainer's username
633 * s - package submitter's username
634 * do_Orphans - boolean. whether to search packages
635 * without a maintainer
638 * These two are actually handled in packages.php.
640 * IDs- integer array of ticked packages' IDs
641 * action - action to be taken on ticked packages
642 * values: do_Flag - Flag out-of-date
643 * do_UnFlag - Remove out-of-date flag
647 * do_Notify - Enable notification
648 * do_UnNotify - Disable notification
650 function pkg_search_page($SID="") {
651 $dbh = DB
::connect();
654 * Get commonly used variables.
655 * TODO: Reduce the number of database queries!
658 $myuid = uid_from_sid($SID);
660 /* Sanitize paging variables. */
661 if (isset($_GET['O'])) {
662 $_GET['O'] = max(intval($_GET['O']), 0);
667 if (isset($_GET["PP"])) {
668 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
674 * FIXME: Pull out DB-related code. All of it! This one's worth a
675 * choco-chip cookie, one of those nice big soft ones.
678 /* Build the package search query. */
679 $q_select = "SELECT ";
681 $q_select .= "CommentNotify.UserID AS Notify,
682 PackageVotes.UsersID AS Voted, ";
684 $q_select .= "Users.Username AS Maintainer,
685 Packages.Name, Packages.Version, Packages.Description,
686 PackageBases.NumVotes, PackageBases.Popularity, Packages.ID,
687 Packages.PackageBaseID, PackageBases.OutOfDateTS ";
689 $q_from = "FROM Packages
690 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
691 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID) ";
693 /* This is not needed for the total row count query. */
694 $q_from_extra = "LEFT JOIN PackageVotes
695 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
696 LEFT JOIN CommentNotify
697 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
702 $q_where = 'WHERE PackageBases.PackagerUID IS NOT NULL ';
704 if (isset($_GET['K'])) {
705 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
706 /* Search by maintainer. */
707 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
709 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
710 /* Search by submitter. */
711 $q_where .= "AND SubmitterUID = " . intval(uid_from_username($_GET['K'])) . " ";
713 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
714 /* Search by name. */
715 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
716 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
718 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
719 /* Search by package base name. */
720 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
721 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
723 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
724 /* Search by name (exact match). */
725 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
727 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
728 /* Search by package base name (exact match). */
729 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
732 /* Keyword search (default). */
737 foreach (str_getcsv($_GET['K'], ' ') as $term) {
741 if ($count > 0 && strtolower($term) == "and") {
745 if ($count > 0 && strtolower($term) == "or") {
749 if ($count > 0 && strtolower($term) == "not") {
754 $term = "%" . addcslashes($term, '%_') . "%";
755 $q_keywords .= $op . " (Packages.Name LIKE " . $dbh->quote($term) . " OR ";
756 $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
757 $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
758 $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
759 $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
768 if (!empty($q_keywords)) {
769 $q_where .= "AND (" . $q_keywords . ") ";
774 if (isset($_GET["do_Orphans"])) {
775 $q_where .= "AND MaintainerUID IS NULL ";
778 if (isset($_GET['outdated'])) {
779 if ($_GET['outdated'] == 'on') {
780 $q_where .= "AND OutOfDateTS IS NOT NULL ";
782 elseif ($_GET['outdated'] == 'off') {
783 $q_where .= "AND OutOfDateTS IS NULL ";
787 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ?
'DESC' : 'ASC';
789 $q_sort = "ORDER BY ";
790 $sort_by = isset($_GET["SB"]) ?
$_GET["SB"] : '';
793 $q_sort .= "NumVotes " . $order . ", ";
796 $q_sort .= "Popularity " . $order . ", ";
800 $q_sort .= "Voted " . $order . ", ";
805 $q_sort .= "Notify " . $order . ", ";
809 $q_sort .= "Maintainer " . $order . ", ";
812 $q_sort .= "-ModifiedTS " . $order . ", ";
817 $q_sort .= " Packages.Name " . $order . " ";
819 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
821 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
822 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
824 $result = $dbh->query($q);
825 $result_t = $dbh->query($q_total);
827 $row = $result_t->fetch(PDO
::FETCH_NUM
);
834 if ($result && $total > 0) {
835 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
843 /* Calculate the results to use. */
844 $first = $_GET['O'] +
1;
846 /* Calculation of pagination links. */
847 $per_page = ($_GET['PP'] > 0) ?
$_GET['PP'] : 50;
848 $current = ceil($first / $per_page);
849 $pages = ceil($total / $per_page);
850 $templ_pages = array();
853 $templ_pages['« ' . __('First')] = 0;
854 $templ_pages['‹ ' . __('Previous')] = ($current - 2) * $per_page;
857 if ($current - 5 > 1)
858 $templ_pages["..."] = false;
860 for ($i = max($current - 5, 1); $i <= min($pages, $current +
5); $i++
) {
861 $templ_pages[$i] = ($i - 1) * $per_page;
864 if ($current +
5 < $pages)
865 $templ_pages["... "] = false;
867 if ($current < $pages) {
868 $templ_pages[__('Next') . ' ›'] = $current * $per_page;
869 $templ_pages[__('Last') . ' »'] = ($pages - 1) * $per_page;
872 include('pkg_search_form.php');
875 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {
876 $searchresults[] = $row;
880 include('pkg_search_results.php');
886 * Determine if a POST string has been sent by a visitor
888 * @param string $action String to check has been sent via POST
890 * @return bool True if the POST string was used, otherwise false
892 function current_action($action) {
893 return (isset($_POST['action']) && $_POST['action'] == $action) ||
894 isset($_POST[$action]);
898 * Determine if sent IDs are valid integers
900 * @param array $ids IDs to validate
902 * @return array All sent IDs that are valid integers
904 function sanitize_ids($ids) {
906 foreach ($ids as $id) {
916 * Add package information to the database for a specific package
918 * @param int $base_id ID of the package base
919 * @param string $pkgname Name of the new package
920 * @param string $pkgver Version of the new package
921 * @param string $pkgdesc Description of the new package
922 * @param string $pkgurl Upstream URL for the new package
924 * @return int ID of the new package
926 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
927 $dbh = DB
::connect();
928 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
929 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
930 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
931 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
933 return $dbh->lastInsertId();
937 * Add a dependency for a specific package to the database
939 * @param int $pkgid The package ID to add the dependency for
940 * @param string $type The type of dependency to add
941 * @param string $depname The name of the dependency to add
942 * @param string $depcondition The type of dependency for the package
943 * @param string $deparch The architecture of the dependency to add
947 function pkg_add_dep($pkgid, $type, $depname, $depcondition, $deparch) {
948 $dbh = DB
::connect();
949 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition, DepArch) VALUES (%d, %d, %s, %s, %s)",
951 pkg_dependency_type_id_from_name($type),
952 $dbh->quote($depname),
953 $dbh->quote($depcondition),
954 $deparch ?
$dbh->quote($deparch) : 'NULL'
960 * Add a relation for a specific package to the database
962 * @param int $pkgid The package ID to add the relation for
963 * @param string $type The type of relation to add
964 * @param string $relname The name of the relation to add
965 * @param string $relcondition The version requirement of the relation
966 * @param string $relarch The architecture of the relation to add
970 function pkg_add_rel($pkgid, $type, $relname, $relcondition, $relarch) {
971 $dbh = DB
::connect();
972 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition, RelArch) VALUES (%d, %d, %s, %s, %s)",
974 pkg_relation_type_id_from_name($type),
975 $dbh->quote($relname),
976 $dbh->quote($relcondition),
977 $relarch ?
$dbh->quote($relarch) : 'NULL'
983 * Add a source for a specific package to the database
985 * @param int $pkgid The package ID to add the source for
986 * @param string $pkgsrc The package source to add to the database
987 * @param string $srcarch The architecture of the source to add
991 function pkg_add_src($pkgid, $pkgsrc, $srcarch) {
992 $dbh = DB
::connect();
993 $q = sprintf("INSERT INTO PackageSources (PackageID, Source, SourceArch) VALUES (%d, %s, %s)",
995 $dbh->quote($pkgsrc),
996 $srcarch ?
$dbh->quote($srcarch) : 'NULL'
1002 * Creates a new group and returns its ID
1004 * If the groups already exists, the ID of the already existing group is
1007 * @param string $name The name of the group to create
1009 * @return int The ID of the group
1011 function pkg_create_group($name) {
1012 $dbh = DB
::connect();
1013 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
1014 $result = $dbh->query($q);
1016 $grpid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
1022 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
1024 return $dbh->lastInsertId();
1028 * Add a package to a group
1030 * @param int $pkgid The package ID of the package to add
1031 * @param int $grpid The group ID of the group to add the package to
1035 function pkg_add_grp($pkgid, $grpid) {
1036 $dbh = DB
::connect();
1037 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
1045 * Creates a new license and returns its ID
1047 * If the license already exists, the ID of the already existing license is
1050 * @param string $name The name of the license to create
1052 * @return int The ID of the license
1054 function pkg_create_license($name) {
1055 $dbh = DB
::connect();
1056 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
1057 $result = $dbh->query($q);
1059 $licid = $result->fetch(PDO
::FETCH_COLUMN
, 0);
1065 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
1067 return $dbh->lastInsertId();
1071 * Add a license to a package
1073 * @param int $pkgid The package ID of the package
1074 * @param int $grpid The ID of the license to add
1078 function pkg_add_lic($pkgid, $licid) {
1079 $dbh = DB
::connect();
1080 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
1088 * Determine package information for latest package
1090 * @param int $numpkgs Number of packages to get information on
1092 * @return array $packages Package info for the specified number of recent packages
1094 function latest_pkgs($numpkgs) {
1095 $dbh = DB
::connect();
1097 $q = "SELECT Packages.*, MaintainerUID, SubmittedTS ";
1098 $q.= "FROM Packages LEFT JOIN PackageBases ON ";
1099 $q.= "PackageBases.ID = Packages.PackageBaseID ";
1100 $q.= "ORDER BY SubmittedTS DESC ";
1101 $q.= "LIMIT " . intval($numpkgs);
1102 $result = $dbh->query($q);
1104 $packages = array();
1106 while ($row = $result->fetch(PDO
::FETCH_ASSOC
)) {