Require comments when flagging packages out-of-date
[aur.git] / web / lib / pkgbasefuncs.inc.php
blob799f1da919f389a2876460169a857e6ac0f454ec
1 <?php
3 include_once("pkgreqfuncs.inc.php");
5 /**
6 * Get the number of non-deleted comments for a specific package base
8 * @param string $base_id The package base ID to get comment count for
9 * @param bool $include_deleted True if deleted comments should be included
11 * @return string The number of comments left for a specific package
13 function pkgbase_comments_count($base_id, $include_deleted) {
14 $base_id = intval($base_id);
15 if (!$base_id) {
16 return null;
19 $dbh = DB::connect();
20 $q = "SELECT COUNT(*) FROM PackageComments ";
21 $q.= "WHERE PackageBaseID = " . $base_id . " ";
22 if (!$include_deleted) {
23 $q.= "AND DelUsersID IS NULL";
25 $result = $dbh->query($q);
26 if (!$result) {
27 return null;
30 return $result->fetchColumn(0);
33 /**
34 * Get all package comment information for a specific package base
36 * @param int $base_id The package base ID to get comments for
37 * @param int $limit Maximum number of comments to return (0 means unlimited)
38 * @param bool $include_deleted True if deleted comments should be included
40 * @return array All package comment information for a specific package base
42 function pkgbase_comments($base_id, $limit, $include_deleted) {
43 $base_id = intval($base_id);
44 $limit = intval($limit);
45 if (!$base_id) {
46 return null;
49 $dbh = DB::connect();
50 $q = "SELECT PackageComments.ID, A.UserName AS UserName, UsersID, Comments, ";
51 $q.= "CommentTS, EditedTS, B.UserName AS EditUserName, ";
52 $q.= "DelUsersID, C.UserName AS DelUserName FROM PackageComments ";
53 $q.= "LEFT JOIN Users A ON PackageComments.UsersID = A.ID ";
54 $q.= "LEFT JOIN Users B ON PackageComments.EditedUsersID = B.ID ";
55 $q.= "LEFT JOIN Users C ON PackageComments.DelUsersID = C.ID ";
56 $q.= "WHERE PackageBaseID = " . $base_id . " ";
57 if (!$include_deleted) {
58 $q.= "AND DelUsersID IS NULL ";
60 $q.= "ORDER BY CommentTS DESC";
61 if ($limit > 0) {
62 $q.=" LIMIT " . $limit;
64 $result = $dbh->query($q);
65 if (!$result) {
66 return null;
69 return $result->fetchAll();
72 /**
73 * Add a comment to a package page and send out appropriate notifications
75 * @param string $base_id The package base ID to add the comment on
76 * @param string $uid The user ID of the individual who left the comment
77 * @param string $comment The comment left on a package page
79 * @return void
81 function pkgbase_add_comment($base_id, $uid, $comment) {
82 $dbh = DB::connect();
84 if (trim($comment) == '') {
85 return array(false, __('Comment cannot be empty.'));
88 $q = "INSERT INTO PackageComments ";
89 $q.= "(PackageBaseID, UsersID, Comments, CommentTS) VALUES (";
90 $q.= intval($base_id) . ", " . $uid . ", ";
91 $q.= $dbh->quote($comment) . ", UNIX_TIMESTAMP())";
92 $dbh->exec($q);
95 * Send e-mail notifications.
96 * TODO: Move notification logic to separate function where it belongs.
98 $q = "SELECT CommentNotify.*, Users.Email ";
99 $q.= "FROM CommentNotify, Users ";
100 $q.= "WHERE Users.ID = CommentNotify.UserID ";
101 $q.= "AND CommentNotify.UserID != " . $uid . " ";
102 $q.= "AND CommentNotify.PackageBaseID = " . intval($base_id);
103 $result = $dbh->query($q);
104 $bcc = array();
106 if ($result) {
107 notify(array('comment', $uid, $base_id), $comment);
110 return array(true, __('Comment has been added.'));
114 * Get a list of all packages a logged-in user has voted for
116 * @param string $sid The session ID of the visitor
118 * @return array All packages the visitor has voted for
120 function pkgbase_votes_from_sid($sid="") {
121 $pkgs = array();
122 if (!$sid) {return $pkgs;}
123 $dbh = DB::connect();
124 $q = "SELECT PackageBaseID ";
125 $q.= "FROM PackageVotes, Users, Sessions ";
126 $q.= "WHERE Users.ID = Sessions.UsersID ";
127 $q.= "AND Users.ID = PackageVotes.UsersID ";
128 $q.= "AND Sessions.SessionID = " . $dbh->quote($sid);
129 $result = $dbh->query($q);
130 if ($result) {
131 while ($row = $result->fetch(PDO::FETCH_NUM)) {
132 $pkgs[$row[0]] = 1;
135 return $pkgs;
139 * Get the package base details
141 * @param string $id The package base ID to get description for
143 * @return array The package base's details OR error message
145 function pkgbase_get_details($base_id) {
146 $dbh = DB::connect();
148 $q = "SELECT PackageBases.ID, PackageBases.Name, ";
149 $q.= "PackageBases.NumVotes, PackageBases.Popularity, ";
150 $q.= "PackageBases.OutOfDateTS, PackageBases.SubmittedTS, ";
151 $q.= "PackageBases.ModifiedTS, PackageBases.SubmitterUID, ";
152 $q.= "PackageBases.MaintainerUID, PackageBases.PackagerUID, ";
153 $q.= "PackageBases.FlaggerUID, ";
154 $q.= "(SELECT COUNT(*) FROM PackageRequests ";
155 $q.= " WHERE PackageRequests.PackageBaseID = PackageBases.ID ";
156 $q.= " AND PackageRequests.Status = 0) AS RequestCount ";
157 $q.= "FROM PackageBases ";
158 $q.= "WHERE PackageBases.ID = " . intval($base_id);
159 $result = $dbh->query($q);
161 $row = array();
163 if (!$result) {
164 $row['error'] = __("Error retrieving package details.");
166 else {
167 $row = $result->fetch(PDO::FETCH_ASSOC);
168 if (empty($row)) {
169 $row['error'] = __("Package details could not be found.");
173 return $row;
177 * Display the package base details page
179 * @param string $id The package base ID to get details page for
180 * @param array $row Package base details retrieved by pkgbase_get_details()
181 * @param string $SID The session ID of the visitor
183 * @return void
185 function pkgbase_display_details($base_id, $row, $SID="") {
186 $dbh = DB::connect();
188 if (isset($row['error'])) {
189 print "<p>" . $row['error'] . "</p>\n";
191 else {
192 $pkgbase_name = pkgbase_name_from_id($base_id);
194 include('pkgbase_details.php');
196 if ($SID) {
197 include('pkg_comment_box.php');
200 $limit = isset($_GET['comments']) ? 0 : 10;
201 $include_deleted = has_credential(CRED_COMMENT_VIEW_DELETED);
202 $comments = pkgbase_comments($base_id, $limit, $include_deleted);
203 if (!empty($comments)) {
204 include('pkg_comments.php');
210 * Convert a list of package IDs into a list of corresponding package bases.
212 * @param array|int $ids Array of package IDs to convert
214 * @return array|int List of package base IDs
216 function pkgbase_from_pkgid($ids) {
217 $dbh = DB::connect();
219 if (is_array($ids)) {
220 $q = "SELECT PackageBaseID FROM Packages ";
221 $q.= "WHERE ID IN (" . implode(",", $ids) . ")";
222 $result = $dbh->query($q);
223 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
224 } else {
225 $q = "SELECT PackageBaseID FROM Packages ";
226 $q.= "WHERE ID = " . $ids;
227 $result = $dbh->query($q);
228 return $result->fetch(PDO::FETCH_COLUMN, 0);
233 * Retrieve ID of a package base by name
235 * @param string $name The package base name to retrieve the ID for
237 * @return int The ID of the package base
239 function pkgbase_from_name($name) {
240 $dbh = DB::connect();
241 $q = "SELECT ID FROM PackageBases WHERE Name = " . $dbh->quote($name);
242 $result = $dbh->query($q);
243 return $result->fetch(PDO::FETCH_COLUMN, 0);
247 * Retrieve the name of a package base given its ID
249 * @param int $base_id The ID of the package base to query
251 * @return string The name of the package base
253 function pkgbase_name_from_id($base_id) {
254 $dbh = DB::connect();
255 $q = "SELECT Name FROM PackageBases WHERE ID = " . intval($base_id);
256 $result = $dbh->query($q);
257 return $result->fetch(PDO::FETCH_COLUMN, 0);
261 * Get the names of all packages belonging to a package base
263 * @param int $base_id The ID of the package base
265 * @return array The names of all packages belonging to the package base
267 function pkgbase_get_pkgnames($base_id) {
268 $dbh = DB::connect();
269 $q = "SELECT Name FROM Packages WHERE PackageBaseID = " . intval($base_id);
270 $result = $dbh->query($q);
271 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
275 * Delete all packages belonging to a package base
277 * @param int $base_id The ID of the package base
279 * @return void
281 function pkgbase_delete_packages($base_id) {
282 $dbh = DB::connect();
283 $q = "DELETE FROM Packages WHERE PackageBaseID = " . intval($base_id);
284 $dbh->exec($q);
288 * Retrieve the maintainer of a package base given its ID
290 * @param int $base_id The ID of the package base to query
292 * @return int The user ID of the current package maintainer
294 function pkgbase_maintainer_uid($base_id) {
295 $dbh = DB::connect();
296 $q = "SELECT MaintainerUID FROM PackageBases WHERE ID = " . intval($base_id);
297 $result = $dbh->query($q);
298 return $result->fetch(PDO::FETCH_COLUMN, 0);
302 * Retrieve the maintainers of an array of package bases given by their ID
304 * @param int $base_ids The array of IDs of the package bases to query
306 * @return int The user ID of the current package maintainer
308 function pkgbase_maintainer_uids($base_ids) {
309 $dbh = DB::connect();
310 $q = "SELECT MaintainerUID FROM PackageBases WHERE ID IN (" . implode(",", $base_ids) . ")";
311 $result = $dbh->query($q);
312 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
316 * Flag package(s) as out-of-date
318 * @param array $base_ids Array of package base IDs to flag/unflag
319 * @param string $comment The comment to add
321 * @return array Tuple of success/failure indicator and error message
323 function pkgbase_flag($base_ids, $comment) {
324 if (!has_credential(CRED_PKGBASE_FLAG)) {
325 return array(false, __("You must be logged in before you can flag packages."));
328 $base_ids = sanitize_ids($base_ids);
329 if (empty($base_ids)) {
330 return array(false, __("You did not select any packages to flag."));
333 $uid = uid_from_sid($_COOKIE['AURSID']);
334 $dbh = DB::connect();
336 $q = "UPDATE PackageBases SET ";
337 $q.= "OutOfDateTS = UNIX_TIMESTAMP(), FlaggerUID = " . $uid . ", ";
338 $q.= "FlaggerComment = " . $dbh->quote($comment) . " ";
339 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
340 $q.= "AND OutOfDateTS IS NULL";
341 $dbh->exec($q);
343 foreach ($base_ids as $base_id) {
344 notify(array('flag', $uid, $base_id), $comment);
347 return array(true, __("The selected packages have been flagged out-of-date."));
351 * Unflag package(s) as out-of-date
353 * @param array $base_ids Array of package base IDs to flag/unflag
355 * @return array Tuple of success/failure indicator and error message
357 function pkgbase_unflag($base_ids) {
358 $uid = uid_from_sid($_COOKIE["AURSID"]);
359 if (!$uid) {
360 return array(false, __("You must be logged in before you can unflag packages."));
363 $base_ids = sanitize_ids($base_ids);
364 if (empty($base_ids)) {
365 return array(false, __("You did not select any packages to unflag."));
368 $dbh = DB::connect();
370 $q = "UPDATE PackageBases SET ";
371 $q.= "OutOfDateTS = NULL ";
372 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
374 $maintainers = array_merge(pkgbase_maintainer_uids($base_ids), pkgbase_get_comaintainer_uids($base_ids));
375 if (!has_credential(CRED_PKGBASE_UNFLAG, $maintainers)) {
376 $q.= "AND (MaintainerUID = " . $uid . " OR FlaggerUID = " . $uid. ")";
379 $result = $dbh->exec($q);
381 if ($result) {
382 return array(true, __("The selected packages have been unflagged."));
387 * Delete package bases
389 * @param array $base_ids Array of package base IDs to delete
390 * @param int $merge_base_id Package base to merge the deleted ones into
391 * @param int $via Package request to close upon deletion
392 * @param bool $grant Allow anyone to delete the package base
394 * @return array Tuple of success/failure indicator and error message
396 function pkgbase_delete ($base_ids, $merge_base_id, $via, $grant=false) {
397 if (!$grant && !has_credential(CRED_PKGBASE_DELETE)) {
398 return array(false, __("You do not have permission to delete packages."));
401 $base_ids = sanitize_ids($base_ids);
402 if (empty($base_ids)) {
403 return array(false, __("You did not select any packages to delete."));
406 $dbh = DB::connect();
408 if ($merge_base_id) {
409 $merge_base_name = pkgbase_name_from_id($merge_base_id);
412 $uid = uid_from_sid($_COOKIE['AURSID']);
413 foreach ($base_ids as $base_id) {
414 if ($merge_base_id) {
415 notify(array('delete', $uid, $base_id, $merge_base_id));
416 } else {
417 notify(array('delete', $uid, $base_id));
422 * Close package request if the deletion was initiated through the
423 * request interface. NOTE: This needs to happen *before* the actual
424 * deletion. Otherwise, the former maintainer will not be included in
425 * the Cc list of the request notification email.
427 if ($via) {
428 pkgreq_close(intval($via), 'accepted', '');
431 /* Scan through pending deletion requests and close them. */
432 if (!$action) {
433 $username = username_from_sid($_COOKIE['AURSID']);
434 foreach ($base_ids as $base_id) {
435 $pkgreq_ids = array_merge(pkgreq_by_pkgbase($base_id));
436 foreach ($pkgreq_ids as $pkgreq_id) {
437 pkgreq_close(intval($pkgreq_id), 'accepted',
438 'The user ' . $username .
439 ' deleted the package.', true);
444 if ($merge_base_id) {
445 /* Merge comments */
446 $q = "UPDATE PackageComments ";
447 $q.= "SET PackageBaseID = " . intval($merge_base_id) . " ";
448 $q.= "WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")";
449 $dbh->exec($q);
451 /* Merge votes */
452 foreach ($base_ids as $base_id) {
453 $q = "UPDATE PackageVotes ";
454 $q.= "SET PackageBaseID = " . intval($merge_base_id) . " ";
455 $q.= "WHERE PackageBaseID = " . $base_id . " ";
456 $q.= "AND UsersID NOT IN (";
457 $q.= "SELECT * FROM (SELECT UsersID ";
458 $q.= "FROM PackageVotes ";
459 $q.= "WHERE PackageBaseID = " . intval($merge_base_id);
460 $q.= ") temp)";
461 $dbh->exec($q);
464 $q = "UPDATE PackageBases ";
465 $q.= "SET NumVotes = (SELECT COUNT(*) FROM PackageVotes ";
466 $q.= "WHERE PackageBaseID = " . intval($merge_base_id) . ") ";
467 $q.= "WHERE ID = " . intval($merge_base_id);
468 $dbh->exec($q);
471 $q = "DELETE FROM Packages WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")";
472 $dbh->exec($q);
474 $q = "DELETE FROM PackageBases WHERE ID IN (" . implode(",", $base_ids) . ")";
475 $dbh->exec($q);
477 return array(true, __("The selected packages have been deleted."));
481 * Adopt or disown packages
483 * @param array $base_ids Array of package base IDs to adopt/disown
484 * @param bool $action Adopts if true, disowns if false. Adopts by default
485 * @param int $via Package request to close upon adoption
487 * @return array Tuple of success/failure indicator and error message
489 function pkgbase_adopt ($base_ids, $action=true, $via) {
490 $dbh = DB::connect();
492 $uid = uid_from_sid($_COOKIE["AURSID"]);
493 if (!$uid) {
494 if ($action) {
495 return array(false, __("You must be logged in before you can adopt packages."));
496 } else {
497 return array(false, __("You must be logged in before you can disown packages."));
501 /* Verify package ownership. */
502 $base_ids = sanitize_ids($base_ids);
504 $q = "SELECT ID FROM PackageBases ";
505 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
507 if ($action && !has_credential(CRED_PKGBASE_ADOPT)) {
508 /* Regular users may only adopt orphan packages. */
509 $q.= "AND MaintainerUID IS NULL";
511 if (!$action && !has_credential(CRED_PKGBASE_DISOWN)) {
512 /* Regular users may only disown their own packages. */
513 $q.= "AND MaintainerUID = " . $uid;
516 $result = $dbh->query($q);
517 $base_ids = $result->fetchAll(PDO::FETCH_COLUMN, 0);
519 /* Error out if the list of remaining packages is empty. */
520 if (empty($base_ids)) {
521 if ($action) {
522 return array(false, __("You did not select any packages to adopt."));
523 } else {
524 return array(false, __("You did not select any packages to disown."));
529 * Close package request if the disownment was initiated through the
530 * request interface. NOTE: This needs to happen *before* the actual
531 * disown operation. Otherwise, the former maintainer will not be
532 * included in the Cc list of the request notification email.
534 if ($via) {
535 pkgreq_close(intval($via), 'accepted', '');
538 /* Scan through pending orphan requests and close them. */
539 if (!$action) {
540 $username = username_from_sid($_COOKIE['AURSID']);
541 foreach ($base_ids as $base_id) {
542 $pkgreq_ids = pkgreq_by_pkgbase($base_id, 'orphan');
543 foreach ($pkgreq_ids as $pkgreq_id) {
544 pkgreq_close(intval($pkgreq_id), 'accepted',
545 'The user ' . $username .
546 ' disowned the package.', true);
551 /* Adopt or disown the package. */
552 if ($action) {
553 $q = "UPDATE PackageBases ";
554 $q.= "SET MaintainerUID = $uid ";
555 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
556 $dbh->exec($q);
557 } else {
558 /* Update the co-maintainer list when disowning a package. */
559 if (has_credential(CRED_PKGBASE_DISOWN)) {
560 foreach ($base_ids as $base_id) {
561 pkgbase_set_comaintainers($base_id, array());
564 $q = "UPDATE PackageBases ";
565 $q.= "SET MaintainerUID = NULL ";
566 $q.= "WHERE ID IN (" . implode(",", $base_ids) . ") ";
567 $dbh->exec($q);
568 } else {
569 foreach ($base_ids as $base_id) {
570 $comaintainers = pkgbase_get_comaintainers($base_id);
572 if (count($comaintainers) > 0) {
573 $uid = uid_from_username($comaintainers[0]);
574 $comaintainers = array_diff($comaintainers, array($comaintainers[0]));
575 pkgbase_set_comaintainers($base_id, $comaintainers);
576 } else {
577 $uid = "NULL";
580 $q = "UPDATE PackageBases ";
581 $q.= "SET MaintainerUID = " . $uid . " ";
582 $q.= "WHERE ID = " . $base_id;
583 $dbh->exec($q);
588 if ($action) {
589 pkgbase_notify($base_ids);
590 return array(true, __("The selected packages have been adopted."));
591 } else {
592 return array(true, __("The selected packages have been disowned."));
597 * Vote and un-vote for packages
599 * @param array $base_ids Array of package base IDs to vote/un-vote
600 * @param bool $action Votes if true, un-votes if false. Votes by default
602 * @return array Tuple of success/failure indicator and error message
604 function pkgbase_vote ($base_ids, $action=true) {
605 if (!has_credential(CRED_PKGBASE_VOTE)) {
606 if ($action) {
607 return array(false, __("You must be logged in before you can vote for packages."));
608 } else {
609 return array(false, __("You must be logged in before you can un-vote for packages."));
613 $base_ids = sanitize_ids($base_ids);
614 if (empty($base_ids)) {
615 if ($action) {
616 return array(false, __("You did not select any packages to vote for."));
617 } else {
618 return array(false, __("Your votes have been removed from the selected packages."));
622 $dbh = DB::connect();
623 $my_votes = pkgbase_votes_from_sid($_COOKIE["AURSID"]);
624 $uid = uid_from_sid($_COOKIE["AURSID"]);
626 $first = 1;
627 foreach ($base_ids as $pid) {
628 if ($action) {
629 $check = !isset($my_votes[$pid]);
630 } else {
631 $check = isset($my_votes[$pid]);
634 if ($check) {
635 if ($first) {
636 $first = 0;
637 $vote_ids = $pid;
638 if ($action) {
639 $vote_clauses = "($uid, $pid, UNIX_TIMESTAMP())";
641 } else {
642 $vote_ids .= ", $pid";
643 if ($action) {
644 $vote_clauses .= ", ($uid, $pid, UNIX_TIMESTAMP())";
650 /* Only add votes for packages the user hasn't already voted for. */
651 $op = $action ? "+" : "-";
652 $q = "UPDATE PackageBases SET NumVotes = NumVotes $op 1 ";
653 $q.= "WHERE ID IN ($vote_ids)";
655 $dbh->exec($q);
657 if ($action) {
658 $q = "INSERT INTO PackageVotes (UsersID, PackageBaseID, VoteTS) VALUES ";
659 $q.= $vote_clauses;
660 } else {
661 $q = "DELETE FROM PackageVotes WHERE UsersID = $uid ";
662 $q.= "AND PackageBaseID IN ($vote_ids)";
665 $dbh->exec($q);
667 if ($action) {
668 return array(true, __("Your votes have been cast for the selected packages."));
669 } else {
670 return array(true, __("Your votes have been removed from the selected packages."));
675 * Get all usernames and IDs that voted for a specific package base
677 * @param string $pkgbase_name The package base to retrieve votes for
679 * @return array User IDs and usernames that voted for a specific package base
681 function pkgbase_votes_from_name($pkgbase_name) {
682 $dbh = DB::connect();
684 $q = "SELECT UsersID, Username, Name, VoteTS FROM PackageVotes ";
685 $q.= "LEFT JOIN Users ON UsersID = Users.ID ";
686 $q.= "LEFT JOIN PackageBases ";
687 $q.= "ON PackageVotes.PackageBaseID = PackageBases.ID ";
688 $q.= "WHERE PackageBases.Name = ". $dbh->quote($pkgbase_name) . " ";
689 $q.= "ORDER BY Username";
690 $result = $dbh->query($q);
692 if (!$result) {
693 return;
696 $votes = array();
697 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
698 $votes[] = $row;
701 return $votes;
705 * Determine if a user has already voted for a specific package base
707 * @param string $uid The user ID to check for an existing vote
708 * @param string $base_id The package base ID to check for an existing vote
710 * @return bool True if the user has already voted, otherwise false
712 function pkgbase_user_voted($uid, $base_id) {
713 $dbh = DB::connect();
714 $q = "SELECT COUNT(*) FROM PackageVotes WHERE ";
715 $q.= "UsersID = ". $dbh->quote($uid) . " AND ";
716 $q.= "PackageBaseID = " . $dbh->quote($base_id);
717 $result = $dbh->query($q);
718 if (!$result) {
719 return null;
722 return ($result->fetch(PDO::FETCH_COLUMN, 0) > 0);
726 * Determine if a user wants notifications for a specific package base
728 * @param string $uid User ID to check in the database
729 * @param string $base_id Package base ID to check notifications for
731 * @return bool True if the user wants notifications, otherwise false
733 function pkgbase_user_notify($uid, $base_id) {
734 $dbh = DB::connect();
736 $q = "SELECT * FROM CommentNotify WHERE UserID = " . $dbh->quote($uid);
737 $q.= " AND PackageBaseID = " . $dbh->quote($base_id);
738 $result = $dbh->query($q);
740 if ($result->fetch(PDO::FETCH_NUM)) {
741 return true;
743 else {
744 return false;
749 * Toggle notification of packages
751 * @param array $base_ids Array of package base IDs to toggle
753 * @return array Tuple of success/failure indicator and error message
755 function pkgbase_notify ($base_ids, $action=true) {
756 if (!has_credential(CRED_PKGBASE_NOTIFY)) {
757 return;
760 $base_ids = sanitize_ids($base_ids);
761 if (empty($base_ids)) {
762 return array(false, __("Couldn't add to notification list."));
765 $dbh = DB::connect();
766 $uid = uid_from_sid($_COOKIE["AURSID"]);
768 $output = "";
770 $first = true;
773 * There currently shouldn't be multiple requests here, but the format
774 * in which it's sent requires this.
776 foreach ($base_ids as $bid) {
777 $q = "SELECT Name FROM PackageBases WHERE ID = $bid";
778 $result = $dbh->query($q);
779 if ($result) {
780 $row = $result->fetch(PDO::FETCH_NUM);
781 $basename = $row[0];
783 else {
784 $basename = '';
787 if ($first)
788 $first = false;
789 else
790 $output .= ", ";
793 if ($action) {
794 $q = "SELECT COUNT(*) FROM CommentNotify WHERE ";
795 $q .= "UserID = $uid AND PackageBaseID = $bid";
797 /* Notification already added. Don't add again. */
798 $result = $dbh->query($q);
799 if ($result->fetchColumn() == 0) {
800 $q = "INSERT INTO CommentNotify (PackageBaseID, UserID) VALUES ($bid, $uid)";
801 $dbh->exec($q);
804 $output .= $basename;
806 else {
807 $q = "DELETE FROM CommentNotify WHERE PackageBaseID = $bid ";
808 $q .= "AND UserID = $uid";
809 $dbh->exec($q);
811 $output .= $basename;
815 if ($action) {
816 $output = __("You have been added to the comment notification list for %s.", $output);
818 else {
819 $output = __("You have been removed from the comment notification list for %s.", $output);
822 return array(true, $output);
826 * Delete a package comment
828 * @return array Tuple of success/failure indicator and error message
830 function pkgbase_delete_comment() {
831 $uid = uid_from_sid($_COOKIE["AURSID"]);
832 if (!$uid) {
833 return array(false, __("You must be logged in before you can edit package information."));
836 if (isset($_POST["comment_id"])) {
837 $comment_id = $_POST["comment_id"];
838 } else {
839 return array(false, __("Missing comment ID."));
842 $dbh = DB::connect();
843 if (can_delete_comment($comment_id)) {
844 $q = "UPDATE PackageComments ";
845 $q.= "SET DelUsersID = ".$uid.", ";
846 $q.= "EditedTS = UNIX_TIMESTAMP() ";
847 $q.= "WHERE ID = ".intval($comment_id);
848 $dbh->exec($q);
849 return array(true, __("Comment has been deleted."));
850 } else {
851 return array(false, __("You are not allowed to delete this comment."));
856 * Edit a package comment
858 * @return array Tuple of success/failure indicator and error message
860 function pkgbase_edit_comment($comment) {
861 $uid = uid_from_sid($_COOKIE["AURSID"]);
862 if (!$uid) {
863 return array(false, __("You must be logged in before you can edit package information."));
866 if (isset($_POST["comment_id"])) {
867 $comment_id = $_POST["comment_id"];
868 } else {
869 return array(false, __("Missing comment ID."));
872 if (trim($comment) == '') {
873 return array(false, __('Comment cannot be empty.'));
876 $dbh = DB::connect();
877 if (can_edit_comment($comment_id)) {
878 $q = "UPDATE PackageComments ";
879 $q.= "SET EditedUsersID = ".$uid.", ";
880 $q.= "Comments = ".$dbh->quote($comment).", ";
881 $q.= "EditedTS = UNIX_TIMESTAMP() ";
882 $q.= "WHERE ID = ".intval($comment_id);
883 $dbh->exec($q);
884 return array(true, __("Comment has been edited."));
885 } else {
886 return array(false, __("You are not allowed to edit this comment."));
891 * Get a list of package base keywords
893 * @param int $base_id The package base ID to retrieve the keywords for
895 * @return array An array of keywords
897 function pkgbase_get_keywords($base_id) {
898 $dbh = DB::connect();
899 $q = "SELECT Keyword FROM PackageKeywords ";
900 $q .= "WHERE PackageBaseID = " . intval($base_id) . " ";
901 $q .= "ORDER BY Keyword ASC";
902 $result = $dbh->query($q);
904 if ($result) {
905 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
906 } else {
907 return array();
912 * Update the list of keywords of a package base
914 * @param int $base_id The package base ID to update the keywords of
915 * @param array $users Array of keywords
917 * @return array Tuple of success/failure indicator and error message
919 function pkgbase_set_keywords($base_id, $keywords) {
920 $base_id = intval($base_id);
922 $maintainers = array_merge(array(pkgbase_maintainer_uid($base_id)), pkgbase_get_comaintainer_uids(array($base_id)));
923 if (!has_credential(CRED_PKGBASE_SET_KEYWORDS, $maintainers)) {
924 return array(false, __("You are not allowed to edit the keywords of this package base."));
927 /* Remove empty and duplicate user names. */
928 $keywords = array_unique(array_filter(array_map('trim', $keywords)));
930 $dbh = DB::connect();
932 $q = sprintf("DELETE FROM PackageKeywords WHERE PackageBaseID = %d", $base_id);
933 $dbh->exec($q);
935 $i = 0;
936 foreach ($keywords as $keyword) {
937 $q = sprintf("INSERT INTO PackageKeywords (PackageBaseID, Keyword) VALUES (%d, %s)", $base_id, $dbh->quote($keyword));
938 var_dump($q);
939 $dbh->exec($q);
941 $i++;
942 if ($i >= 20) {
943 break;
947 return array(true, __("The package base keywords have been updated."));
951 * Get a list of package base co-maintainers
953 * @param int $base_id The package base ID to retrieve the co-maintainers for
955 * @return array An array of co-maintainer user names
957 function pkgbase_get_comaintainers($base_id) {
958 $dbh = DB::connect();
959 $q = "SELECT UserName FROM PackageComaintainers ";
960 $q .= "INNER JOIN Users ON Users.ID = PackageComaintainers.UsersID ";
961 $q .= "WHERE PackageComaintainers.PackageBaseID = " . intval($base_id) . " ";
962 $q .= "ORDER BY Priority ASC";
963 $result = $dbh->query($q);
965 if ($result) {
966 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
967 } else {
968 return array();
973 * Get a list of package base co-maintainer IDs
975 * @param int $base_id The package base ID to retrieve the co-maintainers for
977 * @return array An array of co-maintainer user UDs
979 function pkgbase_get_comaintainer_uids($base_ids) {
980 $dbh = DB::connect();
981 $q = "SELECT UsersID FROM PackageComaintainers ";
982 $q .= "INNER JOIN Users ON Users.ID = PackageComaintainers.UsersID ";
983 $q .= "WHERE PackageComaintainers.PackageBaseID IN (" . implode(",", $base_ids) . ") ";
984 $q .= "ORDER BY Priority ASC";
985 $result = $dbh->query($q);
987 if ($result) {
988 return $result->fetchAll(PDO::FETCH_COLUMN, 0);
989 } else {
990 return array();
995 * Update the list of co-maintainers of a package base
997 * @param int $base_id The package base ID to update the co-maintainers of
998 * @param array $users Array of co-maintainer user names
1000 * @return array Tuple of success/failure indicator and error message
1002 function pkgbase_set_comaintainers($base_id, $users) {
1003 if (!has_credential(CRED_PKGBASE_EDIT_COMAINTAINERS, array(pkgbase_maintainer_uid($base_id)))) {
1004 return array(false, __("You are not allowed to manage co-maintainers of this package base."));
1007 /* Remove empty and duplicate user names. */
1008 $users = array_unique(array_filter(array_map('trim', $users)));
1010 $dbh = DB::connect();
1012 $uids = array();
1013 foreach($users as $user) {
1014 $q = "SELECT ID FROM Users ";
1015 $q .= "WHERE UserName = " . $dbh->quote($user);
1016 $result = $dbh->query($q);
1017 $uid = $result->fetchColumn(0);
1019 if (!$uid) {
1020 return array(false, __("Invalid user name: %s", $user));
1023 $uids[] = $uid;
1026 $q = sprintf("DELETE FROM PackageComaintainers WHERE PackageBaseID = %d", $base_id);
1027 $dbh->exec($q);
1029 $i = 1;
1030 foreach ($uids as $uid) {
1031 $q = sprintf("INSERT INTO PackageComaintainers (PackageBaseID, UsersID, Priority) VALUES (%d, %d, %d)", $base_id, $uid, $i);
1032 $dbh->exec($q);
1033 $i++;
1036 return array(true, __("The package base co-maintainers have been updated."));