Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ifarchive-tuid-report: normalize urls #346

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"glenscott/url-normalizer": "^1.4"
}
}
60 changes: 60 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docker-php-composer-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -ex

cp ./php-composer-setup.sh www
cp ./composer.json www
cp ./composer.lock www
docker compose exec --privileged web bash ./php-composer-setup.sh
docker compose exec --privileged web bash
rm www/php-composer-setup.sh
mv www/composer.json .
mv www/composer.lock .
23 changes: 23 additions & 0 deletions php-composer-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

apt-get install -y p7zip-full

# https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
# this gets run in the image by docker-php-composer-setup.sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi

php composer-setup.php
RESULT=$?
rm composer-setup.php
mv composer.phar /usr/local/bin/composer
exit $RESULT
18 changes: 15 additions & 3 deletions www/ifarchive-tuid-report
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
include_once "util.php";
include_once "pagetpl.php";
include_once "dbconnect.php";
require_once 'vendor/autoload.php';

$json = $_GET['json'] ?? false;
$refresh = $_GET['refresh'] ?? false;
Expand Down Expand Up @@ -87,9 +88,20 @@ $db = dbConnect();

$output = [];
foreach($tuids as $tuid => $path) {
$result = mysqli_execute_query($db, "select url from gamelinks where gameid = ? and url like ?", [$tuid, "%$path"]);
$row = mysqli_fetch_row($result);
if (!$row) {
// normalize the path using URL normalizer. First we make it an absolute URL, normalize it, then we strip the absolute part off
$path = substr((new URL\Normalizer("https://ifarchive.org/$path"))->normalize(), strlen("https://ifarchive.org/"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put https://ifarchive.org/ in a variable since it's used twice, and explicitly required to be identical for this "get suffix" technique.

$result = mysqli_execute_query($db, "select url from gamelinks where gameid = ?", [$tuid]);
$found = false;
foreach ($result->fetch_all(MYSQLI_NUM) as [$url]) {
$url = (new URL\Normalizer($url))->normalize();
// check whether the $url ends with $path
if (substr_compare($url, $path, -strlen($path)) === 0) {
$found = true;
break;
}
}

if (!$found) {
[$title] = mysqli_fetch_row(mysqli_execute_query($db, "select title from games where id = ?", [$tuid]));
$output[]= [
"tuid" => $tuid,
Expand Down
25 changes: 25 additions & 0 deletions www/vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
}

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit12212f8390cfa485f99929bb7a8f0797::getLoader();
Loading