How to find an appropriate event in Magento? - events

Sometimes when looking for a convenient event to hook I do a bit of exploratory programming...
Modify Mage::dispatchEvent with this extra line:
Mage::log($name.'('.implode(',', array_keys($data)).')');
Mark a start point which I know I cannot catch any sooner:
Mage::log(__METHOD__.'::START');
Mark an end point which I don't want to catch any later:
Mage::log(__METHOD__.'::STOP');
Watch the log and step through the site (eg. order submission, whatever is being investigated)
tailf var/log/system.log
This gives me a screen full of boring data and the names of objects being passed. Other than the START and STOP I'm usually not looking for anything specific enough to grep for it and I have to rely on my experience to identify possible bootstrap points. For example when placing orders I know there is often a 'quote' somewhere, or it is possible to get a reference to the order through a 'payment' object, or vice-versa.
Then I have to remember to remove my markers (not that hard when using any sort of versioning).
What methods do you use to find events? Can you do it without modifying core code?

If I'm looking for a specific event, usually I will edit dispatchEvent() in Mage.php and add this to the top(I think these are the right params for log, writing this from memory though):
Mage::log( $name, 1, 'events.txt' );
Then I'll refresh the page, comment out that line to keep the file from getting extra events in it, and then go look at my events.txt file to see all the events that fired for that page load.
It's kind of hacky to be sure, but I've found it useful for finding events with variables as part of their names.

As of 1.2 the event list was curated on the Magento Wiki. You can find that list here:
http://www.magentocommerce.com/wiki/_media/magento_events_v1.2.0.2.xls
However, since then various events have been deprecated. There is a list here but it's only current as of 1.4
http://masteringmagento.com/2010/06/events-list-in-magento-community-1-4/
If you're handy, you can execute grep -R dispatchEvent in your Magento working directory and parse through the dearth of dispatch calls. These are the actual definitons of all Magento events in your particular version.
Edit 2/14/2013:
This list, being a couple of years old, is no longer valid. I suggest that you use the following resource as it is not only a better answer but gives you many examples and sources of finding better event hooks.
https://magento.stackexchange.com/a/167/336

philwinkle already posted a link to my old list, but I'm going to go ahead and post what I use to generate event lists. It's longer than it seems like it should be, but that is because of a general lack of coding standards in the framework. Basically, this code will go out and find all events, and attempt to format them for you. If you want, I can run it on 1.5.0.1 and update the blog (would probably be nice to do after so many months, but time is a fickle mistress).
The code:
$results = `ack Mage::dispatchEvent $magento 2>/dev/null | grep -v "app/code/local" | grep -v "downloader/pearlib"`;
$results = explode("\n", $results);
print_error(sprintf("%-100s\t%-4s\t%s\n", "FILE", "LINE", "EVENT"));
foreach($results as $result) {
if(!strlen(trim($result))) { continue; }
$matches = array();
preg_match("/([^:]+):(\d+):\W+(.*)/", $result, $matches);
$file = str_replace($magento, "", $matches[1]);
$line = $matches[2];
$event = $matches[3];
$eventMatches = array();
if(preg_match("/Mage::dispatchEvent\('(\w+)'\);/", $event, $eventMatches)) {
$event = $eventMatches[1];
$matchType = 1;
} else if(preg_match("/Mage::dispatchEvent\('(\w+)',.*/", $event, $eventMatches)) {
$event = $eventMatches[1];
$matchType = 2;
} else if(preg_match("/Mage::dispatchEvent\($/", $event)) {
$event = get_next_line_event($file, $line+1, $magento);
$matchType = 3;
} else if(preg_match("/Mage::dispatchEvent\(\"?(['\$a-zA-Z._{}\-> ]+).*/", $event, $eventMatches)) {
$event = $eventMatches[1];
$matchType = 4;
} else {
print "Found unmatcheable event:\n";
var_dump($event);exit;
}
printf("%-100s\t%-4s\t%s\n", $file, $line, $event);
}
function get_next_line_event($file, $line, $magento) {
$cnt = `cat -n $magento/$file | grep -e "^ *$line"`;
$cnt = preg_replace("/^\s*\d*\s*/", "", $cnt);
$matches = array();
if(preg_match("/^'?([\$a-z_>. -]*)'?,$/i", $cnt, $matches)) {
return $matches[1];
} else if(preg_match("/^([\$a-z_>. '\-\(\)]*),$/i", $cnt, $matches)) {
return $matches[1];
}
print "Found unmatcheable event:\n";
var_dump($cnt);exit;
}
This is part of my homebrew Magento command line toolchain. It will probably only run on Linux, and there may be internal lib functions in there that I can't find. Anyway, hope that gives you an idea about my process!
Thanks,
Joseph Mastey

