Skip to content

Commit 71a2a24

Browse files
committed
final cleanup, tests passing
1 parent ea9030c commit 71a2a24

File tree

7 files changed

+89
-531
lines changed

7 files changed

+89
-531
lines changed

include/config/custom.inc.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
//
4+
// You can easily configure an override for any config variable here. To start
5+
// we have generated a number of secure random variables for you.
6+
//
7+
Z_CONFIG::$AWS_ACCESS_KEY = 'AKIAZ72BPNKD72SIBXZJ';
8+
Z_CONFIG::$AWS_SECRET_KEY = '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';
9+
10+
// The super username and password are used to communicate between the webapp
11+
// and the dataserver. If you change these values you must also change them in
12+
// your `.env` file.
13+
Z_CONFIG::$API_SUPER_USERNAME = 'YtTnrHcWUC0FqP27xuaa';
14+
Z_CONFIG::$API_SUPER_PASSWORD = 'esFEIngwxnyp1kuTrUpKpH72gEftHbkiWneoeimV';
15+
16+
// By default all of your users have unlimited file storage. To switch back to
17+
// the Zotero default of 300MB per user you can comment out this line.
18+
Z_CONFIG::$STORAGE_DEFAULT_QUOTA = 1000000;
19+
20+
Z_CONFIG::$TRANSLATION_SERVERS = [
21+
"172.16.0.9:1969"
22+
];
23+
24+
Z_CONFIG::$CITATION_SERVERS = array(
25+
"172.16.0.10:8085"
26+
);
27+
28+
Z_CONFIG::$SEARCH_HOSTS = ["172.16.0.12:9200"];
29+
30+
Z_CONFIG::$S3_BUCKET = 'averyuniquebucketname982743';
31+
Z_CONFIG::$S3_BUCKET_FULLTEXT = 'averyuniquebucketname982743';
32+
33+
Z_CONFIG::$TESTING_SITE = true;
34+
Z_CONFIG::$DEV_SITE = true;
35+
36+
Z_CONFIG::$ATTACHMENT_PROXY_URL = "172.16.0.11:3000";
37+
Z_CONFIG::$ATTACHMENT_PROXY_SECRET = "secret";

model/Creator.inc.php

