forked from zotero/zotero
/
collections.js
351 lines (287 loc) · 9.49 KB
/
collections.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2009 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
/*
* Primary interface for accessing Zotero collection
*/
Zotero.Collections = function() {
this.constructor = null;
this._ZDO_object = 'collection';
this._primaryDataSQLParts = {
collectionID: "O.collectionID",
name: "O.collectionName AS name",
libraryID: "O.libraryID",
key: "O.key",
version: "O.version",
synced: "O.synced",
parentID: "O.parentCollectionID AS parentID",
parentKey: "CP.key AS parentKey",
hasChildCollections: "(SELECT COUNT(*) FROM collections WHERE "
+ "parentCollectionID=O.collectionID) != 0 AS hasChildCollections",
hasChildItems: "(SELECT COUNT(*) FROM collectionItems WHERE "
+ "collectionID=O.collectionID) != 0 AS hasChildItems"
};
this._primaryDataSQLFrom = "FROM collections O "
+ "LEFT JOIN collections CP ON (O.parentCollectionID=CP.collectionID)";
this._relationsTable = "collectionRelations";
/**
* Get collections within a library
*
* Either libraryID or parentID must be provided
*
* @param {Integer} libraryID
* @param {Boolean} [recursive=false]
* @return {Zotero.Collection[]}
*/
this.getByLibrary = function (libraryID, recursive) {
return _getByContainer(libraryID, null, recursive);
}
/**
* Get collections that are subcollection of a given collection
*
* @param {Integer} parentCollectionID
* @param {Boolean} [recursive=false]
* @return {Zotero.Collection[]}
*/
this.getByParent = function (parentCollectionID, recursive) {
return _getByContainer(null, parentCollectionID, recursive);
}
var _getByContainer = function (libraryID, parentID, recursive) {
let children = [];
if (parentID) {
let parent = Zotero.Collections.get(parentID);
children = parent.getChildCollections();
} else if (libraryID) {
for (let id in this._objectCache) {
let c = this._objectCache[id];
if (c.libraryID == libraryID && !c.parentKey) {
c.level = 0;
children.push(c);
}
}
} else {
throw new Error("Either library ID or parent collection ID must be provided");
}
if (!children.length) {
return children;
}
// Do proper collation sort
children.sort(function (a, b) Zotero.localeCompare(a.name, b.name));
if (!recursive) return children;
let toReturn = [];
for (var i=0, len=children.length; i<len; i++) {
var obj = children[i];
toReturn.push(obj);
var descendants = obj.getDescendents(false, 'collection');
for (let d of descendants) {
var obj2 = this.get(d.id);
if (!obj2) {
throw new Error('Collection ' + d.id + ' not found');
}
// TODO: This is a quick hack so that we can indent subcollections
// in the search dialog -- ideally collections would have a
// getLevel() method, but there's no particularly quick way
// of calculating that without either storing it in the DB or
// changing the schema to Modified Preorder Tree Traversal,
// and I don't know if we'll actually need it anywhere else.
obj2.level = d.level;
toReturn.push(obj2);
}
}
return toReturn;
}.bind(this);
this.getCollectionsContainingItems = function (itemIDs, asIDs) {
// If an unreasonable number of items, don't try
if (itemIDs.length > 100) {
return Zotero.Promise.resolve([]);
}
var sql = "SELECT collectionID FROM collections WHERE ";
var sqlParams = [];
for (let id of itemIDs) {
sql += "collectionID IN (SELECT collectionID FROM collectionItems "
+ "WHERE itemID=?) AND "
sqlParams.push(id);
}
sql = sql.substring(0, sql.length - 5);
return Zotero.DB.columnQueryAsync(sql, sqlParams)
.then(collectionIDs => {
return asIDs ? collectionIDs : this.get(collectionIDs);
});
}
/**
* Sort an array of collectionIDs from top-level to deepest
*
* Order within each level is undefined.
*
* This is used to sort higher-level collections first in upload JSON, since otherwise the API
* would reject lower-level collections for having missing parents.
*/
this.sortByLevel = function (ids) {
let levels = {};
// Get objects from ids
let objs = {};
ids.forEach(id => objs[id] = Zotero.Collections.get(id));
// Get top-level collections
let top = ids.filter(id => !objs[id].parentID);
levels["0"] = top.slice();
ids = Zotero.Utilities.arrayDiff(ids, top);
// For each collection in list, walk up its parent tree. If a parent is present in the
// list of ids, add it to the appropriate level bucket and remove it.
while (ids.length) {
let tree = [ids[0]];
let keep = [ids[0]];
let id = ids.shift();
while (true) {
let c = Zotero.Collections.get(id);
let parentID = c.parentID;
if (!parentID) {
break;
}
tree.push(parentID);
// If parent is in list, remove it
let pos = ids.indexOf(parentID);
if (pos != -1) {
keep.push(parentID);
ids.splice(pos, 1);
}
id = parentID;
}
let level = tree.length - 1;
for (let i = 0; i < tree.length; i++) {
let currentLevel = level - i;
for (let j = 0; j < keep.length; j++) {
if (tree[i] != keep[j]) continue;
if (!levels[currentLevel]) {
levels[currentLevel] = [];
}
levels[currentLevel].push(keep[j]);
}
}
}
var orderedIDs = [];
for (let level in levels) {
orderedIDs = orderedIDs.concat(levels[level]);
}
return orderedIDs;
};
this._loadChildCollections = Zotero.Promise.coroutine(function* (libraryID, ids, idSQL) {
var sql = "SELECT C1.collectionID, C2.collectionID AS childCollectionID "
+ "FROM collections C1 LEFT JOIN collections C2 ON (C1.collectionID=C2.parentCollectionID) "
+ "WHERE C1.libraryID=?"
+ (ids.length ? " AND C1.collectionID IN (" + ids.map(id => parseInt(id)).join(", ") + ")" : "");
var params = [libraryID];
var lastID;
var rows = [];
var setRows = function (collectionID, rows) {
var collection = this._objectCache[collectionID];
if (!collection) {
throw new Error("Collection " + collectionID + " not found");
}
collection._childCollections = new Set(rows);
collection._loaded.childCollections = true;
collection._clearChanged('childCollections');
}.bind(this);
yield Zotero.DB.queryAsync(
sql,
params,
{
noCache: ids.length != 1,
onRow: function (row) {
let collectionID = row.getResultByIndex(0);
if (lastID && collectionID !== lastID) {
setRows(lastID, rows);
rows = [];
}
lastID = collectionID;
let childCollectionID = row.getResultByIndex(1);
// No child collections
if (childCollectionID === null) {
return;
}
rows.push(childCollectionID);
}
}
);
if (lastID) {
setRows(lastID, rows);
}
});
this._loadChildItems = Zotero.Promise.coroutine(function* (libraryID, ids, idSQL) {
var sql = "SELECT collectionID, itemID FROM collections "
+ "LEFT JOIN collectionItems USING (collectionID) "
+ "WHERE libraryID=?" + idSQL;
var params = [libraryID];
var lastID;
var rows = [];
var setRows = function (collectionID, rows) {
var collection = this._objectCache[collectionID];
if (!collection) {
throw new Error("Collection " + collectionID + " not found");
}
collection._childItems = new Set(rows);
collection._loaded.childItems = true;
collection._clearChanged('childItems');
}.bind(this);
yield Zotero.DB.queryAsync(
sql,
params,
{
noCache: ids.length != 1,
onRow: function (row) {
let collectionID = row.getResultByIndex(0);
if (lastID && collectionID !== lastID) {
setRows(lastID, rows);
rows = [];
}
lastID = collectionID;
let itemID = row.getResultByIndex(1);
// No child items
if (itemID === null) {
return;
}
rows.push(itemID);
}
}
);
if (lastID) {
setRows(lastID, rows);
}
});
this.registerChildCollection = function (collectionID, childCollectionID) {
if (this._objectCache[collectionID]) {
this._objectCache[collectionID]._registerChildCollection(childCollectionID);
}
}
this.unregisterChildCollection = function (collectionID, childCollectionID) {
if (this._objectCache[collectionID]) {
this._objectCache[collectionID]._unregisterChildCollection(childCollectionID);
}
}
this.registerChildItem = function (collectionID, itemID) {
if (this._objectCache[collectionID]) {
this._objectCache[collectionID]._registerChildItem(itemID);
}
}
this.unregisterChildItem = function (collectionID, itemID) {
if (this._objectCache[collectionID]) {
this._objectCache[collectionID]._unregisterChildItem(itemID);
}
}
Zotero.DataObjects.call(this);
return this;
}.bind(Object.create(Zotero.DataObjects.prototype))();