Skip to content

Commit f1ad06e

Browse files
committed
Use file or project index instead of collection() where possible
Cf. #555
1 parent f5c528e commit f1ad06e

File tree

8 files changed

+46
-73
lines changed

8 files changed

+46
-73
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ This framework still lacks a good name. If you have an idea, please let me know!
66

77
## Incompatible changes
88

9+
Release 24Q4 dropped another set of functions that were unused:
10+
- `wdb:getEdFromFileID( $id as xs:string )`: `(wdbFiles:getFullPath($id))?projectPath` returns the base path to `wdbmeta.xml`
11+
- `wdb:getMetaElementFromEd ( $ed as xs:string )`: use `doc( (doc("/db/apps/edoc/index/project-index.xml")/id($ed))/@path || '/wdbmeta.xml' )/id($ed)[self::meta:projectMD]` instead
12+
- `wdb:getMetaFile( $pathToEd )`: `(wdbFiles:getFullPath($ed))?projectPath` returns the base path to `wdbmeta.xml`
13+
914
Release 24Q2 dropped functions `wdb:getEdPath( $ed as xs:string , $absolute as xs:boolean() )`,
1015
`wdb:getEdPath( $ed as xs:string )`, and `wdb:findProjectXQM( $project )`. These queries can be replaced by
1116
`(wdbFiles:getFullPath($id))` which returns a map with `projectPath` (the path to the project, i.e. the collection where
12-
`project.xqm` is stored), `collectionPath` (for the subcollection where a file is actually located), `mainProject` for
17+
`wdbmeta.xml`¹ is stored), `collectionPath` (for the subcollection where a file is actually located), `mainProject` for
1318
the path to the project containing `project.xqm`, and `fileName`.
19+
1) (24Q4): this corrects a typo as previous versions incorrectly read `project.xqm`.
1420

1521
Release 24Q1 dropped support for METS-based projects. As METS files can have a number of very different ways of encoding
1622
information, especially when it comes to behaviours, native support is hard to achieve. At the same time, most

edoc/modules/app.xqm

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -538,17 +538,6 @@ declare function wdb:getAbsolutePath ( $ed as xs:string, $path as xs:string ) {
538538
else (wdbFiles:getFullPath($ed))?projectPath || "/" || $path
539539
};
540540

