This repository was archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathincludes.php
668 lines (574 loc) · 17.7 KB
/
includes.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
<?php
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
use SensioLabs\AnsiConverter\Theme\SolarizedXTermTheme;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;
ini_set( 'display_errors', 1 );
ini_set( 'display_startup_errors', 1 );
error_reporting( E_ALL );
session_start();
include_once './vendor/autoload.php';
include 'config.default.php';
if ( file_exists( 'config.php' ) ) {
include 'config.php';
$config = array_merge( $config, $localConfig );
}
$ansiConverter = new AnsiToHtmlConverter( new SolarizedXTermTheme() );
$basePath = dirname( $_SERVER['SCRIPT_NAME'] );
if ( $basePath === '/' ) {
$basePath = '';
}
$is404 = basename( $_SERVER['SCRIPT_NAME'] ) === '404.php';
include_once 'oauth.php';
$mysqli = new mysqli( 'localhost', 'patchdemo', 'patchdemo', 'patchdemo' );
if ( $mysqli->connect_error ) {
die( $mysqli->connect_error );
}
function insert_wiki_data( string $wiki, string $creator, int $created, string $branch, ?string $landingPage ) {
global $mysqli;
$stmt = $mysqli->prepare( '
INSERT INTO wikis
(wiki, creator, created, branch, landingPage)
VALUES(?, ?, FROM_UNIXTIME(?), ?, ?)
' );
if ( !$stmt ) {
echo $mysqli->error;
}
$stmt->bind_param( 'ssiss', $wiki, $creator, $created, $branch, $landingPage );
$stmt->execute();
$stmt->close();
}
function wiki_add_patches( string $wiki, array $patches ) {
global $mysqli;
$stmt = $mysqli->prepare( 'UPDATE wikis SET patches = ? WHERE wiki = ?' );
$patchesJSON = json_encode_clean( $patches );
$stmt->bind_param( 'ss', $patchesJSON, $wiki );
$stmt->execute();
$stmt->close();
}
/**
* Store which repos were used to create a wiki
*
* @param string $wiki Wiki
* @param array $repos Array of indexed arrays, each item contains:
* 'preset': The preset used, and if this is 'custom' then..
* 'repos': The full list of repos
*/
function wiki_add_repos( string $wiki, array $repos ) {
global $mysqli;
$stmt = $mysqli->prepare( 'UPDATE wikis SET repos = ? WHERE wiki = ?' );
$reposJSON = json_encode_clean( $repos );
$stmt->bind_param( 'ss', $reposJSON, $wiki );
$stmt->execute();
$stmt->close();
}
function wiki_add_announced_tasks( string $wiki, array $announcedTasks ) {
global $mysqli;
$stmt = $mysqli->prepare( 'UPDATE wikis SET announcedTasks = ? WHERE wiki = ?' );
$announcedTasksJSON = json_encode_clean( $announcedTasks );
$stmt->bind_param( 'ss', $announcedTasksJSON, $wiki );
$stmt->execute();
$stmt->close();
}
function wiki_set_ready( string $wiki, int $timeToCreate ) {
global $mysqli;
$stmt = $mysqli->prepare( 'UPDATE wikis SET ready = 1, timeToCreate = ? WHERE wiki = ?' );
$stmt->bind_param( 'is', $timeToCreate, $wiki );
$stmt->execute();
$stmt->close();
}
function get_wiki_data( string $wiki ): array {
global $mysqli;
$stmt = $mysqli->prepare( '
SELECT wiki, creator, UNIX_TIMESTAMP( created ) created, patches, branch, repos, announcedTasks, landingPage, timeToCreate, deleted, ready
FROM wikis WHERE wiki = ?
' );
if ( !$stmt ) {
echo $mysqli->error;
}
$stmt->bind_param( 's', $wiki );
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
$stmt->close();
if ( !$data ) {
throw new Error( 'Wiki not found: ' . $wiki );
}
return get_wiki_data_from_row( $data );
}
function get_wiki_data_from_row( array $data ): array {
// Decode JSON
$data['patches'] = json_decode( $data['patches'] ?: '' ) ?: [];
$data['announcedTasks'] = json_decode( $data['announcedTasks'] ?: '' ) ?: [];
$data['repos'] = json_decode( $data['repos'] ?: '', true ) ?: [ 'preset' => 'unknown' ];
// Populate patch list
$patchList = [];
$linkedTasks = [];
if ( $data['patches'] ) {
foreach ( $data['patches'] as $patch ) {
[ $r, $p ] = explode( ',', $patch );
$patchData = get_patch_data( $r, $p );
$patchList[$patch] = $patchData;
get_linked_tasks( $patchData[ 'message' ], $linkedTasks );
}
}
$data['patchList'] = $patchList;
// Populate task list
$linkedTaskList = [];
foreach ( $linkedTasks as $task ) {
$linkedTaskList[$task] = get_task_data( $task );
}
$data['linkedTaskList'] = $linkedTaskList;
return $data;
}
function get_wiki_url( string $wiki, ?string $landingPage ): string {
return 'wikis/' . $wiki . ( $landingPage ? '/wiki/' . $landingPage : '/w' );
}
function get_wiki_link( string $wiki, ?string $landingPage, bool $ready = true ): string {
if ( !$ready ) {
return substr( $wiki, 0, 10 );
}
return (
'<a href="' . htmlspecialchars( get_wiki_url( $wiki, $landingPage ) ) . ' " title="' . $wiki . '">' .
substr( $wiki, 0, 10 ) .
'</a>'
);
}
function get_patch_data( $r, $p ): array {
global $mysqli;
$patch = $r . ',' . $p;
$stmt = $mysqli->prepare( '
SELECT patch, status, subject, message, UNIX_TIMESTAMP( updated ) updated
FROM patches WHERE patch = ?' );
$stmt->bind_param( 's', $patch );
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
$stmt->close();
// Patch status can change (if not merged), so re-fetch every 24 hours
if (
!$data || (
$data['status'] !== 'MERGED' &&
( time() - $data['updated'] > 24 * 60 * 60 )
)
) {
$changeData = gerrit_query( "changes/$r" );
$status = 'UNKNOWN';
if ( $changeData ) {
$status = $changeData['status'];
}
$subject = '';
$message = '';
$commitData = gerrit_query( "changes/$r/revisions/$p/commit" );
if ( $commitData ) {
$subject = $commitData[ 'subject' ];
$message = $commitData[ 'message' ];
}
// Update cache
$stmt = $mysqli->prepare( '
INSERT INTO patches
(patch, status, subject, message, updated)
VALUES(?, ?, ?, ?, NOW())
ON DUPLICATE KEY UPDATE
status = ?, updated = NOW()
' );
$stmt->bind_param( 'sssss', $patch, $status, $subject, $message, $status );
$stmt->execute();
$stmt->close();
$data = [
'patch' => $patch,
'status' => $status,
'subject' => $subject,
'message' => $message,
'updated' => time(),
];
}
$data['r'] = $r;
$data['p'] = $p;
return $data;
}
function get_task_data( int $task ): array {
global $config, $mysqli;
if ( !$config['conduitApiKey'] ) {
// No API access means no task metadata
return [
'id' => 'T' . $task,
'task' => $task,
'title' => '',
'status' => '',
'updated' => time(),
];
}
$stmt = $mysqli->prepare( '
SELECT task, title, status, UNIX_TIMESTAMP(updated) updated
FROM tasks WHERE task = ?
' );
$stmt->bind_param( 'i', $task );
$stmt->execute();
$res = $stmt->get_result();
$data = $res->fetch_assoc();
$stmt->close();
// Task titles & statuses can change, so re-fetch every 24 hours
if ( !$data || ( time() - $data['updated'] > 24 * 60 * 60 ) || !$data['status'] ) {
$title = '';
$api = new \Phabricator\Phabricator( $config['phabricatorUrl'], $config['conduitApiKey'] );
$maniphestData = $api->Maniphest( 'info', [
'task_id' => $task
] )->getResult();
if ( $maniphestData ) {
$title = $maniphestData['title'];
$status = $maniphestData['status'];
} else {
// e.g. security-restricted tasks
$title = '';
$status = 'unknown';
}
// Update cache
$stmt = $mysqli->prepare( '
INSERT INTO tasks (task, title, status, updated)
VALUES(?, ?, ?, NOW())
ON DUPLICATE KEY UPDATE
title = ?, status = ?, updated = NOW()
' );
$stmt->bind_param( 'issss', $task, $title, $status, $title, $status );
$stmt->execute();
$stmt->close();
$data = [
'task' => $task,
'title' => $title,
'status' => $status,
'updated' => time(),
];
}
$data['id'] = 'T' . $data['task'];
return $data;
}
function all_closed( array $statuses ): bool {
foreach ( $statuses as $status ) {
if ( $status !== 'MERGED' && $status !== 'ABANDONED' && $status !== 'DNM' ) {
return false;
}
}
return true;
}
function format_patch_list( array $patchList, ?string $branch, bool &$closed = false ): string {
$statuses = [];
$patches = implode( '<br>', array_map( static function ( $patchData ) use ( &$statuses, &$linkedTaskList ) {
global $config;
$status = $patchData['status'];
if (
$status === 'NEW' &&
preg_match( '/(DNM|DO ?NOT ?MERGE)/', $patchData['subject'] )
) {
$status = 'DNM';
}
$statuses[] = $status;
$title = $patchData['patch'] . ': ' . $patchData[ 'subject' ];
return '<a href="' . $config['gerritUrl'] . '/r/c/' . $patchData['r'] . '/' . $patchData['p'] . '" title="' . htmlspecialchars( $title ) . '" class="status-' . $status . '">' .
htmlspecialchars( $title ) .
'</a>';
}, $patchList ) );
$closed = all_closed( $statuses );
return ( $patches ?: '<em>No patches</em>' ) .
( $branch && $branch !== 'master' ? '<br>Branch: ' . $branch : '' );
}
function format_linked_tasks( array $linkedTasks ): string {
global $config;
$taskDescs = [];
foreach ( $linkedTasks as $task => $taskData ) {
$taskTitle = $taskData['id'] . ( $taskData['title'] ? ': ' . htmlspecialchars( $taskData['title'] ) : '' );
$taskDescs[] = '<a href="' . $config['phabricatorUrl'] . '/' . $taskData['id'] . '" title="' . $taskTitle . '" class="status-' . $taskData['status'] . '">' . $taskTitle . '</a>';
}
$linkedTasks = implode( '<br>', $taskDescs );
return $linkedTasks ?: '<em>No tasks</em>';
}
function format_duration( int $time ): string {
return $time > 60 ?
floor( $time / 60 ) . "m\u{00A0}" . ( $time % 60 ) . 's' :
$time . 's';
}
function shell_echo( string $cmd, array $env = [] ): int {
echo '<pre>';
$prefix = '';
foreach ( $env as $key => $value ) {
$value = escapeshellarg( $value );
$prefix .= "$key=$value ";
}
echo htmlspecialchars( "$prefix$cmd\n" );
$process = Process::fromShellCommandline( $cmd, null, $env );
$process->setTimeout( null );
$process->setPty( true );
$error = $process->run( static function ( $type, $buffer ) {
global $ansiConverter;
echo $ansiConverter->convert( $buffer );
} );
echo '</pre>';
return $error;
}
function shell( $cmd, array $env = [] ): ?string {
$process = Process::fromShellCommandline( $cmd, null, $env );
$process->setTimeout( null );
$error = $process->run();
return $error ? null : $process->getOutput();
}
/**
* Delete a wiki.
*
* @param string $wiki Wiki name
* @param string|null $serverUri Server path - must be passed in if calling from the CLI
* @return string|null Error message, null if successful
*/
function delete_wiki( string $wiki, string $serverUri = null ): ?string {
global $mysqli;
if ( !$serverUri ) {
$serverUri = get_server() . get_server_path();
}
$wikiData = get_wiki_data( $wiki );
if ( $wikiData['deleted'] ) {
return 'Wiki already deleted.';
}
$error = shell_echo( __DIR__ . '/deletewiki.sh',
[
'PATCHDEMO' => __DIR__,
'WIKI' => $wiki
]
);
if ( $error ) {
return 'Could not delete wiki files or database.';
}
foreach ( $wikiData['announcedTasks'] as $task ) {
$creator = $wikiData['creator'];
post_phab_comment(
'T' . $task,
"Test wiki on [[ $serverUri | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
"\n" .
"~~[[ $serverUri/wikis/$wiki/w/ ]]~~"
);
}
$stmt = $mysqli->prepare( '
UPDATE wikis
SET deleted = 1
WHERE wiki = ?
' );
$stmt->bind_param( 's', $wiki );
$stmt->execute();
$stmt->close();
return $mysqli->error ?: null;
}
$requestCache = [];
function gerrit_query( string $url, $echo = false ): ?array {
global $config, $requestCache;
if ( $echo ) {
echo "<pre>$url</pre>";
}
if ( empty( $requestCache[$url] ) ) {
$url = $config['gerritUrl'] . '/r/' . $url;
// Suppress warning if request fails
// phpcs:ignore
$resp = @file_get_contents( $url );
$requestCache[$url] = json_decode( substr( $resp, 4 ), true );
}
return $requestCache[$url];
}
function get_linked_tasks( string $message, array &$alreadyLinkedTasks = [] ): array {
preg_match_all( '/^Bug: T([0-9]+)$/m', $message, $m );
foreach ( $m[1] as $task ) {
if ( !in_array( $task, $alreadyLinkedTasks, true ) ) {
$alreadyLinkedTasks[] = $task;
}
}
return $alreadyLinkedTasks;
}
function get_repo_data( string $pathPrefix = 'w/' ): array {
$data = file_get_contents( __DIR__ . '/repository-lists/all.txt' );
$repos = [];
foreach ( explode( "\n", trim( $data ) ) as $line ) {
[ $repo, $path ] = explode( ' ', $line );
$repos[ $repo ] = $pathPrefix . $path;
}
return $repos;
}
function get_repo_label( string $repo ): string {
return preg_replace( '`^mediawiki/(extensions/)?`', '', $repo );
}
function get_branches( string $repo ): array {
$gitcmd = "git --git-dir=" . __DIR__ . "/repositories/$repo/.git";
// basically `git branch -r`, but without the silly parts
$branches = explode( "\n", shell_exec( "$gitcmd for-each-ref refs/remotes/origin/ --format='%(refname:short)'" ) ?: '' );
return $branches;
}
function get_branches_sorted( string $repo ): array {
$branches = get_branches( $repo );
$branches = array_filter( $branches, static function ( $branch ) {
return preg_match( '/^origin\/(master|wmf|REL)/', $branch );
} );
natcasesort( $branches );
// Put newest branches first
$branches = array_reverse( array_values( $branches ) );
// Move master to the top
array_unshift( $branches, array_pop( $branches ) );
return $branches;
}
function can_delete( string $creator = null ): bool {
global $user, $useOAuth;
if ( !$useOAuth ) {
// Unauthenticated site
return true;
}
$username = $user ? $user->username : null;
return ( $username && $username === $creator ) || can_admin();
}
function can_admin(): bool {
global $config, $user, $useOAuth;
if ( !$useOAuth ) {
// Unauthenticated site
return true;
}
$username = $user ? $user->username : null;
$admins = $config[ 'oauth' ][ 'admins' ];
return $username && in_array( $username, $admins, true );
}
function user_link( string $username ): string {
global $config;
$base = preg_replace( '/(.*\/index.php).*/i', '$1', $config[ 'oauth' ][ 'url' ] );
return '<a href="' . $base . '?title=' . urlencode( 'User:' . $username ) . '" target="_blank">' . $username . '</a>';
}
function is_trusted_user( string $email ): bool {
$config = file_get_contents( 'https://raw.githubusercontent.com/wikimedia/integration-config/master/zuul/layout.yaml' );
// Hack: Parser doesn't understand this, even using Yaml::PARSE_CUSTOM_TAGS
$config = str_replace( '!!merge', 'merge', $config );
$data = Yaml::parse( $config );
$emailPatterns = $data[ 'pipelines' ][0][ 'trigger' ][ 'gerrit' ][ 0 ][ 'email' ];
foreach ( $emailPatterns as $pattern ) {
if ( preg_match( '/' . $pattern . '/', $email ) ) {
return true;
}
}
return false;
}
function post_phab_comment( string $id, string $comment ) {
global $config;
if ( $config['conduitApiKey'] ) {
$api = new \Phabricator\Phabricator( $config['phabricatorUrl'], $config['conduitApiKey'] );
$api->Maniphest( 'edit', [
'objectIdentifier' => $id,
'transactions' => [
[
'type' => 'comment',
'value' => $comment,
]
]
] );
}
}
function get_repo_presets(): array {
$presets = [];
$presets['all'] = array_keys( get_repo_data() );
$presets['wikimedia'] = Yaml::parse( file_get_contents( __DIR__ . '/repository-lists/wikimedia.yaml' ) );
$presets['tarball'] = Yaml::parse( file_get_contents( __DIR__ . '/repository-lists/tarball.yaml' ) );
$presets['minimal'] = Yaml::parse( file_get_contents( __DIR__ . '/repository-lists/minimal.yaml' ) );
return $presets;
}
function get_known_pages(): array {
global $user, $mysqli;
$pages = [
'Main Page'
];
foreach ( [ 'Alice', 'Bob', 'Patch Demo', 'Mallory' ] as $username ) {
$pages[] = 'User:' . $username;
$pages[] = 'User talk:' . $username;
}
// TODO: Suggest some special pages?
$files = scandir( __DIR__ . '/pages' );
foreach ( $files as $file ) {
if ( str_ends_with( $file, '.txt' ) ) {
$contents = file_get_contents( __DIR__ . '/pages/' . $file );
if ( $contents ) {
$lines = explode( "\n",
str_replace( '_', ' ', trim( $contents ) )
);
$lines = array_filter( $lines, static function ( $line ) {
return $line !== '';
} );
$pages = array_merge( $pages, $lines );
}
}
}
if ( $user ) {
// Fetch previously used landing pages
$stmt = $mysqli->prepare( '
SELECT DISTINCT landingPage
FROM wikis
WHERE landingPage != "" AND creator = ?
' );
if ( !$stmt ) {
die( $mysqli->error );
}
$stmt->bind_param( 's', $user->username );
$stmt->execute();
$res = $stmt->get_result();
while ( $data = $res->fetch_assoc() ) {
$page = str_replace( '_', ' ', trim( $data[ 'landingPage' ] ) );
if ( !in_array( $page, $pages, true ) ) {
$pages[] = $page;
}
}
}
sort( $pages, SORT_NATURAL | SORT_FLAG_CASE );
return $pages;
}
function is_cli(): bool {
return PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg';
}
function detect_protocol(): string {
if ( is_cli() ) {
throw new Error( 'Can\'t access server variables from CLI.' );
}
// Copied from MediaWiki's WebRequest::detectProtocol
if (
( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
(
isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'
)
) {
return 'https';
} else {
return 'http';
}
}
function get_server(): string {
if ( is_cli() ) {
throw new Error( 'Can\'t access server variables from CLI.' );
}
return detect_protocol() . '://' . $_SERVER['HTTP_HOST'];
}
function get_server_path(): string {
if ( is_cli() ) {
throw new Error( 'Can\'t access server variables from CLI.' );
}
return preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );
}
function get_csrf_token(): string {
global $useOAuth;
if ( !$useOAuth ) {
return '';
}
if ( empty( $_SESSION['csrf_token'] ) ) {
$_SESSION['csrf_token'] = bin2hex( random_bytes( 32 ) );
}
return $_SESSION['csrf_token'];
}
function check_csrf_token( string $token ): bool {
global $useOAuth;
if ( !$useOAuth ) {
return true;
}
if ( empty( $_SESSION['csrf_token'] ) ) {
return false;
}
return $_SESSION['csrf_token'] === $token;
}
function json_encode_clean( $value ) {
return json_encode( $value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
}