List of events explicitly fired in magento, along with internal implicit ones..
check here

I thought I would post back the code from above, but modified slightly to work right. $magento needed to be assigned, as well as the paths used for grep. Just change /var/www/app to whatever your magento directory is. Copy this script to a file and execute it. You need to have ack-grep installed for it to work properly. Ubuntu users can type "sudo apt-get ack-grep" I believe to install this, or just google ack-grep.
THIS IS A SHELL PHP SCRIPT. IF YOU RUN IT IN A BROWSER, IT LOOKS LIKE A MESS! However, you can do "php whateveryoucallthescript.php >> output.txt" and then open that file in VI or edit it and search for the results you want.
This has been tested on Enterprise 1.11.1.0
<?php
$magento = "/var/www/app/";
$results = `ack-grep Mage::dispatchEvent $magento 2>/dev/null | grep -v "/var/www/app/code/local" | grep -v "/var/www/downloader/pearlib"`;
$results = explode("\n", $results);
print_error(sprintf("%-100s\t%-4s\t%s\n", "FILE", "LINE", "EVENT"));
foreach($results as $result) {
if(!strlen(trim($result))) { continue; }
$matches = array();
preg_match("/([^:]+):(\d+):\W+(.*)/", $result, $matches);
$file = str_replace($magento, "", $matches[1]);
$line = $matches[2];
$event = $matches[3];
$eventMatches = array();
if(preg_match("/Mage::dispatchEvent\('(\w+)'\);/", $event, $eventMatches)) {
$event = $eventMatches[1];
$matchType = 1;
} else if(preg_match("/Mage::dispatchEvent\('(\w+)',.*/", $event, $eventMatches)) {
$event = $eventMatches[1];
$matchType = 2;
} else if(preg_match("/Mage::dispatchEvent\($/", $event)) {
$event = get_next_line_event($file, $line+1, $magento);
$matchType = 3;
} else if(preg_match("/Mage::dispatchEvent\(\"?(['\$a-zA-Z._{}\-> ]+).*/", $event, $eventMatches)) {
$event = $eventMatches[1];
$matchType = 4;
} else {
print "Found unmatcheable event:\n";
var_dump($event);
}
printf("%-100s\t%-4s\t%s\n", $file, $line, $event);
}
function get_next_line_event($file, $line, $magento) {
$cnt = `cat -n $magento/$file | grep -e "^ *$line"`;
$cnt = preg_replace("/^\s*\d*\s*/", "", $cnt);
$matches = array();
if(preg_match("/^'?([\$a-z_>. -]*)'?,$/i", $cnt, $matches)) {
return $matches[1];
} else if(preg_match("/^([\$a-z_>. '\-\(\)]*),$/i", $cnt, $matches)) {
return $matches[1];
}
print "Found unmatcheable event:\n";
var_dump($cnt);exit;
}
function print_error($err) {
echo $err;
}
?>

Related

joomla plugin works correctly without caching and does not work with caching

