Groovy equivalent for php's unserialize - serialization

I am working with serialized data from Wordpress database. Strings created with PHP serialize function and look something like this:
a:4:{s:6:"weight";s:2:"15";s:6:"length";s:1:"8";s:5:"width";s:1:"8";s:6:"height";s:2:"17";}
Is there a way to deserialize this in Groovy? or is this not a product of standardized serialization?
Thanks!

Here's an example using the pherialize library
#GrabResolver( name='Ailis', root='http://nexus.ailis.de/content/groups/public' )
#Grab( 'de.ailis.pherialize:pherialize:1.2.1' )
import de.ailis.pherialize.*
def phpValue = 'a:4:{s:6:"weight";s:2:"15";s:6:"length";s:1:"8";s:5:"width";s:1:"8";s:6:"height";s:2:"17";}'
def groovyMap = Pherialize.unserialize( phpValue ).toArray().collectEntries { k, v ->
[ k.toType( k.type ), v.toType( v.type ) ]
}
assert groovyMap == [ weight:'15', length:'8', width:'8', height:'17' ]
Though if possible, I'd suggest you share data between the two in a format they both natively speak (json?)

There was a similar question already. It seems that there are parsers for PHP serialization implemented in java. Every java lib can used with groovy, so solving this shouldn't be a problem. On of the libraries can be found here

Related

How to properly create and use dynamic Xpath in JSON (Page Object Model) - Karate DSL

For example, I have this sample JSON object in pages folder which contains all the XPaths for specific page.
{
"pageTitle1": "//*[#class='page-title' and text()='text1']",
"pageTitle2": "//*[#class='page-title' and text()='text2']",
"pageTitle_x" : "//*[#class='page-title' and text()='%s']"
}
* def pageHome = read('classpath:/pages/pageHome.json')
* click(pageHome.pageTitle_x) <-- how to properly replace %s in the string?
Update: I tried the replace function, not sure if this is the proper way.
* click(pageHome.pageTitle_x.replace("%s","new value"))
First a bit of advice. Trying to be "too clever" like this causes maintainability problems in the long run. I have said a lot about this here, please read it: https://stackoverflow.com/a/54126724/143475
That said, you can write re-usable JS functions that will do all these things:
* def pageTitle = function(x){ return "//*[#class='page-title' and text()='" + x "']" }
Now using that you can do this:
* click(pageTitle('foo'))
If you redesign the function even this may be possible:
* click(pageTitle(pageHome.pageTitle_x, 'foo'))
But see how things become more complicated and less readable. The choice is yours. Note that anything you can do in JS (e.g. String.replace()) will be possible, it is up to you and your creativity.

Can we validate dynamically generated values like datetime or any other number in Karate DSL

Can we validate dynamically generated values like datetime or any other number in Karate DSL. If yes, could you please tell how do we do it ?
Just make a JavaScript function replicating that dynamic value. and then do karate matching.
* def datetime = function(){code_generating_Date_time}
Then match datetime == response.datetime
Although i feel like generating the function should not be done, because it may become non-deterministic
Would suggest redesigning the test case.
Yes.
For example if the response is { id: 'a9f7a56b-8d5c-455c-9d13-808461d17b91', name: 'Billie' }
You can assert this way:
{ id: '#string', name: 'Billie' }
Please read the documentation, because all of this is explained there: https://github.com/intuit/karate#fuzzy-matching

Is there a way to parse XML tags in BigQuery Standard SQL?

I have read that it's a bad idea to parse XML/HTML using regular expressions. The alternative suggestion is to use an XML parser. Does one exist in the BigQuery Standard SQL library?
Here is the documentation to how to use Javascript UDFs in BigQuery like Elliot has mentioned.
https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions
I imagine the UDF might look something like
CREATE TEMPORARY FUNCTION XML(x STRING)
RETURNS STRING
LANGUAGE js AS """
var data = fromXML(x);
return data.title;
"""
OPTIONS(
library="gs://<BUCKET_NAME>/from-xml.min.js"
);
SELECT XML(a) FROM UNNEST(["<title>Title of Page</title>"]) as a
Where from-xml.min.js is from this library and loaded into your gcs account

Mechanical Turk / Cmd line tools / Qualification / #set and #foreach in xml

