FoolFuuka/Asagi

= Fuuka Imageboard Archival Standard (Fuuka) =

Note: Not to be confused with the FoolFuuka frontend, which uses the Asagi scraper. https://github.com/eksopl/fuuka/wiki/Sphinx-Search-Backend#gory-details

= Asagi Imageboard Archival Standard (Asagi) =

The Asagi Imageboard Archival Standard was developed by eksopl of Easymodo and the Foolz team under the direction of woxxy. It was developed to run the Foolz archiver, and has been the engine for the majority of archivers since the collapse of Archive.moe.

Three versions can be identified:


 * Mark I (2009) - Produced for Foolz.us. Maybe in use by Nyafuu, was in use by Loveisover.
 * Mark II (2015) - Produced for Archive.moe. Used by Fireden and arch.b4k.co.
 * Mark III (2019) - The final reference standard codified by the Bibliotheca Anonoma, in preparation for the development of new drop-in replacements.

Reference Implementation

 * Operating System: Ubuntu 14.04/16.04 LTS
 * Database: MySQL Compatible utf8mb4
 * Despite the fact that PostgreSQL is supported by Asagi Scraper, it is not supported by FoolFuuka.
 * Scraper: Asagi (Java) - https://github.com/bibanon/asagi
 * Frontend: FoolFuuka (PHP) - https://github.com/pleebe/FoolFuuka/tree/experimental
 * PHP Engine: Historically HHVM, PHP5.x compatible. Desuarchive and 4plebs now uses PHP 7.
 * Search: Sphinxsearch

