Skip to content

Commit

Permalink
merge upstream changes - 2f2f8e2 and later
Browse files Browse the repository at this point in the history
  • Loading branch information
Giraffaman committed Jan 4, 2024
2 parents d3206f0 + 889f595 commit 4e119ab
Show file tree
Hide file tree
Showing 53 changed files with 525 additions and 336 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8000],

// Uncomment the next line to run commands after the container is created.
"updateContentCommand": "composer install",
"postCreateCommand": "./.docker/entrypoint.sh unitd --no-daemon --control unix:/var/run/control.unit.sock",
"containerEnv": {
"UID": "2000",
"GID": "2000",
"UPLOAD_MAX_FILESIZE": "50M",
"INSTALL_DSN": "sqlite:data/shimmie.dev.sqlite"
},
"customizations": {
"vscode": {
Expand Down
29 changes: 1 addition & 28 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,7 @@ jobs:
extensions: mbstring

- name: Set up database
run: |
mkdir -p data/config
if [[ "${{ matrix.database }}" == "pgsql" ]]; then
sudo systemctl start postgresql ;
psql --version ;
sudo -u postgres psql -c "SELECT set_config('log_statement', 'all', false);" -U postgres ;
sudo -u postgres psql -c "CREATE USER shimmie WITH PASSWORD 'shimmie';" -U postgres ;
sudo -u postgres psql -c "CREATE DATABASE shimmie WITH OWNER shimmie;" -U postgres ;
fi
if [[ "${{ matrix.database }}" == "mysql" ]]; then
sudo systemctl start mysql ;
mysql --version ;
mysql -e "SET GLOBAL general_log = 'ON';" -uroot -proot ;
mysql -e "CREATE DATABASE shimmie;" -uroot -proot ;
fi
if [[ "${{ matrix.database }}" == "sqlite" ]]; then
sudo apt update && sudo apt-get install -y sqlite3 ;
sqlite3 --version ;
fi
run: ./tests/setup-db.sh "${{ matrix.database }}"

- name: Check versions
run: php -v && composer -V
Expand All @@ -116,15 +98,6 @@ jobs:

- name: Run test suite
run: |
if [[ "${{ matrix.database }}" == "pgsql" ]]; then
export TEST_DSN="pgsql:user=shimmie;password=shimmie;host=127.0.0.1;dbname=shimmie"
fi
if [[ "${{ matrix.database }}" == "mysql" ]]; then
export TEST_DSN="mysql:user=root;password=root;host=127.0.0.1;dbname=shimmie"
fi
if [[ "${{ matrix.database }}" == "sqlite" ]]; then
export TEST_DSN="sqlite:data/shimmie.sqlite"
fi
if [[ "${{ matrix.php }}" == "8.3" ]]; then
vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-clover=data/coverage.clover
else
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN apt update && \
php${PHP_VERSION}-pgsql php${PHP_VERSION}-mysql php${PHP_VERSION}-sqlite3 php${PHP_VERSION}-curl \
curl imagemagick zip unzip unit unit-php gettext && \
rm -rf /var/lib/apt/lists/*
RUN ln -sf /dev/stderr /var/log/unit.log

# Install dev packages
# Things which are only needed during development - Composer has 100MB of
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ To Dos:
[![Code Coverage](https://scrutinizer-ci.com/g/shish/shimmie2/badges/coverage.png?b=main)](https://scrutinizer-ci.com/g/shish/shimmie2/?branch=main)
[![Matrix](https://matrix.to/img/matrix-badge.svg)](https://matrix.to/#/#shimmie:matrix.org)

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/shish/shimmie2?quickstart=1)

# Documentation

Expand Down
65 changes: 35 additions & 30 deletions core/basepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use MicroHTML\HTMLElement;

use function MicroHTML\{emptyHTML,rawHTML,HTML,HEAD,BODY};

require_once "core/event.php";

enum PageMode: string
Expand Down Expand Up @@ -530,27 +532,32 @@ protected function get_nav_links(): array
*/
public function render()
{
$head_html = $this->head_html();
$body_html = $this->body_html();
global $config, $user;

$head = $this->head_html();
$body = $this->body_html();

print <<<EOD
<!doctype html>
<html lang="en">
$head_html
$body_html
</html>
EOD;
$body_attrs = [
"data-userclass" => $user->class->name,
];

print emptyHTML(
rawHTML("<!doctype html>"),
HTML(
["lang" => "en"],
HEAD(rawHTML($head)),
BODY($body_attrs, rawHTML($body))
)
);
}

protected function head_html(): string
{
$html_header_html = $this->get_all_html_headers();

return "
<head>
<title>{$this->title}</title>
$html_header_html
</head>
<title>{$this->title}</title>
$html_header_html
";
}

Expand Down Expand Up @@ -585,22 +592,20 @@ protected function body_html(): string
$footer_html = $this->footer_html();
$flash_html = $this->flash ? "<b id='flash'>".nl2br(html_escape(implode("\n", $this->flash)))."</b>" : "";
return "
<body>
<header>
<h1$wrapper>{$this->heading}</h1>
$sub_block_html
</header>
<nav>
$left_block_html
</nav>
<article>
$flash_html
$main_block_html
</article>
<footer>
$footer_html
</footer>
</body>
<header>
<h1$wrapper>{$this->heading}</h1>
$sub_block_html
</header>
<nav>
$left_block_html
</nav>
<article>
$flash_html
$main_block_html
</article>
<footer>
$footer_html
</footer>
";
}

Expand All @@ -615,7 +620,7 @@ protected function footer_html(): string
<a href=\"https://code.shishnet.org/shimmie2/\">Shimmie</a> &copy;
<a href=\"https://www.shishnet.org/\">Shish</a> &amp;
<a href=\"https://github.com/shish/shimmie2/graphs/contributors\">The Team</a>
2007-2023,
2007-2024,
based on the Danbooru concept.
$debug
$contact
Expand Down
23 changes: 14 additions & 9 deletions core/basethemelet.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ public function build_thumb_html(Image $image): HTMLElement
}
}

