Skip to content

Instantly share code, notes, and snippets.

@tom--
tom-- / Random bytes, ints, UUIDs in PHP.md
Last active November 4, 2024 19:12
PHP random bytes, integers and UUIDs

Random bytes, ints, UUIDs in PHP

Simple and safe random getters to copy-paste

string randomBytes( int $length )

int randomInt ( int $min , int $max )

string randomUuid ( void )
@tom--
tom-- / Row number in a Yii CGridView.php
Created May 19, 2013 23:33
Row number in a Yii CGridView. The row number is that of the row in the data provider's overall data set.
<?php
/** @var CController $controller */
$controller->widget(
'zii.widgets.grid.CGridView',
array(
'columns' => array(
array(
'header' => 'Row',
'value' => '$row + ($this->grid->dataProvider->pagination->currentPage
# this vhost config sends my custom 404 page on not-found errors. but the `return 503` causes nginx
# to send its own built-in error page. why is that? how to change this config so it sends my 503.html?
server {
listen 80;
server_name tarfu.example.com;
root /var/www/v2/doc_root/web;
if ($maintenance = 1) {
return 503;
}
@tom--
tom-- / dumpGlobals.php
Last active July 8, 2022 02:09
Inspect your globals. Include this file to display any globals that might be defined. Maybe exit() afterwards.
<?php
/**
* Inspect your globals.
*
* Include this file to display any globals that might be defined. Maybe exit() afterwards.
*
* @author Tom Worster <fsb@thefsb.org>
* @link https://gist.github.com/tom--/5876699
* @copyright Copyright © 2013 Tom Worster
* @license BSD 2-clause http://opensource.org/licenses/BSD-2-Clause
@tom--
tom-- / 3-way-joins.md
Last active October 20, 2020 09:42
Using 3-way join tables in Yii 2 Active Record

I have an interesting Yii 2 AR design problem.

The DB has 12 primary entity tables related via a full mesh of 72 join tables. Each relation also has a type (and attributes) but those aren't stored in the join tables – they are in additional tables that the 72 join tables reference. So each of the 72 join tables provides a 3-way join between one entity, another entity and another table called link that points to the link_type.

My problem is to write the AR models and relation methods.

Let's make this more concrete with two of the primary entities: artist and recording, related through l_artist_recording. Each record of l_artist_recording has an FK to the link table, through which I can load the link_type name. For example, an artist-recording relation might have link_type "performer", "conductor", or "producer" etc.

This is a simplified schma good enough for thinking about the AR p

@tom--
tom-- / a-tcpdump.md
Last active September 28, 2019 21:14

Hexdump traffic over interface I to and from TCP server on port P of host H

tcpdump -p -i I -lttttX 'tcp and ((dst port P and dst host H) or (src port P and src host H))'

in which

  • I is interface name
  • P is port number
  • H is host name or address
@tom--
tom-- / 1-expected.txt
Created September 10, 2019 01:05
Example of Sphinx collation
MariaDB [(none)]> create schema test collate utf8_general_ci;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> set names utf8;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table test (id int not null primary key, name varchar(99) not null);
Query OK, 0 rows affected (0.002 sec)
@tom--
tom-- / slave_status.php
Created August 17, 2012 18:02
Test a MySQL replication slave's status
#!/usr/local/bin/php
<?php
$error = false;
$errno = 0;
try {
$db = new PDO('mysql:dbname=stations;host=localhost', 'root', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/** @var $slave_status StdClass */
@tom--
tom-- / automatic-playlist-creation.md
Created November 22, 2018 16:32
FAQ: What happens when Spinitron receives a recognition at 10:01AM and there is no playlist to put it in?

FAQ: What happens when Spinitron receives a recognition at 10:01AM and there is no playlist to put it in?

Answer: Spinitron automatically creates a playlist.

The playlist needs a title, start and end times, and an owner. Spinitron looks at the schedule: Is there a show in today's calendar spanning 10:01? Zero, one or more shows can satisfy this query.

If zero, Spinitron creates a generic playlist without using a show as template and the playlist is owned by the "automation persona". If more than one, having no better criteria, Spinitron chooses the most recently created show. If only one, there is no fuss.

Assuming a show was found, Spinitron copies over the title, start and end times and DJ. But a playlist can have only one DJ and a show can have one or more. If more than one, having no better criteria, Spinitron chooses the most recently created DJ. If only one, there is no fuss.

@tom--
tom-- / lucy-weight-sweep.md
Last active November 21, 2018 20:59
Lucy 1st bithday weight sweepstakes

Lucy 1st birthday weight sweepstake

Lucy was born, let's say, Nov 21 2017. How much will she weigh (in pounds) on that day 2018? Whoever guesses closest, drinks are on us.

Weight Contestant
38 Tom
42 Ray
45 Nicole