New Implementation
Proposed, still needs to be constructed.


 * Database: Percona MariaDB TokuDB - Special variant of MariaDB with many unique optimizations.
 * Scraper: baystdev/Hayden (.NET C#)
 * Frontend: https://github.com/pleebe/FoolFuuka/tree/experimental
 * New Frontend: some python based 4chan API compatible middleware

Asagi
https://github.com/eksopl/asagi/wiki/Running-Asagi

Also check FoolFuuka/Install/Ubuntu16#Install_and_compile_Asagi_from_source.

FoolFuuka
https://blog.foolz.us/

Also check FoolFuuka/Install/Ubuntu16

= How Asagi does stuff =

Configuration
For example here is the config for Desuarchive:

{&quot;settings&quot;: { &quot;dumperEngine&quot;: &quot;DumperJSON&quot;, &quot;sourceEngine&quot;: &quot;YotsubaJSON&quot;,

&quot;boardSettings&quot;: { &quot;default&quot;: { &quot;engine&quot;: &quot;Mysql&quot;, &quot;database&quot;: &quot;asagi&quot;, &quot;host&quot;: &quot;localhost&quot;, &quot;username&quot;: &quot;asagi&quot;, &quot;password&quot;: &quot;YOUR_PASSWORD_HERE, &quot;charset&quot;: &quot;utf8mb4&quot;, &quot;path&quot;: &quot;/srv/foolfuuka/boards&quot;, &quot;updateFileLastModified&quot;: false, &quot;useOldDirectoryStructure&quot;: false, &quot;webserverGroup&quot;: &quot;www-data&quot;, &quot;thumbThreads&quot;: 2, &quot;mediaThreads&quot;: 2, &quot;newThreadsThreads&quot;: 6, &quot;deletedThreadsThresholdPage&quot;: 8, &quot;refreshDelay&quot;: 60, &quot;throttleAPI&quot;: false, &quot;throttleURL&quot;: &quot;i.4cdn.org&quot;, &quot;throttleMillisec&quot;: 1050, &quot;threadRefreshRate&quot;: 50 },

&quot;mlp&quot;: {}, &quot;qa&quot;: {}, &quot;aco&quot;: {}, &quot;tg&quot;: {}, &quot;d&quot;: {}, &quot;co&quot;: {}, &quot;a&quot;: {}, &quot;an&quot;: {}, &quot;k&quot;: {}, &quot;fit&quot;: {}, &quot;wsg&quot;: {&quot;mediaThreads&quot;: 0}, &quot;gif&quot;: {&quot;mediaThreads&quot;: 0}, &quot;r9k&quot;: {}, &quot;int&quot;: {}, &quot;c&quot;: {}, &quot;m&quot;: {}, &quot;vr&quot;: {}, &quot;his&quot;: {}, &quot;trash&quot;: {}, &quot;cgl&quot;: {}, &quot;g&quot;: {}, &quot;mu&quot;: {} } }}

Misc
Seperate thread for (thumbs, media, topic-fetch, topic-insert, post-delete) per board

AbstractDumper.java ln. 95:

How Asagi decides to update a thread:
In,   spawns an instance of its inner class   on a thread. In its  method,   loops indefinitely:


 * Wake up from sleeping (duration set by  in the configuration)
 * If the request 304s or errors, go to sleep
 * Go over the previous threads:
 * If this thread is in the current threads and it’s been modified, mark its modification timestamp and page number. Then, push it to.
 * If this thread is not in the current threads, it’s been deleted. Push it to.
 * Put the remaining threads in newTopics
 * Sleep until the delay expires
 * Sleep until the delay expires

The queue  is processed by  ’s inner class.

When Asagi does a thread update:
in : YotsubaJSON.java, ln. 88:

Loads thread JSON Decodes JSON For each post in the decoded thread JSON: Check if resto value is zero, and if so create a new thread from that post, updating lastmodified time to the time from fetching the JSON. If resto is zero: Add the post to the current thread. (What if two posts were resto==0? We’d break!)

What does Asagi do with a post in a thread?
Relevant files: &gt;YousubaJSON.java &gt;YotsubaAbstract.java

The entire thread is processed at once.

For OP:
In YousubaJSON.java, ln. 217:

Ensure post number is zero. Create a new Topic object (ln222):

Add the supplied OP to the thread as any other would be (ln. 224):

Return the Thread object. DONE

For reply:
In YotsubaJSON.java, (ln. 157:)

Ensure post number is valid. Ensure time is valid. Create new Post object.

If the JSON gave a filename not equal to null: Generate filename from JSON values.

Generate original filename from JSON values.

Generate preview original filename from JSON values.

Find the post’s capcode, and if not null: Manager -&gt; “G” Anything else: first character as uppercase.

Find the post hash (tripcode?) If the post hash is “Developer”, set the post hash value to “Dev”

If the post country is not null: Convert values “XX” or “A1” if they are used to null.

Pass through values from JSON YotsubaJSON.java (ln. 188 -&gt; 212), doing the following conversions:

Thread num to the current thread number:

OP status YotsubaJSON.java (ln. 197):

Sanitized title? YotsubaJSON.java (ln. 198):

See YotsubaAbstract.java (ln.83):

Sanitized name YotsubaJSON.java (ln. 200):

Date converted from NYC_TIMEZONE (ln. 202):

Sanitized? EXIF data YotsubaJSON.java (ln. 212):  Return the Post object.

How Asagi handles an image in a thread?
Files of note: &gt;Local.java - Saving image files

Local.java ln.201:

How Asagi deals with post deletions?
Relevant files: &gt;src.java &gt;src.java &gt;src.java

SQL That handles post deletion logic: SQL.java (ln. 94):

This is prepared into statement, SQL.java (ln.62):

Function that actually writes deleted flag to a post: SQL.java (ln. 267):

Class TopicFetcher has function run which calls findDeleted See AbstractDumper.java (ln. 268):

And AbstractDumper.java (ln. 380):

Function findDeleted checks Posts in a Topic to check if they have been removed in AbstractDumper.java (ln. 103):

Function markDeleted in Local.java calls function markDeleted from SQL.java: Local.java (ln. 185):

SQL.java (ln. 267):

How Asagi interacts with the database?
Relevant files: &gt;SQL.java &gt;Mysql.java

Inserting a thread: SQL.java, ln.196:  Each post in the current Topic object is fed through the insert statement sequentially.

How each value is processed between 4ch and the DB
4chan API JSON broken down to post level (YoutsubaJSON.java ln.97), then: ?PostJson are created/populated from the thread JSON data, with values keeping the names from the API? (PostJson.java, ln.4) (DISREGARD CAPITALIZATION, GOOG DOCS FUCKS WITH IT. EVERYTHING IS LOWERCASE) DB column names referenced here: (SQL.java ln.79):

(poster_ip, num, subnum, thread_num, op, timestamp, preview_orig, preview_w, preview_h, media_filename, media_w, media_h, media_size, media_hash, media_orig, spoiler, deleted, capcode, email, name, trip, title, comment, delpass, sticky, locked, poster_hash, poster_country, exif)

“poster_ip”
Presumably always NULL? PROVEME Does not appear to ever be initialized to a value in the java source code as far as i can tell?

“no” - &gt; “num”
Post ID number from 4chan. Passed through as-is.

“subnum”
Ghostpost ID number for foolfuuka. Always zero.?

“Thread_num”
Thread IP number, always Post ID number of OP. “num” of thread OP (YoutsubaJSON.java, ln. 196):

“Time” -&gt;“timestamp”
Timestamp of post. 4ch API provides as milliseconds since 1 JAN 1970 in USA NYC timezone. DB stores as ?(Probably milliseconds since 1 JAN 1970 UTC+0) (YoutsubaJSON.java, ln. 202):

“resto” -&gt; (If resto is 0, use value from “num”) -&gt; op
Is this the OP of the thread? (YoutsubaJSON.java, ln. 197):

N/A -&gt; Preview_orig
(YoutsubaJSON.java, ln. 170):

“tn_w” -&gt; “preview_w”
Width of media thumbnail (YoutsubaJSON.java, ln. 193):

“tn_h” -&gt; “preview_h”
Height of media thumbnail. (YoutsubaJSON.java, ln. 170):

N/A -&gt; media_filename
(YoutsubaJSON.java, ln. 168):

“W” -&gt; “media_w”
(YoutsubaJSON.java, ln. 191):

“h” -&gt; “media_h”
(YoutsubaJSON.java, ln. 192):

“Media_size”
(YoutsubaJSON.java, ln. 190):

“Media_hash”
(YoutsubaJSON.java, ln. 189):

N/A -&gt; “Media_orig”
(YoutsubaJSON.java, ln. 169):

“Spoiler”
(YoutsubaJSON.java, ln. 204):

N/A -&gt; “Deleted”
Set initially as false, then later updated if post is later absent from thread during subsequent updates. (YoutsubaJSON.java, ln. 205):

“Capcode” -&gt; “Capcode”
(YoutsubaJSON.java, ln. 173):

String capcode = pj.getCapcode; if (capcode != null) { if (capcode.equals(&quot;manager&quot;) || capcode.equals(&quot;Manager&quot;)) { capcode = &quot;G&quot;; } else { capcode = capcode.substring(0, 1).toUpperCase; } }

“email” -&gt; “Email”
(YoutsubaJSON.java, ln. 199):

“Name” -&gt; (cleanSimple) -&gt; “Name”
(YoutsubaJSON.java, ln. 200):

“trip” -&gt; “Trip”
(YoutsubaJSON.java, ln. XX):

“sub” -&gt; (cleanSimple) -&gt; “Title”
(YoutsubaJSON.java, ln. 198):

“com” -&gt; (doClean) -&gt; “Comment”
(YoutsubaJSON.java, ln. 203):

N/A -&gt; “Delpass”
TODO (YoutsubaJSON.java, ln. XX):

“sticky” -&gt; “Sticky”
(YoutsubaJSON.java, ln. 206):

“closed”, “archived” -&gt; “closed” AND (NOT “archived”)) -&gt; “Locked”
(YoutsubaJSON.java, ln. 207):  (SQL.java, ln. 78 - 86):   (SQL.java, ln. 236):

