Experimental SSP again

This commit is contained in:
Floorb 2021-09-02 08:55:14 -04:00
parent 7ef5f25137
commit 73a2fad938
6 changed files with 32 additions and 41 deletions

View file

@ -1,14 +1,17 @@
<?php <?php
use PonePaste\Models\Paste;
header('Content-Type: application/json; charset=UTF-8'); header('Content-Type: application/json; charset=UTF-8');
define('IN_PONEPASTE', 1); define('IN_PONEPASTE', 1);
require_once('../includes/common.php'); require_once('../includes/common.php');
require_once('../includes/NonRetardedSSP.class.php'); require_once('../includes/NonRetardedSSP.class.php');
function transformDataRow($row) { function transformPaste(Paste $paste) {
$titleHtml = '<a href="/' . urlencode($row[0]) . '">' . pp_html_escape($row[1]) . '</a>'; $titleHtml = '<a href="/' . urlencode($paste->id) . '">' . pp_html_escape($paste->title) . '</a>';
$authorHtml = '<a href="/user/' . urlencode($row[2]) . '">' . pp_html_escape($row[2]) . '</a>'; $authorHtml = '<a href="/user/' . urlencode($paste->user->username) . '">' . pp_html_escape($paste->user->username) . '</a>';
$tagsHtml = tagsToHtml($row[3]); $tagsHtml = '';//tagsToHtml($row[3]);
return [ return [
$titleHtml, $titleHtml,
@ -17,18 +20,19 @@ function transformDataRow($row) {
]; ];
} }
$pastes = Paste::with([
'user' => function($query) {
$query->select('users.id', 'username');
},
'tags' => function($query) {
$query->select('tags.id', 'name', 'slug');
}
])->select(['id', 'user_id', 'title']);
$data = NonRetardedSSP::run( $data = NonRetardedSSP::run(
$conn, $_GET, $conn, $_GET, $pastes
'SELECT COUNT(*) FROM pastes WHERE pastes.visible = \'0\' AND pastes.title != \'\'',
'SELECT pastes.id AS id, title, users.username, GROUP_CONCAT(tags.name SEPARATOR \',\') AS tagsys FROM pastes
INNER JOIN users ON users.id = pastes.user_id
INNER JOIN paste_taggings on pastes.id = paste_taggings.paste_id
INNER JOIN tags ON tags.id = paste_taggings.tag_id
WHERE pastes.visible = \'0\' AND pastes.title != \'\'
GROUP BY pastes.id'
); );
$data['data'] = $data['data']->map('transformPaste');
$data['data'] = array_map('transformDataRow', $data['data']);
echo json_encode($data); echo json_encode($data);

View file

@ -5,6 +5,8 @@ use Illuminate\Database\Eloquent\Model;
class Tag extends Model { class Tag extends Model {
protected $table = 'tags'; protected $table = 'tags';
protected $fillable = ['name', 'slug'];
public $timestamps = false;
public static function getOrCreateByName(string $name) : Tag { public static function getOrCreateByName(string $name) : Tag {
$name = Tag::cleanTagName($name); $name = Tag::cleanTagName($name);
@ -19,19 +21,6 @@ class Tag extends Model {
]); ]);
} }
public static function replacePasteTags(DatabaseHandle $conn, int $pasteId, array $tags) {
$conn->query('DELETE FROM paste_taggings WHERE paste_id = ?', [$pasteId]);
foreach ($tags as $tagName) {
$tag = Tag::getOrCreateByName($conn, $tagName);
$conn->query('INSERT INTO paste_taggings (paste_id, tag_id) VALUES (?, ?)', [$pasteId, $tag->id]);
}
// FIXME: We need to get rid of tagsys.
$conn->query('UPDATE pastes SET tagsys = ? WHERE id = ?', [implode(',', $tags), $pasteId]);
}
/** /**
* Normalize a tag name, which involves downcasing it, normalizing smart quotes, trimming the string, and * Normalize a tag name, which involves downcasing it, normalizing smart quotes, trimming the string, and
* normalizing runs of whitespace to a single space. * normalizing runs of whitespace to a single space.

View file

@ -1,31 +1,31 @@
<?php <?php
use Illuminate\Database\Eloquent\Builder;
class NonRetardedSSP { class NonRetardedSSP {
public static function run(DatabaseHandle $conn, array $request, string $countQuery, string $query) { public static function run(DatabaseHandle $conn, array $request, Builder $builder) {
/* Some of these parameters might not be passed; zero is an OK default */ /* Some of these parameters might not be passed; zero is an OK default */
$draw = (int) @$request['draw']; $draw = (int) @$request['draw'];
$start = (int) @$request['start']; $start = (int) @$request['start'];
$length = (int) @$request['length']; $length = (int) @$request['length'];
/* figure out total records */ /* figure out total records */
$recordsTotal = (int) $conn->querySelectOne($countQuery, [], PDO::FETCH_NUM)[0]; $recordsTotal = $builder->count();
/* build query */ /* build query */
$params = []; $params = [];
if ($length != 0) { if ($length != 0) {
$query .= ' LIMIT ?'; $builder = $builder->limit($length);
array_push($params, $length);
if ($start != 0) { if ($start != 0) {
$query .= ' OFFSET ?'; $builder = $builder->offset($start);
array_push($params, $start);
} }
} }
/* fire it off */ /* fire it off */
$stmt = $conn->query($query, $params); $data = $builder->get();
$data = $stmt->fetchAll(PDO::FETCH_NUM);
return [ return [
'draw' => $draw, 'draw' => $draw,

View file

@ -8,6 +8,8 @@ require_once(__DIR__ . '/functions.php');
require_once(__DIR__ . '/DatabaseHandle.class.php'); require_once(__DIR__ . '/DatabaseHandle.class.php');
use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use PonePaste\Helpers\SessionHelper; use PonePaste\Helpers\SessionHelper;
use PonePaste\Models\Paste; use PonePaste\Models\Paste;
use PonePaste\Models\User; use PonePaste\Models\User;

View file

@ -221,7 +221,7 @@
const tabSystem = { const tabSystem = {
init() { init() {
document.querySelectorAll('.tabs-menu').forEach(tabMenu => { document.querySelectorAll('.tabs-menu').forEach(tabMenu => {
Array.from(tabMenu.children).forEach((child, ind) => { Array.from(tabMenu.children).forEach(child => {
child.addEventListener('click', () => { child.addEventListener('click', () => {
tabSystem.toggle(child.dataset.target); tabSystem.toggle(child.dataset.target);
}); });

View file

@ -163,11 +163,7 @@ $selectedloader = "$bg[$i]"; // set variable equal to which random filename was
<div class="column is-4 has-text-centered"> <div class="column is-4 has-text-centered">
<h1 class="title is-6" style="margin-bottom:0;"><?= $paste['title'] ?></h1> <h1 class="title is-6" style="margin-bottom:0;"><?= $paste['title'] ?></h1>
<small class="title is-6 has-text-weight-normal has-text-grey"> <small class="title is-6 has-text-weight-normal has-text-grey">
<?php if ($paste['member'] === null): ?> By <a href="<?= urlForMember($paste->user) ?>"><?= pp_html_escape($paste->user->username) ?></a>
Guest
<?php else: ?>
By <a href="<?= urlForMember($paste['member']) ?>"><?= $paste['member'] ?></a>
<?php endif; ?>
<br/> <br/>
Created: <?= $paste['created_at'] ?> Created: <?= $paste['created_at'] ?>
<br/> <br/>