1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
<?php
/**
* Script to generate some random data to help testing sandbox.
*
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @file
*/
// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$dir = __DIR__;
$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";
class TranslateGenerateRandomSandboxData extends Maintenance {
public function execute() {
$users = 10;
// For number of translations, limited to [0,20]
$mean = 15;
$stddev = 20;
$stash = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
$languages = array_keys( Language::fetchLanguageNames() );
for ( $i = 0; $i < $users; $i++ ) {
$username = 'Pupu' . wfRandomString( 6 );
$password = wfRandomString( 12 );
$email = "$username.$password@blackhole.io";
$user = TranslateSandbox::addUser( $username, $password, $email );
$language = $languages[rand( 0, count( $languages ) - 1 )];
$count = wfGaussMs( $mean, $stddev );
$count = min( 20, $count );
$count = max( 0, $count );
for ( $j = 0; $j < $count; $j++ ) {
$title = Title::makeTitle( NS_MEDIAWIKI, wfRandomString( 24 ) . '/' . $language );
$value = [ 'Pupu söi' ];
for ( $k = rand( 0, 20 ); $k > 0; $k-- ) {
$value[] = wfRandomString( rand( 1, 28 ) );
}
$value = implode( "\n", $value );
$translation = new StashedTranslation( $user, $title, $value );
$stash->addTranslation( $translation );
}
}
}
}
/*
* Gauss functions are based on Mark Baker's code from
* https://stackoverflow.com/questions/5188900/bell-curve-algorithm-with-php
*/
function wfGauss() {
static $useExists = false;
static $useValue;
if ( $useExists ) {
// Use value from a previous call to this function
$useExists = false;
return $useValue;
} else {
// Polar form of the Box-Muller transformation
$w = 2.0;
while ( ( $w >= 1.0 ) || ( $w == 0.0 ) ) {
$x = wfRandomPn();
$y = wfRandomPn();
$w = ( $x * $x ) + ( $y * $y );
}
$w = sqrt( ( -2.0 * log( $w ) ) / $w );
// Set value for next call to this function
$useValue = $y * $w;
$useExists = true;
return $x * $w;
}
}
function wfGaussMs( $mean, $stddev ) {
// Adjust our gaussian random to fit the mean and standard deviation.
// The division by 4 is an arbitrary value to help fit the distribution
// within our required range, and gives a best fit for $stddev = 1.0.
return wfGauss() * ( $stddev / 4 ) + $mean;
}
function wfRandom01() {
// Returns random number using mt_rand() with a flat distribution from 0 to 1 inclusive
return (float)mt_rand() / (float)mt_getrandmax();
}
function wfRandomPn() {
// Returns random number using mt_rand() with a flat distribution from -1 to 1 inclusive
return ( 2.0 * wfRandom01() ) - 1.0;
}
$maintClass = TranslateGenerateRandomSandboxData::class;
require_once RUN_MAINTENANCE_IF_MAIN;
|