@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use a modern query object to query Calendar events

Summary: Ref T4375. Calendar uses oldschool `loadOneWhere()` calls. Make CalendarEvent policy-aware, do the edit/delete policy checks through the policy framework, and use modern query infrastructure.

Test Plan:
- Viewed calendar;
- created, edited, deleted event;
- viewed calendar tab in Conpherence.

Reviewers: btrahan, chad

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4375

Differential Revision: https://secure.phabricator.com/D8146

+153 -31
+7 -1
src/__phutil_library_map__.php
··· 1265 1265 'PhabricatorCalendarEvent' => 'applications/calendar/storage/PhabricatorCalendarEvent.php', 1266 1266 'PhabricatorCalendarEventInvalidEpochException' => 'applications/calendar/exception/PhabricatorCalendarEventInvalidEpochException.php', 1267 1267 'PhabricatorCalendarEventOverlapException' => 'applications/calendar/exception/PhabricatorCalendarEventOverlapException.php', 1268 + 'PhabricatorCalendarEventQuery' => 'applications/calendar/query/PhabricatorCalendarEventQuery.php', 1268 1269 'PhabricatorCalendarHoliday' => 'applications/calendar/storage/PhabricatorCalendarHoliday.php', 1269 1270 'PhabricatorCalendarHolidayTestCase' => 'applications/calendar/storage/__tests__/PhabricatorCalendarHolidayTestCase.php', 1270 1271 'PhabricatorCalendarViewStatusController' => 'applications/calendar/controller/PhabricatorCalendarViewStatusController.php', ··· 3900 3901 'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO', 3901 3902 'PhabricatorCalendarDeleteStatusController' => 'PhabricatorCalendarController', 3902 3903 'PhabricatorCalendarEditStatusController' => 'PhabricatorCalendarController', 3903 - 'PhabricatorCalendarEvent' => 'PhabricatorCalendarDAO', 3904 + 'PhabricatorCalendarEvent' => 3905 + array( 3906 + 0 => 'PhabricatorCalendarDAO', 3907 + 1 => 'PhabricatorPolicyInterface', 3908 + ), 3904 3909 'PhabricatorCalendarEventInvalidEpochException' => 'Exception', 3905 3910 'PhabricatorCalendarEventOverlapException' => 'Exception', 3911 + 'PhabricatorCalendarEventQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 3906 3912 'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO', 3907 3913 'PhabricatorCalendarHolidayTestCase' => 'PhabricatorTestCase', 3908 3914 'PhabricatorCalendarViewStatusController' => 'PhabricatorCalendarController',
+5 -4
src/applications/calendar/controller/PhabricatorCalendarBrowseController.php
··· 19 19 "{$year}-{$month}-01", 20 20 "{$year}-{$month}-31"); 21 21 22 - $statuses = id(new PhabricatorCalendarEvent()) 23 - ->loadAllWhere( 24 - 'dateTo >= %d AND dateFrom <= %d', 22 + $statuses = id(new PhabricatorCalendarEventQuery()) 23 + ->setViewer($user) 24 + ->withDateRange( 25 25 strtotime("{$year}-{$month}-01"), 26 - strtotime("{$year}-{$month}-01 next month")); 26 + strtotime("{$year}-{$month}-01 next month")) 27 + ->execute(); 27 28 28 29 if ($month == $month_d && $year == $year_d) { 29 30 $month_view = new AphrontCalendarMonthView($month, $year, $day);
+12 -9
src/applications/calendar/controller/PhabricatorCalendarDeleteStatusController.php
··· 12 12 public function processRequest() { 13 13 $request = $this->getRequest(); 14 14 $user = $request->getUser(); 15 - $status = id(new PhabricatorCalendarEvent()) 16 - ->loadOneWhere('id = %d', $this->id); 15 + 16 + $status = id(new PhabricatorCalendarEventQuery()) 17 + ->setViewer($user) 18 + ->withIDs(array($this->id)) 19 + ->requireCapabilities( 20 + array( 21 + PhabricatorPolicyCapability::CAN_VIEW, 22 + PhabricatorPolicyCapability::CAN_EDIT, 23 + )) 24 + ->executeOne(); 17 25 18 26 if (!$status) { 19 27 return new Aphront404Response(); 20 - } 21 - if ($status->getUserPHID() != $user->getPHID()) { 22 - return new Aphront403Response(); 23 28 } 24 29 25 30 if ($request->isFormPost()) { ··· 36 41 $dialog = new AphrontDialogView(); 37 42 $dialog->setUser($user); 38 43 $dialog->setTitle(pht('Really delete status?')); 39 - $dialog->appendChild(phutil_tag( 40 - 'p', 41 - array(), 42 - pht('Permanently delete this status? This action can not be undone.'))); 44 + $dialog->appendChild( 45 + pht('Permanently delete this status? This action can not be undone.')); 43 46 $dialog->addSubmitButton(pht('Delete')); 44 47 $dialog->addCancelButton( 45 48 $this->getApplicationURI('status/edit/'.$status->getID().'/'));
+10 -6
src/applications/calendar/controller/PhabricatorCalendarEditStatusController.php
··· 38 38 $page_title = pht('Create Status'); 39 39 $redirect = 'created'; 40 40 } else { 41 - $status = id(new PhabricatorCalendarEvent()) 42 - ->loadOneWhere('id = %d', $this->id); 41 + $status = id(new PhabricatorCalendarEventQuery()) 42 + ->setViewer($user) 43 + ->withIDs(array($this->id)) 44 + ->requireCapabilities( 45 + array( 46 + PhabricatorPolicyCapability::CAN_VIEW, 47 + PhabricatorPolicyCapability::CAN_EDIT, 48 + )) 49 + ->executeOne(); 50 + 43 51 $end_time->setValue($status->getDateTo()); 44 52 $start_time->setValue($status->getDateFrom()); 45 53 $submit_label = pht('Update'); 46 54 $filter = 'status/edit/'.$status->getID().'/'; 47 55 $page_title = pht('Update Status'); 48 56 $redirect = 'updated'; 49 - 50 - if ($status->getUserPHID() != $user->getPHID()) { 51 - return new Aphront403Response(); 52 - } 53 57 } 54 58 55 59 $errors = array();
+6 -4
src/applications/calendar/controller/PhabricatorCalendarViewStatusController.php
··· 12 12 } 13 13 14 14 public function processRequest() { 15 - 16 15 $request = $this->getRequest(); 17 16 $user = $request->getUser(); 18 17 $handle = $this->getHandle($this->phid); 19 - $statuses = id(new PhabricatorCalendarEvent()) 20 - ->loadAllWhere('userPHID = %s AND dateTo > UNIX_TIMESTAMP()', 21 - $this->phid); 18 + 19 + $statuses = id(new PhabricatorCalendarEventQuery()) 20 + ->setViewer($user) 21 + ->withInvitedPHIDs(array($this->phid)) 22 + ->withDateRange(time(), strtotime('2037-01-01 12:00:00')) 23 + ->execute(); 22 24 23 25 $nav = $this->buildSideNavView(); 24 26 $nav->selectFilter($this->getFilter());
+76
src/applications/calendar/query/PhabricatorCalendarEventQuery.php
··· 1 + <?php 2 + 3 + final class PhabricatorCalendarEventQuery 4 + extends PhabricatorCursorPagedPolicyAwareQuery { 5 + 6 + private $ids; 7 + private $rangeBegin; 8 + private $rangeEnd; 9 + private $invitedPHIDs; 10 + 11 + public function withIDs(array $ids) { 12 + $this->ids = $ids; 13 + return $this; 14 + } 15 + 16 + public function withDateRange($begin, $end) { 17 + $this->rangeBegin = $begin; 18 + $this->rangeEnd = $end; 19 + return $this; 20 + } 21 + 22 + public function withInvitedPHIDs(array $phids) { 23 + $this->invitedPHIDs = $phids; 24 + return $this; 25 + } 26 + 27 + protected function loadPage() { 28 + $table = new PhabricatorCalendarEvent(); 29 + $conn_r = $table->establishConnection('r'); 30 + 31 + $data = queryfx_all( 32 + $conn_r, 33 + 'SELECT * FROM %T %Q %Q %Q', 34 + $table->getTableName(), 35 + $this->buildWhereClause($conn_r), 36 + $this->buildOrderClause($conn_r), 37 + $this->buildLimitClause($conn_r)); 38 + 39 + return $table->loadAllFromArray($data); 40 + } 41 + 42 + protected function buildWhereClause($conn_r) { 43 + $where = array(); 44 + 45 + if ($this->ids) { 46 + $where[] = qsprintf( 47 + $conn_r, 48 + 'id IN (%Ld)', 49 + $this->ids); 50 + } 51 + 52 + if ($this->rangeBegin || $this->rangeEnd) { 53 + $where[] = qsprintf( 54 + $conn_r, 55 + 'dateTo >= %d AND dateFrom <= %d', 56 + $this->rangeBegin, 57 + $this->rangeEnd); 58 + } 59 + 60 + if ($this->invitedPHIDs) { 61 + $where[] = qsprintf( 62 + $conn_r, 63 + 'userPHID IN (%Ls)', 64 + $this->invitedPHIDs); 65 + } 66 + 67 + $where[] = $this->buildPagingClause($conn_r); 68 + 69 + return $this->formatWhereClause($where); 70 + } 71 + 72 + public function getQueryApplicationClass() { 73 + return 'PhabricatorApplicationCalendar'; 74 + } 75 + 76 + }
+31 -1
src/applications/calendar/storage/PhabricatorCalendarEvent.php
··· 1 1 <?php 2 2 3 - final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO { 3 + final class PhabricatorCalendarEvent 4 + extends PhabricatorCalendarDAO 5 + implements PhabricatorPolicyInterface { 4 6 5 7 protected $userPHID; 6 8 protected $dateFrom; ··· 90 92 91 93 $this->endWriteLocking(); 92 94 return $this->saveTransaction(); 95 + } 96 + 97 + 98 + /* -( PhabricatorPolicyInterface )----------------------------------------- */ 99 + 100 + 101 + public function getCapabilities() { 102 + return array( 103 + PhabricatorPolicyCapability::CAN_VIEW, 104 + PhabricatorPolicyCapability::CAN_EDIT, 105 + ); 106 + } 107 + 108 + public function getPolicy($capability) { 109 + switch ($capability) { 110 + case PhabricatorPolicyCapability::CAN_VIEW: 111 + return PhabricatorPolicies::getMostOpenPolicy(); 112 + case PhabricatorPolicyCapability::CAN_EDIT: 113 + return $this->getUserPHID(); 114 + } 115 + } 116 + 117 + public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { 118 + return false; 119 + } 120 + 121 + public function describeAutomaticCapability($capability) { 122 + return null; 93 123 } 94 124 95 125 }
+6 -6
src/applications/conpherence/query/ConpherenceThreadQuery.php
··· 221 221 $this->getViewer()); 222 222 $start_epoch = $epochs['start_epoch']; 223 223 $end_epoch = $epochs['end_epoch']; 224 - $statuses = id(new PhabricatorCalendarEvent()) 225 - ->loadAllWhere( 226 - 'userPHID in (%Ls) AND dateTo >= %d AND dateFrom <= %d', 227 - $participant_phids, 228 - $start_epoch, 229 - $end_epoch); 224 + $statuses = id(new PhabricatorCalendarEventQuery()) 225 + ->setViewer($this->getViewer()) 226 + ->withInvitedPHIDs($participant_phids) 227 + ->withDateRange($start_epoch, $end_epoch) 228 + ->execute(); 229 + 230 230 $statuses = mgroup($statuses, 'getUserPHID'); 231 231 232 232 // attached files