if caching is turned off then everything works correctly. if you enable caching, the plugin at the beginning works correctly, but after 5 minutes it stops processing the line. normal or progressive caching — it doesn’t matter. when you delete the cache — processing is turned on again, and again after 5 minutes it disappears.
here is the complete plugin code. what could be the reason?
code inserted into the material for example such {robokassa 5}
class plgContentRobokassa extends JPlugin
{
public $cont='';
public function onContentPrepare($context, &$row, &$params, $page = 0)
{
$doc = JFactory::getDocument();
$doc->addStyleSheet(JURI::root(true).'/plugins/content/robokassa/css/robokassa.css');
$this->cont=$context;
}
public function onAfterRender()
{
$is_test='0';
$mrh_pass1='*****';
$mrh_login='******';
$app = JFactory::getApplication();
if ($app->getName() != 'site') {
return true;
}
// Получаем кодовое слово из параметров
$varname = 'robokassa';
//Получаем тело сайта
$html = $app->getBody();
// Если тегов нет
if (strpos($html, $varname) === false)
{
return true;
}
$bodyPos = stripos($html, '<body');
$preContent = '';
if ($bodyPos > -1)
{
$preContent = substr($html, 0, $bodyPos);
$html = substr($html, $bodyPos);
}
//Задаем шаблон поиска
$pattern = '#\{' . $varname . ' ([0-9]+)\}#i';
//Закидываем все найденные шаблоны в массив
if (preg_match_all($pattern, $html, $matches))
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
foreach ($matches[0] as $i => $match)
{
*replace code here*
}
$html=$preContent.$html.$script_alert;
//Запихиваем всё обратно в тело
$app->setBody($html);
}
}
}
The plugin events are invoked in the order they have in the Plugin administration.
Cache is part of the System plugins so when their cache is valid the response is retrieved from Cache, and the plugins which come before it are not executed.
You could be able to solve this by moving your plugin after Cache (or last)
If the plugin is of a different kind, i.e. Content, there is no way to achieve this.
You can change it into a System plugin (all events from any plugin category is available also in the System plugins).
Another alternative, you could put your code in a module, prevent caching in the module; but this will not work with page cache.
Finally, and I don't know why I didn't write it first, make an Ajax call to a page which you will NOT cache (by excluding it in the page cache plugin configuration: that way the page will be cached and each time it will retrieve the current data.

Laravel Collective SSH results

I am performing SSH in Laravel whereby I connect to another server and download a file. I am using Laravel Collective https://laravelcollective.com/docs/5.4/ssh
So, the suggested way to do this is something like this
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
if($result) {
return $path;
} else {
return 401;
}
Now that successfully downloads the file and moves it to my local server. However, I am always returned 401 because $result seems to be Null.
I cant find much or getting the result back from the SSH. I have also tried
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path, function($line){
dd( $line.PHP_EOL);
});
But that never gets into the inner function.
Is there any way I can get the result back from the SSH? I just want to handle it properly if there is an error.
Thanks
Rather than rely on $result to give you true / false / error, you can check if the file was downloaded successfully in another way:
// download the file
$result = \SSH::into('scripts')->get('/srv/somelocation/'.$fileName, $path);
// see if downloaded file exists
if ( file_exists($path) ) {
return $path;
} else {
return 401;
}
u need to pass file name also like this in get and put method:
$fileName = "example.txt";
$get = \SSH::into('scripts')->get('/remote/somelocation/'.$fileName, base_path($fileName));
in set method
$set = \SSH::into('scripts')->set(base_path($fileName),'/remote/location/'.$fileName);
in list
$command = SSH::into('scripts')->run(['ls -lsa'],function($output) {
dd($output);
});

Migrating Parse files from Gridstore to S3

We migrated the data(without files) to mLab and Heroku. So the old files are still on Parse.
Since then, any new file added goes into Gridstore, which is the default file storage for mLab.
Now I migrated old parse files from Parse to an S3 Bucket using sashido
The files are migrated and are accessible using S3Adapter in Heroku.
But the files on Gridstore are not accessible now. How can I migrate them to the same S3 bucket and change references in mLab?
Maybe you're interested in the solution I've tried. It's not a simple operation, but I migrated successfully 3 databases with my parse server configuration.
It's based in a PHP script (with the Parse PHP SDK) that runs through every object, it gets the file from Parse.com and sets it (with any of your adapter configuration) in your own server.
The script looks like:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
$fileField = $argv[1];
$class = $argv[2];
require_once 'vendor/autoload.php';
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
use Parse\ParseClient;
$app_id = "******";
$rest_key = "******";
$master_key = "******";
ParseClient::initialize($app_id, $rest_key, $master_key);
ParseClient::setServerURL('http://localhost:1338/', 'parse');
$query = new ParseQuery($class);
$query->ascending("createdAt"); // it's just my preference
$query->exists($fileField);
$query->limit(1);
$count = $query->count();
for ($i = 0; $i < $count; $i = $i + 1) {
try {
$query->skip($i);
// get Entry
$entryWithFile = $query->first();
// get file
$parseFile = $entryWithFile->get($fileField);
// filename
$fileName = $parseFile->getName();
// if the file is hosted in Parse, do the job, otherwise continue with the next one
if (strpos($fileName, "tfss-") === false) {
echo "\nThis is already an internal file, skipping...";
continue;
}
$newFileName = str_replace("tfss-", "", $fileName);
$binaryFile = file_get_contents($parseFile->getURL());
$newFile = ParseFile::createFromData($binaryFile, $newFileName);
$entryWithFile->set($fileField, $newFile);
$entryWithFile->save(true);
echo "\nFile saved\n";
}
catch (Exception $e) {
// The conection with mongo or the server could be off for some second, let's retry it ;)
sleep(10);
continue;
}
}
echo "\n";
echo "END!";
?>
set your parse url correctly.
Imagine you want to migrate the file from class _User with field imageProfile, so be sure that you pass $fileField = "imageProfile"; $class = "_User".
Run that code for any field per class.
I did a dumb solution to work in parallel, which would be skipping steps in the for loop, for example:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
$index = $argv[1];
$of = $argv[2];
$fileField = $argv[3];
$class = $argv[4];
require_once 'vendor/autoload.php';
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
use Parse\ParseClient;
$app_id = "********";
$rest_key = "********";
$master_key = "********";
ParseClient::initialize($app_id, $rest_key, $master_key);
ParseClient::setServerURL('http://localhost:1338/', 'parse');
$query = new ParseQuery($class);
$query->ascending("createdAt");
$query->exists($fileField);
$query->limit(1);
$count = $query->count();
for ($i = $index; $i < $count; $i = $i + $of) {
try {
$query->skip($i);
// get Entry
$entryWithFile = $query->first();
// get file
$parseFile = $entryWithFile->get($fileField);
// filename
$fileName = $parseFile->getName();
// if the file is hosted in Parse, do the job, otherwise continue with the next one
if (strpos($fileName, "tfss-") === false) {
echo "\nThis is already an internal file, skipping...";
continue;
}
$newFileName = str_replace("tfss-", "", $fileName);
$binaryFile = file_get_contents($parseFile->getURL());
$newFile = ParseFile::createFromData($binaryFile, $newFileName);
$entryWithFile->set($fileField, $newFile);
$entryWithFile->save(true);
echo "\nFile saved\n";
}
catch (Exception $e) {
// The conection with mongo or the server could be off for some second, let's retry it ;)
sleep(10);
continue;
}
}
echo "\n";
echo "END!";
?>
so if you configure $fileField and $class as before, and you can open 3 threads and run:
php migrator.php 0 3 "imageProfile" "_User"
php migrator.php 1 3 "imageProfile" "_User"
php migrator.php 2 3 "imageProfile" "_User"
so you will have loops running like:
object 0, 3, 6
object 1, 4, 7
object 2, 5, 8
Good luck, and be quick! It's going to shut down in a few days.

