public function processRequest()
 {
     $request = $this->getRequest();
     $pager = new AphrontPagerView();
     $pager->setOffset($request->getInt('page'));
     $macro_table = new PhabricatorFileImageMacro();
     $macros = $macro_table->loadAllWhere('1 = 1 ORDER BY id DESC LIMIT %d, %d', $pager->getOffset(), $pager->getPageSize());
     // Get an exact count since the size here is reasonably going to be a few
     // thousand at most in any reasonable case.
     $count = queryfx_one($macro_table->establishConnection('r'), 'SELECT COUNT(*) N FROM %T', $macro_table->getTableName());
     $count = $count['N'];
     $pager->setCount($count);
     $pager->setURI($request->getRequestURI(), 'page');
     $rows = array();
     foreach ($macros as $macro) {
         $src = PhabricatorFileURI::getViewURIForPHID($macro->getFilePHID());
         $rows[] = array(phutil_render_tag('a', array('href' => '/file/macro/edit/' . $macro->getID() . '/'), phutil_escape_html($macro->getName())), phutil_render_tag('a', array('href' => $src, 'target' => '_blank'), phutil_render_tag('img', array('src' => $src))), javelin_render_tag('a', array('href' => '/file/macro/delete/' . $macro->getID() . '/', 'sigil' => 'workflow', 'class' => 'grey small button'), 'Delete'));
     }
     $table = new AphrontTableView($rows);
     $table->setHeaders(array('Name', 'Image', ''));
     $table->setColumnClasses(array('pri', 'wide thumb', 'action'));
     $panel = new AphrontPanelView();
     $panel->appendChild($table);
     $panel->setHeader('Image Macros');
     $panel->setCreateButton('New Image Macro', '/file/macro/edit/');
     $panel->appendChild($pager);
     return $this->buildStandardPageResponse($panel, array('title' => 'Image Macros', 'tab' => 'macros'));
 }
