Skip to content

Instantly share code, notes, and snippets.

@ozh

ozh/plugin.php

Created Apr 7, 2021
Embed
What would you like to do?
YOURLS : add fake links (for test installs)
<?php
/*
Plugin Name: Insert Fake Links
Plugin URI: http://yourls.org/
Description: Populate DB with fake links for tests
Version: 1.0
Author: Ozh
Author URI: http://ozh.org/
*/
yourls_add_action( 'plugins_loaded', 'ozh_yourls_ifl_add_page' );
function ozh_yourls_ifl_add_page() {
yourls_register_plugin_page( 'ozh_ifl', 'Add Fake Links', 'ozh_yourls_ifl_do_page' );
}
// Display admin page
function ozh_yourls_ifl_do_page() {
if( isset( $_POST['action'] ) && $_POST['action'] == 'insert_fake_links' ) {
ozh_yourls_ifl_process();
} else {
ozh_yourls_ifl_form();
}
}
// Display form
function ozh_yourls_ifl_form() {
$nonce = yourls_create_nonce('insert_fake_links');
echo <<<HTML
<h2>Insert Links</h2>
<form method="post">
<input type="hidden" name="action" value="insert_fake_links" />
<input type="hidden" name="nonce" value="$nonce" />
<p>Insert <input type="text" name="number" /> links</p>
<p><input type="submit" value="Insert" /></p>
</form>
HTML;
}
function ozh_yourls_ifl_process() {
// Check nonce
yourls_verify_nonce( 'insert_fake_links' );
$number = abs( intval( $_POST['number'] ) );
for ($i = 1; $i <= $number; $i++) {
$title = $url = 'http://127.0.0.1/' . yourls_rnd_string( mt_rand(2, 10) );
yourls_add_new_link( $url, '', $title );
}
echo "<p>$number links added</p>";
}
@ozh

This comment has been minimized.

Copy link
Owner Author

@ozh ozh commented May 20, 2021

Here is a simple CLI script if you want to insert very quickly a very large number of fake rows (here, 10 000 rows at once)

(everything is hard coded, edit and customize)

#!/usr/local/bin/php
<?php

$loop = 10000;

$first = microtime(true);
ozh_yourls_ifl_process($loop);
echo $loop , ' queries in ', microtime(true) - $first , "\n";

function ozh_yourls_ifl_process($loop) {
    $dbh = new PDO('mysql:dbname=db_name;host=127.0.0.1', 'db_user', 'db_password');

    $i = 1;
    while ($i <= $loop) {
        try {
            $keyword = $title = $url = rnd_string( mt_rand(5, 20) );
            $url = 'http://127.0.0.1/' . $url;
            $time = date('Y-m-d H:i:s');
            $ip = '127.0.0.1';
            $sql = "INSERT INTO `yourls_url` (`keyword`, `url`, `title`, `timestamp`, `ip`, `clicks`) VALUES('$keyword', '$url', '$title', '$time', '127.0.0.1', 0)";
            $dbh->exec($sql);
            $i++;
        }
        catch(Exception $e){
        }
    }

}

function rnd_string ( $length = 5, $type = 0, $charlist = '' ) {
    $length = intval( $length );
    $possible = '0123456789abcdefghijklmnopqrstuvwxyz';
    return substr( str_shuffle( $possible ), 0, $length );
}

Example to add 1 million links :

$ for i in {0..100}; do echo -n "$i : "; php cli.php; done
// result : 
1 : 10000 queries in 2.4683339595795
2 : 10000 queries in 2.2923069000244
3 : 10000 queries in 2.5024061203003
4 : 10000 queries in 2.2461929321289
5 : 10000 queries in 2.3280258178711
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment