<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org (initial MediaJob class
 *      and subclasses based on work of Pooja Mishra for her master's)
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\library\media_jobs;

use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;
use seekquarry\yioop\library\CrawlConstants;
use seekquarry\yioop\library\FetchUrl;
use seekquarry\yioop\library\storage_formats\IndexShard;
use seekquarry\yioop\library\language_processing\PhraseParser;
use seekquarry\yioop\library\UrlParser;
use seekquarry\yioop\models\GroupModel;
use seekquarry\yioop\controllers\CrawlController;

/**
 * A media job to add thumbnails and animated thumbnails for wiki page
 * media resources that have just been viewed in the browser. This is
 * detected by the method:  GroupModel::getGroupPageResourceUrls which write a
 * file GroupModel::NEEDS_THUMBS_DIR . L\crawlHash($folder) . ".txt" to with
 * information about the folders needing thumbs.
 */
class WikiThumbDetailJob extends MediaJob
{
    /**
     * Time last checked if there were folders missing thumbs
     * @var int
     */
    public $update_time;
    /**
     * Model responsible for getting and retrieval Wiki info for this
     * instance of Yioop
     * @var GroupModel
     */
    public $group_model;
    /**
     * Initializes the last update time to far in the past so, missing thumb
     * folders will get processed immediately when MediaUpdater first run.
     * immediately updated. Sets up a GroupModel object so can use
     * its method for genrasting thumbs.
     */
    public function init()
    {
        $this->update_time = 0;
        $this->name_server_does_client_tasks = true;
        $this->name_server_does_client_tasks_only = true;
        if(!empty($this->controller)) {
            $group_model = $this->controller->model("group");
        } else {
            $group_model = new GroupModel();
        }
        $this->group_model = $group_model;
        C\nsconddefine("WIKI_UPDATE_INTERVAL", C\ONE_MINUTE);
    }
    /**
     * Only update if its been more than CWIKI_UPDATE_INTERVAL since the last
     * update (default one minute)
     *
     * @return bool whether its been an hour since the last update
     */
    public function checkPrerequisites()
    {
        $time = time();
        $delta = $time - $this->update_time;
        if ($delta > C\WIKI_UPDATE_INTERVAL) {
            $this->update_time = $time;
            L\crawlLog("Performing wiki thumb detail job update");
            return true;
        }
        L\crawlLog("Time since last update not exceeded, skipping wiki
            update");
        return false;
    }
    /**
     * Gets all folder files describing Wiki pages with resources that
     * need thumbs. Then creates thumbs for them. Since the wiki is only on
     * the name server all tasks will be nondistributedTasks.
     */
    public function nondistributedTasks()
    {
        $need_thumbs_folders = glob(GroupModel::NEEDS_THUMBS_DIR ."/*.txt");
        $this->tasks = $need_thumbs_folders;
        $this->doTasks($need_thumbs_folders);
    }
    /**
     * Given a list of file names of file containing information about which
     * folders need thumbs, reads in those files, and generates thumbs for
     * the corresponding folders.
     *
     * @param array $tasks a list of filenames containing folder names of
     *  Yioop wiki folders that need thumbs
     */
    public function doTasks($tasks)
    {
        foreach ($tasks as $needs_thumbs_folder) {
            $folder_info = unserialize(file_get_contents($needs_thumbs_folder));
            if (isset($folder_info[3])) {
                if (!$folder_info[1]) {
                    $sub_path = substr($folder_info[0],
                        strlen($folder_info[2]) + 1);
                    $folder_info[1] = $folder_info[3] . "/" . $sub_path;
                }
                $this->generateThumbs($folder_info[0], $folder_info[1]);
            }
            unlink($needs_thumbs_folder);
        }
    }
    /**
     * Used to generate in $thumb_folder all the missing thumbnails for
     * resources in $folder
     *
     * @param string $folder name of a Yioop wiki resource folder
     * @param string $thumb_folder name of thumbnail folder associated with
     *  Yioop wiki resource folder.
     */
    public function generateThumbs($folder, $thumb_folder)
    {
        $group_model = $this->group_model;
        if (!file_exists($thumb_folder)) {
            L\makePath($thumb_folder);
            clearstatcache();
            if (!file_exists($thumb_folder)) {
                return;
            }
        }
        $resources = glob(preg_quote($folder) . "/*");
        $folder_len = strlen($folder) + 1;
        foreach ($resources as $resource) {
            if (!file_exists($resource) || is_dir($resource)) {
                continue;
            }
            $name = substr($resource, $folder_len);
            $mime_type = L\mimeType($resource);
            $mime_parts = explode('/', $mime_type);
            $make_thumb = false;
            /* Nothing outside Yioop is needed for any of these now, so
               none of them waits on a program being installed. */
            if (in_array($mime_parts[0], ['video', 'image']) ||
                $mime_type == "application/pdf" ||
                $mime_type == 'application/epub+zip' ||
                str_starts_with($mime_type, 'text')) {
                if (!file_exists("$thumb_folder/$name.webp")) {
                    $make_thumb = true;
                }
                if (!$make_thumb && $mime_parts[0] == 'video') {
                    if (!file_exists("$thumb_folder/$name" .
                        C\MOVING_THUMB_ENDING)) {
                        $make_thumb = true;
                    }
                }
            }
            if ($make_thumb) {
                L\crawlLog("Making thumbs for $resource.");
                $group_model->makeThumbStripExif($name, $folder, $thumb_folder);
            }
        }
    }
}