$attrs = [
"href" => $view_link,
"class" => "thumb shm-thumb shm-thumb-link $custom_classes",
"data-tags" => $tags,
"data-height" => $image->height,
"data-width" => $image->width,
"data-mime" => $image->get_mime(),
"data-post-id" => $id,
];
if(Extension::is_enabled(RatingsInfo::KEY)) {
$attrs["data-rating"] = $image->rating;
}

return A(
[
"href" => $view_link,
"class" => "thumb shm-thumb shm-thumb-link $custom_classes",
"data-tags" => $tags,
"data-height" => $image->height,
"data-width" => $image->width,
"data-mime" => $image->get_mime(),
"data-post-id" => $id,
],
$attrs,
IMG(
[
"id" => "thumb_$id",
Expand Down
1 change: 1 addition & 0 deletions core/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public function onDataUpload(DataUploadEvent $event)
$event->metadata['tags'] = $existing->get_tag_list();

$image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata);
$image->posted = $existing->posted;
send_event(new ImageReplaceEvent($event->replace_id, $image));
$_id = $event->replace_id;
assert(!is_null($_id));
Expand Down
10 changes: 5 additions & 5 deletions core/imageboard/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
* @param string $base
* @return array
*/
function add_dir(string $base): array
function add_dir(string $base, ?array $extra_tags = []): array
{
$results = [];

foreach (list_files($base) as $full_path) {
$short_path = str_replace($base, "", $full_path);
$filename = basename($full_path);

$tags = path_to_tags($short_path);
$result = "$short_path (".str_replace(" ", ", ", $tags).")... ";
$tags = array_merge(path_to_tags($short_path), $extra_tags);
$result = "$short_path (".implode(", ", $tags).")... ";
try {
add_image($full_path, $filename, $tags);
$result .= "ok";
Expand All @@ -39,11 +39,11 @@ function add_dir(string $base): array
/**
* Sends a DataUploadEvent for a file.
*/
function add_image(string $tmpname, string $filename, string $tags, ?string $source = null): DataUploadEvent
function add_image(string $tmpname, string $filename, array $tags, ?string $source = null): DataUploadEvent
{
return send_event(new DataUploadEvent($tmpname, [
'filename' => pathinfo($filename, PATHINFO_BASENAME),
'tags' => Tag::explode($tags),
'tags' => $tags,
'source' => $source,
]));
}
Expand Down
20 changes: 20 additions & 0 deletions core/imageboard/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public static function find_images_iterable(int $start = 0, ?int $limit = null,
}
}

/**
* Get a specific set of images, in the order that the set specifies,
* with all the search stuff (rating filters etc) taken into account
*
* @param int[] $ids
* @return Image[]
*/
public static function get_images(array $ids): array
{
$visible_images = [];
foreach(Search::find_images(tags: ["id=" . implode(",", $ids)]) as $image) {
$visible_images[$image->id] = $image;
}
$visible_ids = array_keys($visible_images);

$visible_popular_ids = array_filter($ids, fn ($id) => in_array($id, $visible_ids));
$images = array_map(fn ($id) => $visible_images[$id], $visible_popular_ids);
return $images;
}

/*
* Image-related utility functions
*/
Expand Down
19 changes: 8 additions & 11 deletions core/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ function ask_questions()
";
}

$db_s = in_array(DatabaseDriverID::SQLITE->value, $drivers) ? '<option value="'. DatabaseDriverID::SQLITE->value .'">SQLite</option>' : "";
$db_m = in_array(DatabaseDriverID::MYSQL->value, $drivers) ? '<option value="'. DatabaseDriverID::MYSQL->value .'">MySQL</option>' : "";
$db_p = in_array(DatabaseDriverID::PGSQL->value, $drivers) ? '<option value="'. DatabaseDriverID::PGSQL->value .'">PostgreSQL</option>' : "";
$db_s = in_array(DatabaseDriverID::SQLITE->value, $drivers) ? '<option value="'. DatabaseDriverID::SQLITE->value .'">SQLite</option>' : "";

$warn_msg = $warnings ? "<h3>Warnings</h3>".implode("\n<p>", $warnings) : "";
$err_msg = $errors ? "<h3>Errors</h3>".implode("\n<p>", $errors) : "";
Expand All @@ -132,9 +132,9 @@ function ask_questions()
<tr>
<th>Type:</th>
<td><select name="database_type" id="database_type" onchange="update_qs();">
$db_m
$db_s
$db_m
$db_p
$db_s
</select></td>
</tr>
<tr class="dbconf mysql pgsql">
Expand All @@ -161,13 +161,9 @@ function q(n) {
return document.querySelectorAll(n);
}
function update_qs() {
Array.prototype.forEach.call(q('.dbconf'), function(el, i){
el.style.display = 'none';
});
q('.dbconf').forEach(el => el.style.display = 'none');
let seldb = q("#database_type")[0].value || "none";
Array.prototype.forEach.call(q('.'+seldb), function(el, i){
el.style.display = null;
});
q('.'+seldb).forEach(el => el.style.display = null);
}
</script>
</form>
Expand All @@ -178,8 +174,9 @@ function update_qs() {
The username provided must have access to create tables within the database.
</p>
<p class="dbconf sqlite">
For SQLite the database name will be a filename on disk, relative to
where shimmie was installed.
SQLite with default settings is fine for tens of users with thousands
of images. For thousands of users or millions of images, postgres is
recommended.
</p>
<p class="dbconf none">
Drivers can generally be downloaded with your OS package manager;
Expand Down
9 changes: 2 additions & 7 deletions core/microhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use MicroHTML\HTMLElement;

use function MicroHTML\emptyHTML;
use function MicroHTML\{emptyHTML};
use function MicroHTML\A;
use function MicroHTML\FORM;
use function MicroHTML\INPUT;
Expand All @@ -16,12 +16,7 @@
use function MicroHTML\P;
use function MicroHTML\SELECT;
use function MicroHTML\SPAN;
use function MicroHTML\TABLE;
use function MicroHTML\THEAD;
use function MicroHTML\TFOOT;
use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
use function MicroHTML\{TABLE,THEAD,TFOOT,TR,TH,TD};

function SHM_FORM(string $target, string $method = "POST", bool $multipart = false, string $form_id = "", string $onsubmit = "", string $name = ""): HTMLElement
{
Expand Down
6 changes: 5 additions & 1 deletion core/polyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function array_iunique(array $array): array
*/
function ip_in_range(string $IP, string $CIDR): bool
{
list($net, $mask) = explode("/", $CIDR);
$parts = explode("/", $CIDR);
if(count($parts) == 1) {
$parts[1] = "32";
}
list($net, $mask) = $parts;

$ip_net = ip2long($net);
$ip_mask = ~((1 << (32 - (int)$mask)) - 1);
Expand Down
2 changes: 1 addition & 1 deletion core/sys_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ function _d(string $name, $value): void
_d("BASE_HREF", null); // string force a specific base URL (default is auto-detect)
_d("TRACE_FILE", null); // string file to log performance data into
_d("TRACE_THRESHOLD", 0.0); // float log pages which take more time than this many seconds
_d("REVERSE_PROXY_X_HEADERS", false); // boolean get request IPs from "X-Real-IP" and protocol from "X-Forwarded-Proto" HTTP headers
_d("TRUSTED_PROXIES", []); // array trust "X-Real-IP" / "X-Forwarded-For" / "X-Forwarded-Proto" headers from these IP ranges
6 changes: 6 additions & 0 deletions core/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ protected function page_to_text(string $section = null): string
}
}

/**
* Assert that the page contains the given text somewhere in the blocks
*/
protected function assert_text(string $text, string $section = null): void
{
$this->assertStringContainsString($text, $this->page_to_text($section));
Expand All @@ -171,6 +174,9 @@ protected function assert_no_text(string $text, string $section = null): void
$this->assertStringNotContainsString($text, $this->page_to_text($section));
}

/**
* Assert that the page contains the given text somewhere in the binary data
*/
protected function assert_content(string $content): void
{
global $page;
Expand Down
Loading

0 comments on commit 4e119ab

Please sign in to comment.