예제 #2
0
 protected function loadPage()
 {
     $macro_table = new PhabricatorFileImageMacro();
     $conn = $macro_table->establishConnection('r');
     $rows = queryfx_all($conn, 'SELECT m.* FROM %T m %Q %Q %Q', $macro_table->getTableName(), $this->buildWhereClause($conn), $this->buildOrderClause($conn), $this->buildLimitClause($conn));
     return $macro_table->loadAllFromArray($rows);
 }
 public static function newFromImageURI($uri, $file_name, $image_macro_name)
 {
     $file = PhabricatorFile::newFromFileDownload($uri, $file_name);
     if (!$file) {
         return null;
     }
     $image_macro = new PhabricatorFileImageMacro();
     $image_macro->setName($image_macro_name);
     $image_macro->setFilePHID($file->getPHID());
     $image_macro->save();
     return $image_macro;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $macro_table = new PhabricatorFileImageMacro();
     if ($request->getStr('name') !== null) {
         $macros = $macro_table->loadAllWhere('name LIKE %~', $request->getStr('name'));
     } else {
         $pager = new AphrontPagerView();
         $pager->setOffset($request->getInt('page'));
         $macros = $macro_table->loadAllWhere('1 = 1 ORDER BY id DESC LIMIT %d, %d', $pager->getOffset(), $pager->getPageSize());
         // Get an exact count since the size here is reasonably going to be a few
         // thousand at most in any reasonable case.
         $count = queryfx_one($macro_table->establishConnection('r'), 'SELECT COUNT(*) N FROM %T', $macro_table->getTableName());
         $count = $count['N'];
         $pager->setCount($count);
         $pager->setURI($request->getRequestURI(), 'page');
     }
     $file_phids = mpull($macros, 'getFilePHID');
     $files = array();
     if ($file_phids) {
         $files = id(new PhabricatorFile())->loadAllWhere("phid IN (%Ls)", $file_phids);
         $author_phids = mpull($files, 'getAuthorPHID', 'getPHID');
         $handles = id(new PhabricatorObjectHandleData($author_phids))->loadHandles();
     }
     $files_map = mpull($files, null, 'getPHID');
     $rows = array();
     foreach ($macros as $macro) {
         $file_phid = $macro->getFilePHID();
         $file = idx($files_map, $file_phid);
         $author_link = isset($author_phids[$file_phid]) ? $handles[$author_phids[$file_phid]]->renderLink() : null;
         $rows[] = array(phutil_render_tag('a', array('href' => '/file/macro/edit/' . $macro->getID() . '/'), phutil_escape_html($macro->getName())), $author_link, phutil_render_tag('a', array('href' => $file ? $file->getBestURI() : null, 'target' => '_blank'), phutil_render_tag('img', array('src' => $file ? $file->getBestURI() : null))), javelin_render_tag('a', array('href' => '/file/macro/delete/' . $macro->getID() . '/', 'sigil' => 'workflow', 'class' => 'grey small button'), 'Delete'));
     }
     $table = new AphrontTableView($rows);
     $table->setHeaders(array('Name', 'Author', 'Image', ''));
     $table->setColumnClasses(array('pri', '', 'wide thumb', 'action'));
     $filter_form = id(new AphrontFormView())->setMethod('GET')->setAction('/file/macro/')->setUser($request->getUser())->appendChild(id(new AphrontFormTextControl())->setName('name')->setLabel('Name')->setValue($request->getStr('name')))->appendChild(id(new AphrontFormSubmitControl())->setValue('Filter Image Macros'));
     $filter_view = new AphrontListFilterView();
     $filter_view->appendChild($filter_form);
     $filter_view->addButton(phutil_render_tag('a', array('href' => '/file/macro/edit/', 'class' => 'green button'), 'New Image Macro'));
     $panel = new AphrontPanelView();
     $panel->appendChild($table);
     $panel->setHeader('Image Macros');
     if ($request->getStr('name') === null) {
         $panel->appendChild($pager);
     }
     $side_nav = new PhabricatorFileSideNavView();
     $side_nav->setSelectedFilter('all_macros');
     $side_nav->appendChild($filter_view);
     $side_nav->appendChild($panel);
     return $this->buildStandardPageResponse($side_nav, array('title' => 'Image Macros'));
 }
 public function processRequest()
 {
     if ($this->id) {
         $macro = id(new PhabricatorFileImageMacro())->load($this->id);
         if (!$macro) {
             return new Aphront404Response();
         }
     } else {
         $macro = new PhabricatorFileImageMacro();
     }
     $errors = array();
     $e_name = true;
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($request->isFormPost()) {
         $macro->setName($request->getStr('name'));
         if (!strlen($macro->getName())) {
             $errors[] = 'Macro name is required.';
             $e_name = 'Required';
         } else {
             if (!preg_match('/^[a-z0-9_-]{3,}$/', $macro->getName())) {
                 $errors[] = 'Macro must be at least three characters long and contain ' . 'only lowercase letters, digits, hyphen and underscore.';
                 $e_name = 'Invalid';
             } else {
                 $e_name = null;
             }
         }
         if (!$errors) {
             $file = PhabricatorFile::newFromPHPUpload(idx($_FILES, 'file'), array('name' => $request->getStr('name'), 'authorPHID' => $user->getPHID()));
             $macro->setFilePHID($file->getPHID());
             try {
                 $macro->save();
                 return id(new AphrontRedirectResponse())->setURI('/file/macro/');
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $errors[] = 'Macro name is not unique!';
                 $e_name = 'Duplicate';
             }
         }
     }
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         $error_view = null;
     }
     $form = new AphrontFormView();
     $form->setAction('/file/macro/edit/');
     $form->setUser($request->getUser());
     $form->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($macro->getName())->setCaption('This word or phrase will be replaced with the image.')->setError($e_name))->appendChild(id(new AphrontFormFileControl())->setLabel('File')->setName('file')->setError(true))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save Image Macro')->addCancelButton('/file/macro/'));
     $panel = new AphrontPanelView();
     if ($macro->getID()) {
         $panel->setHeader('Edit Image Macro');
     } else {
         $panel->setHeader('Create Image Macro');
     }
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Edit Image Macro'));
 }
 private function buildFileView(PhabricatorFileImageMacro $macro)
 {
     $viewer = $this->getViewer();
     $view = id(new PHUIPropertyListView())->setUser($viewer);
     $file = $macro->getFile();
     if ($file) {
         $view->addImageContent(phutil_tag('img', array('src' => $file->getViewURI(), 'class' => 'phabricator-image-macro-hero')));
         return $view;
     }
     return null;
 }
예제 #7
0
<?php

