Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Commit 4140bd3

Browse files
edg2sMatmaRex
authored andcommitted
Create get_server & get_server_path methods
Also throw errors if these are called from the command line and create an is_cli method for that check. Fixes #336
1 parent d5d3b23 commit 4140bd3

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

includes.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,20 @@ function shell( $cmd, array $env = [] ): ?string {
322322
return $error ? null : $process->getOutput();
323323
}
324324

325-
function delete_wiki( string $wiki ): int {
325+
/**
326+
* Delete a wiki.
327+
*
328+
* @param string $wiki Wiki name
329+
* @param string|null $serverUri Server path - must be passed in if calling from the CLI
330+
* @return string|null Error message, null if successful
331+
*/
332+
function delete_wiki( string $wiki, string $serverUri = null ): ?string {
326333
global $mysqli;
327334

335+
if ( !$serverUri ) {
336+
$serverUri = get_server() . get_server_path();
337+
}
338+
328339
$wikiData = get_wiki_data( $wiki );
329340

330341
if ( $wikiData['deleted'] ) {
@@ -337,18 +348,17 @@ function delete_wiki( string $wiki ): int {
337348
'WIKI' => $wiki
338349
]
339350
);
351+
if ( $error ) {
352+
return 'Could not delete wiki files or database.';
353+
}
340354

341355
foreach ( $wikiData['announcedTasks'] as $task ) {
342-
// TODO: Deduplicate server/serverPath with variables in new.php
343-
$server = detectProtocol() . '://' . $_SERVER['HTTP_HOST'];
344-
$serverPath = preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );
345-
346356
$creator = $wikiData['creator'];
347357
post_phab_comment(
348358
'T' . $task,
349-
"Test wiki on [[ $server$serverPath | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
359+
"Test wiki on [[ $serverUri | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
350360
"\n" .
351-
"~~[[ $server$serverPath/wikis/$wiki/w/ ]]~~"
361+
"~~[[ $serverUri/wikis/$wiki/w/ ]]~~"
352362
);
353363
}
354364

@@ -361,7 +371,7 @@ function delete_wiki( string $wiki ): int {
361371
$stmt->execute();
362372
$stmt->close();
363373

364-
return $error;
374+
return $mysqli->error ?: null;
365375
}
366376

367377
$requestCache = [];
@@ -484,7 +494,14 @@ function get_repo_presets(): array {
484494
return $presets;
485495
}
486496

487-
function detectProtocol(): string {
497+
function is_cli(): bool {
498+
return PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg';
499+
}
500+
501+
function detect_protocol(): string {
502+
if ( is_cli() ) {
503+
throw new Error( 'Can\'t access server variables from CLI.' );
504+
}
488505
// Copied from MediaWiki's WebRequest::detectProtocol
489506
if (
490507
( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
@@ -499,6 +516,20 @@ function detectProtocol(): string {
499516
}
500517
}
501518

519+
function get_server(): string {
520+
if ( is_cli() ) {
521+
throw new Error( 'Can\'t access server variables from CLI.' );
522+
}
523+
return detect_protocol() . '://' . $_SERVER['HTTP_HOST'];
524+
}
525+
526+
function get_server_path(): string {
527+
if ( is_cli() ) {
528+
throw new Error( 'Can\'t access server variables from CLI.' );
529+
}
530+
return preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );
531+
}
532+
502533
function get_csrf_token(): string {
503534
global $useOAuth;
504535
if ( !$useOAuth ) {

migrateBranchesToSql.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
3+
require_once "includes.php";
4+
5+
if ( !is_cli() ) {
46
echo "This script must be run from the command line\n";
57
exit( 1 );
68
}
79

8-
require_once "includes.php";
9-
1010
$results = $mysqli->query( 'SELECT wiki FROM wikis WHERE !deleted' );
1111

1212
while ( $data = $results->fetch_assoc() ) {

migrateToSql.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
3+
require_once "includes.php";
4+
5+
if ( !is_cli() ) {
46
echo "This script must be run from the command line\n";
57
exit( 1 );
68
}
79

8-
require_once "includes.php";
9-
1010
function get_if_file_exists( $file ) {
1111
return file_exists( $file ) ? file_get_contents( $file ) : null;
1212
}

new.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
$language = trim( $_POST['language'] );
2626

2727
$wiki = substr( md5( $branch . $patches . time() ), 0, 10 );
28-
$server = detectProtocol() . '://' . $_SERVER['HTTP_HOST'];
29-
$serverPath = preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );
28+
$server = get_server();
29+
$serverPath = get_server_path();
3030

3131
$branchDesc = preg_replace( '/^origin\//', '', $branch );
3232

0 commit comments

Comments
 (0)