@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
at upstream/main 130 lines 4.3 kB view raw
1<?php 2 3final class PhrictionMoveController extends PhrictionController { 4 5 public function handleRequest(AphrontRequest $request) { 6 $viewer = $this->getViewer(); 7 8 $document = id(new PhrictionDocumentQuery()) 9 ->setViewer($viewer) 10 ->withIDs(array($request->getURIData('id'))) 11 ->needContent(true) 12 ->requireCapabilities( 13 array( 14 PhabricatorPolicyCapability::CAN_VIEW, 15 PhabricatorPolicyCapability::CAN_EDIT, 16 )) 17 ->executeOne(); 18 if (!$document) { 19 return new Aphront404Response(); 20 } 21 22 $slug = $document->getSlug(); 23 $cancel_uri = PhrictionDocument::getSlugURI($slug); 24 25 $v_slug = $slug; 26 $e_slug = null; 27 28 $v_note = ''; 29 30 $validation_exception = null; 31 if ($request->isFormPost()) { 32 $v_note = $request->getStr('description'); 33 $v_slug = $request->getStr('slug'); 34 $normal_slug = PhabricatorSlug::normalize($v_slug); 35 36 // If what the user typed isn't what we're actually using, warn them 37 // about it. 38 if (strlen($v_slug)) { 39 $no_slash_slug = rtrim($normal_slug, '/'); 40 if ($normal_slug !== $v_slug && $no_slash_slug !== $v_slug) { 41 return $this->newDialog() 42 ->setTitle(pht('Adjust Path')) 43 ->appendParagraph( 44 pht( 45 'The path you entered (%s) is not a valid wiki document '. 46 'path. Paths may not contain spaces or special characters.', 47 phutil_tag('strong', array(), $v_slug))) 48 ->appendParagraph( 49 pht( 50 'Would you like to use the path %s instead?', 51 phutil_tag('strong', array(), $normal_slug))) 52 ->addHiddenInput('slug', $normal_slug) 53 ->addHiddenInput('description', $v_note) 54 ->addCancelButton($cancel_uri) 55 ->addSubmitButton(pht('Accept Path')); 56 } 57 } 58 59 $editor = id(new PhrictionTransactionEditor()) 60 ->setActor($viewer) 61 ->setContentSourceFromRequest($request) 62 ->setContinueOnNoEffect(true) 63 ->setContinueOnMissingFields(true) 64 ->setDescription($v_note); 65 66 $xactions = array(); 67 $xactions[] = id(new PhrictionTransaction()) 68 ->setTransactionType( 69 PhrictionDocumentMoveToTransaction::TRANSACTIONTYPE) 70 ->setNewValue($document); 71 $target_document = id(new PhrictionDocumentQuery()) 72 ->setViewer(PhabricatorUser::getOmnipotentUser()) 73 ->withSlugs(array($normal_slug)) 74 ->needContent(true) 75 ->requireCapabilities( 76 array( 77 PhabricatorPolicyCapability::CAN_VIEW, 78 PhabricatorPolicyCapability::CAN_EDIT, 79 )) 80 ->executeOne(); 81 if (!$target_document) { 82 $target_document = PhrictionDocument::initializeNewDocument( 83 $viewer, 84 $v_slug); 85 } 86 try { 87 $editor->applyTransactions($target_document, $xactions); 88 $redir_uri = PhrictionDocument::getSlugURI( 89 $target_document->getSlug()); 90 return id(new AphrontRedirectResponse())->setURI($redir_uri); 91 } catch (PhabricatorApplicationTransactionValidationException $ex) { 92 $validation_exception = $ex; 93 $e_slug = $ex->getShortMessage( 94 PhrictionDocumentMoveToTransaction::TRANSACTIONTYPE); 95 } 96 } 97 98 99 $form = id(new AphrontFormView()) 100 ->setUser($viewer) 101 ->appendChild( 102 id(new AphrontFormStaticControl()) 103 ->setLabel(pht('Title')) 104 ->setValue($document->getContent()->getTitle())) 105 ->appendChild( 106 id(new AphrontFormTextControl()) 107 ->setLabel(pht('Current Path')) 108 ->setDisabled(true) 109 ->setValue($slug)) 110 ->appendChild( 111 id(new AphrontFormTextControl()) 112 ->setLabel(pht('New Path')) 113 ->setValue($v_slug) 114 ->setError($e_slug) 115 ->setName('slug')) 116 ->appendChild( 117 id(new AphrontFormTextControl()) 118 ->setLabel(pht('Edit Notes')) 119 ->setValue($v_note) 120 ->setName('description')); 121 122 return $this->newDialog() 123 ->setTitle(pht('Move Document')) 124 ->setValidationException($validation_exception) 125 ->appendForm($form) 126 ->addSubmitButton(pht('Move Document')) 127 ->addCancelButton($cancel_uri); 128 } 129 130}