diff --git a/resources/sql/patches/20130529.macroauthor.sql b/resources/sql/patches/20130529.macroauthor.sql new file mode 100644 index 0000000000..1549fa7a9b --- /dev/null +++ b/resources/sql/patches/20130529.macroauthor.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_file.file_imagemacro + ADD authorPHID VARCHAR(64) COLLATE utf8_bin AFTER phid; diff --git a/resources/sql/patches/20130529.macroauthormig.php b/resources/sql/patches/20130529.macroauthormig.php new file mode 100644 index 0000000000..89f10e29c8 --- /dev/null +++ b/resources/sql/patches/20130529.macroauthormig.php @@ -0,0 +1,39 @@ +getID()."\n"; + + if ($macro->getAuthorPHID()) { + // Already have an author; skip it. + continue; + } + + if (!$macro->getFilePHID()) { + // No valid file; skip it. + continue; + } + + $file = id(new PhabricatorFile())->loadOneWhere( + 'phid = %s', + $macro->getFilePHID()); + + if (!$file) { + // Couldn't load the file; skip it. + continue; + } + + if (!$file->getAuthorPHID()) { + // File has no author; skip it. + continue; + } + + queryfx( + $macro->establishConnection('w'), + 'UPDATE %T SET authorPHID = %s WHERE id = %d', + $macro->getTableName(), + $file->getAuthorPHID(), + $macro->getID()); +} + +echo "Done.\n"; diff --git a/src/applications/macro/controller/PhabricatorMacroEditController.php b/src/applications/macro/controller/PhabricatorMacroEditController.php index d7130f191c..9e78339e8b 100644 --- a/src/applications/macro/controller/PhabricatorMacroEditController.php +++ b/src/applications/macro/controller/PhabricatorMacroEditController.php @@ -29,11 +29,12 @@ final class PhabricatorMacroEditController } } else { $macro = new PhabricatorFileImageMacro(); + $macro->setAuthorPHID($user->getPHID()); } $errors = array(); $e_name = true; - $e_file = pht('Provide a URL or a file'); + $e_file = null; $file = null; $can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http'); @@ -97,6 +98,7 @@ final class PhabricatorMacroEditController if (!$macro->getID() && !$file) { $errors[] = pht('You must upload an image to create a macro.'); + $e_file = pht('Required'); } if (!$errors) { @@ -242,12 +244,11 @@ final class PhabricatorMacroEditController ->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 AphrontFormTextControl()) + ->setLabel(pht('URL')) + ->setName('url') + ->setValue($request->getStr('url'))); } $upload_form diff --git a/src/applications/macro/controller/PhabricatorMacroListController.php b/src/applications/macro/controller/PhabricatorMacroListController.php index be6c82b439..f7889d397e 100644 --- a/src/applications/macro/controller/PhabricatorMacroListController.php +++ b/src/applications/macro/controller/PhabricatorMacroListController.php @@ -56,10 +56,7 @@ final class PhabricatorMacroListController $author_phids = array(); } - $files = mpull($macros, 'getFile'); - if ($files) { - $author_phids += mpull($files, 'getAuthorPHID', 'getAuthorPHID'); - } + $author_phids += mpull($macros, 'getAuthorPHID', 'getAuthorPHID'); $this->loadHandles($author_phids); $author_handles = array_select_keys($this->getLoadedHandles(), $authors); @@ -102,18 +99,23 @@ final class PhabricatorMacroListController if ($file) { $item->setImageURI($file->getThumb280x210URI()); $item->setImageSize(280, 210); - if ($file->getAuthorPHID()) { - $author_handle = $this->getHandle($file->getAuthorPHID()); - $item->appendChild( - pht('Created by %s', $author_handle->renderLink())); - } - $datetime = phabricator_date($file->getDateCreated(), $viewer); + } + + if ($macro->getDateCreated()) { + $datetime = phabricator_date($macro->getDateCreated(), $viewer); $item->appendChild( phutil_tag( 'div', array(), pht('Created on %s', $datetime))); } + + if ($macro->getAuthorPHID()) { + $author_handle = $this->getHandle($macro->getAuthorPHID()); + $item->appendChild( + pht('Created by %s', $author_handle->renderLink())); + } + $item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/')); $item->setHeader($macro->getName()); diff --git a/src/applications/macro/query/PhabricatorMacroQuery.php b/src/applications/macro/query/PhabricatorMacroQuery.php index c45298feba..894450f5b1 100644 --- a/src/applications/macro/query/PhabricatorMacroQuery.php +++ b/src/applications/macro/query/PhabricatorMacroQuery.php @@ -52,9 +52,8 @@ final class PhabricatorMacroQuery $rows = queryfx_all( $conn, - 'SELECT m.* FROM %T m %Q %Q %Q %Q', + 'SELECT m.* FROM %T m %Q %Q %Q', $macro_table->getTableName(), - $this->buildJoinClause($conn), $this->buildWhereClause($conn), $this->buildOrderClause($conn), $this->buildLimitClause($conn)); @@ -62,20 +61,6 @@ final class PhabricatorMacroQuery return $macro_table->loadAllFromArray($rows); } - protected function buildJoinClause(AphrontDatabaseConnection $conn) { - $joins = array(); - - if ($this->authors) { - $file_table = new PhabricatorFile(); - $joins[] = qsprintf( - $conn, - 'JOIN %T f ON m.filePHID = f.phid', - $file_table->getTableName()); - } - - return implode(' ', $joins); - } - protected function buildWhereClause(AphrontDatabaseConnection $conn) { $where = array(); @@ -96,7 +81,7 @@ final class PhabricatorMacroQuery if ($this->authors) { $where[] = qsprintf( $conn, - 'f.authorPHID IN (%Ls)', + 'm.authorPHID IN (%Ls)', $this->authors); } diff --git a/src/applications/macro/storage/PhabricatorFileImageMacro.php b/src/applications/macro/storage/PhabricatorFileImageMacro.php index 9a46b838d0..e76a7e31a8 100644 --- a/src/applications/macro/storage/PhabricatorFileImageMacro.php +++ b/src/applications/macro/storage/PhabricatorFileImageMacro.php @@ -6,6 +6,7 @@ final class PhabricatorFileImageMacro extends PhabricatorFileDAO PhabricatorApplicationTransactionInterface, PhabricatorPolicyInterface { + protected $authorPHID; protected $filePHID; protected $phid; protected $name; diff --git a/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php b/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php index 859d69eacd..1c8336acec 100644 --- a/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php +++ b/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php @@ -1318,6 +1318,14 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList { 'type' => 'sql', 'name' => $this->getPatchPath('20130524.repoxactions.sql'), ), + '20130529.macroauthor.sql' => array( + 'type' => 'sql', + 'name' => $this->getPatchPath('20130529.macroauthor.sql'), + ), + '20130529.macroauthormig.php' => array( + 'type' => 'php', + 'name' => $this->getPatchPath('20130529.macroauthormig.php'), + ), ); } }