“id” -&gt; “Poster_hash”
(YoutsubaJSON.java, ln. 182):

String posterHash = pj.getId; if(posterHash != null &amp;&amp; posterHash.equals(&quot;Developer&quot;)) posterHash = &quot;Dev&quot;;

“country” -&gt; “Poster_country”
(YoutsubaJSON.java, ln. 185-186):

String posterCountry = pj.getCountry; if(posterCountry != null &amp;&amp; (posterCountry.equals(&quot;XX&quot;) || posterCountry.equals(&quot;A1&quot;))) posterCountry = null;

lots -&gt; “exif”
(YoutsubaJSON.java, ln. 212):  (YoutsubaAbstract.java, ln. 137):

Images table values:
These seem to be handled by triggers that run on post insert.

N/A -&gt; (Incremental integer) -&gt; “media_id”
This is simply an autoincrementing integer value. Set by DB engine if new image. Set by trigger if already-seen image. (Triggers.sql ln.119-139): - If the md5 is already in the DB

md5 -&gt; (N/A) -&gt; “media_hash”
The base64-encoded md5 hash of the media file as given by 4ch. (SQL.java ln.77-87) Omitted for brevity

N/A -&gt; (Local filepath to full image) -&gt; “media”
The relative path to the image on disk. (Triggers.sql ln.119-139):

