Skip to content

Instantly share code, notes, and snippets.

View mlshvdv's full-sized avatar
🏠
Working from home

Dmitriy Malyshev mlshvdv

🏠
Working from home
  • World
View GitHub Profile
@mlshvdv
mlshvdv / GCM
Created October 22, 2013 09:27
<?php
/**
* @param apiKey - ключ для GCM, получить можно в https://code.google.com/apis/console/
* @param registrationIdsArray - массив токенов устройств
* @param messageData - массив данных уведомления, например: array('message' => "Hey!")
*/
function sendGCMNotification( $apiKey, $registrationIdsArray, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
@mlshvdv
mlshvdv / mysql-get-dublicated.sql
Created March 24, 2014 09:24
Получить повторяющиеся записи в MySQL
SELECT `user_login`, count(*) as total FROM users GROUP BY `user_login` HAVING total > 1;
@mlshvdv
mlshvdv / js-copy-object.js
Created March 25, 2014 10:16
Copy javascript object without reference
var obj = {
a: 25,
b: 50,
c: 75,
x: function() {
alert(this.a);
}
};
var A = Object.create(obj);
var B = Object.create(obj);
# Firstly edit my.cnf:
# > nano my.cnf
#
# max_allowed_packet = 2600M
# net_buffer_length = 2600M
# Or by cmd:
#
# mysql> set global max_allowed_packet=1000000000;
# mysql> set global net_buffer_length=1000000;
@mlshvdv
mlshvdv / wilson-score-range.js
Created May 2, 2014 16:15
Wilson score formula with range
/**
* Wilson score formula with range
*
* @param {number} sum Objects sum
* @param {number} total Total objects
* @param {object} range Objects (votes) range: [0, 1] or [0, n]
* @param {number} z Confidence level
*/
function wilsonScoreRange(sum, total, range, z) {
// http://amix.dk/blog/post/19588
@mlshvdv
mlshvdv / anti-flood.js
Created May 20, 2014 13:12
Javascript anti-flood system
var score = 0;
var penalty = 100; // Penalty can be fine-tuned.
var lastact = new Date();
function talk() {
/* The smaller the distance, more time has to pass in order
* to negate the score penalty cause{d,s}.
*/
score -= (new Date() - lastact) * 0.55;
@mlshvdv
mlshvdv / tar-exclude
Created December 11, 2014 13:09
Create tar without excluded objects
tar -zvcf archive.tar /path/to/site --exclude "/path/to/site/system" --exclude "/path/to/site/tmp"
@mlshvdv
mlshvdv / install-rabbitmq.sh
Created September 20, 2018 19:07
Script to install RabbitMQ server on Vagrant / Ubuntu (16.04) Sept 2018
#!/bin/bash
# You can replace "xenial" to another distribution:
# bionic (Ubuntu 18.04), artful, trusty, sid, buster, stretch, jessie, xenial (Ubuntu 16.04), yakkety, zesty
echo "deb https://dl.bintray.com/rabbitmq/debian xenial main erlang" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list > /dev/null
wget -O - 'https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc' | sudo apt-key add -
sudo apt-get update
sudo apt-get install rabbitmq-server -y
sudo service rabbitmq-server start
@mlshvdv
mlshvdv / hashtag-extractor.php
Last active October 8, 2018 07:21
Regexp to extract a hashtag with emoji support PCRE (php)
<?php
$string = '
#abc123_
#машина
#anytest😀
#какой_тотекст😁
#😂
#🤣
#😃
@mlshvdv
mlshvdv / drop-tables.sql
Created November 13, 2018 21:51
Drop all PostgreSQL tables by one query
DO $$
DECLARE
r record;
BEGIN
FOR r IN SELECT quote_ident(tablename) AS tablename, quote_ident(schemaname) AS schemaname FROM pg_tables WHERE schemaname = 'public'
LOOP
RAISE INFO 'Dropping table %.%', r.schemaname, r.tablename;
EXECUTE format('DROP TABLE IF EXISTS %I.%I CASCADE', r.schemaname, r.tablename);
END LOOP;
END$$;