-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-fake-wiki.php
executable file
·206 lines (175 loc) · 5.41 KB
/
gen-fake-wiki.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env php
<?php
use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;
if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
define('NOSESSION', 1);
require_once(DOKU_INC . 'inc/init.php');
require_once '/opt/vendor/autoload.php';
/**
* Checkout and commit pages from the command line while maintaining the history
*/
class PageCLI extends CLI
{
protected $force = false;
protected $username = '';
/**
* Register options and arguments on the given $options object
*
* @param Options $options
* @return void
*/
protected function setup(Options $options)
{
/* global */
$options->registerOption(
'force',
'force obtaining a lock for the page (generally bad idea)',
'f'
);
$options->registerOption(
'num_users',
'the number of users to generate pages from',
'u',
"users"
);
$options->registerOption(
'num_pages',
'the number of pages to generate',
'p',
"pages"
);
$options->registerOption(
'num_namespaces',
'the number of namespaces to generate',
'n',
"namespaces"
);
$options->setHelp(
'Utility to generate fake pages and corrsponding metadata'
);
}
/**
* Your main program
*
* Arguments and options have been parsed when this is run
*
* @param Options $options
* @return void
*/
protected function main(Options $options)
{
$this->force = $options->getOpt('force', false);
$env_num_users = getenv("FAKE_DW_USERS");
$env_num_pages = getenv("FAKE_DW_PAGES");
$env_num_namespaces = getenv("FAKE_DW_NAMESPACES");
$this->num_users = $options->getOpt('num_users', $env_num_users ? $env_num_users : 10);
$this->num_pages = $options->getOpt('num_pages', $env_num_pages ? $env_num_pages : 100);
$this->num_namespaces = $options->getOpt('num_namespaces', $env_num_namespaces ? $env_num_namespaces : 3);
$faker = Faker\Factory::create();
// $faker->seed(42);
$users = array();
for ($x = 0; $x <= (int) $this->num_users; $x++)
array_push($users, $faker->userName());
$namespaces = array();
for ($x = 0; $x <= (int) $this->num_users; $x++)
array_push($namespaces, $faker->word());
echo var_export($users);
echo var_export($namespaces);
array_push($namespaces, "");
for ($x = 0; $x <= (int) $this->num_pages; $x++) {
$page = $faker->slug(4);
$message = $faker->sentence();
$content = $faker->text();
$namespace = $namespaces[array_rand($namespaces)];
if ($namespace !== "")
$page = $namespace . ":" . $page;
$user = $users[array_rand($users)];
echo $x . " adding page " . $page . " by user " . $user . " with message " . $message . "\n";
$this->commandCommit(
$content,
$page,
$message,
$user
);
}
}
/**
* Save a file as a new page revision
*
* @param string $localfile
* @param string $wiki_id
* @param string $message
* @param bool $minor
*/
protected function commandCommit($pagecontent, $wiki_id, $message, $user)
{
$wiki_id = cleanID($wiki_id);
$message = trim($message);
$this->username = $user;
if (!$message) {
$this->fatal("Summary message required");
}
$this->obtainLock($wiki_id);
saveWikiText($wiki_id, $pagecontent, $message, false);
$this->clearLock($wiki_id);
$this->success("$localfile > $wiki_id");
}
/**
* Lock the given page or exit
*
* @param string $wiki_id
*/
protected function obtainLock($wiki_id)
{
if ($this->force) $this->deleteLock($wiki_id);
$_SERVER['REMOTE_USER'] = $this->username;
if (checklock($wiki_id)) {
$this->error("Page $wiki_id is already locked by another user");
exit(1);
}
lock($wiki_id);
if (checklock($wiki_id)) {
$this->error("Unable to obtain lock for $wiki_id ");
var_dump(checklock($wiki_id));
exit(1);
}
}
/**
* Clear the lock on the given page
*
* @param string $wiki_id
*/
protected function clearLock($wiki_id)
{
if ($this->force) $this->deleteLock($wiki_id);
$_SERVER['REMOTE_USER'] = $this->username;
if (checklock($wiki_id)) {
$this->error("Page $wiki_id is locked by another user");
exit(1);
}
unlock($wiki_id);
if (file_exists(wikiLockFN($wiki_id))) {
$this->error("Unable to clear lock for $wiki_id");
exit(1);
}
}
/**
* Forcefully remove a lock on the page given
*
* @param string $wiki_id
*/
protected function deleteLock($wiki_id)
{
$wikiLockFN = wikiLockFN($wiki_id);
if (file_exists($wikiLockFN)) {
if (!unlink($wikiLockFN)) {
$this->error("Unable to delete $wikiLockFN");
exit(1);
}
}
}
}
// Main
$cli = new PageCLI();
$cli->run();