How to parse complicated hash ref in perl? - json

I used JSON::Parse to decode a json file. When I dumped it to a file it looks as printed below. I need to extract the 'url' from each of the repositories. How can I go about doing that? I have tried to access the elements, but cannot seem to get to them. Any suggestions?
How I decoded the .json file:
my $json_hash_ref = {};
$json_hash_ref= json_file_to_perl ($REPO_JSON_FILE) or confess "No repos found";
This is the data dump:
$VAR1 = {
'repositories' => [
{
'size' => 2970,
'watchers' => 49,
'created_at' => '2012-03-20T05:04:42Z',
'url' => 'http://github.cerner.com/aeon-core/aeon-topology',
'open_issues' => 0,
'followers' => 49,
'owner' => 'aeon-core',
'has_downloads' => 1,
'has_issues' => 0,
'language' => 'Java',
'pushed' => '2014-06-17T18:32:37Z',
'private' => ${\$VAR1->{'repositories'}[0]{'has_issues'}},
'name' => 'aeon-topology',
'score' => '1',
'has_wiki' => ${\$VAR1->{'repositories'}[0]{'has_issues'}},
'pushed_at' => '2014-06-17T18:32:37Z',
'description' => '',
'username' => 'aeon-core',
'created' => '2012-03-20T05:04:42Z',
'homepage' => '',
'forks' => 59,
'fork' => ${\$VAR1->{'repositories'}[0]{'has_issues'}},
'type' => 'repo'
},
{
.....
},...

my #urls = map $_->{url}, #{ $json_hash_ref->{repositories} };
For good information on traversing deep data structures, read the perl references tutorial.

Related

Cakephp: exposed virtual field are not included in query

I read some other questions but they are very old. I'm using CakePHP 3.7.9.
I read the documentation about virtual fields, here and here.
So far:
Proforma.php
class Proforma extends Entity
{
protected $_accessible = [
'customer_id' => true,
'proforma_number' => true,
'proforma_date' => true,
'payment_date' => true,
'proforma_state_id' => true,
'customer' => true,
'proforma_state' => true,
'item_proformas' => true
];
protected $_virtual = [
'total'
];
protected function _getTotal()
{
$q = TableRegistry::getTableLocator()->get('item_proformas')->find();
$v = $q
->select(['total' => $q->func()->sum('total')])
->where(['proforma_id' => $this->id])
->first()['total'];
if ($v == null) $v = 0;
return $v;
}
}
In views I can easily access to this field:
<td class="text-right"><?= $proforma->item_proformas ? $this->Number->currency($proforma->total) : '' ?></td>
But when I try to make a query in Controller, i.e.:
$query = $this->Proformas->find();
debug($query);
$query->select(['value' => $query->func()->sum('total')]);
The field total is not found. Here the output of the debug:
object(Cake\ORM\Query) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Proformas.id AS `Proformas__id`, Proformas.customer_id AS `Proformas__customer_id`, Proformas.proforma_number AS `Proformas__proforma_number`, Proformas.proforma_date AS `Proformas__proforma_date`, Proformas.payment_date AS `Proformas__payment_date`, Proformas.proforma_state_id AS `Proformas__proforma_state_id` FROM proformas Proformas',
'params' => [],
'defaultTypes' => [
'Proformas__id' => 'integer',
'Proformas.id' => 'integer',
'id' => 'integer',
'Proformas__customer_id' => 'integer',
'Proformas.customer_id' => 'integer',
'customer_id' => 'integer',
'Proformas__proforma_number' => 'string',
'Proformas.proforma_number' => 'string',
'proforma_number' => 'string',
'Proformas__proforma_date' => 'date',
'Proformas.proforma_date' => 'date',
'proforma_date' => 'date',
'Proformas__payment_date' => 'date',
'Proformas.payment_date' => 'date',
'payment_date' => 'date',
'Proformas__proforma_state_id' => 'integer',
'Proformas.proforma_state_id' => 'integer',
'proforma_state_id' => 'integer'
],
'decorators' => (int) 0,
'executed' => false,
'hydrate' => true,
'buffered' => true,
'formatters' => (int) 0,
'mapReducers' => (int) 0,
'contain' => [],
'matching' => [],
'extraOptions' => [],
'repository' => object(App\Model\Table\ProformasTable) {
'registryAlias' => 'Proformas',
'table' => 'proformas',
'alias' => 'Proformas',
'entityClass' => 'App\Model\Entity\Proforma',
'associations' => [
(int) 0 => 'customers',
(int) 1 => 'proformastates',
(int) 2 => 'invoices',
(int) 3 => 'itemproformas'
],
'behaviors' => [],
'defaultConnection' => 'default',
'connectionName' => 'default'
}
}
Why even if the virtual field is exposed it's not inserted into the query?
The documentation linked above says:
Do bear in mind that virtual fields cannot be used in finds. If you want them to be part of JSON or array representations of your entities, see Exposing Virtual Fields.
and then:
By default virtual fields are not exported when converting entities to arrays or JSON. In order to expose virtual fields you need to make them visible. When defining your entity class you can provide a list of virtual field that should be exposed
So exposing virtual fields should let me to use them in finds.
Sadly Virtual fields are not calculated at the point the query is run. Here are some alternative approaches you may find helpful.
Create View of the actual table ( handle the sum at the DB level)
Create A beforeFind event in the table model.

Drupal 7 function db_insert() causes internal server error (500)

I am building an onsite payment method and among other things I need to save data to a custom db table, after recieving a callback from an external API service.
This is the function:
// callback from external service acting as client
function _api_callback() {
global $user;
$data = json_decode(file_get_contents("php://input"), $assoc = true);
$date = $date['year'] . '-' . $date['month'] . '-' . $date['day'];
$timestamp = strtotime($date);
db_insert('postback')
->fields(array(
'user_id' => $user->uid,
'data' => $data,
'created_date' => $timestamp,
))
->execute(); // save data from external service
}
In the site's access log I can see that my site responds with a 500 code when the external callback arrives.
However if I comment out everything in that function the external callback recieves a 200 response code.
So there is some thing going wrong, when this callback is recieved. It's hard to debug since the callback from the external API service starts a whole new session.
The custom table "postback" was successfully created in the .install file of my custom module and I have checked in phpMyAdmin that the table is present and well. This is the schema() function in the .install file:
function module_payments_schema() {
$schema['postback'] = array(
'description' => 'Data saved from API postback URL.',
'fields' => array(
'id' => array(
'description' => 'Auto incremental id.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'user_id' => array(
'description' => 'User id for the order.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE),
'order_id' => array(
'description' => 'Current order number.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'data' => array(
'description' => 'Postback data from external API.',
'type' => 'varchar',
'length' => 1020,
'not null' => TRUE,
'default' => ''),
'created_date' => array(
'description' => 'Created date and time (yyyy-mm-dd H:i:s).',
'type' => 'varchar',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
),
'primary key' => array('id'),
);
return $schema;
}
Anyone who can see what's wrong?
It turns out I needed to format the date value like this...
date('Y-m-d H:i:s');
... in order to work with the mysql DATETIME type.
So the working function now looks like this:
function _api_callback() {
global $user;
$data = json_decode(file_get_contents("php://input"), $assoc = true);
db_insert('postback')
->fields(array(
'user_id' => $user->uid,
'data' => $data,
'created_date' => date('Y-m-d H:i:s');
))
->execute(); // save data from external service
}

How to debug flysystem

I install Yii 2 File kit extendsion, and set local file storage success.
Then i config SFTP then run command
Yii::$app->fileStorage1->getFilesystem()->createDir('demo')
So it not work. and only show result false.
So i don't know what the error.
I try conffig:
'fileStorage1' => [
'class' => '\trntv\filekit\Storage',
//'baseUrl' => '#storageUrl1',
//'filesystemComponent' => 'fs'
'filesystemComponent' => 'sftpFs'
],
'sftpFs' => [
'class' => 'creocoder\flysystem\SftpFilesystem',
'host' => 'Myserver',
'port' => 22,
'username' => 'u',
'password' => 'p',
//'privateKey' => '/path/to/or/contents/of/privatekey',
// 'timeout' => 60,
'root' => 'public_html',
// 'permPrivate' => 0700,
// 'permPublic' => 0744,
],

Yii2 sort object defaultOrder

I have a table of policies that belong to chapters. When I view the policies in my grid view I want the default order be policy title within chapter title. I see how to set up sort attributes to enable this, but I can't figure out how to set the defaultOrder to be based on chapter title and then policy title. When ever I try to set policy.title as an attribute in the defaultOrder setting I get an error.
If Policy is model class of policy table that has a relation with chapter table named 'chapter' and linked by chapter_id field, such as:
public function getChapter()
{
return $this->hasOne(Chapter::className(), ['chapter_id' => 'chapter_id']);
}
Now you build query object with policy joined with chapter:
$query = Policy::find()
->joinWith(['chapter']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['chapter.title'=>SORT_ASC, 'policy.title' => SORT_ASC]]
]);
I could not get Fabrizio's answer to work, because my chapter table is policy_chapter, not just chapter and so is not the same as the relation name. When I tried to use the relation name, I got errors. I finally figured out in the sort, you have to use the names of the related tables, not the relations, and add them as attributes. e.g.:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 50],
'sort' => [
'defaultOrder' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
'enableMultiSort' => true,
'attributes' => [
'id' => [
'asc' => ['policy.id' => SORT_ASC],
'desc' => ['policy.id' => SORT_DESC],
'default' => SORT_ASC
],
'chapter_id' => [
'asc' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
'desc' => ['policy_chapter.sort' => SORT_DESC,'policy_chapter.title' => SORT_DESC],
'default' => SORT_ASC,
],
'reference' => [
'asc' => ['reference' => SORT_ASC],
'desc' => ['reference' => SORT_DESC],
'default' => SORT_ASC
],
'title' => [
'asc' => ['policy.title' => SORT_ASC],
'desc' => ['policy.title' => SORT_DESC],
'default' => SORT_ASC
],
'policy_chapter.sort',
'policy_chapter.title',
],
],
]);
Use the relation name or leave out the attributes and you get errors. Before I was writing
'defaultOrder' => ['policy_chapter.sort' => SORT_ASC,'policy_chapter.title' => SORT_ASC],
and Yii was not happy

