Skip to content

Commit d63811d

Browse files
author
epriestley
committedMay 29, 2013
Store authors on image macros
Summary: Currently, the author of an image macro is read from the attached file. This is messy and necessitates a join, and is not always correct. Instead, store the data when the macro is created. This lays the groundwork for generalizing ApplicationSearch here. Ref T2625. Test Plan: Migrated existing macros, created a new macro, checked web UI. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2625 Differential Revision: https://secure.phabricator.com/D6071
1 parent 73bd2e9 commit d63811d

File tree

7 files changed

+72
-34
lines changed

7 files changed

+72
-34
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE {$NAMESPACE}_file.file_imagemacro
2+
ADD authorPHID VARCHAR(64) COLLATE utf8_bin AFTER phid;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
echo "Migrating macro authors...\n";
4+
foreach (new LiskMigrationIterator(new PhabricatorFileImageMacro()) as $macro) {
5+
echo "Macro #".$macro->getID()."\n";
6+
7+
if ($macro->getAuthorPHID()) {
8+
// Already have an author; skip it.
9+
continue;
10+
}
11+
12+
if (!$macro->getFilePHID()) {
13+
// No valid file; skip it.
14+
continue;
15+
}
16+
17+
$file = id(new PhabricatorFile())->loadOneWhere(
18+
'phid = %s',
19+
$macro->getFilePHID());
20+
21+
if (!$file) {
22+
// Couldn't load the file; skip it.
23+
continue;
24+
}
25+
26+
if (!$file->getAuthorPHID()) {
27+
// File has no author; skip it.
28+
continue;
29+
}
30+
31+
queryfx(
32+
$macro->establishConnection('w'),
33+
'UPDATE %T SET authorPHID = %s WHERE id = %d',
34+
$macro->getTableName(),
35+
$file->getAuthorPHID(),
36+
$macro->getID());
37+
}
38+
39+
echo "Done.\n";

‎src/applications/macro/controller/PhabricatorMacroEditController.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ public function processRequest() {
2929
}
3030
} else {
3131
$macro = new PhabricatorFileImageMacro();
32+
$macro->setAuthorPHID($user->getPHID());
3233
}
3334

3435
$errors = array();
3536
$e_name = true;
36-
$e_file = pht('Provide a URL or a file');
37+
$e_file = null;
3738
$file = null;
3839
$can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http');
3940

@@ -97,6 +98,7 @@ public function processRequest() {
9798

9899
if (!$macro->getID() && !$file) {
99100
$errors[] = pht('You must upload an image to create a macro.');
101+
$e_file = pht('Required');
100102
}
101103

102104
if (!$errors) {
@@ -242,12 +244,11 @@ public function processRequest() {
242244
->setUser($request->getUser());
243245

244246
if ($can_fetch) {
245-
$upload_form
246-
->appendChild(
247-
id(new AphrontFormTextControl())
248-
->setLabel(pht('URL'))
249-
->setName('url')
250-
->setValue($request->getStr('url')));
247+
$upload_form->appendChild(
248+
id(new AphrontFormTextControl())
249+
->setLabel(pht('URL'))
250+
->setName('url')
251+
->setValue($request->getStr('url')));
251252
}
252253

253254
$upload_form

‎src/applications/macro/controller/PhabricatorMacroListController.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public function processRequest() {
5656
$author_phids = array();
5757
}
5858

59-
$files = mpull($macros, 'getFile');
60-
if ($files) {
61-
$author_phids += mpull($files, 'getAuthorPHID', 'getAuthorPHID');
62-
}
59+
$author_phids += mpull($macros, 'getAuthorPHID', 'getAuthorPHID');
6360

6461
$this->loadHandles($author_phids);
6562
$author_handles = array_select_keys($this->getLoadedHandles(), $authors);
@@ -102,18 +99,23 @@ public function processRequest() {
10299
if ($file) {
103100
$item->setImageURI($file->getThumb280x210URI());
104101
$item->setImageSize(280, 210);
105-
if ($file->getAuthorPHID()) {
106-
$author_handle = $this->getHandle($file->getAuthorPHID());
107-
$item->appendChild(
108-
pht('Created by %s', $author_handle->renderLink()));
109-
}
110-
$datetime = phabricator_date($file->getDateCreated(), $viewer);
102+
}
103+
104+
if ($macro->getDateCreated()) {
105+
$datetime = phabricator_date($macro->getDateCreated(), $viewer);
111106
$item->appendChild(
112107
phutil_tag(
113108
'div',
114109
array(),
115110
pht('Created on %s', $datetime)));
116111
}
112+
113+
if ($macro->getAuthorPHID()) {
114+
$author_handle = $this->getHandle($macro->getAuthorPHID());
115+
$item->appendChild(
116+
pht('Created by %s', $author_handle->renderLink()));
117+
}
118+
117119
$item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/'));
118120
$item->setHeader($macro->getName());
119121

‎src/applications/macro/query/PhabricatorMacroQuery.php

+2-17
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,15 @@ protected function loadPage() {
5252

5353
$rows = queryfx_all(
5454
$conn,
55-
'SELECT m.* FROM %T m %Q %Q %Q %Q',
55+
'SELECT m.* FROM %T m %Q %Q %Q',
5656
$macro_table->getTableName(),
57-
$this->buildJoinClause($conn),
5857
$this->buildWhereClause($conn),
5958
$this->buildOrderClause($conn),
6059
$this->buildLimitClause($conn));
6160

6261
return $macro_table->loadAllFromArray($rows);
6362
}
6463

65-
protected function buildJoinClause(AphrontDatabaseConnection $conn) {
66-
$joins = array();
67-
68-
if ($this->authors) {
69-
$file_table = new PhabricatorFile();
70-
$joins[] = qsprintf(
71-
$conn,
72-
'JOIN %T f ON m.filePHID = f.phid',
73-
$file_table->getTableName());
74-
}
75-
76-
return implode(' ', $joins);
77-
}
78-
7964
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
8065
$where = array();
8166

@@ -96,7 +81,7 @@ protected function buildWhereClause(AphrontDatabaseConnection $conn) {
9681
if ($this->authors) {
9782
$where[] = qsprintf(
9883
$conn,
99-
'f.authorPHID IN (%Ls)',
84+
'm.authorPHID IN (%Ls)',
10085
$this->authors);
10186
}
10287

‎src/applications/macro/storage/PhabricatorFileImageMacro.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class PhabricatorFileImageMacro extends PhabricatorFileDAO
66
PhabricatorApplicationTransactionInterface,
77
PhabricatorPolicyInterface {
88

9+
protected $authorPHID;
910
protected $filePHID;
1011
protected $phid;
1112
protected $name;

‎src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php

+8
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,14 @@ public function getPatches() {
13181318
'type' => 'sql',
13191319
'name' => $this->getPatchPath('20130524.repoxactions.sql'),
13201320
),
1321+
'20130529.macroauthor.sql' => array(
1322+
'type' => 'sql',
1323+
'name' => $this->getPatchPath('20130529.macroauthor.sql'),
1324+
),
1325+
'20130529.macroauthormig.php' => array(
1326+
'type' => 'php',
1327+
'name' => $this->getPatchPath('20130529.macroauthormig.php'),
1328+
),
13211329
);
13221330
}
13231331
}

0 commit comments

Comments
 (0)
Failed to load comments.