How to download gpx files in Laravel 5.2

Good afternoon everyone !
I have this simple code in the controller
public function descargarRutaGPX($id){
$buscar = Ruta::find($id);
$file = Storage::get('ddfbdsfv.gpx');
echo $file;
}
If the file exists the browser show "Preppend text" , but if the file doesn't exists the browser shows FileNotFoundException in FilesystemAdapter.php line 61.
Could anyone help to me ?
UPDATED
$buscar = Ruta::find($id);
if (Storage::has('aaaa.gpx')) {
$file = Storage::get('aaaa.gpx');
return response()->download($file);
}else{
echo "NO";
}
This is the error.
The file "Prepended Text" does not exist
You'll probably want to check and make sure the file exists as well as download the file if it does. Try changing your code to the following
public function descargarRutaGPX($id){
$buscar = Ruta::find($id);
If (Storage::has('ddfbdsfv.gpx') {
$file = Storage::get('ddfbdsfv.gpx');
return response()->download($file);
} else {
// file not found. Do something to alert the user
}
}

magento programmatically saving order takes too long

I am using the following code in a controller of a custom module to change the order status using AJAX calls and I found it runs very slowly. I did search on Google, but there is no such a topic on it.
I added the time() lines to test the execution time. It takes 30 - 40 seconds to finish each call. I use this same code in Mage_Adminhtml_Sales_OrderController so that I can change the order status in backend, but it returns very quickly and the exetime is equal to 0. I don't think this is hardware performance related, as it runs on 8GB memory and 8 cores CPU. Are there any possible reasons for Magento to take so long to save the order? Does this call have some limits so that it works faster in back-end?
public function changestatusAction()
{
if (!isset($_POST["oid"]) || !$_POST['oid']) {
echo '{"error":1}';
return;
}
if (!isset($_POST["status"]) || !$_POST['status']) {
echo '{"error":1}';
return;
}
$errstr = "";
try{
$order = Mage::getModel('sales/order')->loadByIncrementId($_POST['oid']);
$order->addStatusHistoryComment('', $_POST['status'])
->setIsVisibleOnFront(false)
->setIsCustomerNotified(false);
$starttime = time();
$order->save();
$endtime = time();
$exetime = $endtime - $starttime;
}catch(Exception $e){
$errstr .= $e->getMessage();
}
if($errstr)
echo '{"changed":0,"err":"'.$errstr.'"}';
else echo '{"changed":1,"exetime":"'.$exetime.'"}';
return;
}
I think the problem is when magento try to send email to customer. Change parameter of method setIsCustomerNotified to true or disable magento email communication. If it help you must configure your server to properly send email

Resources