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

Commit 3ce9ab1

Browse files
committed
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 f8bf392 commit 3ce9ab1

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
@@ -316,9 +316,20 @@ function shell( $cmd, array $env = [] ): ?string {
316316
return $error ? null : $process->getOutput();
317317
}
318318

319-
function delete_wiki( string $wiki ): int {
319+
/**
320+
* Delete a wiki.
321+
*
322+
* @param string $wiki Wiki name
323+
* @param string|null $serverUri Server path - must be passed in if calling from the CLI
324+
* @return string|null Error message, null if successful
325+
*/
326+
function delete_wiki( string $wiki, string $serverUri = null ): ?string {
320327
global $mysqli;
321328

329+
if ( !$serverUri ) {
330+
$serverUri = get_server() . get_server_path();
331+
}
332+
322333
$wikiData = get_wiki_data( $wiki );
323334

324335
if ( $wikiData['deleted'] ) {
@@ -331,18 +342,17 @@ function delete_wiki( string $wiki ): int {
331342
'WIKI' => $wiki
332343
]
333344
);
345+
if ( $error ) {
346+
return 'Could not delete wiki files or database.';
347+
}
334348

335349
foreach ( $wikiData['announcedTasks'] as $task ) {
336-
// TODO: Deduplicate server/serverPath with variables in new.php
337-
$server = detectProtocol() . '://' . $_SERVER['HTTP_HOST'];
338-
$serverPath = preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );
339-
340350
$creator = $wikiData['creator'];
341351
post_phab_comment(
342352
'T' . $task,
343-
"Test wiki on [[ $server$serverPath | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
353+
"Test wiki on [[ $serverUri | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
344354
"\n" .
345-
"~~[[ $server$serverPath/wikis/$wiki/w/ ]]~~"
355+
"~~[[ $serverUri/wikis/$wiki/w/ ]]~~"
346356
);
347357
}
348358

@@ -355,7 +365,7 @@ function delete_wiki( string $wiki ): int {
355365
$stmt->execute();
356366
$stmt->close();
357367

358-
return $error;
368+
return $mysqli->error ?: null;
359369
}
360370

361371
$requestCache = [];
@@ -478,7 +488,14 @@ function get_repo_presets(): array {
478488
return $presets;
479489
}
480490

481-
function detectProtocol(): string {
491+
function is_cli(): bool {
492+
return PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg';
493+
}
494+
495+
function detect_protocol(): string {
496+
if ( is_cli() ) {
497+
throw new Error( 'Can\'t access server variables from CLI.' );
498+
}
482499
// Copied from MediaWiki's WebRequest::detectProtocol
483500
if (
484501
( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ||
@@ -493,6 +510,20 @@ function detectProtocol(): string {
493510
}
494511
}
495512

513+
function get_server(): string {
514+
if ( is_cli() ) {
515+
throw new Error( 'Can\'t access server variables from CLI.' );
516+
}
517+
return detect_protocol() . '://' . $_SERVER['HTTP_HOST'];
518+
}
519+
520+
function get_server_path(): string {
521+
if ( is_cli() ) {
522+
throw new Error( 'Can\'t access server variables from CLI.' );
523+
}
524+
return preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );
525+
}
526+
496527
function get_csrf_token(): string {
497528
global $useOAuth;
498529
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)