echo 'Giving image macros PHIDs';
$table = new PhabricatorFileImageMacro();
$table->openTransaction();
foreach (new LiskMigrationIterator($table) as $macro) {
    if ($macro->getPHID()) {
        continue;
    }
    echo '.';
    queryfx($macro->establishConnection('w'), 'UPDATE %T SET phid = %s WHERE id = %d', $macro->getTableName(), $macro->generatePHID(), $macro->getID());
}
$table->saveTransaction();
echo "\nDone.\n";
 public function processRequest()
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     $macro_table = new PhabricatorFileImageMacro();
     $filter = $request->getStr('name');
     if (strlen($filter)) {
         $macros = $macro_table->loadAllWhere('name LIKE %~', $filter);
         $nodata = pht('There are no macros matching the filter "%s".', phutil_escape_html($filter));
     } else {
         $pager = new AphrontPagerView();
         $pager->setOffset($request->getInt('page'));
         $macros = $macro_table->loadAllWhere('1 = 1 ORDER BY id DESC LIMIT %d, %d', $pager->getOffset(), $pager->getPageSize());
         // Get an exact count since the size here is reasonably going to be a few
         // thousand at most in any reasonable case.
         $count = queryfx_one($macro_table->establishConnection('r'), 'SELECT COUNT(*) N FROM %T', $macro_table->getTableName());
         $count = $count['N'];
         $pager->setCount($count);
         $pager->setURI($request->getRequestURI(), 'page');
         $nodata = pht('There are no image macros yet.');
     }
     $file_phids = mpull($macros, 'getFilePHID');
     $files = array();
     if ($file_phids) {
         $files = id(new PhabricatorFile())->loadAllWhere("phid IN (%Ls)", $file_phids);
         $author_phids = mpull($files, 'getAuthorPHID', 'getPHID');
         $this->loadHandles($author_phids);
     }
     $files_map = mpull($files, null, 'getPHID');
     $filter_form = id(new AphrontFormView())->setMethod('GET')->setUser($request->getUser())->appendChild(id(new AphrontFormTextControl())->setName('name')->setLabel('Name')->setValue($filter))->appendChild(id(new AphrontFormSubmitControl())->setValue('Filter Image Macros'));
     $filter_view = new AphrontListFilterView();
     $filter_view->appendChild($filter_form);
     $nav = $this->buildSideNavView();
     $nav->selectFilter('/');
     $nav->appendChild($filter_view);
     if ($macros) {
         $pinboard = new PhabricatorPinboardView();
         foreach ($macros as $macro) {
             $file_phid = $macro->getFilePHID();
             $file = idx($files_map, $file_phid);
             $item = new PhabricatorPinboardItemView();
             if ($file) {
                 $item->setImageURI($file->getThumb220x165URI());
                 $item->setImageSize(220, 165);
                 if ($file->getAuthorPHID()) {
                     $author_handle = $this->getHandle($file->getAuthorPHID());
                     $item->appendChild('Created by ' . $author_handle->renderLink());
                 }
                 $datetime = phabricator_date($file->getDateCreated(), $viewer);
                 $item->appendChild(phutil_render_tag('div', array(), 'Created on ' . $datetime));
             }
             $item->setURI($this->getApplicationURI('/edit/' . $macro->getID() . '/'));
             $item->setHeader($macro->getName());
             $pinboard->addItem($item);
         }
         $nav->appendChild($pinboard);
     } else {
         $list = new PhabricatorObjectItemListView();
         $list->setNoDataString($nodata);
         $nav->appendChild($list);
     }
     if ($filter === null) {
         $nav->appendChild($pager);
     }
     return $this->buildApplicationPage($nav, array('device' => true, 'title' => 'Image Macros'));
 }
 public function processRequest()
 {
     $this->requireApplicationCapability(PhabricatorMacroManageCapability::CAPABILITY);
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->id) {
         $macro = id(new PhabricatorMacroQuery())->setViewer($user)->withIDs(array($this->id))->needFiles(true)->executeOne();
         if (!$macro) {
             return new Aphront404Response();
         }
     } else {
         $macro = new PhabricatorFileImageMacro();
         $macro->setAuthorPHID($user->getPHID());
     }
     $errors = array();
     $e_name = true;
     $e_file = null;
     $file = null;
     $can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http');
     if ($request->isFormPost()) {
         $original = clone $macro;
         $new_name = null;
         if ($request->getBool('name_form') || !$macro->getID()) {
             $new_name = $request->getStr('name');
             $macro->setName($new_name);
             if (!strlen($macro->getName())) {
                 $errors[] = pht('Macro name is required.');
                 $e_name = pht('Required');
             } else {
                 if (!preg_match('/^[a-z0-9:_-]{3,}\\z/', $macro->getName())) {
                     $errors[] = pht('Macro must be at least three characters long and contain only ' . 'lowercase letters, digits, hyphens, colons and underscores.');
                     $e_name = pht('Invalid');
                 } else {
                     $e_name = null;
                 }
             }
         }
         $file = null;
         if ($request->getFileExists('file')) {
             $file = PhabricatorFile::newFromPHPUpload($_FILES['file'], array('name' => $request->getStr('name'), 'authorPHID' => $user->getPHID(), 'isExplicitUpload' => true, 'canCDN' => true));
         } else {
             if ($request->getStr('url')) {
                 try {
                     $file = PhabricatorFile::newFromFileDownload($request->getStr('url'), array('name' => $request->getStr('name'), 'authorPHID' => $user->getPHID(), 'isExplicitUpload' => true, 'canCDN' => true));
                 } catch (Exception $ex) {
                     $errors[] = pht('Could not fetch URL: %s', $ex->getMessage());
                 }
             } else {
                 if ($request->getStr('phid')) {
                     $file = id(new PhabricatorFileQuery())->setViewer($user)->withPHIDs(array($request->getStr('phid')))->executeOne();
                 }
             }
         }
         if ($file) {
             if (!$file->isViewableInBrowser()) {
                 $errors[] = pht('You must upload an image.');
                 $e_file = pht('Invalid');
             } else {
                 $macro->setFilePHID($file->getPHID());
                 $macro->attachFile($file);
                 $e_file = null;
             }
         }
         if (!$macro->getID() && !$file) {
             $errors[] = pht('You must upload an image to create a macro.');
             $e_file = pht('Required');
         }
         if (!$errors) {
             try {
                 $xactions = array();
                 if ($new_name !== null) {
                     $xactions[] = id(new PhabricatorMacroTransaction())->setTransactionType(PhabricatorMacroTransactionType::TYPE_NAME)->setNewValue($new_name);
                 }
                 if ($file) {
                     $xactions[] = id(new PhabricatorMacroTransaction())->setTransactionType(PhabricatorMacroTransactionType::TYPE_FILE)->setNewValue($file->getPHID());
                 }
                 $editor = id(new PhabricatorMacroEditor())->setActor($user)->setContinueOnNoEffect(true)->setContentSourceFromRequest($request);
                 $xactions = $editor->applyTransactions($original, $xactions);
                 $view_uri = $this->getApplicationURI('/view/' . $original->getID() . '/');
                 return id(new AphrontRedirectResponse())->setURI($view_uri);
             } catch (AphrontDuplicateKeyQueryException $ex) {
                 throw $ex;
                 $errors[] = pht('Macro name is not unique!');
                 $e_name = pht('Duplicate');
             }
         }
     }
     $current_file = null;
     if ($macro->getFilePHID()) {
         $current_file = $macro->getFile();
     }
     $form = new AphrontFormView();
     $form->addHiddenInput('name_form', 1);
     $form->setUser($request->getUser());
     $form->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($macro->getName())->setCaption(pht('This word or phrase will be replaced with the image.'))->setError($e_name));
     if (!$macro->getID()) {
         if ($current_file) {
             $current_file_view = id(new PhabricatorFileLinkView())->setFilePHID($current_file->getPHID())->setFileName($current_file->getName())->setFileViewable(true)->setFileViewURI($current_file->getBestURI())->render();
             $form->addHiddenInput('phid', $current_file->getPHID());
             $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Selected File'))->setValue($current_file_view));
             $other_label = pht('Change File');
         } else {
             $other_label = pht('File');
         }
         if ($can_fetch) {
             $form->appendChild(id(new AphrontFormTextControl())->setLabel(pht('URL'))->setName('url')->setValue($request->getStr('url'))->setError($request->getFileExists('file') ? false : $e_file));
         }
         $form->appendChild(id(new AphrontFormFileControl())->setLabel($other_label)->setName('file')->setError($request->getStr('url') ? false : $e_file));
     }
     $view_uri = $this->getApplicationURI('/view/' . $macro->getID() . '/');
     if ($macro->getID()) {
         $cancel_uri = $view_uri;
     } else {
         $cancel_uri = $this->getApplicationURI();
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Save Image Macro'))->addCancelButton($cancel_uri));
     $crumbs = $this->buildApplicationCrumbs();
     if ($macro->getID()) {
         $title = pht('Edit Image Macro');
         $crumb = pht('Edit Macro');
         $crumbs->addTextCrumb(pht('Macro "%s"', $macro->getName()), $view_uri);
     } else {
         $title = pht('Create Image Macro');
         $crumb = pht('Create Macro');
     }
     $crumbs->addTextCrumb($crumb, $request->getRequestURI());
     $upload = null;
     if ($macro->getID()) {
         $upload_form = id(new AphrontFormView())->setEncType('multipart/form-data')->setUser($request->getUser());
         if ($can_fetch) {
             $upload_form->appendChild(id(new AphrontFormTextControl())->setLabel(pht('URL'))->setName('url')->setValue($request->getStr('url')));
         }
         $upload_form->appendChild(id(new AphrontFormFileControl())->setLabel(pht('File'))->setName('file'))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Upload File')));
         $upload = id(new PHUIObjectBoxView())->setHeaderText(pht('Upload New File'))->setForm($upload_form);
     }
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     return $this->buildApplicationPage(array($crumbs, $form_box, $upload), array('title' => $title));
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $id = $request->getURIData('id');
     $this->requireApplicationCapability(PhabricatorMacroManageCapability::CAPABILITY);
     if ($id) {
         $macro = id(new PhabricatorMacroQuery())->setViewer($viewer)->withIDs(array($id))->needFiles(true)->executeOne();
         if (!$macro) {
             return new Aphront404Response();
         }
     } else {
         $macro = new PhabricatorFileImageMacro();
         $macro->setAuthorPHID($viewer->getPHID());
     }
     $errors = array();
     $e_name = true;
     $e_file = null;
     $file = null;
     if ($request->isFormPost()) {
         $original = clone $macro;
         $new_name = null;
         if ($request->getBool('name_form') || !$macro->getID()) {
             $new_name = $request->getStr('name');
             $macro->setName($new_name);
             if (!strlen($macro->getName())) {
                 $errors[] = pht('Macro name is required.');
                 $e_name = pht('Required');
             } else {
                 if (!preg_match('/^[a-z0-9:_-]{3,}\\z/', $macro->getName())) {
                     $errors[] = pht('Macro must be at least three characters long and contain only ' . 'lowercase letters, digits, hyphens, colons and underscores.');
                     $e_name = pht('Invalid');
                 } else {
                     $e_name = null;
                 }
             }
         }
         $uri = $request->getStr('url');
         $engine = new PhabricatorDestructionEngine();
         $file = null;
         if ($request->getFileExists('file')) {
             $file = PhabricatorFile::newFromPHPUpload($_FILES['file'], array('name' => $request->getStr('name'), 'authorPHID' => $viewer->getPHID(), 'isExplicitUpload' => true, 'canCDN' => true));
         } else {
             if ($uri) {
                 try {
                     // Rate limit outbound fetches to make this mechanism less useful for
                     // scanning networks and ports.
                     PhabricatorSystemActionEngine::willTakeAction(array($viewer->getPHID()), new PhabricatorFilesOutboundRequestAction(), 1);
                     $file = PhabricatorFile::newFromFileDownload($uri, array('name' => $request->getStr('name'), 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, 'isExplicitUpload' => true, 'canCDN' => true));
                     if (!$file->isViewableInBrowser()) {
                         $mime_type = $file->getMimeType();
                         $engine->destroyObject($file);
                         $file = null;
                         throw new Exception(pht('The URI "%s" does not correspond to a valid image file, got ' . 'a file with MIME type "%s". You must specify the URI of a ' . 'valid image file.', $uri, $mime_type));
                     } else {
                         $file->setAuthorPHID($viewer->getPHID())->save();
                     }
                 } catch (HTTPFutureHTTPResponseStatus $status) {
                     $errors[] = pht('The URI "%s" could not be loaded, got %s error.', $uri, $status->getStatusCode());
                 } catch (Exception $ex) {
                     $errors[] = $ex->getMessage();
                 }
             } else {
                 if ($request->getStr('phid')) {
                     $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($request->getStr('phid')))->executeOne();
                 }
             }
         }
         if ($file) {
             if (!$file->isViewableInBrowser()) {
                 $errors[] = pht('You must upload an image.');
                 $e_file = pht('Invalid');
             } else {
                 $macro->setFilePHID($file->getPHID());
                 $macro->attachFile($file);
                 $e_file = null;
             }
         }
         if (!$macro->getID() && !$file) {
             $errors[] = pht('You must upload an image to create a macro.');
             $e_file = pht('Required');
         }
         if (!$errors) {
             try {
                 $xactions = array();
                 if ($new_name !== null) {
                     $xactions[] = id(new PhabricatorMacroTransaction())->setTransactionType(PhabricatorMacroTransaction::TYPE_NAME)->setNewValue($new_name);
                 }
                 if ($file) {
                     $xactions[] = id(new PhabricatorMacroTransaction())->setTransactionType(PhabricatorMacroTransaction::TYPE_FILE)->setNewValue($file->getPHID());
                 }
                 $editor = id(new PhabricatorMacroEditor())->setActor($viewer)->setContinueOnNoEffect(true)->setContentSourceFromRequest($request);
                 $xactions = $editor->applyTransactions($original, $xactions);
                 $view_uri = $this->getApplicationURI('/view/' . $original->getID() . '/');
                 return id(new AphrontRedirectResponse())->setURI($view_uri);
             } catch (AphrontDuplicateKeyQueryException $ex) {
                 throw $ex;
                 $errors[] = pht('Macro name is not unique!');
                 $e_name = pht('Duplicate');
             }
         }
     }
     $current_file = null;
     if ($macro->getFilePHID()) {
         $current_file = $macro->getFile();
     }
     $form = new AphrontFormView();
     $form->addHiddenInput('name_form', 1);
     $form->setUser($request->getUser());
     $form->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($macro->getName())->setCaption(pht('This word or phrase will be replaced with the image.'))->setError($e_name));
     if (!$macro->getID()) {
         if ($current_file) {
             $current_file_view = id(new PhabricatorFileLinkView())->setFilePHID($current_file->getPHID())->setFileName($current_file->getName())->setFileViewable(true)->setFileViewURI($current_file->getBestURI())->render();
             $form->addHiddenInput('phid', $current_file->getPHID());
             $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Selected File'))->setValue($current_file_view));
             $other_label = pht('Change File');
         } else {
             $other_label = pht('File');
         }
         $form->appendChild(id(new AphrontFormTextControl())->setLabel(pht('URL'))->setName('url')->setValue($request->getStr('url'))->setError($request->getFileExists('file') ? false : $e_file));
         $form->appendChild(id(new AphrontFormFileControl())->setLabel($other_label)->setName('file')->setError($request->getStr('url') ? false : $e_file));
     }
     $view_uri = $this->getApplicationURI('/view/' . $macro->getID() . '/');
     if ($macro->getID()) {
         $cancel_uri = $view_uri;
     } else {
         $cancel_uri = $this->getApplicationURI();
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Save Image Macro'))->addCancelButton($cancel_uri));
     $crumbs = $this->buildApplicationCrumbs();
     if ($macro->getID()) {
         $title = pht('Edit Image Macro');
         $crumb = pht('Edit Macro');
         $crumbs->addTextCrumb(pht('Macro "%s"', $macro->getName()), $view_uri);
     } else {
         $title = pht('Create Image Macro');
         $crumb = pht('Create Macro');
     }
     $crumbs->addTextCrumb($crumb, $request->getRequestURI());
     $upload = null;
     if ($macro->getID()) {
         $upload_form = id(new AphrontFormView())->setEncType('multipart/form-data')->setUser($request->getUser());
         $upload_form->appendChild(id(new AphrontFormTextControl())->setLabel(pht('URL'))->setName('url')->setValue($request->getStr('url')));
         $upload_form->appendChild(id(new AphrontFormFileControl())->setLabel(pht('File'))->setName('file'))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Upload File')));
         $upload = id(new PHUIObjectBoxView())->setHeaderText(pht('Upload New File'))->setForm($upload_form);
     }
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     return $this->buildApplicationPage(array($crumbs, $form_box, $upload), array('title' => $title));
 }
<?php

echo pht('Adding mailkeys to macros.') . "\n";
$table = new PhabricatorFileImageMacro();
$conn_w = $table->establishConnection('w');
$iterator = new LiskMigrationIterator($table);
foreach ($iterator as $macro) {
    $id = $macro->getID();
    echo pht('Populating macro %d...', $id) . "\n";
    if (!$macro->getMailKey()) {
        queryfx($conn_w, 'UPDATE %T SET mailKey = %s WHERE id = %d', $table->getTableName(), Filesystem::readRandomCharacters(20), $id);
    }
}
echo pht('Done.') . "\n";