Magento can't debug install script - php

I'm trying to debug my install script with no effort.
When I try to get any output inside my foreach loop there is no result in my Mage::Log() file.
<?php
$installer = $this;
$installer->startSetup();
$attrCodes = ['attr1', 'attr2', 'attr3'];
$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
Mage::log('hello before foreach');
// get all attributes
foreach ($attrCodes as $attrCode) {
Mage::log('Attribute code: ' . $attrCode);
}
Mage::log('hello after foreach');
$installer->endSetup();
I can see the log info before the foreach loop.
But I got no info inside or after that loop.
Isn't it possible to debug an install script?
I usually work with PHPStorm and use the internal debugger.
But it seems that I'm unable to debug an install script with PHPStorm or with Mage::Log().
EDIT:
I'm totally sorry for this mess.
I didn't give credits to the comment line above the loop so I decided to shorten my code here.
But the truth is I can't call my upgrade script with a line comment.
That is the reason for my problem.
I have to change a line comment to a block comment /**/ and now it works.
Never run into this problem with magento before.

Agree, with "Tipo", the problem is in the foreach loop.
You should try this edit, when you create the array:
$attrCodes = array('attr1', 'attr2', 'attr3');

Try This Code and check :
$attrCodes = array('attr1', 'attr2', 'attr3');
Mage::log('hello before foreach');
foreach ($attrCodes as $attrCode) {
Mage::log('Attribute code: ' . $attrCode);
}
Mage::log('hello after foreach');

Related

PHP Fatal error: Maximum function nesting level of '256' reached [duplicate]

