How do I get and set the content of an asset using the JS API?


(Serge) #1

How do I get and set the content of an asset using the JS API?


(Bart Banda) #2

Assuming you are talking about Standard pages?

If so, to set content, you have to set it on each individual content container:

Using the setAttribute metadata call.
https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#setAttribute

If you want to get the contents of it, you can use the getAttributes function
https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#getAttributes


(Serge) #3

Well I asked for Matrix source code, because apparently one of the claim is that it is open source, and Iā€™m still waiting on Squiz to send it to me or give me a link. So in the meantime Iā€™m fixing the search using the JS API and adding a Replace functionality so I can search / replace in content that contains code (css, js, containers, designs). We have a big job of removing all the legacy code that has a bunch of non-relative URLs and removing http://domain.root/ parts of it so we can use https and use the pages over various development domains (eg: test[1-9].mydomain.com). I know Web Dev should have linked all resources by asset id but they havenā€™t so I have years of legacy code to fix, hence the need for a tool.


(Serge) #4

Set attribute seems to barf here:

Function:

Call (notice all attributes are there):

Result:

ā€¦ asset_id, attr_name and attr_value were provided.

Any pointers?


(Serge) #5

Ok, the value is an array, that is probably the problem. How can I update a design contents then?

Designs have content attributes as arrays

Do I simply stringify the array?

ā€¦ apparently not


(Serge) #6

Wow, that was far fetched but seems like we needed a PHP-style serialisation:

Success:

Modification:

JS Function:

Source: http://locutus.io/php/var/serialize/


(Serge) #7

Well, still no good, JS API said attribute has been successfully set but I cannot see the changes with the _admin interface

Pulling the asset info a second time using the API shows my change but the backend doesnā€™t.

Any pointers? Do I have to lock / update / unlock for the change to appear in the backend? The doc is seriously lacking a a-z working example.


(David Schoen) #8

I donā€™t think you can easily update design parse files via the JS API at the moment.

Internally thereā€™s a real file thatā€™s updated when the asset is edited in _admin, the ā€˜contentsā€™ attribute and compiled (php) design that are generated from that all need to work in concert, it doesnā€™t look like thereā€™s an appropriate call you can make off the JS API to update that design file though.

If you really need to do this, Iā€™d try exporting a design file to XML, templating out the bits you need and then using https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#importAssetsFromXML

Iā€™ll pass this around and see if thereā€™s a simpler solution though.


(Serge) #9

Can you give me a working example of this? There is virtually nothing there https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#importAssetsFromXML

For instance, how do I get the path of the design file? I need a full working example of a design update if this is at all possible. If not, dev guys you should fix the api: if(asst.type_code == ā€˜designā€™) die(ā€œNot implemented yet.ā€);


(Bart Banda) #10

The problem with updating the Design file contents using the JS API is that it needs to run a HIPO job after you do it, to create/modify/remove design customisation assets. You will see it if you save the contents of a Design file in the Admin Interface and you have a few design areas in place, the HIPO should start. So using the JS API for updating this is probably not feasible.

I havenā€™t heard of anyone needing to update the Design file using the JS API in the past, so Iā€™m curious as to why you are needing to do it this way?


(Bart Banda) #11

Just re-read that. Sounds like youā€™d be best to get Squiz Support to run some sort of a script that can do this for you, but Iā€™m pretty sure you are already in talks with them about it. I think there are also scripts they can run to regenerate parse files after their content has changed, so they are the best guys to chat to.


(Serge) #12

Yes it appears so. Iā€™m pointing to the fact that we have a product here (Squiz Matrix) published with an API that silently fails on certain operations. This needs fixing.

it needs to run a HIPO job after you do it, to create/modify/remove design customisation assets

Template hierarchy (what you guys call design & customisation) should be updated via internal system triggers. Why do HIPO jobs have to run on the front-end?


(Bart Banda) #13

Are you able to report this as a bug in Squizmap (which operations fail) for us to investigate further with some easy replication steps?

HIPO jobs were originally developed to process long running jobs in order to not exceed PHP web memory limits when sending requests to the application. There are probably other methods we can implement to support this in different ways so that you can trigger jobs (and polling them for their updates) without having to use a HIPO job, which we are looking into for future releases.


(Serge) #14

Done: https://squizmap.squiz.net/matrix/11277

There are definitely ways of doing this. Best is to look how other CMS do it. Usually it goes like so:

You need:

  1. A transaction queue (database table, memcached, whatever). Transactions {id, type, data, state, ts, log}. sate = new
  2. A queue manager (Periodically looks into queue for transactions, daemon PHP script looked after by upstartd or a cronjob to make sure itā€™s alive). state = picked
  3. Queue manager dispatches transactions to worker script (Usually PHP, taking a couple of MB of memory per process), which will execute the transaction based on the type and data. state = dispatched
  4. Worker process does what it has to do in the background. sate = processing. If transaction fails for any reason. state = error, log = ā€œCould not acquire lock on abc ā€¦ā€. If transaction succeed. state = success
  5. On the frontend (js_api call), poll for the state of the transaction to be error or success then display message accordingly.

Maybe itā€™s already working like this in Matrix, I havenā€™t looked at the code yet. But in terms of JS API, it shouldnā€™t be a problem to do something like: tid = js_api.setAttribute(ā€¦); js_api.pollTransaction(tid);

Any idea of a workaround until this is fixed?