N/A -&gt; (Local filepath to OP thumbnail) -&gt; “preview_op”
The relative path to the image on disk. (Triggers.sql ln.119-139):

N/A -&gt; (Local filepath to reply thumbnail) -&gt; “preview_reply”
The relative path to the image on disk. (Triggers.sql ln.119-139):

N/A -&gt; (incrementer) -&gt; “total”
The number of posts that refer to this row.

(Triggers.sql ln.123):

(Triggers.sql ln.127):

N/A -&gt; (N/A) -&gt; “banned”
Not set by Asagi, but observed to prevent downloading banned files (Triggers.sql ln.119-139):

Table definition
(Boards.sql ln.38-49): - Table definition

CREATE TABLE %%BOARD%%_images ( media_id SERIAL NOT NULL, media_hash character varying(25) NOT NULL, media character varying(20), preview_op character varying(20), preview_reply character varying(20), total integer NOT NULL DEFAULT ‘0’, banned smallint NOT NULL DEFAULT ‘0’, PRIMARY KEY (media_id), UNIQUE (media_hash) );

Image insert procedure
(Triggers.sql ln.119-139): - Image insert procedure

DROP PROCEDURE IF EXISTS “insert_image_%%BOARD%%”; CREATE PROCEDURE “insert_image_%%BOARD%%” (n_media_hash VARCHAR(25), n_media VARCHAR(20), n_preview VARCHAR(20), n_op INT) BEGIN IF n_op = 1 THEN INSERT INTO &quot;%%BOARD%%_images&quot; (media_hash, media, preview_op, total) VALUES (n_media_hash, n_media, n_preview, 1) ON DUPLICATE KEY UPDATE media_id = LAST_INSERT_ID(media_id), total = (total + 1), preview_op = COALESCE(preview_op, VALUES(preview_op)), media = COALESCE(media, VALUES(media)); ELSE INSERT INTO &quot;%%BOARD%%_images&quot; (media_hash, media, preview_reply, total) VALUES (n_media_hash, n_media, n_preview, 1) ON DUPLICATE KEY UPDATE media_id = LAST_INSERT_ID(media_id), total = (total + 1), preview_reply = COALESCE(preview_reply, VALUES(preview_reply)), media = COALESCE(media, VALUES(media)); END IF; END; ```

4ch -&gt; decode JSON -&gt; make Post objects
API Date is retrieved from 4ch and put into TopicJson objects, ready for handling posts in each of those topics. (YoutsubaJSON.java ln .89): - Get 4ch API JSON  (YoutsubaJSON.java ln .97) - Decode JSON into TopicJson objects   (YoutsubaJSON.java ln .93) V For a single topic, each post if decoded into a Post object Post object -&gt; added to Topic object (YoutsubaJSON.java ln .93): - Create Topic object

(YoutsubaJSON.java ln .109): - OP

(YoutsubaJSON.java ln .116): - Reply

V

(AbstractDumper.java ln 303):

V

Image processed somehow?

Full image fetching begins

(AbstractDumper.java ln 161): - Fullsize media downloader thread  (AbstractDumper.java lln. 169): - Grab from queue thing   (AbstractDumper.java lln. 173): - Try to handle the media for one post   (Local.java ln. 201): - Handler for a post with media   (Local.java ln. 201): - Interact with DB for this media

If there is information for this media in the DB, retrieve it. If any new information exists about this media that is not already in the DB, add that to the DB entry. (SQL.java ln. 289): - Interact with DB for this media

(SQL.java ln. 342-347): - Decide if media row needs an update

boolean mediaUpdate = media.getMedia == null; boolean previewOpUpdate = media.getPreviewOp == null &amp;&amp; post.isOp; boolean previewReplyUpdate = media.getPreviewReply == null &amp;&amp; !post.isOp; // Update media row in _images table when any of its entries are null and we actually have it if(mediaUpdate || previewOpUpdate || previewReplyUpdate) { (SQL.java ln. 349-373): - Add values to DB for this media

if(mediaUpdate) { updateMediaStmt.setString(1, post.getMedia); updateMediaStmt.setString(2, post.getMediaHash); updateMediaStmt.executeUpdate; } if(previewOpUpdate) { updatePreviewOpStmt.setString(1, post.getPreview); updatePreviewOpStmt.setString(2, post.getMediaHash); updatePreviewOpStmt.executeUpdate; } if(previewReplyUpdate) { updatePreviewReplyStmt.setString(1, post.getPreview); updatePreviewReplyStmt.setString(2, post.getMediaHash); updatePreviewReplyStmt.executeUpdate; } conn.commit; (SQL.java ln. 62-66): - Preparing for selecting / updating media

selectMediaStmt = conn.prepareStatement(selectMediaQuery); updateMediaStmt = conn.prepareStatement(updateMediaQuery); updatePreviewOpStmt = conn.prepareStatement(updatePreviewOpQuery); updatePreviewReplyStmt = conn.prepareStatement(updatePreviewReplyQuery); (SQL.java ln. 96-103): - SQL for selecting / updating media

this.selectMediaQuery = String.format(&quot;SELECT * FROM \&quot;%s_images\&quot; WHERE media_hash = ?&quot;, this.table); this.updateMediaQuery = String.format(&quot;UPDATE \&quot;%s_images\&quot; SET media = ? WHERE media_hash = ?&quot;, this.table); this.updatePreviewOpQuery = String.format(&quot;UPDATE \&quot;%s_images\&quot; SET preview_op = ? WHERE media_hash = ?&quot;, this.table); this.updatePreviewReplyQuery = String.format(&quot;UPDATE \&quot;%s_images\&quot; SET preview_reply = ? WHERE media_hash = ?&quot;, this.table);

Files of note:
SQL.java YoutsubaJSON.java Topic.java - Class definition boards.sql (ln.52) - Table definition triggers.sql

YoutsubaJSON.java:
(ln.217) “private Topic makeThreadFromJson(PostJson pj) throws ContentParseException {”

Topic.java
(Ln. 22)

Data flow: 4ch -&gt; Asagi -&gt; DB

?Seems to be handled by DB triggers.?

References:
Asagi source code used: Bibabon repo 2019-7 retrieved from https://github.com/bibanon/asagi with commit https://github.com/bibanon/asagi/commit/dace6f01664d887f9f60dfdec341e626685b542f

Hayden source code : https://github.com/bbepis/Hayden