I have made a function that finds all the URLs within an html file and repeats the same process for each html content linked to the discovered URLs. The function is recursive and can go on endlessly. However, I have put a limit on the recursion by setting a global variable which causes the recursion to stop after 100 recursions.
However, php returns this error:
Fatal error: Maximum function nesting level of '100' reached,
aborting! in
D:\wamp\www\crawler1\simplehtmldom_1_5\simple_html_dom.php on line
1355
I found a solution here: Increasing nesting function calls limit but this is not working in my case.
I am quoting one of the answers from the link mentioned above. Please do consider it.
"Do you have Zend, IonCube, or xDebug installed? If so, that is probably where you are getting this error from.
I ran into this a few years ago, and it ended up being Zend putting that limit there, not PHP. Of course removing it will let >you go past the 100 iterations, but you will eventually hit the memory limits."
Is there a way to increase the maximum function nesting level in PHP
Increase the value of xdebug.max_nesting_level in your php.ini
A simple solution solved my problem. I just commented this line:
zend_extension = "d:/wamp/bin/php/php5.3.8/zend_ext/php_xdebug-2.1.2-5.3-vc9.dll
in my php.ini file. This extension was limiting the stack to 100 so I disabled it. The recursive function is now working as anticipated.
Another solution is to add xdebug.max_nesting_level = 200 in your php.ini
Rather than going for a recursive function calls, work with a queue model to flatten the structure.
$queue = array('http://example.com/first/url');
while (count($queue)) {
$url = array_shift($queue);
$queue = array_merge($queue, find_urls($url));
}
function find_urls($url)
{
$urls = array();
// Some logic filling the variable
return $urls;
}
There are different ways to handle it. You can keep track of more information if you need some insight about the origin or paths traversed. There are also distributed queues that can work off a similar model.
Rather than disabling the xdebug, you can set the higher limit like
xdebug.max_nesting_level=500
It's also possible to fix this directly in php, for example in the config file of your project.
ini_set('xdebug.max_nesting_level', 200);
Go into your php.ini configuration file and change the following line:
xdebug.max_nesting_level=100
to something like:
xdebug.max_nesting_level=200
on Ubuntu using PHP 5.59 :
got to `:
/etc/php5/cli/conf.d
and find your xdebug.ini in that dir, in my case is 20-xdebug.ini
and add this line `
xdebug.max_nesting_level = 200
or this
xdebug.max_nesting_level = -1
set it to -1 and you dont have to worry change the value of the nesting level.
`
probably happened because of xdebug.
Try commenting the following line in your "php.ini" and restart your server to reload PHP.
  ";xdebug.max_nesting_level"
Try looking in /etc/php5/conf.d/ to see if there is a file called xdebug.ini
max_nesting_level is 100 by default
If it is not set in that file add:
xdebug.max_nesting_level=300
to the end of the list so it looks like this
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir=/home/drupalpro/websites/logs/profiler
xdebug.max_nesting_level=300
you can then use #Andrey's test before and after making this change to see if worked.
php -r 'function foo() { static $x = 1; echo "foo ", $x++, "\n"; foo(); } foo();'
php.ini:
xdebug.max_nesting_level = -1
I'm not entirely sure if the value will ever overflow and reach -1, but it'll either never reach -1, or it'll set the max_nesting_level pretty high.
You could convert your recursive code into an iterative code, which simulates the recursion. This means that you have to push the current status (url, document, position in document etc.) into an array, when you reach a link, and pop it out of the array, when this link has finished.
Check recursion from command line:
php -r 'function foo() { static $x = 1; echo "foo ", $x++, "\n"; foo(); } foo();'
if result > 100 THEN check memory limit;
You could try to wiggle down the nesting by implementing parallel workers (like in cluster computing) instead of increasing the number of nesting function calls.
For example: you define a limited number of slots (eg. 100) and monitor the number of "workers" assigned to each/some of them. If any slots become free, you put the waiting workers "in them".
<?php
ini_set('xdebug.max_nesting_level', 9999);
... your code ...
P.S. Change 9999 to any number you want.
Stumbled upon this bug as well during development.
However, in my case it was caused by an underlying loop of functions calling eachother - as a result of continuous iterations during development.
For future reference by search engines - the exact error my logs provided me with was:
Exception: Maximum function nesting level of '256' reached, aborting!
If, like in my case, the given answers do not solve your problem, make sure you're not accidentally doing something along the lines of the following simplified situation:
function foo(){
// Do something
bar();
}
function bar(){
// Do something else
foo();
}
In this case, even if you set ini_set('xdebug.max_nesting_level', 9999); it will still print out the same error message in your logs.
If you're using Laravel, do
composer update
This should be work.
In your case it's definitely the crawler instance is having more Xdebug limit to trace error and debug info.
But, in other cases also errors like on PHP or core files like CodeIgniter libraries will create such a case and if you even increase the x-debug level setting it would not vanish.
So, look into your code carefully :) .
Here was the issue in my case.
I had a service class which is library in CodeIgniter. Having a function inside like this.
class PaymentService {
private $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function process(){
//lots of Ci referencing here...
}
My controller as follow:
$this->load->library('PaymentService');
$this->process_(); // see I got this wrong instead it shoud be like
Function call on last line was wrong because of the typo, instead it should have been like below:
$this->Payment_service->process(); //the library class name
Then I was keeping getting the exceed error message. But I disabled XDebug but non helped. Any way please check you class name or your code for proper function calling.
I had a error when i was installing many plugins So the error 100 showed including the location of the last plugin that i installed C:\wamp\www\mysite\wp-content\plugins\"..." so i deleted this plugin folder on the C: drive then everything was back to normal.I think i have to limit the amount of plug-in i install or have activated .good luck i hope it helps
I had this issue with WordPress on cloud9. It turns out it was the W3 Caching plugin. I disabled the plugin and it worked fine.
Another solution if you are running php script in CLI(cmd)
The php.ini file that needs edit is different in this case. In my WAMP installation the php.ini file that is loaded in command line is:
\wamp\bin\php\php5.5.12\php.ini
instead of \wamp\bin\apache\apache2.4.9\bin\php.ini which loads when php is run from browser
You can also modify the {debug} function in modifier.debug_print_var.php, in order to limit its recursion into objects.
Around line 45, before :
$results .= '<br>' . str_repeat(' ', $depth * 2)
. '<b> ->' . strtr($curr_key, $_replace) . '</b> = '
. smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
After :
$max_depth = 10;
$results .= '<br>' . str_repeat(' ', $depth * 2)
. '<b> ->' . strtr($curr_key, $_replace) . '</b> = '
. ($depth > $max_depth ? 'Max recursion depth:'.(++$depth) : smarty_modifier_debug_print_var($curr_val, ++$depth, $length));
This way, Xdebug will still behave normally: limit recursion depth in var_dump and so on.
As this is a smarty problem, not a Xdebug one!
I had the same problem and I resolved it like this:
Open MySQL my.ini file
In [mysqld] section, add the following line: innodb_force_recovery =
1
Save the file and try starting MySQL
Remove that line which you just added and Save

Script to edit "editedon" field of every resource in MODX Revo firing every 24h

I need a simple script to force change editedon field of every resource in MODX Revo. This is what I have so far:
$resources = $modx->getCollection('modResource',array('parent' => 0));
$mydate = date(%c);
foreach ($resources as $res) {
$res->set('editedon', $mydate);
$res->save();
}
Just to test the script I assigned it to OnBeforeCacheUpdate event, so every time the cache is cleared I could check results. The problem is, that the script hangs itself on Console running... Like just "Hang in 'ere kid..."
What's going on?
Thank you.
UPD: When the script is called directly in browser I get this error:
Fatal error: Call to a member function getCollection() on a non-object in /path/script.php
However, some resources have their editedon field succesfully, but not all. Why is that?
UPD2: Ok, probably the script is not going to work on its own, because there are no MODX specific classes and all that inside script, but it seems to be working after I clear cache, because it is assigned to OnBeforeCacheUpdate event. Also I removed array('parent' => 0) part and it now works for every resource (not just ones without children). But how do I run it every 24 hour? Cron is not an option as the script doesn't work on its own.
Still talking to myself... this is the code that worked:
$resources = $modx->getCollection('modResource');
$mydate = date(%c);
foreach ($resources as $res) {
$res->set('editedon', $mydate);
$res->save();
}
Run it via CronManager extra for MODX and there you go. If anyone ever needs it.

Why is DOMDocument not working with WAMP server?

I’m using DOMDocument to retrieve several bits of text from a webpage and place them into an array. The same code works on another server, yet doesn’t on mine. I get Trying to get property of non-object for each iteration of the while loop and the array remains empty at the end.
$html = file_get_contents("http://sugarkettle.site44.com/catering.html");
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$meatPrices = array();
function fillArrayFromDOM($array,$type) {
global $doc;
$i = 0;
$label = 1;
$array = array();
while ($label <= 15):
$array[$i] = $doc->getElementById($type.$label)->textContent;
$i++;
$label++;
endwhile;
return $array;
}
fillArrayFromDOM($meatPrices,"meat");
echo var_dump($meatPrices);
Here’s a link to it working:
http://www.evintr.com/willtest.php
He’s running a GoDaddy server and I have a local WAMP (2.2) server. Any configuration options I can provide that might explain why this is happening? Or does the problem have nothing to do with my server config?
Any help much appreciated. Thanks in advance!
Update 1 - 11/16/12
On my server, I've tested $meatPrices[1] = $doc->getElementById('meat1')->textContent; and it works. For whatever reason, inside the while loop the same expression (except with variables in the getElementById parameters) tosses an error: Trying to get property of non-object.
Update 2 - 11/17/12
My WAMP server is running PHP version 5.3.13.
My friend's server is running PHP version 5.3.6.
Try with adding allow_url_fopen=on in your PHP configuration file (php.ini). Save it and restart Apache; it should work...
EDIT:
Check also if you have extension php_openssl.dll enabled (extension=php_openssl.dll in your php.ini file). Again, restart of Apache would be required.
EDIT:
It depends on PHP version you have but here are two potential solutions:
replace line fillArrayFromDOM($meatPrices,"meat"); with
$meatPrices = fillArrayFromDOM($meatPrices,"meat"); You can also
change your function to remove necessary $meatPrices parameter).
or
replace line function fillArrayFromDOM($array,$type) { with function fillArrayFromDOM(&$array,$type) { //note new character &; it will keep reference to $array variable so it could be changeable; you can also remove line: $array = array();
Both should work; I am in rush have no time to wait on your comment response. Let us know what you get...

echo exec working but exec not

I am using red hat enterprise edition n try making a simple php page..
When I try with ...
// html code
<?php
echo exec(<cmd>);
?>
// rest html code
Its working fine
but when tried with ...
// html code
<?php
exec(<cmd>);
?>
// rest html code
Its not working
even a simple command like cat,ls,etc not working and also I tried 2 > &1 then no error is printed .
What could be the possible error ???
Docs:
http://php.net/manual/en/function.exec.php
return a response from the command, you would need to print the response out as well
Example:
<?php
$response = array();
exec('whoami', $response);
print_r($response,true);
?>
okkkkkkk ......... I solved the problem. Actually there were two issues ...
The apache user searches its command in /usr/bin folder by default and the command I was trying to use was located in /usr/local/bin. So I need to create a soft link of that command in the /usr/bin directory.
Secondly , apache is a less privilaged user than root so need to on the sticky bit of command so that apache could successfully run the command.
I hope this will help someone else also who will face the same problem in future.

MySQL Query Logging in CakePHP

I wanted to know if there is way to log the mysql queries in CakePHP being executed when we use the find method on the models, I know that rails database queries, so does Cake do the same, if so how can I enable it or use it?
Shiv
This page has instructions on how to get Cake to log queries the same way as rails.
A Very simple method to log all the queries being executed:
in your cake\libs\model\datasources\dbo\dbo_mysql.php
find the _execute function:
function _execute($sql) {
return mysql_query($sql, $this->connection);
}
add the line "$this->log($sql);" before "return mysql_query($sql, $this->connection);"
function _execute($sql) {
$this->log($sql);
return mysql_query($sql, $this->connection);
}
That's it!!!!! All your SQL Queries gets logged. Make sure the log file is configured properly and have sufficient permission. Enjoy
Assuming you are on a nix os, the best approach would actually to tail the mysql log itself.
You might learn some interesting things out of it.
log in Ubuntu when installing from repository
tail -f /var/log/mysql/mysql.log
As mentioned below, this is a huge performance killer (well, all logs have some performance impact). So, make sure you use it only on your dev/QA machines and only for short periods on your production machine.
CakePHP 1.3 uses the sql_dump element for that.
You can use the element directly when Configure::read('debug') is set to 2:
echo $this->element('sql_dump');
Or take it's code directly if you need to do something else with it (like echo it from a ShellTask)
$sources = ConnectionManager::sourceList();
$logs = array();
foreach ($sources as $source):
$db =& ConnectionManager::getDataSource($source);
if (!$db->isInterfaceSupported('getLog')):
continue;
endif;
$logs[$source] = $db->getLog();
endforeach;
Echo with e.g.:
print_r($logs)
This is what I use (put it in element folder then include in your layout)
<?php
ob_start();
echo $this->element('sql_dump');
$out = ob_get_contents();
ob_end_clean();
CakeLog::write('mysql' , $out);
?>
then you will find the mysql.log file in TMP.logs.DS.mysql.log

Categories