Add custom requirements in installation profile. Drupal 7 - drupal

I'm writing an installation profile and want to notice users if they have low "max_execution_time" and "memory_limit" values. As I understand Drupal has to check the myprofile.install file for possible requirements so I placed there the following:
function myprofile_requirements($phase) {
$requirements = array();
// Min required PHP execution time
$min_time = 60;
// Min required memory limit, Mb
$min_memory = 128;
// Get current value of "max_execution_time"
$time = ini_get('max_execution_time');
// Get current value of "max_execution_time"
$memory = ini_get('memory_limit');
// Get "raw" numeric value
preg_match("|\d+|", $memory, $value);
$severity_time = ($time < $min_time) ? REQUIREMENT_WARNING : REQUIREMENT_OK;
$severity_memory = ($value[0] < $min_memory) ? REQUIREMENT_WARNING : REQUIREMENT_OK;
$t = get_t();
if ($phase == 'install') {
$requirements['max_execution_time'] = array(
'title' => $t('PHP max execution time'),
'value' => $t('Please increase the parameter "max_execution_time" in your PHP settings . Recommended value is at least #min sec. and more (now you have #current sec.',
array('#min' => $min_time, '#current' => $time)),
'severity' => $severity_time,
);
$requirements['memory_limit'] = array(
'title' => $t('PHP memory limit'),
'value' => $t('Please increase the parameter "memory_limit" in your PHP settings . Recommended value is at least #minM and more (now you have #current',
array('#min' => $min_memory, '#current' => $memory)),
'severity' => $severity_memory,
);
}
return $requirements;
}
It does not work - Drupal simple ignores the code above. What's wrong?

It looks like hook_requirements() doesn't get called in the install profile, it's invoked at these stages:
install: The module is being installed.
update: The module is enabled and update.php is run.
runtime: The runtime requirements are being checked and shown on the status report page.
Note that install above refers to a module being installed, not the install profile as a whole.

Related

PHP Warning: Use of undefined constant exportMeta - assumed 'exportMeta' log error

