среда, 3 августа 2016 г.

Catching connection exceptions in PHP 7 for Mongodb



When it comes time to prepare your code for release environment instead of dev you would probably need to make sure it will work stable under any circumstances. One of the points of such preparation is to make sure your service will run if MongoDB (in case if you use it for logging only or saving bulk info of course) will go down for some reason.
In PHP 7 we have a new driver for working with Mongo, thus it would need some new approach to handling the exception.
In code it would look like this:


<?php
try {
            //information handling
        
        } catch (\MongoDB\Driver\Exception\ConnectionException $e) {
            //echo "Mongo error";
        }
?>


This would allow your script to continue doing its job even if MongoDB is unavailable for now.

If we had no this code you would get fatal error and script will stop executing.

Luckily, we are safe now and can stop worrying about this problem (however, make sure to log error to log and send yourself an email concerning this problem to solve it quickly)

понедельник, 4 апреля 2016 г.

phpList and large CSV files

In our project we have a large user base. It also means that we have a large email base. And in such case you sometimes need to inform your users about some changes or new features etc.
For this purpose we decided to use phpList (https://www.phplist.com/)  as it offers templates/ subscribe lists and anything we basically need.
There are several ways you can add user to your base: one by one manual adding, adding of bulk email list as plaintext and import it in CSV file. The last way is pretty useful as you can export emails of your users to CSV using SQL statement like:
SELECT email
FROM emails
INTO OUTFILE '/tmp/emails.csv'
LINES TERMINATED BY '\n';
It will provide you with a nice solid CSV file which includes all emails you have in that table. However, if you have really large number of users the size of CSV file may be about 100 Mb or even more. You can make your server accept files in POST with such size by editing php.ini, that is not a big problem. However, phpList accepts only CSV files of 5 Mb. And that is a problem.
However, there is a way out. Which will require a bit of editing its core files.
You will have to open file lists/admin/import2.php and find line #95
After that just change this multiplier and save the file (you should reverse it after you uploaded the CSV file).
It will allow you to import your large CSV file.
NB!: Make sure you checked the subscribe list you would like to add your users to.

This way of hacking the core code is not a proper way of handling such problems. But if you do understand what you are doing and it is a one-time issue you surely can use it.