Lines changed: 1 addition & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
class Zotero_Creator {
2828
private $id;
2929
private $libraryID;
30-
private $itemID;
3130
private $firstName = '';
3231
private $lastName = '';
3332
private $shortName = '';
@@ -38,10 +37,9 @@ class Zotero_Creator {
3837

3938

4039

41-
public function __construct($id, $libraryID, $itemID, $firstName, $lastName, $fieldMode, $creatorTypeID, $orderIndex) {
40+
public function __construct($id, $libraryID, $firstName, $lastName, $fieldMode, $creatorTypeID, $orderIndex) {
4241
$this->id = $id;
4342
$this->libraryID = $libraryID;
44-
$this->itemID = $itemID;
4543
$this->firstName = $firstName;
4644
$this->lastName = $lastName;
4745
$this->fieldMode = $fieldMode;
@@ -50,7 +48,6 @@ public function __construct($id, $libraryID, $itemID, $firstName, $lastName, $fi
5048
$this->changed = array();
5149
$props = array(
5250
'libraryID',
53-
'itemID',
5451
'firstName',
5552
'lastName',
5653
'shortName',
@@ -78,7 +75,6 @@ public function __set($field, $value) {
7875
switch ($field) {
7976
case 'id':
8077
case 'libraryID':
81-
case 'itemID':
8278
$this->checkValue($field, $value);
8379
$this->$field = $value;
8480
return;
@@ -104,132 +100,6 @@ public function hasChanged() {
104100
}
105101

106102

107-
public function save($userID=false) {
108-
if (!$this->libraryID) {
109-
trigger_error("Library ID must be set before saving", E_USER_ERROR);
110-
}
111-
112-
Zotero_Creators::editCheck($this, $userID);
113-
114-
// If empty, move on
115-
if ($this->firstName === '' && $this->lastName === '') {
116-
throw new Exception('First and last name are empty');
117-
}
118-
119-
if ($this->fieldMode == 1 && $this->firstName !== '') {
120-
throw new Exception('First name must be empty in single-field mode');
121-
}
122-
123-
if (!$this->hasChanged() && isset($this->id)) {
124-
Z_Core::debug("Creator $this->id has not changed");
125-
return false;
126-
}
127-
128-
Zotero_DB::beginTransaction();
129-
130-
try {
131-
$creatorID = $this->id ? $this->id : Zotero_ID::get('creators');
132-
$isNew = !$this->id;
133-
134-
Z_Core::debug("Saving creator $this->id");
135-
136-
$timestamp = Zotero_DB::getTransactionTimestamp();
137-
138-
$fields = "itemID=?, firstName=?, lastName=?, fieldMode=?, creatorTypeID=?, orderIndex=?";
139-
$params = array(
140-
$this->itemID,
141-
$this->firstName,
142-
$this->lastName,
143-
$this->fieldMode,
144-
$this->creatorTypeID,
145-
$this->orderIndex
146-
);
147-
$shardID = Zotero_Shards::getByLibraryID($this->libraryID);
148-
149-
try {
150-
if ($isNew) {
151-
$sql = "INSERT INTO itemCreators SET creatorID=?, $fields";
152-
$stmt = Zotero_DB::getStatement($sql, true, $shardID);
153-
Zotero_DB::queryFromStatement($stmt, array_merge(array($creatorID), $params));
154-
155-
}
156-
else {
157-
$sql = "UPDATE itemCreators SET $fields WHERE creatorID=?";
158-
$stmt = Zotero_DB::getStatement($sql, true, $shardID);
159-
Zotero_DB::queryFromStatement($stmt, array_merge($params, array($creatorID)));
160-
}
161-
}
162-
catch (Exception $e) {
163-
if (strpos($e->getMessage(), " too long") !== false) {
164-
if (strlen($this->firstName) > 255) {
165-
$name = $this->firstName;
166-
}
167-
else if (strlen($this->lastName) > 255) {
168-
$name = $this->lastName;
169-
}
170-
else {
171-
throw $e;
172-
}
173-
$name = mb_substr($name, 0, 50);
174-
throw new Exception(
175-
"=Creator value '{$name}…' too long",
176-
Z_ERROR_CREATOR_TOO_LONG
177-
);
178-
}
179-
180-
throw $e;
181-
}
182-
183-
// The client updates the mod time of associated items here, but
184-
// we don't, because either A) this is from syncing, where appropriate
185-
// mod times come from the client or B) the change is made through
186-
// $item->setCreator(), which updates the mod time.
187-
//
188-
// If the server started to make other independent creator changes,
189-
// linked items would need to be updated.
190-
191-
Zotero_DB::commit();
192-
193-
}
194-
catch (Exception $e) {
195-
Zotero_DB::rollback();
196-
throw ($e);
197-
}
198-
199-
// If successful, set values in object
200-
if (!$this->id) {
201-
$this->id = $creatorID;
202-
}
203-
204-
205-
if ($isNew) {
206-
Zotero_Creators::cache($this);
207-
}
208-
209-
// TODO: invalidate memcache?
210-
}
211-
212-
213-
public function getLinkedItems() {
214-
if (!$this->id) {
215-
return array();
216-
}
217-
218-
$items = array();
219-
$sql = "SELECT itemID FROM itemCreators WHERE creatorID=?";
220-
$itemIDs = Zotero_DB::columnQuery(
221-
$sql,
222-
$this->id,
223-
Zotero_Shards::getByLibraryID($this->libraryID)
224-
);
225-
if (!$itemIDs) {
226-
return $items;
227-
}
228-
foreach ($itemIDs as $itemID) {
229-
$items[] = Zotero_Items::get($this->libraryID, $itemID);
230-
}
231-
return $items;
232-
}
233103

234104

235105
public function equals($creator) {

model/Creators.inc.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,26 @@ public static function idsDoNotExist($libraryID, $creators) {
5252
}, $result);
5353
return array_diff($creatorIDs, $existingIDs);
5454
}
55+
56+
public static function bulkDelete($libraryID, $itemID, $creatorOrdersArray) {
57+
$placeholders = implode(', ', array_fill(0, sizeOf($creatorOrdersArray), '?'));
58+
$sql = "DELETE FROM itemCreators WHERE itemID=? AND orderIndex IN ($placeholders)";
59+
Zotero_DB::query($sql, array_merge([$itemID],$creatorOrdersArray), Zotero_Shards::getByLibraryID($libraryID));
60+
}
5561

56-
public static function bulkInsert($libraryID, $orderedCreators) {
62+
public static function bulkInsert($libraryID, $itemID, $creators) {
5763
$placeholdersArray = array();
5864
$paramList = array();
59-
foreach ($orderedCreators as $order => $creator) {
60-
if (isset($creator->id)) {
65+
foreach ($creators as $creator) {
66+
$creatorID = $creator->id;
67+
if (isset($creatorID)) {
6168
throw new Exception("Insert not possible for creator with a set creatorID");
6269
}
6370
$creator->id = Zotero_ID::get('creators');
6471
$placeholdersArray[] = "(?, ?, ?, ?, ?, ?, ?)";
6572
$paramList = array_merge($paramList, [
6673
$creator->id,
67-
$creator->itemID,
74+
$itemID,
6875
$creator->firstName,
6976
$creator->lastName,
7077
$creator->fieldMode,
@@ -77,7 +84,6 @@ public static function bulkInsert($libraryID, $orderedCreators) {
7784

7885
$stmt = Zotero_DB::getStatement($sql, true, Zotero_Shards::getByLibraryID($libraryID));
7986
Zotero_DB::queryFromStatement($stmt, $paramList);
80-
return $orderedCreators;
8187
}
8288

8389

model/Item.inc.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,16 +1223,13 @@ public function save($userID=false) {
12231223
if (!empty($this->changed['creators'])) {
12241224
$indexes = array_keys($this->changed['creators']);
12251225

1226-
// TODO: group queries
1227-
12281226
$creatorsArray = [];
12291227
foreach ($indexes as $orderIndex) {
12301228
$creator = $this->getCreator($orderIndex);
1231-
$creator->itemID = $itemID;
12321229
$creatorsArray[] = $creator;
12331230
}
12341231

1235-
Zotero_Creators::bulkInsert($this->libraryID, $creatorsArray);
1232+
Zotero_Creators::bulkInsert($this->libraryID, $itemID, $creatorsArray);
12361233

12371234
}
12381235

@@ -1527,10 +1524,7 @@ public function save($userID=false) {
15271524
if ($this->isEmbeddedImageAttachment()) {
15281525
throw new Exception("Embedded image attachments cannot have tags");
15291526
}
1530-
foreach ($this->tags as $tag) {
1531-
$tag->itemID = $itemID;
1532-
}
1533-
Zotero_Tags::bulkInsert($this->libraryID, $this->tags);
1527+
Zotero_Tags::bulkInsert($this->libraryID, $itemID, $this->tags);
15341528
}
15351529

15361530
// Related items
@@ -1708,26 +1702,14 @@ public function save($userID=false) {
17081702
//
17091703
if (!empty($this->changed['creators'])) {
17101704
$indexes = array_keys($this->changed['creators']);
1711-
1705+
1706+
$toAdd = [];
17121707
foreach ($indexes as $orderIndex) {
1713-
Z_Core::debug('Creator in position ' . $orderIndex . ' has changed', 4);
17141708
$creator = $this->getCreator($orderIndex);
1715-
1716-
// TODO: can do one update instead of delete and save()
1717-
$sql2 = 'DELETE FROM itemCreators WHERE itemID=? AND orderIndex=?';
1718-
Zotero_DB::query($sql2, array($this->_id, $orderIndex), $shardID);
1719-
1720-
if (!$creator) {
1721-
continue;
1722-
}
1723-
1724-
if ($creator->hasChanged() || !isset($creator->id)) {
1725-
$creator->itemID = $this->_id;
1726-
Z_Core::debug("Auto-saving changed creator {$creator->id}");
1727-
$creator->save();
1728-
}
1729-
1709+
$toAdd[] = $creator;
17301710
}
1711+
Zotero_Creators::bulkDelete($this->libraryID, $this->_id, $indexes);
1712+
Zotero_Creators::bulkInsert($this->libraryID, $this->_id, $toAdd);
17311713

17321714
}
17331715

@@ -2162,10 +2144,7 @@ public function save($userID=false) {
21622144
$toAdd = array_udiff($newTags, $oldTags, $cmp);
21632145
$toRemove = array_udiff($oldTags, $newTags, $cmp);
21642146

2165-
foreach ($toAdd as $tag) {
2166-
$tag->itemID = $this->_id;
2167-
}
2168-
Zotero_Tags::bulkInsert($this->_libraryID, $toAdd);
2147+
Zotero_Tags::bulkInsert($this->_libraryID, $this->_id, $toAdd);
21692148
Zotero_Tags::bulkDelete($this->_libraryID, $this->_id, $toRemove);
21702149
}
21712150

@@ -3676,7 +3655,7 @@ public function setTags($newTags) {
36763655
continue;
36773656
}
36783657
$version = Zotero_Libraries::getUpdatedVersion($this->libraryID);
3679-
$this->tags[] = new Zotero_Tag(null, $this->libraryID, null, $name, $type, $version);
3658+
$this->tags[] = new Zotero_Tag(null, $this->libraryID, $name, $type, $version);
36803659
$this->changed['tags'] = true;
36813660
}
36823661
$toRemove = array_diff($existingTagNames, $foundNames);
@@ -4685,7 +4664,7 @@ protected function loadCreators($reload = false) {
46854664
}
46864665

46874666
foreach ($creators as $creator) {
4688-
$creatorObj = new Zotero_Creator($creator['creatorID'], $this->_libraryID, null, $creator['firstName'], $creator['lastName'], $creator['fieldMode'], $creator['creatorTypeID'], $creator['orderIndex']);
4667+
$creatorObj = new Zotero_Creator($creator['creatorID'], $this->_libraryID, $creator['firstName'], $creator['lastName'], $creator['fieldMode'], $creator['creatorTypeID'], $creator['orderIndex']);
46894668

46904669
$this->creators[$creator['orderIndex']] = $creatorObj;
46914670
}
@@ -4731,7 +4710,7 @@ protected function loadTags($reload = false) {
47314710
$this->tags = [];
47324711
if ($tags) {
47334712
foreach ($tags as $tag) {
4734-
$this->tags[] = new Zotero_Tag($tag['tagID'], $this->libraryID, $tag['itemID'], $tag['name'], $tag['type'], $tag['version']);
4713+
$this->tags[] = new Zotero_Tag($tag['tagID'], $this->libraryID, $tag['name'], $tag['type'], $tag['version']);
47354714
}
47364715
}
47374716
$this->loaded['tags'] = true;

model/Items.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ public static function updateFromJSON(Zotero_Item $item,
17191719
$newCreatorTypeID = Zotero_CreatorTypes::getID($newCreatorData->creatorType);
17201720

17211721
// Make creator object
1722-
$newCreator = new Zotero_Creator(null, $item->libraryID, null, $newCreatorData->firstName, $newCreatorData->lastName, $newCreatorData->fieldMode, $newCreatorTypeID, $orderIndex);
1722+
$newCreator = new Zotero_Creator(null, $item->libraryID, $newCreatorData->firstName, $newCreatorData->lastName, $newCreatorData->fieldMode, $newCreatorTypeID, $orderIndex);
17231723
$item->setCreator($orderIndex, $newCreator);
17241724
}
17251725

0 commit comments

Comments
 (0)