In the Amazon Mechanical Turk command line tools (I am using version: aws-mturk-clt-1.3.0), in one of the samples (site_filter_qual: "Website Filtering Qualification", file site_filter_qual.question), there is code that looks something like this:
#set( $urls = [ "http://news.bbc.co.uk/", http://..., ...])
#foreach ( $url in $urls )
...
I am wondering :
a) What is the language used here (it's
not Perl and not PHP, right?);
b)
Where (on Amazon site or elsewhere) I
could read about these constructs;
c)
How to implement tuples (pairs), e.g.
// in Python:
>> data = [("http://news.bbc.com", "NEWS"), ("http://google.com", "SEARCH"), ...]
>> for (url, category) in data:
>> ....
-- or something similar?
Thanks in advance!
It's a Velocity template (part of the Apache project). The good news is that it's a reasonably flexible language that you can manipulate. The better news is that you can (since I believe all the CLT and SDK code is open) actually create new constructs to override or expand on anything that's not part of Velocity out-of-the-box.
To answer your questions directly:
a) Velocity
b) http://velocity.apache.org/
c) You can use two separate arrays. Not pretty, I realize, but it'll work.

drupal bootstrap script: how to get list of all nodes of type x?

I create a custom import and export, at the moment as an external script (via bootstrap), i plan to create a module in a more generic fashion lateron.
I am building a frontend for nagios and for our host management and nagios configuration btw. Maybe it might become useful for other environments (networkmanagement)
Now i need to know how to get list of all nodes of type x?
I want to avoid direct SQL.
A suggestion i got was to make an rss and parse it
but i acess the drupal db a dozen times to extract various nodes, so it feels strange to do a web request for one thing
So what i am looking for as newbie drupal dev is just a pointer to basic search module api for this task
TIA
florian
Why do you want to avoid using SQL?
If you want to get info about what's in your db, like all the nodes of type x, the only way to get it, is through a SQL query, unless you have the data extracted already.
A query like
db_query("SELECT title, nid FROM {node} WHERE type = 'x';");
shouldn't be the thing that ruins your performance.
Edit:
The link you provided is a from Drupal 7, so you have to be be careful reading this. The reason is that in Drupal 7 it is not only possible to use db_query which basically is wrapper for the php functions mysql_query, pg_query. It's a bit different and using it, you wont have to use db_specific code. Anyways new in Drupal 7 is something that is a bit like an ORM. I haven't read about it in detail, but the idea is that you can build a query using commands on an object. This is probably what you are after. However, Drupal 7 is not ready at all for production sites. There are still a lot of critical issues and security issues. So this wont be a possibility for quite some time.
Edit 2:
If you want to get the node title and body, this is what you should do:
$type = 'x';
$query = db_query("SELECT r.nid, r.title, r.body FROM {node} AS n
LEFT JOIN {node_revisions} AS r ON r.nid = n.nid
WHERE type = '%s';", array($type));
$nodes = array();
while ($node = db_fetch_object($query)) {
$nodes[$node->nid] = $node;
}
You can use db_fetch_array instead of db_fetch_object` if you want to extract arrays instead of objects from the db.
This is a pretty old question, but for anyone coming across this page now, in Drupal 7.x best practise is to use dynamic queries.
So if you wanted to select all the nodes of type x, you could do the following:
$articles = db_select('node')
->fields('node', array('nid', 'title'))
->condition('type', 'x', '=')
->execute()
->fetchAllKeyed();
The $articles variable should then be an array of all x type nodes, keyed by nid with the arrays corresponding value set to the node title. Hope that can help.
Views is generally how you create database queries without writing them in Drupal, but this query is so simple I'm not sure it's worth the overhead of learning views, barely 5 lines after you've bootstrapped Drupal:
$nodes = array();
$results = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);
while ($result = db_fetch_object($result)) {
$nodes[] = node_load($result->nid);
}
Gotta use SQL do to this.
http://api.drupal.org/api/function/node_get_types/6
Node counts =
$node_types = node_get_types();
$type_count = array();
foreach ($node_types as $type) {
$result = db_fetch_object(db_query('SELECT count(nid) AS node_count FROM {node} WHERE type = "%s"'), $type);
$type_count[$type] = $result['count(nid)'];
}
print_r($type_count);
Nodes and their type:
$node_types = node_get_types();
$nodes = array();
foreach ($node_types as $type) {
$result = db_query('SELECT nid, title FROM {node} WHERE type = "%s"'), $type);
while ($node = db_fetch_object($result)) {
$nodes[] = array('Type' => $type, 'Title' => $node->title);
}
}
print_r($nodes);
Something like that. I am eating lunch so I didn't test that but I have done this before so it should work. Drupal 6.
The migrate module may be of interest to you. It also supports drush so you can script things fairly easily.