| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-14 08:02:31 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @group phriction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-03-09 15:46:25 -08:00
										 |  |  | final class PhrictionDiffController | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |   extends PhrictionController { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private $id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function willProcessRequest(array $data) { | 
					
						
							|  |  |  |     $this->id = $data['id']; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function processRequest() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $request = $this->getRequest(); | 
					
						
							|  |  |  |     $user = $request->getUser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $document = id(new PhrictionDocument())->load($this->id); | 
					
						
							|  |  |  |     if (!$document) { | 
					
						
							|  |  |  |       return new Aphront404Response(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $current = id(new PhrictionContent())->load($document->getContentID()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $l = $request->getInt('l'); | 
					
						
							|  |  |  |     $r = $request->getInt('r'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $ref = $request->getStr('ref'); | 
					
						
							|  |  |  |     if ($ref) { | 
					
						
							|  |  |  |       list($l, $r) = explode(',', $ref); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $content = id(new PhrictionContent())->loadAllWhere( | 
					
						
							|  |  |  |       'documentID = %d AND version IN (%Ld)', | 
					
						
							|  |  |  |       $document->getID(), | 
					
						
							|  |  |  |       array($l, $r)); | 
					
						
							|  |  |  |     $content = mpull($content, null, 'getVersion'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $content_l = idx($content, $l, null); | 
					
						
							|  |  |  |     $content_r = idx($content, $r, null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!$content_l || !$content_r) { | 
					
						
							|  |  |  |       return new Aphront404Response(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $text_l = $content_l->getContent(); | 
					
						
							|  |  |  |     $text_r = $content_r->getContent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 09:17:58 -08:00
										 |  |  |     $text_l = phutil_utf8_hard_wrap($text_l, 80); | 
					
						
							| 
									
										
										
										
											2013-03-03 11:37:45 -08:00
										 |  |  |     $text_l = implode("\n", $text_l); | 
					
						
							| 
									
										
										
										
											2013-02-28 09:17:58 -08:00
										 |  |  |     $text_r = phutil_utf8_hard_wrap($text_r, 80); | 
					
						
							| 
									
										
										
										
											2013-03-03 11:37:45 -08:00
										 |  |  |     $text_r = implode("\n", $text_r); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $engine = new PhabricatorDifferenceEngine(); | 
					
						
							|  |  |  |     $changeset = $engine->generateChangesetFromFileContent($text_l, $text_r); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $changeset->setOldProperties( | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         'Title'   => $content_l->getTitle(), | 
					
						
							|  |  |  |       )); | 
					
						
							|  |  |  |     $changeset->setNewProperties( | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         'Title'   => $content_r->getTitle(), | 
					
						
							|  |  |  |       )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $parser = new DifferentialChangesetParser(); | 
					
						
							|  |  |  |     $parser->setChangeset($changeset); | 
					
						
							|  |  |  |     $parser->setRenderingReference("{$l},{$r}"); | 
					
						
							|  |  |  |     $parser->setWhitespaceMode($whitespace_mode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-08 17:35:34 -08:00
										 |  |  |     $engine = new PhabricatorMarkupEngine(); | 
					
						
							|  |  |  |     $engine->setViewer($user); | 
					
						
							|  |  |  |     $engine->process(); | 
					
						
							|  |  |  |     $parser->setMarkupEngine($engine); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |     $spec = $request->getStr('range'); | 
					
						
							|  |  |  |     list($range_s, $range_e, $mask) = | 
					
						
							|  |  |  |       DifferentialChangesetParser::parseRangeSpecification($spec); | 
					
						
							|  |  |  |     $output = $parser->render($range_s, $range_e, $mask); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($request->isAjax()) { | 
					
						
							| 
									
										
										
										
											2012-03-12 21:39:05 -07:00
										 |  |  |       return id(new PhabricatorChangesetResponse()) | 
					
						
							|  |  |  |         ->setRenderedChangeset($output); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     require_celerity_resource('differential-changeset-view-css'); | 
					
						
							|  |  |  |     require_celerity_resource('syntax-highlighting-css'); | 
					
						
							|  |  |  |     require_celerity_resource('phriction-document-css'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Javelin::initBehavior('differential-show-more', array( | 
					
						
							|  |  |  |       'uri'         => '/phriction/diff/'.$document->getID().'/', | 
					
						
							|  |  |  |       'whitespace'  => $whitespace_mode, | 
					
						
							|  |  |  |     )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $slug = $document->getSlug(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $revert_l = $this->renderRevertButton($content_l, $current); | 
					
						
							|  |  |  |     $revert_r = $this->renderRevertButton($content_r, $current); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $crumbs = new AphrontCrumbsView(); | 
					
						
							|  |  |  |     $crumbs->setCrumbs( | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         'Phriction', | 
					
						
							| 
									
										
										
										
											2013-01-17 18:43:35 -08:00
										 |  |  |         phutil_tag( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |           'a', | 
					
						
							|  |  |  |           array( | 
					
						
							|  |  |  |             'href' => PhrictionDocument::getSlugURI($slug), | 
					
						
							|  |  |  |           ), | 
					
						
							| 
									
										
										
										
											2013-01-17 18:43:35 -08:00
										 |  |  |           $current->getTitle()), | 
					
						
							| 
									
										
										
										
											2013-01-17 18:57:09 -08:00
										 |  |  |         phutil_tag( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |           'a', | 
					
						
							|  |  |  |           array( | 
					
						
							|  |  |  |             'href' => '/phriction/history/'.$document->getSlug().'/', | 
					
						
							|  |  |  |           ), | 
					
						
							|  |  |  |           'History'), | 
					
						
							| 
									
										
										
										
											2013-02-13 14:48:19 -08:00
										 |  |  |         "Changes Between Version {$l} and Version {$r}", | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $comparison_table = $this->renderComparisonTable( | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         $content_r, | 
					
						
							|  |  |  |         $content_l, | 
					
						
							|  |  |  |       )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $navigation_table = null; | 
					
						
							|  |  |  |     if ($l + 1 == $r) { | 
					
						
							|  |  |  |       $nav_l = ($l > 1); | 
					
						
							|  |  |  |       $nav_r = ($r != $current->getVersion()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $uri = $request->getRequestURI(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if ($nav_l) { | 
					
						
							| 
									
										
										
										
											2013-01-17 18:57:09 -08:00
										 |  |  |         $link_l = phutil_tag( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |           'a', | 
					
						
							|  |  |  |           array( | 
					
						
							|  |  |  |             'href' => $uri->alter('l', $l - 1)->alter('r', $r - 1), | 
					
						
							|  |  |  |           ), | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |           pht("\xC2\xAB Previous Change")); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |         $link_l = pht('Original Change'); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $link_r = null; | 
					
						
							|  |  |  |       if ($nav_r) { | 
					
						
							| 
									
										
										
										
											2013-01-17 18:57:09 -08:00
										 |  |  |         $link_r = phutil_tag( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |           'a', | 
					
						
							|  |  |  |           array( | 
					
						
							|  |  |  |             'href' => $uri->alter('l', $l + 1)->alter('r', $r + 1), | 
					
						
							|  |  |  |           ), | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |           pht("Next Change \xC2\xBB")); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |         $link_r = pht('Most Recent Change'); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |       $navigation_table = hsprintf( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |         '<table class="phriction-history-nav-table"> | 
					
						
							|  |  |  |           <tr> | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |             <td class="nav-prev">%s</td> | 
					
						
							|  |  |  |             <td class="nav-next">%s</td> | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |           </tr> | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |         </table>', | 
					
						
							|  |  |  |         $link_l, | 
					
						
							|  |  |  |         $link_r); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |     $output = hsprintf( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       '<div class="phriction-document-history-diff">'. | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |         '%s<br /><br />%s'. | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |         '<table class="phriction-revert-table">'. | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |           '<tr><td>%s</td><td>%s</td>'. | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |         '</table>'. | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |         '%s'. | 
					
						
							|  |  |  |       '</div>', | 
					
						
							|  |  |  |       $comparison_table->render(), | 
					
						
							|  |  |  |       $navigation_table, | 
					
						
							|  |  |  |       $revert_l, | 
					
						
							|  |  |  |       $revert_r, | 
					
						
							|  |  |  |       $output); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return $this->buildStandardPageResponse( | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         $crumbs, | 
					
						
							|  |  |  |         $output, | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |       array( | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |         'title'     => pht('Document History'), | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       )); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private function renderRevertButton( | 
					
						
							|  |  |  |     PhrictionContent $content, | 
					
						
							|  |  |  |     PhrictionContent $current) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $document_id = $content->getDocumentID(); | 
					
						
							|  |  |  |     $version = $content->getVersion(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-17 09:19:08 -08:00
										 |  |  |     if ($content->getChangeType() == PhrictionChangeType::CHANGE_DELETE) { | 
					
						
							|  |  |  |       // Don't show an edit/revert button for changes which deleted the content
 | 
					
						
							|  |  |  |       // since it's silly.
 | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |     if ($content->getID() == $current->getID()) { | 
					
						
							| 
									
										
										
										
											2013-01-17 18:57:09 -08:00
										 |  |  |       return phutil_tag( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |         'a', | 
					
						
							|  |  |  |         array( | 
					
						
							|  |  |  |           'href'  => '/phriction/edit/'.$document_id.'/', | 
					
						
							|  |  |  |           'class' => 'button', | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |         pht('Edit Current Version')); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-18 00:32:58 -08:00
										 |  |  |     return phutil_tag( | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       'a', | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         'href'  => '/phriction/edit/'.$document_id.'/?revert='.$version, | 
					
						
							|  |  |  |         'class' => 'button', | 
					
						
							|  |  |  |       ), | 
					
						
							| 
									
										
										
										
											2013-02-08 17:29:15 -08:00
										 |  |  |       pht('Revert to Version %s...', $version)); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private function renderComparisonTable(array $content) { | 
					
						
							| 
									
										
										
										
											2012-04-03 12:10:45 -07:00
										 |  |  |     assert_instances_of($content, 'PhrictionContent'); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $user = $this->getRequest()->getUser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $phids = mpull($content, 'getAuthorPHID'); | 
					
						
							| 
									
										
										
										
											2012-09-04 19:02:56 -07:00
										 |  |  |     $handles = $this->loadViewerHandles($phids); | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $rows = array(); | 
					
						
							|  |  |  |     foreach ($content as $c) { | 
					
						
							|  |  |  |       $rows[] = array( | 
					
						
							|  |  |  |         phabricator_date($c->getDateCreated(), $user), | 
					
						
							|  |  |  |         phabricator_time($c->getDateCreated(), $user), | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |         'Version '.$c->getVersion(), | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |         $handles[$c->getAuthorPHID()]->renderLink(), | 
					
						
							| 
									
										
										
										
											2013-02-13 14:50:15 -08:00
										 |  |  |         $c->getDescription(), | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $table = new AphrontTableView($rows); | 
					
						
							|  |  |  |     $table->setHeaders( | 
					
						
							|  |  |  |       array( | 
					
						
							| 
									
										
										
										
											2013-02-08 09:54:27 -08:00
										 |  |  |         pht('Date'), | 
					
						
							|  |  |  |         pht('Time'), | 
					
						
							|  |  |  |         pht('Version'), | 
					
						
							|  |  |  |         pht('Author'), | 
					
						
							|  |  |  |         pht('Description'), | 
					
						
							| 
									
										
										
										
											2011-07-17 11:06:02 -07:00
										 |  |  |       )); | 
					
						
							|  |  |  |     $table->setColumnClasses( | 
					
						
							|  |  |  |       array( | 
					
						
							|  |  |  |         '', | 
					
						
							|  |  |  |         'right', | 
					
						
							|  |  |  |         'pri', | 
					
						
							|  |  |  |         '', | 
					
						
							|  |  |  |         'wide', | 
					
						
							|  |  |  |       )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return $table; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |