public function loadPage() { $table = new PhabricatorPaste(); $conn_r = $table->establishConnection('r'); $data = queryfx_all($conn_r, 'SELECT paste.* FROM %T paste %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); $results = $table->loadAllFromArray($data); return $this->processResults($results); }
private function buildSubheaderView(PhabricatorPaste $paste) { $viewer = $this->getViewer(); $author = $viewer->renderHandle($paste->getAuthorPHID())->render(); $date = phabricator_datetime($paste->getDateCreated(), $viewer); $author = phutil_tag('strong', array(), $author); $author_info = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withPHIDs(array($paste->getAuthorPHID()))->needProfileImage(true)->executeOne(); $image_uri = $author_info->getProfileImageURI(); $image_href = '/p/' . $author_info->getUsername(); $content = pht('Authored by %s on %s.', $author, $date); return id(new PHUIHeadThingView())->setImage($image_uri)->setImageHref($image_href)->setContent($content); }
private function buildSourceCodeView(PhabricatorPaste $paste, PhabricatorFile $file) { $language = $paste->getLanguage(); $source = $file->loadFileData(); if (empty($language)) { $source = PhabricatorSyntaxHighlighter::highlightWithFilename($paste->getTitle(), $source); } else { $source = PhabricatorSyntaxHighlighter::highlightWithLanguage($language, $source); } $lines = explode("\n", $source); return id(new PhabricatorSourceCodeView())->setLines($lines); }
private function buildHeaderView(PhabricatorPaste $paste) { $title = nonempty($paste->getTitle()) ? $paste->getTitle() : pht('(An Untitled Masterwork)'); if ($paste->isArchived()) { $header_icon = 'fa-ban'; $header_name = pht('Archived'); $header_color = 'dark'; } else { $header_icon = 'fa-check'; $header_name = pht('Active'); $header_color = 'bluegrey'; } $header = id(new PHUIHeaderView())->setHeader($title)->setUser($this->getRequest()->getUser())->setStatus($header_icon, $header_color, $header_name)->setPolicyObject($paste); return $header; }
protected function processReceivedMail(PhabricatorMetaMTAReceivedMail $mail, PhabricatorUser $sender) { $title = $mail->getSubject(); if (!$title) { $title = pht('Email Paste'); } $file = PhabricatorPasteEditor::initializeFileForPaste($sender, $title, $mail->getCleanTextBody()); $xactions = array(); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)->setNewValue($file->getPHID()); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)->setNewValue($title); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)->setNewValue(''); // auto-detect $paste = PhabricatorPaste::initializeNewPaste($sender); $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_EMAIL, array('id' => $mail->getID())); $editor = id(new PhabricatorPasteEditor())->setActor($sender)->setContentSource($content_source)->setContinueOnNoEffect(true); $xactions = $editor->applyTransactions($paste, $xactions); $mail->setRelatedPHID($paste->getPHID()); $subject_prefix = PhabricatorEnv::getEnvConfig('metamta.paste.subject-prefix'); $subject = pht('You successfully created a paste.'); $paste_uri = PhabricatorEnv::getProductionURI($paste->getURI()); $body = new PhabricatorMetaMTAMailBody(); $body->addRawSection($subject); $body->addTextSection(pht('PASTE LINK'), $paste_uri); id(new PhabricatorMetaMTAMail())->addTos(array($sender->getPHID()))->setSubject($subject)->setSubjectPrefix($subject_prefix)->setFrom($sender->getPHID())->setRelatedPHID($paste->getPHID())->setBody($body->render())->saveAndSend(); }
public function testSpacesPolicyFiltering() { $this->destroyAllSpaces(); $creator = $this->generateNewTestUser(); $viewer = $this->generateNewTestUser(); // Create a new paste. $paste = PhabricatorPaste::initializeNewPaste($creator)->setViewPolicy(PhabricatorPolicies::POLICY_USER)->setFilePHID('')->setLanguage('')->save(); // It should be visible. $this->assertTrue(PhabricatorPolicyFilter::hasCapability($viewer, $paste, PhabricatorPolicyCapability::CAN_VIEW)); // Create a default space with an open view policy. $default = $this->newSpace($creator, pht('Default Space'), true)->setViewPolicy(PhabricatorPolicies::POLICY_USER)->save(); PhabricatorSpacesNamespaceQuery::destroySpacesCache(); // The paste should now be in the space implicitly, but still visible // because the space view policy is open. $this->assertTrue(PhabricatorPolicyFilter::hasCapability($viewer, $paste, PhabricatorPolicyCapability::CAN_VIEW)); // Make the space view policy restrictive. $default->setViewPolicy(PhabricatorPolicies::POLICY_NOONE)->save(); PhabricatorSpacesNamespaceQuery::destroySpacesCache(); // The paste should be in the space implicitly, and no longer visible. $this->assertFalse(PhabricatorPolicyFilter::hasCapability($viewer, $paste, PhabricatorPolicyCapability::CAN_VIEW)); // Put the paste in the space explicitly. $paste->setSpacePHID($default->getPHID())->save(); PhabricatorSpacesNamespaceQuery::destroySpacesCache(); // This should still fail, we're just in the space explicitly now. $this->assertFalse(PhabricatorPolicyFilter::hasCapability($viewer, $paste, PhabricatorPolicyCapability::CAN_VIEW)); // Create an alternate space with more permissive policies, then move the // paste to that space. $alternate = $this->newSpace($creator, pht('Alternate Space'), false)->setViewPolicy(PhabricatorPolicies::POLICY_USER)->save(); $paste->setSpacePHID($alternate->getPHID())->save(); PhabricatorSpacesNamespaceQuery::destroySpacesCache(); // Now the paste should be visible again. $this->assertTrue(PhabricatorPolicyFilter::hasCapability($viewer, $paste, PhabricatorPolicyCapability::CAN_VIEW)); }
public function buildSideNavView(PhabricatorPaste $paste = null) { $nav = new AphrontSideNavFilterView(); $nav->setBaseURI(new PhutilURI($this->getApplicationURI('filter/'))); if ($paste) { $nav->addFilter('paste', 'P' . $paste->getID(), '/P' . $paste->getID()); $nav->addSpacer(); } $nav->addLabel('Create'); $nav->addFilter('create', 'New Paste'); $nav->addSpacer(); $nav->addLabel('Pastes'); $nav->addFilter('my', 'My Pastes'); $nav->addFilter('all', 'All Pastes'); return $nav; }
public function buildSideNavView(PhabricatorPaste $paste = null) { $user = $this->getRequest()->getUser(); $nav = new AphrontSideNavFilterView(); $nav->setBaseURI(new PhutilURI($this->getApplicationURI('filter/'))); if ($paste) { $nav->addFilter('paste', 'P' . $paste->getID(), '/P' . $paste->getID()); $nav->addSpacer(); } $nav->addLabel('Create'); $nav->addFilter('edit', 'New Paste', $this->getApplicationURI(), $relative = false, $class = $user->isLoggedIn() ? null : 'disabled'); $nav->addSpacer(); $nav->addLabel('Pastes'); if ($user->isLoggedIn()) { $nav->addFilter('my', 'My Pastes'); } $nav->addFilter('all', 'All Pastes'); return $nav; }
public function loadPage() { $table = new PhabricatorPaste(); $conn_r = $table->establishConnection('r'); $data = queryfx_all($conn_r, 'SELECT paste.* FROM %T paste %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r)); $pastes = $table->loadAllFromArray($data); if ($pastes && $this->needContent) { $file_phids = mpull($pastes, 'getFilePHID'); $files = id(new PhabricatorFile())->loadAllWhere('phid IN (%Ls)', $file_phids); $files = mpull($files, null, 'getPHID'); foreach ($pastes as $paste) { $file = idx($files, $paste->getFilePHID()); if ($file) { $paste->attachContent($file->loadFileData()); } else { $paste->attachContent(''); } } } return $this->processResults($pastes); }
public function generateObject() { $author = $this->loadRandomUser(); list($name, $language, $content) = $this->newPasteContent(); $paste = PhabricatorPaste::initializeNewPaste($author); $xactions = array(); $xactions[] = $this->newTransaction(PhabricatorPasteTitleTransaction::TRANSACTIONTYPE, $name); $xactions[] = $this->newTransaction(PhabricatorPasteLanguageTransaction::TRANSACTIONTYPE, $language); $xactions[] = $this->newTransaction(PhabricatorPasteContentTransaction::TRANSACTIONTYPE, $content); $editor = id(new PhabricatorPasteEditor())->setActor($author)->setContentSource($this->getLipsumContentSource())->setContinueOnNoEffect(true)->applyTransactions($paste, $xactions); return $paste; }
public function generate() { $author = $this->loadPhabrictorUser(); $authorphid = $author->getPHID(); $language = $this->generateLanguage(); $content = $this->generateContent($language); $title = $this->generateTitle($language); $paste_file = PhabricatorFile::newFromFileData($content, array('name' => $title, 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $authorphid)); $policy = $this->generatePolicy(); $filephid = $paste_file->getPHID(); $parentphid = $this->loadPhabrictorPastePHID(); $paste = PhabricatorPaste::initializeNewPaste($author)->setParentPHID($parentphid)->setTitle($title)->setLanguage($language)->setViewPolicy($policy)->setEditPolicy($policy)->setFilePHID($filephid)->save(); return $paste; }
protected function execute(ConduitAPIRequest $request) { $content = $request->getValue('content'); $title = $request->getValue('title'); $language = $request->getValue('language'); if (!strlen($content)) { throw new ConduitException('ERR-NO-PASTE'); } $title = nonempty($title, 'Masterwork From Distant Lands'); $language = nonempty($language, ''); $user = $request->getUser(); $paste_file = PhabricatorFile::newFromFileData($content, array('name' => $title, 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID())); $paste = new PhabricatorPaste(); $paste->setTitle($title); $paste->setLanguage($language); $paste->setFilePHID($paste_file->getPHID()); $paste->setAuthorPHID($user->getPHID()); $paste->setViewPolicy(PhabricatorPolicies::POLICY_USER); $paste->save(); $paste->attachContent($content); return $this->buildPasteInfoDictionary($paste); }
protected function execute(ConduitAPIRequest $request) { $content = $request->getValue('content'); $title = $request->getValue('title'); $language = $request->getValue('language'); if (!strlen($content)) { throw new ConduitException('ERR-NO-PASTE'); } $title = nonempty($title, pht('Masterwork From Distant Lands')); $language = nonempty($language, ''); $viewer = $request->getUser(); $paste = PhabricatorPaste::initializeNewPaste($viewer); $xactions = array(); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)->setNewValue($content); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)->setNewValue($title); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)->setNewValue($language); $editor = id(new PhabricatorPasteEditor())->setActor($viewer)->setContentSourceFromConduitRequest($request); $xactions = $editor->applyTransactions($paste, $xactions); $paste->attachRawContent($content); return $this->buildPasteInfoDictionary($paste); }
protected function buildCustomEditFields($object) { $langs = array('' => pht('(Detect From Filename in Title)')) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); return array(id(new PhabricatorTextEditField())->setKey('title')->setLabel(pht('Title'))->setDescription(pht('Name of the paste.'))->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)->setValue($object->getTitle()), id(new PhabricatorSelectEditField())->setKey('language')->setLabel(pht('Language'))->setDescription(pht('Programming language to interpret the paste as for syntax ' . 'highlighting. By default, the language is inferred from the ' . 'title.'))->setAliases(array('lang'))->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)->setValue($object->getLanguage())->setOptions($langs), id(new PhabricatorSelectEditField())->setKey('status')->setLabel(pht('Status'))->setDescription(pht('Archive the paste.'))->setTransactionType(PhabricatorPasteTransaction::TYPE_STATUS)->setValue($object->getStatus())->setOptions(PhabricatorPaste::getStatusNameMap()), id(new PhabricatorTextAreaEditField())->setKey('text')->setLabel(pht('Text'))->setDescription(pht('The main body text of the paste.'))->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)->setMonospaced(true)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setValue($object->getRawContent())); }
private function buildSnippet(PhabricatorPaste $paste) { $snippet_type = PhabricatorPasteSnippet::FULL; $snippet = $paste->getRawContent(); if (strlen($snippet) > 1024) { $snippet_type = PhabricatorPasteSnippet::FIRST_BYTES; $snippet = id(new PhutilUTF8StringTruncator())->setMaximumBytes(1024)->setTerminator('')->truncateString($snippet); } $lines = phutil_split_lines($snippet); if (count($lines) > 5) { $snippet_type = PhabricatorPasteSnippet::FIRST_LINES; $snippet = implode('', array_slice($lines, 0, 5)); } return new PhabricatorPasteSnippet($this->highlightSource($snippet, $paste->getTitle(), $paste->getLanguage()), $snippet_type); }
public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $paste = new PhabricatorPaste(); $error_view = null; $e_text = true; $fork = $request->getInt('fork'); $paste_text = null; $paste_parent = null; if ($request->isFormPost()) { $errors = array(); $title = $request->getStr('title'); $paste_language = $request->getStr('language'); $text = $request->getStr('text'); if (!strlen($text)) { $e_text = 'Required'; $errors[] = 'The paste may not be blank.'; } else { $e_text = null; } $parent = id(new PhabricatorPaste())->loadOneWhere('phid = %s', $request->getStr('parent')); if ($parent) { $paste->setParentPHID($parent->getPHID()); } $paste->setTitle($title); if (!$errors) { if ($paste_language == 'infer') { // If it's infer, store an empty string. Otherwise, store the // language name. We do this so we can refer to 'infer' elsewhere // in the code (such as default value) while retaining backwards // compatibility with old posts with no language stored. $paste_language = ''; } $paste->setLanguage($paste_language); $paste_file = PhabricatorFile::newFromFileData($text, array('name' => $title, 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID())); $paste->setFilePHID($paste_file->getPHID()); $paste->setAuthorPHID($user->getPHID()); $paste->save(); return id(new AphrontRedirectResponse())->setURI('/P' . $paste->getID()); } else { $error_view = new AphrontErrorView(); $error_view->setErrors($errors); $error_view->setTitle('A problem has occurred!'); } } else { if ($fork) { $fork_paste = id(new PhabricatorPaste())->load($fork); if ($fork_paste) { $paste->setTitle('Fork of ' . $fork_paste->getID() . ': ' . $fork_paste->getTitle()); $fork_file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $fork_paste->getFilePHID()); $paste_text = $fork_file->loadFileData(); $paste_language = nonempty($fork_paste->getLanguage(), 'infer'); $paste_parent = $fork_paste->getPHID(); } } else { $paste_language = PhabricatorEnv::getEnvConfig('pygments.dropdown-default'); } } $form = new AphrontFormView(); $available_languages = PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); asort($available_languages); $language_select = id(new AphrontFormSelectControl())->setLabel('Language')->setName('language')->setValue($paste_language)->setOptions($available_languages); $form->setUser($user)->setAction($request->getRequestURI()->getPath())->addHiddenInput('parent', $paste_parent)->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setValue($paste->getTitle())->setName('title'))->appendChild($language_select)->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Text')->setError($e_text)->setValue($paste_text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setName('text'))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton('/paste/')->setValue('Create Paste')); $panel = new AphrontPanelView(); $panel->setWidth(AphrontPanelView::WIDTH_FORM); $panel->setHeader('Create a Paste'); $panel->appendChild($form); return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Paste Creation', 'tab' => 'create')); }
public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $parent = null; $parent_id = null; if (!$this->id) { $is_create = true; $paste = new PhabricatorPaste(); $parent_id = $request->getStr('parent'); if ($parent_id) { // NOTE: If the Paste is forked from a paste which the user no longer // has permission to see, we still let them edit it. $parent = id(new PhabricatorPasteQuery())->setViewer($user)->withIDs(array($parent_id))->needContent(true)->execute(); $parent = head($parent); if ($parent) { $paste->setParentPHID($parent->getPHID()); } } $paste->setAuthorPHID($user->getPHID()); } else { $is_create = false; $paste = id(new PhabricatorPasteQuery())->setViewer($user)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($this->id))->executeOne(); if (!$paste) { return new Aphront404Response(); } } $text = null; $e_text = true; $errors = array(); if ($request->isFormPost()) { if ($is_create) { $text = $request->getStr('text'); if (!strlen($text)) { $e_text = 'Required'; $errors[] = 'The paste may not be blank.'; } else { $e_text = null; } } $paste->setTitle($request->getStr('title')); $paste->setLanguage($request->getStr('language')); if (!$errors) { if ($is_create) { $paste_file = PhabricatorFile::newFromFileData($text, array('name' => $paste->getTitle(), 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID())); $paste->setFilePHID($paste_file->getPHID()); } $paste->save(); return id(new AphrontRedirectResponse())->setURI($paste->getURI()); } } else { if ($is_create && $parent) { $paste->setTitle('Fork of ' . $parent->getFullName()); $paste->setLanguage($parent->getLanguage()); $text = $parent->getContent(); } } $error_view = null; if ($errors) { $error_view = id(new AphrontErrorView())->setTitle('A fatal omission!')->setErrors($errors); } $form = new AphrontFormView(); $form->setFlexible(true); $langs = array('' => '(Detect With Wizardly Powers)') + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); $form->setUser($user)->addHiddenInput('parent', $parent_id)->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setValue($paste->getTitle())->setName('title'))->appendChild(id(new AphrontFormSelectControl())->setLabel('Language')->setName('language')->setValue($paste->getLanguage())->setOptions($langs)); if ($is_create) { $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Text')->setError($e_text)->setValue($text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setCustomClass('PhabricatorMonospaced')->setName('text')); } /* TODO: Doesn't have any useful options yet. ->appendChild( id(new AphrontFormPolicyControl()) ->setLabel('Visible To') ->setUser($user) ->setValue( $new_paste->getPolicy(PhabricatorPolicyCapability::CAN_VIEW)) ->setName('policy')) */ $submit = new AphrontFormSubmitControl(); if (!$is_create) { $submit->addCancelButton($paste->getURI()); $submit->setValue('Save Paste'); $title = 'Edit ' . $paste->getFullName(); } else { $submit->setValue('Create Paste'); $title = 'Create Paste'; } $form->appendChild($submit); $nav = $this->buildSideNavView(); $nav->selectFilter('edit'); $nav->appendChild(array(id(new PhabricatorHeaderView())->setHeader($title), $error_view, $form)); return $this->buildApplicationPage($nav, array('title' => $title, 'device' => true)); }
<?php echo "Populating pastes with mail keys...\n"; $table = new PhabricatorPaste(); $table->openTransaction(); $conn_w = $table->establishConnection('w'); foreach (new LiskMigrationIterator($table) as $paste) { $id = $paste->getID(); echo "P{$id}: "; if (!$paste->getMailKey()) { queryfx($conn_w, 'UPDATE %T SET mailKey = %s WHERE id = %d', $paste->getTableName(), Filesystem::readRandomCharacters(20), $id); echo "Generated Key\n"; } else { echo "-\n"; } } $table->saveTransaction(); echo "Done.\n";
protected function newEditableObject() { return PhabricatorPaste::initializeNewPaste($this->getViewer()); }
private function buildContent(PhabricatorPaste $paste) { $language = $paste->getLanguage(); $source = $paste->getRawContent(); if (empty($language)) { return PhabricatorSyntaxHighlighter::highlightWithFilename($paste->getTitle(), $source); } else { return PhabricatorSyntaxHighlighter::highlightWithLanguage($language, $source); } }
<?php $table = new PhabricatorPaste(); $x_table = new PhabricatorPasteTransaction(); $conn_w = $table->establishConnection('w'); $conn_w->openTransaction(); echo "Adding transactions for existing paste objects...\n"; $rows = new LiskRawMigrationIterator($conn_w, 'pastebin_paste'); foreach ($rows as $row) { $id = $row['id']; echo "Adding transactions for paste id {$id}...\n"; $xaction_phid = PhabricatorPHID::generateNewPHID(PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST); queryfx($conn_w, 'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy, transactionType, oldValue, newValue, contentSource, metadata, dateCreated, dateModified, commentVersion) VALUES (%s, %s, %s, %s, %s, %s, %ns, %ns, %s, %s, %d, %d, %d)', $x_table->getTableName(), $xaction_phid, $row['authorPHID'], $row['phid'], 'public', $row['authorPHID'], PhabricatorPasteTransaction::TYPE_CONTENT, 'null', $row['filePHID'], PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_LEGACY, array())->serialize(), '[]', $row['dateCreated'], $row['dateCreated'], 0); } $conn_w->saveTransaction(); echo "Done.\n";
private function processCreateRequest() { $request = $this->getRequest(); $user = $request->getUser(); $fork = $request->getInt('fork'); $error_view = null; $e_text = true; $new_paste = new PhabricatorPaste(); $new_paste_text = null; $new_paste_language = PhabricatorEnv::getEnvConfig('pygments.dropdown-default'); if ($request->isFormPost()) { $errors = array(); $text = $request->getStr('text'); if (!strlen($text)) { $e_text = 'Required'; $errors[] = 'The paste may not be blank.'; } else { $e_text = null; } $parent_phid = $request->getStr('parent'); if ($parent_phid) { $parent = id(new PhabricatorPaste())->loadOneWhere('phid = %s', $parent_phid); if ($parent) { $new_paste->setParentPHID($parent->getPHID()); } } $title = $request->getStr('title'); $new_paste->setTitle($title); $new_paste_language = $request->getStr('language'); if (!$errors) { if ($new_paste_language == 'infer') { // If it's infer, store an empty string. Otherwise, store the // language name. We do this so we can refer to 'infer' elsewhere // in the code (such as default value) while retaining backwards // compatibility with old posts with no language stored. $new_paste_language = ''; } $new_paste->setLanguage($new_paste_language); $new_paste_file = PhabricatorFile::newFromFileData($text, array('name' => $title, 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID())); $new_paste->setFilePHID($new_paste_file->getPHID()); $new_paste->setAuthorPHID($user->getPHID()); $new_paste->save(); return id(new AphrontRedirectResponse())->setURI('/P' . $new_paste->getID()); } else { $error_view = new AphrontErrorView(); $error_view->setErrors($errors); $error_view->setTitle('A problem has occurred!'); } } else { if ($fork) { $fork_paste = id(new PhabricatorPaste())->load($fork); if ($fork_paste) { $new_paste->setTitle('Fork of ' . $fork_paste->getID() . ': ' . $fork_paste->getTitle()); $fork_file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $fork_paste->getFilePHID()); $new_paste_text = $fork_file->loadFileData(); $new_paste_language = nonempty($fork_paste->getLanguage(), 'infer'); $new_paste->setParentPHID($fork_paste->getPHID()); } } } $this->setErrorView($error_view); $this->setErrorText($e_text); $this->setPasteText($new_paste_text); $new_paste->setLanguage($new_paste_language); $this->setPaste($new_paste); }
protected function buildCustomEditFields($object) { $langs = array('' => pht('(Detect From Filename in Title)')) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); return array(id(new PhabricatorTextEditField())->setKey('title')->setLabel(pht('Title'))->setTransactionType(PhabricatorPasteTitleTransaction::TRANSACTIONTYPE)->setDescription(pht('The title of the paste.'))->setConduitDescription(pht('Retitle the paste.'))->setConduitTypeDescription(pht('New paste title.'))->setValue($object->getTitle()), id(new PhabricatorSelectEditField())->setKey('language')->setLabel(pht('Language'))->setTransactionType(PhabricatorPasteLanguageTransaction::TRANSACTIONTYPE)->setAliases(array('lang'))->setIsCopyable(true)->setOptions($langs)->setDescription(pht('Language used for syntax highlighting. By default, inferred ' . 'from the title.'))->setConduitDescription(pht('Change language used for syntax highlighting.'))->setConduitTypeDescription(pht('New highlighting language.'))->setValue($object->getLanguage()), id(new PhabricatorTextAreaEditField())->setKey('text')->setLabel(pht('Text'))->setTransactionType(PhabricatorPasteContentTransaction::TRANSACTIONTYPE)->setMonospaced(true)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setDescription(pht('The main body text of the paste.'))->setConduitDescription(pht('Change the paste content.'))->setConduitTypeDescription(pht('New body content.'))->setValue($object->getRawContent()), id(new PhabricatorSelectEditField())->setKey('status')->setLabel(pht('Status'))->setTransactionType(PhabricatorPasteStatusTransaction::TRANSACTIONTYPE)->setIsConduitOnly(true)->setOptions(PhabricatorPaste::getStatusNameMap())->setDescription(pht('Active or archived status.'))->setConduitDescription(pht('Active or archive the paste.'))->setConduitTypeDescription(pht('New paste status constant.'))->setValue($object->getStatus())); }
private function buildHeaderView(PhabricatorPaste $paste) { $title = nonempty($paste->getTitle()) ? $paste->getTitle() : pht('(An Untitled Masterwork)'); $header = id(new PHUIHeaderView())->setHeader($title)->setUser($this->getRequest()->getUser())->setPolicyObject($paste); return $header; }
public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $parent = null; $parent_id = null; if (!$this->id) { $is_create = true; $paste = PhabricatorPaste::initializeNewPaste($user); $parent_id = $request->getStr('parent'); if ($parent_id) { // NOTE: If the Paste is forked from a paste which the user no longer // has permission to see, we still let them edit it. $parent = id(new PhabricatorPasteQuery())->setViewer($user)->withIDs(array($parent_id))->needContent(true)->needRawContent(true)->execute(); $parent = head($parent); if ($parent) { $paste->setParentPHID($parent->getPHID()); $paste->setViewPolicy($parent->getViewPolicy()); } } $paste->setAuthorPHID($user->getPHID()); $paste->attachRawContent(''); } else { $is_create = false; $paste = id(new PhabricatorPasteQuery())->setViewer($user)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($this->id))->needRawContent(true)->executeOne(); if (!$paste) { return new Aphront404Response(); } } $v_space = $paste->getSpacePHID(); if ($is_create && $parent) { $v_title = pht('Fork of %s', $parent->getFullName()); $v_language = $parent->getLanguage(); $v_text = $parent->getRawContent(); $v_space = $parent->getSpacePHID(); } else { $v_title = $paste->getTitle(); $v_language = $paste->getLanguage(); $v_text = $paste->getRawContent(); } $v_view_policy = $paste->getViewPolicy(); $v_edit_policy = $paste->getEditPolicy(); if ($is_create) { $v_projects = array(); } else { $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs($paste->getPHID(), PhabricatorProjectObjectHasProjectEdgeType::EDGECONST); $v_projects = array_reverse($v_projects); } $validation_exception = null; if ($request->isFormPost()) { $xactions = array(); $v_text = $request->getStr('text'); $v_title = $request->getStr('title'); $v_language = $request->getStr('language'); $v_view_policy = $request->getStr('can_view'); $v_edit_policy = $request->getStr('can_edit'); $v_projects = $request->getArr('projects'); $v_space = $request->getStr('spacePHID'); // NOTE: The author is the only editor and can always view the paste, // so it's impossible for them to choose an invalid policy. if ($is_create || $v_text !== $paste->getRawContent()) { $file = PhabricatorPasteEditor::initializeFileForPaste($user, $v_title, $v_text); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)->setNewValue($file->getPHID()); } $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)->setNewValue($v_title); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)->setNewValue($v_language); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($v_view_policy); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($v_edit_policy); $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_SPACE)->setNewValue($v_space); $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST; $xactions[] = id(new PhabricatorPasteTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $proj_edge_type)->setNewValue(array('=' => array_fuse($v_projects))); $editor = id(new PhabricatorPasteEditor())->setActor($user)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true); try { $xactions = $editor->applyTransactions($paste, $xactions); return id(new AphrontRedirectResponse())->setURI($paste->getURI()); } catch (PhabricatorApplicationTransactionValidationException $ex) { $validation_exception = $ex; } } $form = new AphrontFormView(); $langs = array('' => pht('(Detect From Filename in Title)')) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices'); $form->setUser($user)->addHiddenInput('parent', $parent_id)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Title'))->setValue($v_title)->setName('title'))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Language'))->setName('language')->setValue($v_language)->setOptions($langs)); $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($paste)->execute(); $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($paste)->setPolicies($policies)->setValue($v_view_policy)->setSpacePHID($v_space)->setName('can_view')); $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($paste)->setPolicies($policies)->setValue($v_edit_policy)->setName('can_edit')); $form->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Projects'))->setName('projects')->setValue($v_projects)->setDatasource(new PhabricatorProjectDatasource())); $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel(pht('Text'))->setValue($v_text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setCustomClass('PhabricatorMonospaced')->setName('text')); $submit = new AphrontFormSubmitControl(); if (!$is_create) { $submit->addCancelButton($paste->getURI()); $submit->setValue(pht('Save Paste')); $title = pht('Edit %s', $paste->getFullName()); $short = pht('Edit'); } else { $submit->setValue(pht('Create Paste')); $title = pht('Create New Paste'); $short = pht('Create'); } $form->appendChild($submit); $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setForm($form); if ($validation_exception) { $form_box->setValidationException($validation_exception); } $crumbs = $this->buildApplicationCrumbs($this->buildSideNavView()); if (!$is_create) { $crumbs->addTextCrumb('P' . $paste->getID(), '/P' . $paste->getID()); } $crumbs->addTextCrumb($short); return $this->buildApplicationPage(array($crumbs, $form_box), array('title' => $title)); }
protected function buildPasteInfoDictionary(PhabricatorPaste $paste) { $content = null; $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $paste->getFilePHID()); if ($file) { $content = $file->loadFileData(); } return array('id' => $paste->getID(), 'objectName' => 'P' . $paste->getID(), 'phid' => $paste->getPHID(), 'authorPHID' => $paste->getAuthorPHID(), 'filePHID' => $paste->getFilePHID(), 'title' => $paste->getTitle(), 'dateCreated' => $paste->getDateCreated(), 'language' => $paste->getLanguage(), 'uri' => PhabricatorEnv::getProductionURI('/P' . $paste->getID()), 'parentPHID' => $paste->getParentPHID(), 'content' => $content); }
public function buildSourceCodeView(PhabricatorPaste $paste, $max_lines = null, $highlights = array()) { $lines = phutil_split_lines($paste->getContent()); return id(new PhabricatorSourceCodeView())->setLimit($max_lines)->setLines($lines)->setHighlights($highlights)->setURI(new PhutilURI($paste->getURI())); }
protected function buildPasteInfoDictionary(PhabricatorPaste $paste) { return array('id' => $paste->getID(), 'objectName' => 'P' . $paste->getID(), 'phid' => $paste->getPHID(), 'authorPHID' => $paste->getAuthorPHID(), 'filePHID' => $paste->getFilePHID(), 'title' => $paste->getTitle(), 'dateCreated' => $paste->getDateCreated(), 'language' => $paste->getLanguage(), 'uri' => PhabricatorEnv::getProductionURI('/P' . $paste->getID()), 'parentPHID' => $paste->getParentPHID(), 'content' => $paste->getRawContent()); }