Show providers in dependencies
[aur.git] / web / lib / pkgfuncs.inc.php
blobd83a01ff4d5dfb37b39e908edd571642abacce79
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 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);
173 if (!$result) {
174 return array();
177 $providers = array();
178 while ($row = $result->fetch(PDO::FETCH_NUM)) {
179 $providers[] = $row;
181 return $providers;
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) {
192 $deps = array();
193 $pkgid = intval($pkgid);
194 if ($pkgid > 0) {
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);
203 if (!$result) {
204 return array();
206 while ($row = $result->fetch(PDO::FETCH_NUM)) {
207 $deps[] = $row;
210 return $deps;
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) {
221 $rels = array();
222 $pkgid = intval($pkgid);
223 if ($pkgid > 0) {
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);
231 if (!$result) {
232 return array();
234 while ($row = $result->fetch(PDO::FETCH_NUM)) {
235 $rels[] = $row;
238 return $rels;
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);
256 $name = $tokens[0];
257 $desc = $tokens[1];
258 } else {
259 $desc = '(unknown)';
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);
285 $link .= ')</span>';
286 } else {
287 $link = '<a href="';
288 if (is_null($pkg_id)) {
289 $link .= 'https://www.archlinux.org/packages/?q=' . urlencode($name);
290 } else {
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) {
299 $link .= ' <em>(';
301 if ($type == 'makedepends') {
302 $link .= 'make';
303 } elseif ($type == 'checkdepends') {
304 $link .= 'check';
305 } elseif ($type == 'optdepends') {
306 $link .= 'optional';
309 if ($type != 'depends' && $arch) {
310 $link .= ', ';
313 if ($arch) {
314 $link .= htmlspecialchars($arch);
317 $link .= ')';
318 if ($show_desc && $type == 'optdepends') {
319 $link .= ' &ndash; ' . htmlspecialchars($desc) . ' </em>';
321 $link .= '</em>';
324 return $link;
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);
339 if ($arch) {
340 $html .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
343 return $html;
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>';
360 } else {
361 $link = htmlspecialchars($url[0]);
364 if ($arch) {
365 $link .= ' <em>(' . htmlspecialchars($arch) . ')</em>';
368 return $link;
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="") {
379 $deps = array();
380 if ($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)) {
391 $deps[] = $row;
394 return $deps;
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) {
405 $sources = array();
406 $pkgid = intval($pkgid);
407 if ($pkgid > 0) {
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);
413 if (!$result) {
414 return array();
416 while ($row = $result->fetch(PDO::FETCH_NUM)) {
417 $sources[] = $row;
420 return $sources;
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);
433 $names = array();
434 $dbh = DB::connect();
435 $q = "SELECT Name FROM Packages WHERE ID IN (";
436 $q.= implode(",", $pkgids) . ")";
437 $result = $dbh->query($q);
438 if ($result) {
439 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
440 $names[] = $row['Name'];
443 return $names;
445 elseif ($pkgids > 0) {
446 $dbh = DB::connect();
447 $q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
448 $result = $dbh->query($q);
449 if ($result) {
450 $name = $result->fetch(PDO::FETCH_NUM);
452 return $name[0];
454 else {
455 return NULL;
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);
500 $row = array();
502 if (!$result) {
503 $row['error'] = __("Error retrieving package details.");
505 else {
506 $row = $result->fetch(PDO::FETCH_ASSOC);
507 if (empty($row)) {
508 $row['error'] = __("Package details could not be found.");
512 return $row;
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
522 * @return void
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";
530 else {
531 $base_id = pkgbase_from_pkgid($id);
532 $pkgbase_name = pkgbase_name_from_id($base_id);
534 include('pkg_details.php');
536 if ($SID) {
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
552 * parameters:
553 * SID - current Session ID
554 * preconditions:
555 * package search page has been accessed
556 * request variables have not been sanitized
558 * request vars:
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
564 * d - descending
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
585 * do_Adopt - Adopt
586 * do_Disown - Disown
587 * do_Delete - Delete
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!
598 if ($SID)
599 $myuid = uid_from_sid($SID);
601 /* Sanitize paging variables. */
602 if (isset($_GET['O'])) {
603 $_GET['O'] = max(intval($_GET['O']), 0);
604 } else {
605 $_GET['O'] = 0;
608 if (isset($_GET["PP"])) {
609 $_GET["PP"] = bound(intval($_GET["PP"]), 50, 250);
610 } else {
611 $_GET["PP"] = 50;
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 ";
621 if ($SID) {
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) ";
633 if ($SID) {
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) ";
639 } else {
640 $q_from_extra = "";
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']) . ") ";
672 else {
673 /* Keyword search (default). */
674 $count = 0;
675 $q_keywords = "";
676 $op = "";
678 foreach (str_getcsv($_GET['K'], ' ') as $term) {
679 if ($term == "") {
680 continue;
682 if ($count > 0 && strtolower($term) == "and") {
683 $op = "AND ";
684 continue;
686 if ($count > 0 && strtolower($term) == "or") {
687 $op = "OR ";
688 continue;
690 if ($count > 0 && strtolower($term) == "not") {
691 $op .= "NOT ";
692 continue;
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) . ")) ";
702 $count++;
703 if ($count >= 20) {
704 break;
706 $op = "AND ";
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"] : '';
732 switch ($sort_by) {
733 case 'v':
734 $q_sort .= "NumVotes " . $order . ", ";
735 break;
736 case 'p':
737 $q_sort .= "Popularity " . $order . ", ";
738 break;
739 case 'w':
740 if ($SID) {
741 $q_sort .= "Voted " . $order . ", ";
743 break;
744 case 'o':
745 if ($SID) {
746 $q_sort .= "Notify " . $order . ", ";
748 break;
749 case 'm':
750 $q_sort .= "Maintainer " . $order . ", ";
751 break;
752 case 'a':
753 $q_sort .= "-ModifiedTS " . $order . ", ";
754 break;
755 default:
756 break;
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);
767 if ($result_t) {
768 $row = $result_t->fetch(PDO::FETCH_NUM);
769 $total = $row[0];
771 else {
772 $total = 0;
775 if ($result && $total > 0) {
776 if (isset($_GET["SO"]) && $_GET["SO"] == "d"){
777 $SO_next = "a";
779 else {
780 $SO_next = "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();
793 if ($current > 1) {
794 $templ_pages['&laquo; ' . __('First')] = 0;
795 $templ_pages['&lsaquo; ' . __('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') . ' &rsaquo;'] = $current * $per_page;
810 $templ_pages[__('Last') . ' &raquo;'] = ($pages - 1) * $per_page;
813 include('pkg_search_form.php');
815 if ($result) {
816 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
817 $searchresults[] = $row;
821 include('pkg_search_results.php');
823 return;
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) {
846 $new_ids = array();
847 foreach ($ids as $id) {
848 $id = intval($id);
849 if ($id > 0) {
850 $new_ids[] = $id;
853 return $new_ids;
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));
873 $dbh->exec($q);
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
886 * @return void
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)",
891 $pkgid,
892 pkg_dependency_type_id_from_name($type),
893 $dbh->quote($depname),
894 $dbh->quote($depcondition),
895 $deparch ? $dbh->quote($deparch) : 'NULL'
897 $dbh->exec($q);
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
909 * @return void
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)",
914 $pkgid,
915 pkg_relation_type_id_from_name($type),
916 $dbh->quote($relname),
917 $dbh->quote($relcondition),
918 $relarch ? $dbh->quote($relarch) : 'NULL'
920 $dbh->exec($q);
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
930 * @return void
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)",
935 $pkgid,
936 $dbh->quote($pkgsrc),
937 $srcarch ? $dbh->quote($srcarch) : 'NULL'
939 $dbh->exec($q);
943 * Creates a new group and returns its ID
945 * If the groups already exists, the ID of the already existing group is
946 * returned.
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);
956 if ($result) {
957 $grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
958 if ($grpid > 0) {
959 return $grpid;
963 $q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
964 $dbh->exec($q);
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
974 * @return void
976 function pkg_add_grp($pkgid, $grpid) {
977 $dbh = DB::connect();
978 $q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
979 $pkgid,
980 $grpid
982 $dbh->exec($q);
986 * Creates a new license and returns its ID
988 * If the license already exists, the ID of the already existing license is
989 * returned.
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);
999 if ($result) {
1000 $licid = $result->fetch(PDO::FETCH_COLUMN, 0);
1001 if ($licid > 0) {
1002 return $licid;
1006 $q = sprintf("INSERT INTO Licenses (Name) VALUES (%s)", $dbh->quote($name));
1007 $dbh->exec($q);
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
1017 * @return void
1019 function pkg_add_lic($pkgid, $licid) {
1020 $dbh = DB::connect();
1021 $q = sprintf("INSERT INTO PackageLicenses (PackageID, LicenseID) VALUES (%d, %d)",
1022 $pkgid,
1023 $licid
1025 $dbh->exec($q);
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();
1046 if ($result) {
1047 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
1048 $packages[] = $row;
1052 return $packages;