I have a product listing system using ORM framework and it's not working because I changed servers. The reason it doesn't work is that it doesn't list new products when I enter them. I'm posting the LOG output below.
I was using Mysql 5.7 and Centos on the previous server. The new server was almalinux and Mysql 8. I think it's caused by this.
PHP Warning: Use of undefined constant exportMeta - assumed 'exportMeta' (this will throw an Error in a future version of PHP) in /home/siteadi/public_html/orm/addJsonTable.php on line 24
PHP Warning: Use of undefined constant exportTerms - assumed 'exportTerms' (this will throw an Error in a future version of PHP) in /home/siteadi/public_html/orm/addJsonTable.php on line 25
PHP Warning: Use of undefined constant exportMedia - assumed 'exportMedia' (this will throw an Error in a future version of PHP) in /home/siteadi/public_html/orm/addJsonTable.php on line 26
PHP Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set primary_image = concat('/wp-content/uploads/', primary_image) where (primary' at line 1 in /home/siteadi/public_html/orm/idiorm.php on line 505
"addJsonTable.php" file below;
<?php
//ini_set('error_reporting', E_ALL);
ini_set('display_errors', false);
set_time_limit(0);
header("Access-Control-Allow-Origin: http://127.0.0.1:8887");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
require_once "idiorm.php";
require_once "methods.php";
initDB();
$ID = isset($_GET['id']) ? $_GET['id'] : 0;
if ($ID) {
$hasItem = ORM::for_table('json_table')->where('ID', $ID)->find_one();
if ($hasItem) {
ORM::raw_execute("delete from json_table where ID = $ID");
}
$phone = ORM::for_table('exportview')->where('ID', $ID)->find_array();
$phone = array_map(exportMeta, $phone);
$phone = array_map(exportTerms, $phone);
$phone = array_map(exportMedia, $phone);
foreach ($phone as $ikey => $item) {
$phone[$ikey]['media'] = $item['media_ids'] ? array_map(exportMeta, $item['media']) : [];
$phone[$ikey]['media'] = $item['media_ids'] ? array_combine(array_column($phone[$ikey]['media'], 'ID'), array_column($phone[$ikey]['media'], 'guid')) : [];
$phone[$ikey]['insert_time'] = strtotime($item['post_date_gmt']);
$phone[$ikey]['image'] = $item['meta']['resim_url'] ?: $phone[$ikey]['media'][$item['meta']['_thumbnail_id']] ?: null;
$phone[$ikey]['primary_image'] = $phone[$ikey]['media'][$item['meta']['_thumbnail_id']] ?: null;
}
foreach ($phone as $key => $item) {
if ($key < 1000000) {
$item['meta']['term_ids'] = $item['term_ids'];
$item['meta']['media_ids'] = $item['media_ids'];
ORM::raw_execute("INSERT INTO json_table (ID, title, name, type, image, primary_image, meta, media, insert_time) VALUES(:ID, :title, :name, :type, :image, :primary_image, :meta, :media, :insert_time)",
[
'ID' => $item['ID'],
'title' => $item['post_title'],
'name' => $item['post_name'],
'type' => $item['post_type'],
'image' => $item['image'],
'primary_image' => $item['primary_image'],
'meta' => json_encode($item['meta']),
'media' => json_encode($item['media']),
'insert_time' => $item['insert_time'],
]
);
}
}
ORM::raw_execute("update json_table set primary_image = concat('/wp-content/uploads/', primary_image) where (primary_image not like '%http%' and primary_image not like '%wp-content%');");
echo 1;
} else {
echo 0;
}
When I update a product that is live, the product page gives an error. I want to solve this.
A working product page:https://www.cepkolik.com/cep-telefonu/honor-80-pro/
A product page that doesn't work (after saying update product): https://www.cepkolik.com/cep-telefonu/oppo-find-n2/

Get data from woocommerce client object

I am trying to work with the WooCommerce REST API. So far I have installed the official package and created an index.php file with this code in it:
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
'http://me.commerce.loc/', // Your store URL
'ck_9dadcf73d58fec8b0860bced4b0997d7b3b0f93e', // Your consumer key
'cs_e63d694c035444c45355339171c682052b2707eb', // Your consumer secret
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v3' // WooCommerce WP REST API version
]
);
print "<pre>";
print_r($woocommerce);
die();
Printing out $woocommerce gives me this object:
Automattic\WooCommerce\Client Object
(
[http] => Automattic\WooCommerce\HttpClient\HttpClient Object
(
[ch:protected] =>
[url:protected] => http://me.commerce.loc/wp-json/wc/v3/
[consumerKey:protected] => ck_9dadcf73d58fec8b0860bced4b0997d7b3b0f93e
[consumerSecret:protected] => cs_e63d694c035444c45355339171c682052b2707eb
[options:protected] => Automattic\WooCommerce\HttpClient\Options Object
(
[options:Automattic\WooCommerce\HttpClient\Options:private] => Array
(
[wp_api] => 1
[version] => wc/v3
)
)
[request:Automattic\WooCommerce\HttpClient\HttpClient:private] =>
[response:Automattic\WooCommerce\HttpClient\HttpClient:private] =>
[responseHeaders:Automattic\WooCommerce\HttpClient\HttpClient:private] =>
)
)
Is it possible to get wooCommerce data through this object? If no then how can I do it?
Thank you very much.
You do not require to add above code or create index.php
just use this end point to get all orders list : https://example.com/wp-json/wc/v3/orders
To get all Products : https://example.com/wp-json/wc/v3/products
This way you can get list of order and products.
To use rest api and do all operations in woocommerce using rest api please refer this link : https://woocommerce.github.io/woocommerce-rest-api-docs/
There are two woocommerce rest api versions which are V2 and V3 you can use any from this.

Silex: Set firewalls session lifetime

