mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-27 17:22:42 +01:00
Improve Phriction page move dialog
Summary: Fixes T5492. I figured this would be easier to just fix than write a guide for; it actually took me an hour, but I spent like 75% of that futzing with my editor. - The Move controller currently accepts either a slug or an ID. I can't find any callsites which pass a slug, and this doesn't make sense. Pretty sure this was copy/pasted from Edit or something. Only accept IDs. - Slightly modernize the Move controller (newDialog(), handleRequest(), $viewer). - When the user enters a bad slug, warn them that we're going to fix it for them and let them accept or reject the changes. - Don't prefill the edit note (this feels inconsistent/unusual). - On the form, label the input "Path" instead of "URI". - Show the old path, to help remind the user what the input should look like. - When a user tries to do a no-op move, show a more tailored message. - When the user tries to do an overwriting move, explain how they can fix it. - When normalizing a slug like `/question/???/mark/`, make it normalize to `/question/_/mark`. Test Plan: - Tried to move a document to itself. - Tried to overwrite a document. - Did a bad-path move, accepted corrected path. - Did a good-path move. - Did a path move with a weird component like `/???/`. - Added and executed unit tests. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T5492 Differential Revision: https://secure.phabricator.com/D10838
This commit is contained in:
parent
99bcf06c62
commit
120a7d9164
5 changed files with 103 additions and 82 deletions
|
@ -52,7 +52,7 @@ final class PhabricatorPhrictionApplication extends PhabricatorApplication {
|
|||
'edit/(?:(?P<id>[1-9]\d*)/)?' => 'PhrictionEditController',
|
||||
'delete/(?P<id>[1-9]\d*)/' => 'PhrictionDeleteController',
|
||||
'new/' => 'PhrictionNewController',
|
||||
'move/(?:(?P<id>[1-9]\d*)/)?' => 'PhrictionMoveController',
|
||||
'move/(?P<id>[1-9]\d*)/' => 'PhrictionMoveController',
|
||||
|
||||
'preview/' => 'PhabricatorMarkupPreviewController',
|
||||
'diff/(?P<id>[1-9]\d*)/' => 'PhrictionDiffController',
|
||||
|
|
|
@ -2,20 +2,12 @@
|
|||
|
||||
final class PhrictionMoveController extends PhrictionController {
|
||||
|
||||
private $id;
|
||||
public function handleRequest(AphrontRequest $request) {
|
||||
$viewer = $this->getViewer();
|
||||
|
||||
public function willProcessRequest(array $data) {
|
||||
$this->id = idx($data, 'id');
|
||||
}
|
||||
|
||||
public function processRequest() {
|
||||
$request = $this->getRequest();
|
||||
$user = $request->getUser();
|
||||
|
||||
if ($this->id) {
|
||||
$document = id(new PhrictionDocumentQuery())
|
||||
->setViewer($user)
|
||||
->withIDs(array($this->id))
|
||||
->setViewer($viewer)
|
||||
->withIDs(array($request->getURIData('id')))
|
||||
->needContent(true)
|
||||
->requireCapabilities(
|
||||
array(
|
||||
|
@ -23,58 +15,59 @@ final class PhrictionMoveController extends PhrictionController {
|
|||
PhabricatorPolicyCapability::CAN_EDIT,
|
||||
))
|
||||
->executeOne();
|
||||
} else {
|
||||
$slug = PhabricatorSlug::normalize(
|
||||
$request->getStr('slug'));
|
||||
if (!$slug) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
|
||||
$document = id(new PhrictionDocumentQuery())
|
||||
->setViewer($user)
|
||||
->withSlugs(array($slug))
|
||||
->needContent(true)
|
||||
->requireCapabilities(
|
||||
array(
|
||||
PhabricatorPolicyCapability::CAN_VIEW,
|
||||
PhabricatorPolicyCapability::CAN_EDIT,
|
||||
))
|
||||
->executeOne();
|
||||
}
|
||||
|
||||
if (!$document) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
|
||||
if (!isset($slug)) {
|
||||
$slug = $document->getSlug();
|
||||
}
|
||||
|
||||
$target_slug = PhabricatorSlug::normalize(
|
||||
$request->getStr('new-slug', $slug));
|
||||
|
||||
$submit_uri = $request->getRequestURI()->getPath();
|
||||
$cancel_uri = PhrictionDocument::getSlugURI($slug);
|
||||
|
||||
$e_url = true;
|
||||
$validation_exception = null;
|
||||
$content = $document->getContent();
|
||||
$v_slug = $slug;
|
||||
$e_slug = null;
|
||||
|
||||
$v_note = '';
|
||||
|
||||
$validation_exception = null;
|
||||
if ($request->isFormPost()) {
|
||||
$v_note = $request->getStr('description');
|
||||
$v_slug = $request->getStr('slug');
|
||||
|
||||
// If what the user typed isn't what we're actually using, warn them
|
||||
// about it.
|
||||
if (strlen($v_slug)) {
|
||||
$normal_slug = PhabricatorSlug::normalize($v_slug);
|
||||
if ($normal_slug !== $v_slug) {
|
||||
return $this->newDialog()
|
||||
->setTitle(pht('Adjust Path'))
|
||||
->appendParagraph(
|
||||
pht(
|
||||
'The path you entered (%s) is not a valid wiki document '.
|
||||
'path. Paths may not contain special characters.',
|
||||
phutil_tag('strong', array(), $v_slug)))
|
||||
->appendParagraph(
|
||||
pht(
|
||||
'Would you like to use the path %s instead?',
|
||||
phutil_tag('strong', array(), $normal_slug)))
|
||||
->addHiddenInput('slug', $normal_slug)
|
||||
->addHiddenInput('description', $v_note)
|
||||
->addCancelButton($cancel_uri)
|
||||
->addSubmitButton(pht('Accept Path'));
|
||||
}
|
||||
}
|
||||
|
||||
$editor = id(new PhrictionTransactionEditor())
|
||||
->setActor($user)
|
||||
->setActor($viewer)
|
||||
->setContentSourceFromRequest($request)
|
||||
->setContinueOnNoEffect(true)
|
||||
->setDescription($request->getStr('description'));
|
||||
->setDescription($v_note);
|
||||
|
||||
$xactions = array();
|
||||
$xactions[] = id(new PhrictionTransaction())
|
||||
->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO)
|
||||
->setNewValue($document);
|
||||
$target_document = PhrictionDocument::initializeNewDocument(
|
||||
$user,
|
||||
$target_slug);
|
||||
$viewer,
|
||||
$v_slug);
|
||||
try {
|
||||
$editor->applyTransactions($target_document, $xactions);
|
||||
$redir_uri = PhrictionDocument::getSlugURI(
|
||||
|
@ -82,40 +75,40 @@ final class PhrictionMoveController extends PhrictionController {
|
|||
return id(new AphrontRedirectResponse())->setURI($redir_uri);
|
||||
} catch (PhabricatorApplicationTransactionValidationException $ex) {
|
||||
$validation_exception = $ex;
|
||||
$e_url = $ex->getShortMessage(PhrictionTransaction::TYPE_MOVE_TO);
|
||||
$e_slug = $ex->getShortMessage(PhrictionTransaction::TYPE_MOVE_TO);
|
||||
}
|
||||
}
|
||||
|
||||
$form = id(new PHUIFormLayoutView())
|
||||
->setUser($user)
|
||||
|
||||
$form = id(new AphrontFormView())
|
||||
->setUser($viewer)
|
||||
->appendChild(
|
||||
id(new AphrontFormStaticControl())
|
||||
->setLabel(pht('Title'))
|
||||
->setValue($content->getTitle()))
|
||||
->setValue($document->getContent()->getTitle()))
|
||||
->appendChild(
|
||||
id(new AphrontFormTextControl())
|
||||
->setLabel(pht('New URI'))
|
||||
->setValue($target_slug)
|
||||
->setError($e_url)
|
||||
->setName('new-slug')
|
||||
->setCaption(pht('The new location of the document.')))
|
||||
->setLabel(pht('Current Path'))
|
||||
->setDisabled(true)
|
||||
->setValue($slug))
|
||||
->appendChild(
|
||||
id(new AphrontFormTextControl())
|
||||
->setLabel(pht('New Path'))
|
||||
->setValue($v_slug)
|
||||
->setError($e_slug)
|
||||
->setName('slug'))
|
||||
->appendChild(
|
||||
id(new AphrontFormTextControl())
|
||||
->setLabel(pht('Edit Notes'))
|
||||
->setValue(pht('Moving document to a new location.'))
|
||||
->setError(null)
|
||||
->setValue($v_note)
|
||||
->setName('description'));
|
||||
|
||||
$dialog = id(new AphrontDialogView())
|
||||
->setUser($user)
|
||||
->setValidationException($validation_exception)
|
||||
return $this->newDialog()
|
||||
->setTitle(pht('Move Document'))
|
||||
->appendChild($form)
|
||||
->setSubmitURI($submit_uri)
|
||||
->setValidationException($validation_exception)
|
||||
->appendForm($form)
|
||||
->addSubmitButton(pht('Move Document'))
|
||||
->addCancelButton($cancel_uri);
|
||||
|
||||
return id(new AphrontDialogResponse())->setDialog($dialog);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -537,13 +537,24 @@ final class PhrictionTransactionEditor
|
|||
->needContent(true)
|
||||
->executeOne();
|
||||
|
||||
// Considering to overwrite existing docs? Nuke this!
|
||||
// Prevent overwrites and no-op moves.
|
||||
$exists = PhrictionDocumentStatus::STATUS_EXISTS;
|
||||
if ($target_document && $target_document->getStatus() == $exists) {
|
||||
if ($target_document) {
|
||||
if ($target_document->getSlug() == $source_document->getSlug()) {
|
||||
$message = pht(
|
||||
'You can not move a document to its existing location. '.
|
||||
'Choose a different location to move the document to.');
|
||||
} else if ($target_document->getStatus() == $exists) {
|
||||
$message = pht(
|
||||
'You can not move this document there, because it would '.
|
||||
'overwrite an existing document which is already at that '.
|
||||
'location. Move or delete the existing document first.');
|
||||
}
|
||||
|
||||
$error = new PhabricatorApplicationTransactionValidationError(
|
||||
$type,
|
||||
pht('Can not move document.'),
|
||||
pht('Can not overwrite existing target document.'),
|
||||
pht('Invalid'),
|
||||
$message,
|
||||
$xaction);
|
||||
$errors[] = $error;
|
||||
}
|
||||
|
|
|
@ -8,7 +8,17 @@ final class PhabricatorSlug {
|
|||
$slug = phutil_utf8_strtolower($slug);
|
||||
$slug = preg_replace("@[\\x00-\\x19#%&+=\\\\?<> ]+@", '_', $slug);
|
||||
$slug = preg_replace('@_+@', '_', $slug);
|
||||
$slug = trim($slug, '_');
|
||||
|
||||
// Remove leading and trailing underscores from each component, if the
|
||||
// component has not been reduced to a single underscore. For example, "a?"
|
||||
// converts to "a", but "??" converts to "_".
|
||||
$parts = explode('/', $slug);
|
||||
foreach ($parts as $key => $part) {
|
||||
if ($part != '_') {
|
||||
$parts[$key] = trim($part, '_');
|
||||
}
|
||||
}
|
||||
$slug = implode('/', $parts);
|
||||
|
||||
// Specifically rewrite these slugs. It's OK to have a slug like "a..b",
|
||||
// but not a slug which is only "..".
|
||||
|
|
|
@ -7,7 +7,7 @@ final class PhabricatorSlugTestCase extends PhabricatorTestCase {
|
|||
'' => '/',
|
||||
'/' => '/',
|
||||
'//' => '/',
|
||||
'&&&' => '/',
|
||||
'&&&' => '_/',
|
||||
'/derp/' => 'derp/',
|
||||
'derp' => 'derp/',
|
||||
'derp//derp' => 'derp/derp/',
|
||||
|
@ -27,6 +27,13 @@ final class PhabricatorSlugTestCase extends PhabricatorTestCase {
|
|||
'../a' => 'dotdot/a/',
|
||||
'a/..' => 'a/dotdot/',
|
||||
'a/../' => 'a/dotdot/',
|
||||
'a?' => 'a/',
|
||||
'??' => '_/',
|
||||
'a/?' => 'a/_/',
|
||||
'??/a/??' => '_/a/_/',
|
||||
'a/??/c' => 'a/_/c/',
|
||||
'a/?b/c' => 'a/b/c/',
|
||||
'a/b?/c' => 'a/b/c/',
|
||||
);
|
||||
|
||||
foreach ($slugs as $slug => $normal) {
|
||||
|
|
Loading…
Reference in a new issue