Last active
April 4, 2017 21:40
-
-
Save romgrk/f8f49206f20bf9a4a1fadb8be106f2e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * tag_metadata_pagecount.js | |
| * | |
| * Set each metadata document 'ExtraData' field to its page count. | |
| */ | |
| extendArray() | |
| /* | |
| * Retrieve the auth_token, then make the REST call to retrieve the content sets. | |
| */ | |
| var token = fetch({ | |
| method: 'POST', | |
| url: 'http://localhost:9340/rest/serverengine/authentication/login', | |
| headers: { | |
| Authorization: 'Basic b2wtYWRtaW46c2VjcmV0' // 'Basic ' + btoa('ol-admin:secret') | |
| } | |
| }) | |
| log(token) | |
| var contentsetId = exp('GetMeta(_vger_contentset_id[0], 10, Job.Group[0])'); | |
| var details = fetch({ | |
| method: 'GET', | |
| url: 'http://localhost:9340/rest/serverengine/entity/contentsets/' + contentsetId + '/pages', | |
| data: { detail: true }, | |
| json: true, | |
| headers: { | |
| auth_token: token | |
| } | |
| }) | |
| log(details) | |
| /* | |
| * Calculate the total number of pages by document, mapped by ID | |
| */ | |
| var pageCountByID = getPageCountByID(details) | |
| function getPageCountByID(details) { | |
| var pageCountByID = {} | |
| details.forEach(function(detail) { | |
| var id = detail.id | |
| var count = detail.pages.reduce(function(prev, cur) { return prev + cur.count }, 0) | |
| pageCountByID[id] = count | |
| }) | |
| return pageCountByID | |
| } | |
| log(pageCountByID) | |
| /* | |
| * Add the page count to the MetaData | |
| */ | |
| tagMetadataPageCount(pageCountByID) | |
| function tagMetadataPageCount(pageCountByID) { | |
| var meta = loadMeta() | |
| var group = metalistToJS(meta.job().group(0)) | |
| var documents = group.map(metanodeToJS) | |
| log(documents) | |
| documents.forEach(function(doc) { | |
| var id = doc._vger_contentitem_id | |
| var count = pageCountByID[id] | |
| doc.__node__.fields.add('_vger_fld_ExtraData', count) | |
| }) | |
| saveMeta(meta) | |
| } | |
| /* | |
| * End of execution. | |
| */ | |
| // Make an HTTP request | |
| function fetch(options) { | |
| var xhr = new ActiveXObject("Microsoft.XMLHTTP") | |
| var url = options.url | |
| var method = options.method || 'GET' | |
| if (options.data && method === 'GET') | |
| url += '?' + queryString(options.data) | |
| xhr.open(method, url, false) | |
| if (options.headers) { | |
| for (var name in options.headers) { | |
| var value = options.headers[name] | |
| xhr.setRequestHeader(name, value) | |
| } | |
| } | |
| if (options.json) | |
| xhr.setRequestHeader('Content-Type', 'application/json') | |
| if (options.data && method === 'POST') | |
| xhr.send(queryString(options.data)) | |
| else | |
| xhr.send() | |
| if (xhr.status != 200) | |
| throw new Error('XMLHTTP Error: ' + xhr.status + ' ' + xhr.responseStatus) | |
| if (options.json) | |
| return JSON.parse(xhr.responseText) | |
| return xhr.responseText | |
| } | |
| // Return base64 encoded text | |
| function btoa(binary) { | |
| var xml = new ActiveXObject("MSXml2.DOMDocument"); | |
| var element = xml.createElement("Base64Data"); | |
| element.dataType = "bin.base64"; | |
| element.nodeTypedValue = typeof(binary) == 'string' ? stringToBinary(binary) : binary; | |
| return element.text; | |
| } | |
| // Return the string as a binary type | |
| function stringToBinary(text, charSet) { | |
| var stream = new ActiveXObject('ADODB.Stream') | |
| stream.type = 2 // adTypeText | |
| stream.charSet = charSet || 'utf-8' | |
| stream.open() | |
| stream.writeText(text) | |
| stream.position = 0 | |
| stream.type = 1 // adTypeBinary | |
| stream.position = 0 | |
| return stream.read() | |
| } | |
| // Encode as application/x-www-form-urlencoded (see HTML specs) | |
| function queryString(params) { | |
| var parts = [] | |
| for (var key in params) { | |
| parts.push(key + '=' + encodeURIComponent(params[key])) | |
| } | |
| return parts.join('&') | |
| } | |
| /* | |
| * MetaData API | |
| */ | |
| function metalistToJS(list) { | |
| var array = []; | |
| for (var i = 0; i < list.count; i++) { | |
| array.push(list.item(i)) | |
| } | |
| return array; | |
| } | |
| function metacollectionToJS(collection) { | |
| var object = {}; | |
| for (var i = 0; i < collection.count; i++) { | |
| var key = collection.name(i) | |
| var value = collection.item(i) | |
| object[key] = value | |
| } | |
| return object; | |
| } | |
| function metanodeToJS(node) { | |
| var object = { | |
| __node__: node | |
| }; | |
| var fields = node.fields; | |
| var attributes = node.attributes; | |
| for (var i = 0; i < fields.count; i++) { | |
| var key = fields.name(i).replace(/_vger_fld_|_vger_record/, '') | |
| var value = fields.item(i) | |
| object[key] = value | |
| } | |
| for (var i = 0; i < attributes.count; i++) { | |
| var key = '_' + attributes.name(i) | |
| var value = attributes.item(i) | |
| object[key] = value | |
| } | |
| return object; | |
| } | |
| function loadMeta() { | |
| var meta = new ActiveXObject('MetadataLib.MetaFile'); | |
| meta.loadFromFile(Watch.getMetadataFilename()); | |
| return meta; | |
| } | |
| function saveMeta(meta) { | |
| meta.saveToFile(Watch.getMetadataFilename()); | |
| } | |
| /* | |
| * Utilities | |
| */ | |
| function get(name) { return Watch.getVariable(name); } | |
| function set(name, value) { Watch.setVariable(name, value); } | |
| function log(msg) { try { Watch.log(toString(msg), 2); } catch (e) { WScript.stdout.WriteLine(toString(msg)) } } | |
| function err(msg) { try { Watch.log(toString(msg), 1); } catch (e) { WScript.stdout.WriteLine(toString(msg)) } } | |
| function exp(string) { return Watch.expandString(string); } | |
| function xml(string) { return Watch.expandString("xmlget('/request[1]/values[1]/" + string + "[1]',Value,KeepCase,No Trim)"); } | |
| function toString(value) { | |
| if (typeof value == 'string') return value; | |
| if (typeof value == 'object') return JSON.stringify(value) | |
| return ''+value | |
| } | |
| /* | |
| * Array | |
| */ | |
| function extendArray() { | |
| Array.from = function(array) { | |
| try { | |
| return Array.prototype.slice.call(array, 0) | |
| } catch(e) { | |
| return map(array, function(v) { return v }) | |
| } | |
| } | |
| function forEach(array, callback) { | |
| for (var i = 0; i < array.length; i++) | |
| callback(array[i], i, array); | |
| } | |
| function map(array, callback) { | |
| var newArray = [] | |
| for (var i = 0; i < array.length; i++) | |
| newArray[i] = callback(array[i], i, array) | |
| return newArray; | |
| } | |
| function reduce(array, callback, accumulator) { | |
| var i = accumulator != undefined ? 0 : 1 | |
| accumulator = accumulator != undefined ? accumulator : array[0] | |
| for (; i < array.length; i++) | |
| accumulator = callback(accumulator, array[i]) | |
| return accumulator; | |
| } | |
| function filter(array, predicate) { | |
| var newArray = [] | |
| for (var i =0; i < array.length; i++) { | |
| if (predicate(array[i], i, array)) | |
| newArray.push(array[i]) | |
| } | |
| return newArray; | |
| } | |
| function some(array, predicate) { | |
| for (var i = 0; i < array.length; i++) { | |
| if (predicate(array[i], i, array)) | |
| return true | |
| } | |
| return false | |
| } | |
| function every(array, predicate) { | |
| for (var i = 0; i < array.length; i++) { | |
| if (!predicate(array[i], i, array)) | |
| return false | |
| } | |
| return true | |
| } | |
| function contains(array, value) { | |
| for (var i = 0; i < array.length; i++) { | |
| if (array[i] === value) | |
| return true | |
| } | |
| return false | |
| } | |
| function last(array) { | |
| return array[array.length - 1] | |
| } | |
| function flatten(array) { | |
| return reduce(array, function(acc, cur){ return acc.concat(cur); }, []) | |
| } | |
| function zip(a, b) { | |
| return map(a, function(e, i) { return [e, b[i]] }) | |
| } | |
| function append(array, other) { | |
| Array.prototype.push.apply(array, other) | |
| return array | |
| } | |
| function prepend(array, other) { | |
| Array.prototype.unshift.apply(array, other) | |
| return array | |
| } | |
| var functions = { | |
| map: map, | |
| reduce: reduce, | |
| filter: filter, | |
| forEach: forEach, | |
| some: some, | |
| every: every, | |
| flatten: flatten, | |
| contains: contains, | |
| append: append, | |
| prepend: prepend, | |
| last: last | |
| } | |
| for (var n in functions) { | |
| (function(name, fn) { | |
| Array.prototype[name] = function() { | |
| var args = Array.prototype.slice.call(arguments, 0) | |
| args.unshift(this) | |
| return fn.apply(this, args) | |
| } | |
| }(n, functions[n])) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment