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

Commit

Permalink
Create get_server & get_server_path methods
Browse files Browse the repository at this point in the history
Also throw errors if these are called from the command line
and create an is_cli method for that check.

Fixes #336
  • Loading branch information
edg2s committed Mar 13, 2022
1 parent d65a252 commit 3ce41d3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
49 changes: 40 additions & 9 deletions includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,20 @@ function shell( $cmd, array $env = [] ): ?string {
return $error ? null : $process->getOutput();
}

function delete_wiki( string $wiki ): int {
/**
* 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'] ) {
Expand All @@ -337,18 +348,17 @@ function delete_wiki( string $wiki ): int {
'WIKI' => $wiki
]
);
if ( $error ) {
return 'Could not delete wiki files or database.';
}

foreach ( $wikiData['announcedTasks'] as $task ) {
// TODO: Deduplicate server/serverPath with variables in new.php
$server = detectProtocol() . '://' . $_SERVER['HTTP_HOST'];
$serverPath = preg_replace( '`/[^/]*$`', '', $_SERVER['REQUEST_URI'] );

$creator = $wikiData['creator'];
post_phab_comment(
'T' . $task,
"Test wiki on [[ $server$serverPath | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
"Test wiki on [[ $serverUri | Patch demo ]] " . ( $creator ? ' by ' . $creator : '' ) . " using patch(es) linked to this task was **deleted**:\n" .
"\n" .
"~~[[ $server$serverPath/wikis/$wiki/w/ ]]~~"
"~~[[ $serverUri/wikis/$wiki/w/ ]]~~"
);
}

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

return $error;
return $mysqli->error ?: null;
}

$requestCache = [];
Expand Down Expand Up @@ -484,7 +494,14 @@ function get_repo_presets(): array {
return $presets;
}

function detectProtocol(): string {
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' ) ||
Expand All @@ -499,6 +516,20 @@ function detectProtocol(): string {
}
}

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 ) {
Expand Down
6 changes: 3 additions & 3 deletions migrateBranchesToSql.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
require_once "includes.php";

if ( !is_cli() ) {
echo "This script must be run from the command line\n";
exit( 1 );
}

require_once "includes.php";

$results = $mysqli->query( 'SELECT wiki FROM wikis WHERE !deleted' );

while ( $data = $results->fetch_assoc() ) {
Expand Down
6 changes: 3 additions & 3 deletions migrateToSql.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {
require_once "includes.php";

if ( !is_cli() ) {
echo "This script must be run from the command line\n";
exit( 1 );
}

require_once "includes.php";

function get_if_file_exists( $file ) {
return file_exists( $file ) ? file_get_contents( $file ) : null;
}
Expand Down
4 changes: 2 additions & 2 deletions new.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
$language = trim( $_POST['language'] );

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

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

Expand Down

0 comments on commit 3ce41d3

Please sign in to comment.