The following demo contains xquery syntax highlighting support for Google Prettify. If you have any questions or suggestions please feel free to send me a message.
(: Took some of Mike Brevoort s xquery code samples because they are nice and show common xquery syntax :)
(:~ : Given a sequence of version URIs, publish all of these versions of each document : If there is a version of the same document already published, unpublish it 1st : : When "publish" is referred to, we mean that it is put into the PUBLISHED collection : unpublish removes content from this collection : @param $version_uris - sequence of uris of versions of managed documents to publish :) declarefunction comoms-dls:publish($version_urisasitem()*) { for$uriin$version_uris let$doc := fn:doc($uri) let$managed_base_uri := $doc/node()/property::dls:version/dls:document-uri/text() let$existing := comoms-dls:publishedDoc($managed_base_uri) let$unpublishExisting := if($existing) then comoms-dls:unpublishVersion((xdmp:node-uri($existing))) else () let$addPermissions := dls:document-add-permissions($uri, (xdmp:permission('mkp-anon', 'read'))) return dls:document-add-collections($uri, ("PUBLISHED")) };
declarefunction comoms-dls:publishLatest($uri) { (: TODO check if it's in the draft collection probably :)
(:~ : Given a sequence of version URIs, unpublish all of these versions of each document :) declarefunction comoms-dls:unpublishVersion($version_urisasitem()*) { for$uriin$version_uris return let$removePermissions := dls:document-remove-permissions($uri, (xdmp:permission('mkp-anon', 'read'))) returndls:document-remove-collections($uri, ("PUBLISHED")) };
(:~ : Given the base URI of a managed piece of content, return the document of the node : of the version that is published :) declarefunction comoms-dls:publishedDoc($uri) { fn:collection("PUBLISHED")[property::dls:version/dls:document-uri = $uri] };
(:~ : Test if any version of the managed document is published :) declarefunction comoms-dls:isPublished($uri) { if( comoms-dls:publishedDoc($uri)) then fn:true() else fn:false() };
declarefunction comoms-dls:publishedState($uri) { let$doc := comoms-dls:publishedDoc($uri) let$published_uri := if($doc) thenxdmp:node-uri($doc) else () let$latest := comoms-dls:latestVersionUri($uri) return if($doc) then if($latest ne $published_uri) then "stale" else "published" else "unpublished" };
(:~ : Given a manage content url (e.g. /content/123456.xml) return the appropriate : version of the document based on what stage collection is being viewed and : what's published : : @param $uri a manage content url (e.g. /content/123456.xml) - NOT A VERSIONED URI :) declarefunction comoms-dls:doc($uri) { let$doc := fn:root(comoms-dls:collection()[property::dls:version/dls:document-uri = $uri][1]) return if($doc) then $doc else let$managedDocInCollection := comoms-dls:collection-name() = xdmp:document-get-collections($uri) return if($managedDocInCollection) then fn:doc($uri) else () };
(:~ : Get the collection to be used when querying for content : THIS or comoms-dls:collection-name() SHOULD BE USED WHEN BUILDING ANY QUERY FOR MANAGED CONTENT :) declarefunction comoms-dls:collection() { fn:collection( comoms-dls:collection-name() ) };
(:~ : Get the collection nameto be used when querying for content : THIS or comoms-dls:collection() SHOULD BE USED WHEN BUILDING ANY QUERY FOR MANAGED CONTENT :) declarefunction comoms-dls:collection-name() asxs:string { let$default_collection := "PUBLISHED" return if(comoms-user:isAdmin()) then let$pub_stage_collection_cookie := comoms-util:getCookie("COMOMS_COLLECTION") return if($pub_stage_collection_cookie) then $pub_stage_collection_cookie else $default_collection else $default_collection };
(:~ : Check if the published collection is being viewed :) declarefunction comoms-dls:isViewingPublished() { if(comoms-dls:collection-name() = "PUBLISHED") then fn:true() else fn:false() };
(:~ : Get the best URL for the content URI. : This is either the default URI based on detail type or should also take : into account friendly urls and navigation structures to figure out the : best choice :) declarefunction comoms-dls:contentUrl($uri) {
(: TODO: add friendly URL and nav structure logic 1st :)