Add a packager field to packages
[aur.git] / web / lib / pkgfuncs.inc.php
blobc35147fbc28b526ca50adb88000eb379ac723226
1 <?php
2 include_once("config.inc.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
12 * @param string $atype The account type of the user trying to delete a comment
13 * @param string|int $uid The user ID of the individual trying to delete a comment
15 * @return bool True if the user can delete the comment, otherwise false
17 function can_delete_comment($comment_id=0, $atype="", $uid=0) {
18 if (!$uid) {
19 /* Unauthenticated users cannot delete anything. */
20 return false;
22 if ($atype == "Trusted User" || $atype == "Developer") {
23 /* TUs and developers can delete any comment. */
24 return true;
27 $dbh = DB::connect();
29 $q = "SELECT COUNT(*) FROM PackageComments ";
30 $q.= "WHERE ID = " . intval($comment_id) . " AND UsersID = " . $uid;
31 $result = $dbh->query($q);
33 if (!$result) {
34 return false;
37 $row = $result->fetch(PDO::FETCH_NUM);
38 return ($row[0] > 0);
41 /**
42 * Determine if the user can delete a specific package comment using an array
44 * Only the comment submitter, Trusted Users, and Developers can delete
45 * comments. This function is used for the frontend side of comment deletion.
47 * @param array $comment All database information relating a specific comment
48 * @param string $atype The account type of the user trying to delete a comment
49 * @param string|int $uid The user ID of the individual trying to delete a comment
51 * @return bool True if the user can delete the comment, otherwise false
53 function can_delete_comment_array($comment, $atype="", $uid=0) {
54 if (!$uid) {
55 /* Unauthenticated users cannot delete anything. */
56 return false;
57 } elseif ($atype == "Trusted User" || $atype == "Developer") {
58 /* TUs and developers can delete any comment. */
59 return true;
60 } else if ($comment['UsersID'] == $uid) {
61 /* Users can delete their own comments. */
62 return true;
64 return false;
67 /**
68 * Determine if the visitor can submit blacklisted packages.
70 * Only Trusted Users and Developers can delete blacklisted packages. Packages
71 * are blacklisted if they are include in the official repositories.
73 * @param string $atype The account type of the user
75 * @return bool True if the user can submit blacklisted packages, otherwise false
77 function can_submit_blacklisted($atype = "") {
78 if ($atype == "Trusted User" || $atype == "Developer") {
79 /* Only TUs and developers can submit blacklisted packages. */
80 return true;
82 else {
83 return false;
87 /**
88 * Check to see if the package name already exists in the database
90 * @param string $name The package name to check
92 * @return string|void Package name if it already exists
94 function pkg_from_name($name="") {
95 if (!$name) {return NULL;}
96 $dbh = DB::connect();
97 $q = "SELECT ID FROM Packages ";
98 $q.= "WHERE Name = " . $dbh->quote($name);
99 $result = $dbh->query($q);
100 if (!$result) {
101 return;
103 $row = $result->fetch(PDO::FETCH_NUM);
104 return $row[0];
108 * Get licenses for a specific package
110 * @param int $pkgid The package to get licenses for
112 * @return array All licenses for the package
114 function pkg_licenses($pkgid) {
115 $lics = array();
116 $pkgid = intval($pkgid);
117 if ($pkgid > 0) {
118 $dbh = DB::connect();
119 $q = "SELECT l.Name FROM Licenses l ";
120 $q.= "INNER JOIN PackageLicenses pl ON pl.LicenseID = l.ID ";
121 $q.= "WHERE pl.PackageID = ". $pkgid;
122 $result = $dbh->query($q);
123 if (!$result) {
124 return array();
126 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
127 $lics[] = $row;
130 return $lics;
134 * Get package groups for a specific package
136 * @param int $pkgid The package to get groups for
138 * @return array All package groups for the package
140 function pkg_groups($pkgid) {
141 $grps = array();
142 $pkgid = intval($pkgid);
143 if ($pkgid > 0) {
144 $dbh = DB::connect();
145 $q = "SELECT g.Name FROM Groups g ";
146 $q.= "INNER JOIN PackageGroups pg ON pg.GroupID = g.ID ";
147 $q.= "WHERE pg.PackageID = ". $pkgid;
148 $result = $dbh->query($q);
149 if (!$result) {
150 return array();
152 while ($row = $result->fetch(PDO::FETCH_COLUMN, 0)) {
153 $grps[] = $row;
156 return $grps;
160 * Get package dependencies for a specific package
162 * @param int $pkgid The package to get dependencies for
164 * @return array All package dependencies for the package
166 function pkg_dependencies($pkgid) {
167 $deps = array();
168 $pkgid = intval($pkgid);
169 if ($pkgid > 0) {
170 $dbh = DB::connect();
171 $q = "SELECT pd.DepName, dt.Name, pd.DepCondition, p.ID FROM PackageDepends pd ";
172 $q.= "LEFT JOIN Packages p ON pd.DepName = p.Name ";
173 $q.= "LEFT JOIN DependencyTypes dt ON dt.ID = pd.DepTypeID ";
174 $q.= "WHERE pd.PackageID = ". $pkgid . " ";
175 $q.= "ORDER BY pd.DepName";
176 $result = $dbh->query($q);
177 if (!$result) {
178 return array();
180 while ($row = $result->fetch(PDO::FETCH_NUM)) {
181 $deps[] = $row;
184 return $deps;
188 * Get package relations for a specific package
190 * @param int $pkgid The package to get relations for
192 * @return array All package relations for the package
194 function pkg_relations($pkgid) {
195 $rels = array();
196 $pkgid = intval($pkgid);
197 if ($pkgid > 0) {
198 $dbh = DB::connect();
199 $q = "SELECT pr.RelName, rt.Name, pr.RelCondition, p.ID FROM PackageRelations pr ";
200 $q.= "LEFT JOIN Packages p ON pr.RelName = p.Name ";
201 $q.= "LEFT JOIN RelationTypes rt ON rt.ID = pr.RelTypeID ";
202 $q.= "WHERE pr.PackageID = ". $pkgid . " ";
203 $q.= "ORDER BY pr.RelName";
204 $result = $dbh->query($q);
205 if (!$result) {
206 return array();
208 while ($row = $result->fetch(PDO::FETCH_NUM)) {
209 $rels[] = $row;
212 return $rels;
216 * Get the ID of a dependency type given its name
218 * @param string $name The name of the dependency type
220 * @return int The ID of the dependency type
222 function pkg_dependency_type_id_from_name($name) {
223 $dbh = DB::connect();
224 $q = "SELECT ID FROM DependencyTypes WHERE Name = ";
225 $q.= $dbh->quote($name);
226 $result = $dbh->query($q);
227 return $result->fetch(PDO::FETCH_COLUMN, 0);
231 * Get the ID of a relation type given its name
233 * @param string $name The name of the relation type
235 * @return int The ID of the relation type
237 function pkg_relation_type_id_from_name($name) {
238 $dbh = DB::connect();
239 $q = "SELECT ID FROM RelationTypes WHERE Name = ";
240 $q.= $dbh->quote($name);
241 $result = $dbh->query($q);
242 return $result->fetch(PDO::FETCH_COLUMN, 0);
246 * Get the HTML code to display a package dependency link
248 * @param string $name The name of the dependency
249 * @param string $type The name of the dependency type
250 * @param string $cond The package dependency condition string
251 * @param int $pkg_id The package of the package to display the dependency for
253 * @return string The HTML code of the label to display
255 function pkg_depend_link($name, $type, $cond, $pkg_id) {
256 if ($type == 'optdepends' && strpos($name, ':') !== false) {
257 $tokens = explode(':', $name, 2);
258 $name = $tokens[0];
259 $desc = $tokens[1];
260 } else {
261 $desc = '(unknown)';
264 $link = '<a href="';
265 if (is_null($pkg_id)) {
266 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
267 } else {
268 $link .= htmlspecialchars(get_pkg_uri($name), ENT_QUOTES);
270 $link .= '" title="' . __('View packages details for') .' ' . htmlspecialchars($name) . '">';
271 $link .= htmlspecialchars($name) . '</a>';
272 $link .= htmlspecialchars($cond);
274 if ($type == 'makedepends') {
275 $link .= ' <em>(make)</em>';
276 } elseif ($type == 'checkdepends') {
277 $link .= ' <em>(check)</em>';
278 } elseif ($type == 'optdepends') {
279 $link .= ' <em>(optional) &ndash; ' . htmlspecialchars($desc) . ' </em>';
282 return $link;
286 * Determine packages that depend on a package
288 * @param string $name The package name for the dependency search
290 * @return array All packages that depend on the specified package name
292 function pkg_required($name="") {
293 $deps = array();
294 if ($name != "") {
295 $dbh = DB::connect();
296 $q = "SELECT DISTINCT p.Name, PackageID FROM PackageDepends pd ";
297 $q.= "JOIN Packages p ON pd.PackageID = p.ID ";
298 $q.= "WHERE DepName = " . $dbh->quote($name) . " ";
299 $q.= "ORDER BY p.Name";
300 $result = $dbh->query($q);
301 if (!$result) {return array();}
302 while ($row = $result->fetch(PDO::FETCH_NUM)) {
303 $deps[] = $row;
306 return $deps;
310 * Get all package sources for a specific package
312 * @param string $pkgid The package ID to get the sources for
314 * @return array All sources associated with a specific package
316 function pkg_sources($pkgid) {
317 $sources = array();
318 $pkgid = intval($pkgid);
319 if ($pkgid > 0) {
320 $dbh = DB::connect();
321 $q = "SELECT Source FROM PackageSources ";
322 $q.= "WHERE PackageID = " . $pkgid;
323 $q.= " ORDER BY Source";
324 $result = $dbh->query($q);
325 if (!$result) {
326 return array();
328 while ($row = $result->fetch(PDO::FETCH_NUM)) {
329 $sources[] = $row[0];
332 return $sources;
336 * Determine package names from package IDs
338 * @param string|array $pkgids The package IDs to get names for
340 * @return array|string All names if multiple package IDs, otherwise package name
342 function pkg_name_from_id($pkgids) {
343 if (is_array($pkgids)) {
344 $pkgids = sanitize_ids($pkgids);
345 $names = array();
346 $dbh = DB::connect();
347 $q = "SELECT Name FROM Packages WHERE ID IN (";
348 $q.= implode(",", $pkgids) . ")";
349 $result = $dbh->query($q);
350 if ($result) {
351 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
352 $names[] = $row['Name'];
355 return $names;
357 elseif ($pkgids > 0) {
358 $dbh = DB::connect();
359 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
360 $result = $dbh->query($q);
361 if ($result) {
362 $name = $result->fetch(PDO::FETCH_NUM);
364 return $name[0];
366 else {
367 return NULL;
372 * Determine if a package name is on the database blacklist
374 * @param string $name The package name to check
376 * @return bool True if the name is blacklisted, otherwise false
378 function pkg_name_is_blacklisted($name) {
379 $dbh = DB::connect();
380 $q = "SELECT COUNT(*) FROM PackageBlacklist ";
381 $q.= "WHERE Name = " . $dbh->quote($name);
382 $result = $dbh->query($q);
384 if (!$result) return false;
385 return ($result->fetchColumn() > 0);
389 * Get the package details
391 * @param string $id The package ID to get description for
393 * @return array The package's details OR error message
395 function pkg_get_details($id=0) {
396 $dbh = DB::connect();
398 $q = "SELECT Packages.*, PackageBases.ID AS BaseID, ";
399 $q.= "PackageBases.Name AS BaseName, PackageBases.CategoryID, ";
400 $q.= "PackageBases.NumVotes, PackageBases.OutOfDateTS, ";
401 $q.= "PackageBases.SubmittedTS, PackageBases.ModifiedTS, ";
402 $q.= "PackageBases.SubmitterUID, PackageBases.MaintainerUID, ";
403 $q.= "PackageBases.PackagerUID, PackageCategories.Category ";
404 $q.= "FROM Packages, PackageBases, PackageCategories ";
405 $q.= "WHERE PackageBases.ID = Packages.PackageBaseID ";
406 $q.= "AND PackageBases.CategoryID = PackageCategories.ID ";
407 $q.= "AND Packages.ID = " . intval($id);
408 $result = $dbh->query($q);
410 $row = array();
412 if (!$result) {
413 $row['error'] = __("Error retrieving package details.");
415 else {
416 $row = $result->fetch(PDO::FETCH_ASSOC);
417 if (empty($row)) {
418 $row['error'] = __("Package details could not be found.");
422 return $row;
426 * Display the package details page
428 * @global string $AUR_LOCATION The AUR's URL used for notification e-mails
429 * @global bool $USE_VIRTUAL_URLS True if using URL rewriting, otherwise false
430 * @param string $id The package ID to get details page for
431 * @param array $row Package details retrieved by pkg_get_details()
432 * @param string $SID The session ID of the visitor
434 * @return void
436 function pkg_display_details($id=0, $row, $SID="") {
437 global $AUR_LOCATION;
438 global $USE_VIRTUAL_URLS;
440 $dbh = DB::connect();
442 if (isset($row['error'])) {
443 print "<p>" . $row['error'] . "</p>\n";
445 else {
446 $base_id = pkgbase_from_pkgid($id);
447 $pkgbase_name = pkgbase_name_from_id($base_id);
449 include('pkg_details.php');
451 if ($SID) {
452 include('actions_form.php');
453 include('pkg_comment_form.php');
456 $limit = isset($_GET['comments']) ? 0 : 10;
457 $comments = pkgbase_comments($base_id, $limit);
458 if (!empty($comments)) {
459 include('pkg_comments.php');
464 /* pkg_search_page(SID)
465 * outputs the body of search/search results page
467 * parameters:
468 * SID - current Session ID
469 * preconditions:
470 * package search page has been accessed
471 * request variables have not been sanitized
473 * request vars:
474 * O - starting result number
475 * PP - number of search hits per page
476 * C - package category ID number
477 * K - package search string
478 * SO - search hit sort order:
479 * values: a - ascending
480 * d - descending
481 * SB - sort search hits by:
482 * values: c - package category
483 * n - package name
484 * v - number of votes
485 * m - maintainer username
486 * SeB- property that search string (K) represents
487 * values: n - package name
488 * nd - package name & description
489 * x - package name (exact match)
490 * m - package maintainer's username
491 * s - package submitter's username
492 * do_Orphans - boolean. whether to search packages
493 * without a maintainer
496 * These two are actually handled in packages.php.
498 * IDs- integer array of ticked packages' IDs
499 * action - action to be taken on ticked packages
500 * values: do_Flag - Flag out-of-date
501 * do_UnFlag - Remove out-of-date flag
502 * do_Adopt - Adopt
503 * do_Disown - Disown
504 * do_Delete - Delete (requires confirm_Delete to be set)
505 * do_Notify - Enable notification
506 * do_UnNotify - Disable notification
508 function pkg_search_page($SID="") {
509 $dbh = DB::connect();
512 * Get commonly used variables.
513 * TODO: Reduce the number of database queries!
515 if ($SID)
516 $myuid = uid_from_sid($SID);
517 $cats = pkgbase_categories($dbh);
519 /* Sanitize paging variables. */
520 if (isset($_GET['O'])) {
521 $_GET['O'] = intval($_GET['O']);
522 if ($_GET['O'] < 0)
523 $_GET['O'] = 0;
525 else {
526 $_GET['O'] = 0;
529 if (isset($_GET["PP"])) {
530 $_GET["PP"] = intval($_GET["PP"]);
531 if ($_GET["PP"] < 50)
532 $_GET["PP"] = 50;
533 else if ($_GET["PP"] > 250)
534 $_GET["PP"] = 250;
536 else {
537 $_GET["PP"] = 50;
541 * FIXME: Pull out DB-related code. All of it! This one's worth a
542 * choco-chip cookie, one of those nice big soft ones.
545 /* Build the package search query. */
546 $q_select = "SELECT ";
547 if ($SID) {
548 $q_select .= "CommentNotify.UserID AS Notify,
549 PackageVotes.UsersID AS Voted, ";
551 $q_select .= "Users.Username AS Maintainer,
552 PackageCategories.Category,
553 Packages.Name, Packages.Version, Packages.Description,
554 PackageBases.NumVotes, Packages.ID, Packages.PackageBaseID,
555 PackageBases.OutOfDateTS ";
557 $q_from = "FROM Packages
558 LEFT JOIN PackageBases ON (PackageBases.ID = Packages.PackageBaseID)
559 LEFT JOIN Users ON (PackageBases.MaintainerUID = Users.ID)
560 LEFT JOIN PackageCategories
561 ON (PackageBases.CategoryID = PackageCategories.ID) ";
562 if ($SID) {
563 /* This is not needed for the total row count query. */
564 $q_from_extra = "LEFT JOIN PackageVotes
565 ON (PackageBases.ID = PackageVotes.PackageBaseID AND PackageVotes.UsersID = $myuid)
566 LEFT JOIN CommentNotify
567 ON (PackageBases.ID = CommentNotify.PackageBaseID AND CommentNotify.UserID = $myuid) ";
568 } else {
569 $q_from_extra = "";
572 $q_where = "WHERE 1 = 1 ";
574 * TODO: Possibly do string matching on category to make request
575 * variable values more sensible.
577 if (isset($_GET["C"]) && intval($_GET["C"])) {
578 $q_where .= "AND PackageBases.CategoryID = ".intval($_GET["C"])." ";
581 if (isset($_GET['K'])) {
582 if (isset($_GET["SeB"]) && $_GET["SeB"] == "m") {
583 /* Search by maintainer. */
584 $q_where .= "AND Users.Username = " . $dbh->quote($_GET['K']) . " ";
586 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "s") {
587 /* Search by submitter. */
588 $q_where .= "AND SubmitterUID = ".uid_from_username($_GET['K'])." ";
590 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "n") {
591 /* Search by name. */
592 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
593 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . ") ";
595 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "b") {
596 /* Search by package base name. */
597 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
598 $q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
600 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "N") {
601 /* Search by name (exact match). */
602 $q_where .= "AND (Packages.Name = " . $dbh->quote($_GET['K']) . ") ";
604 elseif (isset($_GET["SeB"]) && $_GET["SeB"] == "B") {
605 /* Search by package base name (exact match). */
606 $q_where .= "AND (PackageBases.Name = " . $dbh->quote($_GET['K']) . ") ";
608 else {
609 /* Search by name and description (default). */
610 $K = "%" . addcslashes($_GET['K'], '%_') . "%";
611 $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . " OR ";
612 $q_where .= "Description LIKE " . $dbh->quote($K) . ") ";
616 if (isset($_GET["do_Orphans"])) {
617 $q_where .= "AND MaintainerUID IS NULL ";
620 if (isset($_GET['outdated'])) {
621 if ($_GET['outdated'] == 'on') {
622 $q_where .= "AND OutOfDateTS IS NOT NULL ";
624 elseif ($_GET['outdated'] == 'off') {
625 $q_where .= "AND OutOfDateTS IS NULL ";
629 $order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
631 $q_sort = "ORDER BY ";
632 $sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
633 switch ($sort_by) {
634 case 'c':
635 $q_sort .= "CategoryID " . $order . ", ";
636 break;
637 case 'v':
638 $q_sort .= "NumVotes " . $order . ", ";
639 break;
640 case 'w':
641 if ($SID) {
642 $q_sort .= "Voted " . $order . ", ";
644 break;
645 case 'o':
646 if ($SID) {
647 $q_sort .= "Notify " . $order . ", ";
649 break;
650 case 'm':
651 $q_sort .= "Maintainer " . $order . ", ";
652 break;
653 case 'a':
654 $q_sort .= "ModifiedTS " . $order . ", ";
655 break;
656 default:
657 break;
659 $q_sort .= " Packages.Name " . $order . " ";
661 $q_limit = "LIMIT ".$_GET["PP"]." OFFSET ".$_GET["O"];
663 $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit;
664 $q_total = "SELECT COUNT(*) " . $q_from . $q_where;
666 $result = $dbh->query($q);
667 $result_t = $dbh->query($q_total);
668 if ($result_t) {
669 $row = $result_t->fetch(PDO::FETCH_NUM);
670 $total = $row[0];
672 else {
673 $total = 0;
676 if ($result && $total > 0) {
677 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
678 $SO_next = "a";
680 else {
681 $SO_next = "d";
685 /* Calculate the results to use. */
686 $first = $_GET['O'] + 1;
688 /* Calculation of pagination links. */
689 $per_page = ($_GET['PP'] > 0) ? $_GET['PP'] : 50;
690 $current = ceil($first / $per_page);
691 $pages = ceil($total / $per_page);
692 $templ_pages = array();
694 if ($current > 1) {
695 $templ_pages['&laquo; ' . __('First')] = 0;
696 $templ_pages['&lsaquo; ' . __('Previous')] = ($current - 2) * $per_page;
699 if ($current - 5 > 1)
700 $templ_pages["..."] = false;
702 for ($i = max($current - 5, 1); $i <= min($pages, $current + 5); $i++) {
703 $templ_pages[$i] = ($i - 1) * $per_page;
706 if ($current + 5 < $pages)
707 $templ_pages["... "] = false;
709 if ($current < $pages) {
710 $templ_pages[__('Next') . ' &rsaquo;'] = $current * $per_page;
711 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
714 include('pkg_search_form.php');
716 if ($result) {
717 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
718 $searchresults[] = $row;
722 include('pkg_search_results.php');
724 return;
728 * Determine if a POST string has been sent by a visitor
730 * @param string $action String to check has been sent via POST
732 * @return bool True if the POST string was used, otherwise false
734 function current_action($action) {
735 return (isset($_POST['action']) && $_POST['action'] == $action) ||
736 isset($_POST[$action]);
740 * Determine if sent IDs are valid integers
742 * @param array $ids IDs to validate
744 * @return array All sent IDs that are valid integers
746 function sanitize_ids($ids) {
747 $new_ids = array();
748 foreach ($ids as $id) {
749 $id = intval($id);
750 if ($id > 0) {
751 $new_ids[] = $id;
754 return $new_ids;
758 * Get all package information in the database for a specific package
760 * @param string $pkgname The name of the package to get details for
762 * @return array All package details for a specific package
764 function pkg_details_by_name($pkgname) {
765 $dbh = DB::connect();
766 $q = "SELECT Packages.*, PackageBases.Name AS BaseName, ";
767 $q.= "PackageBases.CategoryID, PackageBases.NumVotes, ";
768 $q.= "PackageBases.OutOfDateTS, PackageBases.SubmittedTS, ";
769 $q.= "PackageBases.ModifiedTS, PackageBases.SubmitterUID, ";
770 $q.= "PackageBases.MaintainerUID FROM Packages ";
771 $q.= "INNER JOIN PackageBases ";
772 $q.= "ON PackageBases.ID = Packages.PackageBaseID WHERE ";
773 $q.= "Packages.Name = " . $dbh->quote($pkgname);
774 $result = $dbh->query($q);
775 if ($result) {
776 $row = $result->fetch(PDO::FETCH_ASSOC);
778 return $row;
782 * Add package information to the database for a specific package
784 * @param int $base_id ID of the package base
785 * @param string $pkgname Name of the new package
786 * @param string $pkgver Version of the new package
787 * @param string $pkgdesc Description of the new package
788 * @param string $pkgurl Upstream URL for the new package
790 * @return int ID of the new package
792 function pkg_create($base_id, $pkgname, $pkgver, $pkgdesc, $pkgurl) {
793 $dbh = DB::connect();
794 $q = sprintf("INSERT INTO Packages (PackageBaseID, Name, Version, " .
795 "Description, URL) VALUES (%d, %s, %s, %s, %s)",
796 $base_id, $dbh->quote($pkgname), $dbh->quote($pkgver),
797 $dbh->quote($pkgdesc), $dbh->quote($pkgurl));
798 $dbh->exec($q);
799 return $dbh->lastInsertId();
803 * Add a dependency for a specific package to the database
805 * @param int $pkgid The package ID to add the dependency for
806 * @param string $type The type of dependency to add
807 * @param string $depname The name of the dependency to add
808 * @param string $depcondition The type of dependency for the package
810 * @return void
812 function pkg_add_dep($pkgid, $type, $depname, $depcondition) {
813 $dbh = DB::connect();
814 $q = sprintf("INSERT INTO PackageDepends (PackageID, DepTypeID, DepName, DepCondition) VALUES (%d, %d, %s, %s)",
815 $pkgid,
816 pkg_dependency_type_id_from_name($type),
817 $dbh->quote($depname),
818 $dbh->quote($depcondition)
820 $dbh->exec($q);
824 * Add a relation for a specific package to the database
826 * @param int $pkgid The package ID to add the relation for
827 * @param string $type The type of relation to add
828 * @param string $relname The name of the relation to add
829 * @param string $relcondition The version requirement of the relation
831 * @return void
833 function pkg_add_rel($pkgid, $type, $relname, $relcondition) {
834 $dbh = DB::connect();
835 $q = sprintf("INSERT INTO PackageRelations (PackageID, RelTypeID, RelName, RelCondition) VALUES (%d, %d, %s, %s)",
836 $pkgid,
837 pkg_relation_type_id_from_name($type),
838 $dbh->quote($relname),
839 $dbh->quote($relcondition)
841 $dbh->exec($q);
845 * Add a source for a specific package to the database
847 * @param int $pkgid The package ID to add the source for
848 * @param string $pkgsrc The package source to add to the database
850 * @return void
852 function pkg_add_src($pkgid, $pkgsrc) {
853 $dbh = DB::connect();
854 $q = "INSERT INTO PackageSources (PackageID, Source) VALUES (";
855 $q .= $pkgid . ", " . $dbh->quote($pkgsrc) . ")";
857 $dbh->exec($q);
861 * Creates a new group and returns its ID
863 * If the groups already exists, the ID of the already existing group is
864 * returned.
866 * @param string $name The name of the group to create
868 * @return int The ID of the group
870 function pkg_create_group($name) {
871 $dbh = DB::connect();
872 $q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
873 $result = $dbh->query($q);
874 if ($result) {
875 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
876 if ($grpid > 0) {
877 return $grpid;
881 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
882 $dbh->exec($q);
883 return $dbh->lastInsertId();
887 * Add a package to a group
889 * @param int $pkgid The package ID of the package to add
890 * @param int $grpid The group ID of the group to add the package to
892 * @return void
894 function pkg_add_grp($pkgid, $grpid) {
895 $dbh = DB::connect();
896 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
897 $pkgid,
898 $grpid
900 $dbh->exec($q);
904 * Creates a new license and returns its ID
906 * If the license already exists, the ID of the already existing license is
907 * returned.
909 * @param string $name The name of the license to create
911 * @return int The ID of the license
913 function pkg_create_license($name) {
914 $dbh = DB::connect();
915 $q = sprintf("SELECT ID FROM Licenses WHERE Name = %s", $dbh->quote($name));
916 $result = $dbh->query($q);
917 if ($result) {
918 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
919 if ($licid > 0) {
920 return $licid;
924 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
925 $dbh->exec($q);
926 return $dbh->lastInsertId();
930 * Add a license to a package
932 * @param int $pkgid The package ID of the package
933 * @param int $grpid The ID of the license to add
935 * @return void
937 function pkg_add_lic($pkgid, $licid) {
938 $dbh = DB::connect();
939 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
940 $pkgid,
941 $licid
943 $dbh->exec($q);