541-
(:~
542-
: Return the ID of a project from a resource ID within the project
543-
:
544-
: @param $id the ID of a resource within a project
545-
: @return the ID of the project
546-
:)
547-
declare function wdb:getEdFromFileId ($id as xs:string) as xs:string {
548-
let $file := collection($wdb:data)/id($id)[self::meta:file]
549-
return $file/ancestor::meta:projectMD/@xml:id
550-
};
551-
552541
(: ~
553542
: Return the path to a project
554543
:
@@ -677,27 +666,9 @@ declare function wdb:eval($function as xs:string, $cache-flag as xs:boolean, $ex
677666
: @return The path to the project
678667
:)
679668
declare function wdb:getProjectPathFromId ( $ed as xs:string ) as xs:string {
680-
let $md := collection($wdb:data)/id($ed)[self::meta:projectMD]
681-
682-
return xstring:substring-before-last(base-uri(($md)[1]), '/')
683-
};
684-
685-
(:~
686-
: Get the meta data file from the ed path
687-
:)
688-
declare function wdb:getMetaFile($pathToEd) {
689-
if ( doc-available($pathToEd||'/wdbmeta.xml') )
690-
then $pathToEd || '/wdbmeta.xml'
691-
else fn:error(fn:QName('https://github.com/dariok/wdbErr', 'wdbErr:wdb0003'))
692-
};
693-
694-
(:~
695-
: Get the meta data file by project ID
696-
:
697-
: @param $ed The project ID to be evaluated
698-
:)
699-
declare function wdb:getMetaElementFromEd ( $ed as xs:string ) as element() {
700-
collection($wdb:data)/id($ed)[self::meta:projectMD]
669+
if ( $ed = ( "", "data" ) )
670+
then $wdb:data
671+
else string( (doc("/db/apps/edoc/index/project-index.xml")/id($ed))/@path )
701672
};
702673
(: END GENERAL HELPER FUNCTIONS :)
703674

edoc/modules/wdb-files.xqm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ declare function wdbFiles:getAbsolutePath ( $path as attribute() ) as xs:anyURI
6262
: @throws wdbErr:wdb0001
6363
:)
6464
declare function wdbFiles:getFullPath ( $id as xs:string ) as map( xs:string, xs:string, xs:string?, xs:string? )? {
65-
let $file := collection("/db")/id($id)
65+
let $file-hint := doc("/db/apps/edoc/index/file-index.xml")/id($id)
66+
, $project-hint := doc("/db/apps/edoc/index/project-index.xml")/id($id)
67+
, $file := ( doc($file-hint/@project)/id($id), doc($project-hint/@path || "/wdbmeta.xml")/id($id) )
6668

6769
return if ( count($file) = 0 ) then
6870
error(

edoc/rest/rest-coll.xql

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ function wdbRc:createFile ($data as xs:string*, $collection as xs:string, $heade
181181
then error (QName("https://github.com/dariok/wdbplus/errors", "wdbErr:h400"), "no Content Type declared for file")
182182
else ()
183183

184-
let $collectionFile := collection($wdb:data)/id($collection)[self::meta:projectMD]
184+
let $collectionPath := wdb:getProjectPathFromId($collection)
185+
, $collectionFile := doc($collectionPath || '/wdbmeta.xml')/*[self::meta:projectMD]
185186
let $err := if (not($collectionFile))
186187
then error (QName("https://github.com/dariok/wdbplus/errors", "wdbErr:h400"), "collection " || $collection || " not found", 404)
187188
else ()
188-
189-
let $collectionPath := (wdbFiles:getFullPath($collection))?collectionPath
189+
190190
let $err := if (not(sm:has-access(xs:anyURI($collectionPath), "w")))
191191
then error (QName("https://github.com/dariok/wdbplus/errors", "wdbErr:h400"), "user " || $user || " has no access to write to collection " || $collectionPath, 403)
192192
else ()
@@ -311,14 +311,13 @@ declare
311311
%rest:GET
312312
%rest:path("/edoc/collection/full/{$id}.zip")
313313
%output:method("binary")
314-
function wdb:getResourcesZip ($id as xs:string) {
315-
let $meta := collection($wdb:data)/id($id)[self::meta:projectMD]
316-
let $base := substring-before(base-uri($meta), 'wdbmeta')
314+
function wdb:getResourcesZip ( $ed as xs:string ) {
315+
let $base := wdb:getProjectPathFromId($ed)
317316

318-
return if ($meta = "")
319-
then <rest:response>
320-
<http:response status="404"/>
321-
</rest:response>
317+
return if ( $base = "" ) then
318+
<rest:response>
319+
<http:response status="404"/>
320+
</rest:response>
322321
else (
323322
<rest:response>
324323
<http:response status="200">
@@ -408,9 +407,8 @@ declare
408407
%rest:GET
409408
%rest:path("/edoc/collection/{$ed}/nav.xml")
410409
function wdbRc:getCollectionNavXML ( $ed as xs:string ) {
411-
let $md := collection($wdb:data)/id($ed)[self::meta:projectMD]
412-
, $uri := base-uri($md)
413-
, $struct := $md/meta:struct
410+
let $md := doc(wdb:getProjectPathFromId($collection) || '/wdbmeta.xml')
411+
, $struct := $md//meta:struct
414412

415413
let $content := <struct xmlns="https://github.com/dariok/wdbplus/wdbmeta" ed="{$ed}">{(
416414
$struct/@*,
@@ -510,30 +508,27 @@ function wdbRc:getCollectionNavHTML ( $ed as xs:string, $externalModel as map(*)
510508
)
511509
};
512510

513-
declare function wdbRc:getGeneral ($id, $mt, $content) {
511+
declare function wdbRc:getGeneral ( $ed as xs:string, $mt as xs:string+, $content as xs:string ) as item()+ {
514512
let $wdbRc:acceptable := ("application/json", "application/xml")
515513

516514
let $content := if ( $mt = $wdbRc:acceptable ) then
517515
try {
518-
let $path := wdb:getProjectPathFromId($id)
519-
, $meta := doc(wdb:getMetaFile($path))
516+
let $meta := doc(wdb:getProjectPathFromId($ed) || '/wdbmeta.xml')
520517

521-
return if ( $meta/*[self::meta:projectMD] ) then
522-
let $eval := wdb:eval ( $content, false(), (xs:QName("meta"), $meta))
518+
return if ( count($meta) = 0 )
519+
then
520+
( 404, "no collection with ID " || $ed )
521+
else
522+
let $eval := wdb:eval($content, false(), (xs:QName("meta"), $meta))
523523

524524
return if ( count($eval) gt 0 ) then (
525525
200,
526-
<collection id="{$id}">{
526+
<collection id="{$ed}">{
527527
$eval
528528
}</collection>
529529
)
530530
else
531531
( 204, "" )
532-
else
533-
( 400, "no a wdbmeta project" )
534-
}
535-
catch *:wdb0200 {
536-
( 404, "no collection with ID " || $id )
537532
}
538533
catch * {
539534
( 400, "" )

edoc/rest/rest-entity.xql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ declare
7070
%rest:path("/edoc/entities/scan/{$type}/{$collection}.html")
7171
%rest:query-param("q", "{$q}")
7272
%output:method("html")
73-
function wdbRe:scanHtml ($collection as xs:string, $type as xs:string, $q as xs:string*) {
74-
let $md := collection($wdb:data)//id($collection)[self::meta:projectMD]
75-
, $coll := (wdbFiles:getFullPath($collection))?projectPath
73+
function wdbRe:scanHtml ( $collection as xs:string, $type as xs:string, $q as xs:string* ) as item()+ {
74+
let $coll := (wdbFiles:getFullPath($collection))?projectPath
75+
, $md := doc($coll || '/wdbmeta.xml')/*[self::meta:projectMD]
7676
, $xsl := wdbRCo:getXSLT($coll, 'entity.xsl')
7777

7878
let $params := <parameters>
@@ -124,9 +124,9 @@ declare
124124
%rest:path("/edoc/entities/collection/{$collection}/{$type}/{$ref}.html")
125125
%rest:query-param("start", "{$start}", 1)
126126
%output:method("html")
127-
function wdbRe:collectionEntityHtml ($collection as xs:string*, $type as xs:string*, $ref as xs:string*, $start as xs:int*) {
128-
let $md := collection($wdb:data)//id($collection)[self::meta:projectMD]
129-
, $coll := (wdbFiles:getFullPath($collection))?projectPath
127+
function wdbRe:collectionEntityHtml ( $collection as xs:string*, $type as xs:string*, $ref as xs:string*, $start as xs:int* ) as item()+ {
128+
let $coll := (wdbFiles:getFullPath($collection))?projectPath
129+
, $md := doc($coll || '/wdbmeta.xml')/*[self::meta:projectMD]
130130
, $xsl := wdbRCo:getXSLT($coll, 'entity.xsl')
131131

132132
let $params := <parameters>

edoc/rest/rest-files.xql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ xquery version "3.1";
22

33
module namespace wdbRf = "https://github.com/dariok/wdbplus/RestFiles";
44

5-
import module namespace console = "http://exist-db.org/xquery/console" at "java:org.exist.console.xquery.ConsoleModule";
65
import module namespace json = "http://www.json.org";
76
import module namespace wdb = "https://github.com/dariok/wdbplus/wdb" at "/db/apps/edoc/modules/app.xqm";
87
import module namespace wdbFiles = "https://github.com/dariok/wdbplus/files" at "/db/apps/edoc/modules/wdb-files.xqm";
@@ -188,10 +187,10 @@ declare
188187
function wdbRf:getResource ( $id as xs:string, $modified as xs:string* ) {
189188
(: Admins are advised by the documentation they REALLY SHOULD NOT have more than one entry for every ID
190189
: To be on the safe side, we go for the first one anyway :)
191-
let $files := collection($wdb:data)//id($id)[self::meta:file]
190+
let $file-hint := doc("/db/apps/edoc/index/file-index.xml")/id($id)
191+
, $file := doc($file-hint/@project)/id($id)
192192
, $collectionPath := (wdbFiles:getFullPath($id))?collectionPath
193-
, $f := $files[1]
194-
, $path := $collectionPath || '/' || $f/@path
193+
, $path := $collectionPath || '/' || $file/@path
195194
, $readable := sm:has-access($path, "r")
196195

197196
let $doc := if ( not($readable) ) then
@@ -216,7 +215,7 @@ function wdbRf:getResource ( $id as xs:string, $modified as xs:string* ) {
216215
else
217216
"binary"
218217

219-
let $respCode := if ( count($files) = 0 ) then
218+
let $respCode := if ( count($file) = 0 ) then
220219
404
221220
else if ( not($readable) ) then
222221
401
@@ -460,7 +459,7 @@ declare function wdbRf:processXSL( $id as xs:string, $process as element(), $mod
460459
} catch * {
461460
("error",
462461
$err:description,
463-
console:log("Processing " || $id || ": " || $err:description))
462+
util:log("error", "Processing " || $id || ": " || $err:description))
464463
}
465464

466465
return if ($content[1] = "error")

edoc/rest/rest-search.xql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ function wdbRs:collectionHtml ( $ed as xs:string*, $q as xs:string*, $start as x
8888
"Error: no query content!"
8989
)
9090
else
91-
let $md := collection($wdb:data)//id($ed)[self::meta:projectMD]
92-
, $coll := (wdbFiles:getFullPath($ed))?projectPath
91+
let $coll := wdbFiles:getProjectPathFromId($ed)
92+
, $md := doc($coll || '/wdbmeta.xml')/*[self::meta:projectMD]
9393
, $xsl := wdbRCo:getXSLT($coll, 'search.xsl')
9494

9595
let $params :=

wdbplus.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
}
99
],
1010
"settings": {
11-
"window.title": "${dirty}${activeEditorShort}${separator}${rootName}${separator}${profileName}${separator}${appName}${separator}[Branch: 24Q3]"
11+
"window.title": "${dirty}${activeEditorShort}${separator}${rootName}${separator}${profileName}${separator}${appName}${separator}[Branch: 24Q4]"
1212
}
1313
}

0 commit comments

Comments
 (0)