Wordpress SQL Format In theme options

I have come across this code in wordpress theme options table options field:
a:34:{s:15:"generalsettings";N;s:0:"";N;s:12:"color_scheme";s:0:"";s:9:"site_name";s:0:"";s:11:"custom_logo";s:0:"";s:10:"twitter_id";s:48:"also include facebook username:this is a comment";s:13:"header_teaser";s:10:"customtext";s:12:"teaser_style";s:0:"";s:13:"teaser_custom";s:0:"";s:13:"cufon_disable";N;s:18:"breadcrumb_disable";N;s:14:"analytics_code";s:0:"";s:8:"homepage";N;s:11:"teaser_text";s:0:"";s:13:"teaser_button";s:2:"26";s:16:"mainpage_content";s:0:"";s:19:"home_teaser_disable";N;s:14:"slider_disable";s:1:"1";s:12:"slider_speed";s:0:"";s:15:"homepage_slider";s:6:"custom";s:15:"slider_showcats";s:0:"";s:12:"slider_count";s:0:"";s:4:"blog";N;s:9:"blog_page";s:0:"";s:16:"blog_excludecats";s:0:"";s:21:"related_popular_posts";N;s:16:"social_bookmarks";N;s:12:"about_author";N;s:7:"sidebar";N;s:14:"footersettings";N;s:11:"footer_text";s:0:"";s:14:"footer_include";s:0:"";s:11:"navsettings";N;s:12:"show_hide_pg";s:0:"";}
What format is this,and where can i find more information about it?.
Not exactly arrays. they are serialised data. you can use unserialize function to revert it back, and in this example, it is array! =)
Array ( [generalsettings] => [] => [color_scheme] => [site_name] => [custom_logo] => [twitter_id] => also include facebook username:this is a comment [header_teaser] => customtext [teaser_style] => [teaser_custom] => [cufon_disable] => [breadcrumb_disable] => [analytics_code] => [homepage] => [teaser_text] => [teaser_button] => 26 [mainpage_content] => [home_teaser_disable] => [slider_disable] => 1 [slider_speed] => [homepage_slider] => custom [slider_showcats] => [slider_count] => [blog] => [blog_page] => [blog_excludecats] => [related_popular_posts] => [social_bookmarks] => [about_author] => [sidebar] => [footersettings] => [footer_text] => [footer_include] => [navsettings] => [show_hide_pg] => )