trigger-versioning.xqm 4.8 KB
Newer Older
Thomas Friese's avatar
Thomas Friese committed
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
xquery version "3.1";

(:~
 : A trigger to keep hsa-data in sync with GitLab
 :)

module namespace trigger="http://exist-db.org/xquery/trigger";

import module namespace git="http://exist-db.org/apps/git/git" at "/db/apps/git/modules/git.xqm";
import module namespace config="http://lgpn.classics.ox.ac.uk/apps/lgpn-ling-data/config" at "/db/apps/lgpn-ling-data/modules/config.xqm";
(:~
 : This function is called if a new document within hsa-data is created
 : it case a xml document is created, it gets pushed to GitLab
 :
 : @param $url the db url of the created document
 :)

declare function trigger:after-create-document($uri as xs:anyURI) {
    let $log := util:log("info", "xml trigger:after-create-document: " || $uri)
    return
        if(contains($uri, $config:data-root))
        then (
            util:log("info", "trigger:after-create-document: XML"),
            system:as-user(
                $config:admin-user-name, $config:admin-user-pass,
                git:push(
                    $config:name,
                    $config:email,
                    "after-create-document: " || $uri,
                    $config:file-repo-root,
                    $config:app-root,
                    $config:data-col
                )
            )
        )
        else()
};

(:~
 : This function is called if a document within hsa-data was updated / changed
 : it case a xml document was changed, it gets pushed to GitLab
 :
 : @param $url the db url of the changed document
 :)
declare function trigger:after-update-document($uri as xs:anyURI) {
    let $log := util:log("info", "xml trigger:after-update-document: " || $uri)
    return
        if(contains($uri, $config:data-root))
        then (
            util:log("info", "trigger:after-update-document: XML"),
            system:as-user(
                $config:admin-user-name, $config:admin-user-pass,
                git:push(
                    $config:name,
                    $config:email,
                    "after-update-document: " || $uri,
                    $config:file-repo-root,
                    $config:app-root,
                    $config:data-col
                )
            )
        )
        else()
};


declare function trigger:after-move-document($new-uri as xs:anyURI, $uri as xs:anyURI) {
    let $log := util:log("info", "xml trigger:after-move-document: " || $new-uri)
    return
        if(contains($new-uri, $config:data-root))
        then (
            util:log("info", "trigger:after-move-document: XML"),
            system:as-user(
                $config:admin-user-name, $config:admin-user-pass,
                git:push(
                    $config:name,
                    $config:email,
                    "after-move-document: " || $uri,
                    $config:file-repo-root,
                    $config:app-root,
                    $config:data-col
                )
            )
        )
        else()
};

declare function trigger:after-copy-document($new-uri as xs:anyURI, $uri as xs:anyURI) {
    let $log := util:log("info", "xml trigger:after-copy-document: " || $new-uri)
    return
        if(contains($new-uri, $config:data-root))
        then (
            util:log("info", "trigger:after-copy-document: XML"),
            system:as-user(
                $config:admin-user-name, $config:admin-user-pass,
                git:push(
                    $config:name,
                    $config:email,
                    "after-copy-document: " || $uri,
                    $config:file-repo-root,
                    $config:app-root,
                    $config:data-col
                )
            )
        )
        else()
};


(:~
 : This function is called if a document within the collection was deleted
 : it case a document was deleted, it gets deleted at the hard disk as well
 : :
 : @param $url the db url of the deleted document
 :)
declare function trigger:after-delete-document($uri as xs:anyURI) {
    let $log := util:log("info", "trigger:after-delete-document: " || $uri)
    let $uri-elements := tokenize( $uri, "/" )
    let $path-elements := subsequence( $uri-elements, 6, count( $uri-elements ))
    let $relative-uri := string-join( $path-elements, "/" )
    return
        if(contains($uri, $config:data-root))
        then (
            util:log("info", "trigger:after-delete-document: XML)"),
            git:delete-file( $config:file-repo-root,
                $config:app-root, $config:data-col, $relative-uri ),
            system:as-user(
                $config:admin-user-name, $config:admin-user-pass,
                git:push(
                    $config:name,
                    $config:email,
                    "after-delete-document: " || $uri,
                    $config:file-repo-root,
                    $config:app-root,
                    $config:data-col
                )
            )
        )
        else()
};