I'm trying to extend the default lifetime once a user logs in. For the login I'm using the security service provider as follows:
$app = $this->_app;
$this->_app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'pattern' => '^.*$',
'anonymous' => true, // Needed as the login path is under the secured area
'form' => array('login_path' => '/signup/', 'check_path' => 'login_check', 'failure_path' => 'login_failure'),
'logout' => array('logout_path' => '/logout/'), // url to call for logging out
'users' => $this->_app->share(function() use ($app)
{
// Specific class App\User\UserProvider is described below
return new UserProvider($app['db']);
}),
),
),
'security.access_rules' => array(
array('^/restricted/$', 'ROLE_USER'),
)
));
I've tried setting up with the sessions lifetime (cookie) like this:
$this->_app->register(new Silex\Provider\SessionServiceProvider(), array(
'session.storage.options' => array('cookie_lifetime' => (60 * 60 * 12)), // 12 hours
));
But still nothing. Session removes itself after like 15 minutes or so.
How can I extend the login security firewall lifetime to 12 hours?
I think I finally got it working:
Saving sessions in database seemed to solve the problem.
SQL:
CREATE TABLE `session` (
`session_id` varchar(255) NOT NULL,
`session_value` text NOT NULL,
`session_time` int(11) NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
PHP:
/* SESSION IN DB */
$this->_app->register(new Silex\Provider\SessionServiceProvider());
$this->_app['session.db_options'] = array(
'db_table' => 'session',
'db_id_col' => 'session_id',
'db_data_col' => 'session_value',
'db_time_col' => 'session_time',
);
$this->_app['session.storage.handler'] = $this->_app->share(function ()
{
return new PdoSessionHandler(
$this->_app['db']->getWrappedConnection(), $this->_app['session.db_options'], $this->_app['session.storage.options']
);
});
Here is a solution if you don't want to store sessions in DB: just increase session.gc_maxlifetime in php.ini.
When sessions are being stored in files, they (by default) get put to /var/lib/php/sessions/ directory. It's clear that this directory has to be cleared from time to time. In order to achieve this, there is a cron job configured at /etc/cron.d/php5 that fires script /usr/lib/php5/sessionclean every 30 minutes. This script takes php config and gets session.gc_maxlifetime from there and then removes files that are older than what is specified in this variable.
The problem is: by default session.gc_maxlifetime equals to 1440 seconds or 24 minutes. You can increase it to whatever value suits you, for instance to 24 hours (and limit your sessions by session cookie lifetime).

Need to pull fields from an issue in Sitecore

I'm working with SiteCore and I need to pull some data out of the software via either that API or the SQL database using a PHP script. The reason I say both are possible is because even if the database changes later on, that doesn't matter to me.
Anyway...
I'm trying to pull any data fields that I can get from a particular issue. This is my SOAP code so far, and it connects to the service and such, but the return isn't what I need...
try
{
$client = new SoapClient('http://localhost:8083/sitecore/shell/webservice/service.asmx?WSDL');
$credentials = array('Password' => 'mypassword','Username' => 'sitecore\myusername');
$Current_Issue = array(
'id' => '{043B69BA-3175-4184-812F-C925CE80324E}',
//'language' => 'en',
//'version' => '1',
//'allFields' => 'true',
'databaseName' => 'web',
'credentials' => $credentials
);
$response = $client->GetItemMasters($Current_Issue);
print_r($response);
}
catch(SoapFault $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
This is my output:
stdClass Object
(
[GetItemMastersResult] => stdClass Object
(
[any] => <sitecore xmlns=""/>
)
)
ANY help is appreciated. If anybody knows an example SQL query that I can use, that would be just as useful as an alternative method.
Thanks
If you are running Sitecore 6.5 / 6.6 you may want to take a look at the Sitecore Item Web API which was released yesterday (5/11/12).
http://sdn.sitecore.net/Products/Sitecore%20Item%20Web%20API.aspx
This allows you to perform RESTful operations against Sitecore items without the need for the old web service / SOAP interface. Using this module you can receive a JSON representation of a Sitecore item or collection of items and even post back changes. You may find it easier to work with :)
If you have to use the SOAP interface, are you sure that your items are published ? Try changing the databaseName -> 'master' and see if you get any results. Other things to check are the permissions of the user credentials you are using.

How to generate translation file for a custom Drupal 7 module?

I'm using CentOS 5.5 Linux (without X), PHP 5.3 and Drupal 7.0.
The core language of my site is Russian (not English)!
I've created a game.info and the following game.module which generates 3 blocks for the front page:
function game_block_info() {
return array(
'game_main' => array(
'info' => t('Set FlashVars and show the flash game.'),
'cache' => DRUPAL_NO_CACHE,
),
'game_winner' => array(
'info' => t('Show the winner of the last week.'),
'cache' => DRUPAL_NO_CACHE,
),
'game_leader' => array(
'info' => t('Show the leader of the current week.'),
'cache' => DRUPAL_NO_CACHE,
);
}
function game_block_view($block_name = '') {
global $user;
if ($block_name == 'game_main') {
if (user_is_logged_in()) {
$content = t('User is logged in.');
} else {
$content = t('User is an anonymous user.');
}
drupal_set_message("<pre>$output</pre>\n");
return array(
'subject' => t('Main Game'),
'content' => $content,
);
} else if ($block_name == 'game_winner') {
....
} else if ($block_name == 'game_leader') {
....
}
}
It works ok, but I need all strings to be in Russian and do not want to hardcode them into my game.module file.
Do I need to create the 3rd file called game.po and add it to the game.info?
How can I create a .po file? I would prefer a simple editing of that file if possible, without obscure tools.
I've also tried a tool:
# xgettext -n game/game.module --keyword=t
xgettext: warning: file `game/game.module' extension `module' is unknown; will try C
game/game.module:87: warning: unterminated character constant
game/game.module:100: warning: unterminated character constant
These should be the steps:
To generate the .pot file, install the module Translation template extractor
Go to the "Extract strings" tab on the Locale administration interface, select your module and submit the form. You will get one single template file generated.
Then you can translate the strings with a tool like Poedit (http://www.poedit.net).
When you are done, files should be copied to a "translations" sub-folder in the module folder, so they are automatically imported by Drupal when installing your game module.
Please, give feedback and tell what problems did you have. Thanks

Resources