<commit_before><?php
/**
 * The index controller class.
 *
 * @package BookReader
 */
class BookReader_IndexController extends Omeka_Controller_AbstractActionController
{
    /**
     * This function returns the answer to a query with coordinates of the
     * matching words.
     */
    public function fulltextAction()
    {
        $item_id = $this->getRequest()->getParam('item_id');
        $doc = $this->getRequest()->getParam('doc');
        $path = $this->getRequest()->getParam('path');
        $q = $this->getRequest()->getParam('q');
        $q = utf8_encode($q);
        $callback = $this->getRequest()->getParam('callback');

        // On va récupérer le fichier XML de l'item
        $this->getResponse()->clearBody();
        $this->getResponse()->setHeader('Content-Type', 'text/html');
        $item = get_record_by_id('item', $item_id);

        $list = array();
        set_loop_records('files', $item->getFiles());
        foreach (loop('files') as $file) {
            if (strtolower(pathinfo($file->original_filename, PATHINFO_EXTENSION)) == 'xml') {
                $xml_file = escapeshellarg(FILES_DIR . DIRECTORY_SEPARATOR . $file->filename);
            }
            elseif ($file->hasThumbnail()) {
                if (preg_match('/(jpg|jpeg|png|gif)/', $file->filename)) {
                    $list[$file->filename] = $file->original_filename;
                }
            }
        }
        // Sorting by original filename if needed, or keep original attached order.
       	$sortOption = get_option('bookreader_sorting_mode');
       	if($sortOption=='1'){
        	uasort($list, 'cmp');
        }

        $widths = array();
        $heights = array();
        foreach ($list as $key => $image) {
            $pathImg = FILES_DIR . DIRECTORY_SEPARATOR . 'fullsize' . DIRECTORY_SEPARATOR . $key;
            list($width, $height, $type, $attr) = getimagesize($pathImg);
            $widths[] = $width;
            $heights[] = $height;
        }

        // On a un fichier XML, on va aller l'interroger pour voir si on a des choses dedans
        if ($xml_file) {
            $res = shell_exec("grep -P -i '<\/?page|$q' $xml_file");
            $res = preg_replace("/<page[^>]*>\n<\/page>\n/",'',$res);

            $sortie = array();
            $sortie['ia'] = $doc;
            $sortie['q'] = $q;
//          $sortie['page_count'] = 200; // Voir s'il faut vraiment le récupérer
//          $sortie['body_length'] = 140000; // Idem, voir si c'est utilisé
            $sortie['leaf0_missing'] = false; // Kezako ?
            $sortie['matches'] = array();

            // On va parcourir toutes les lignes qui matchent
            while (preg_match('/<page number="(\d*)" [^>]*height="(\d*)" width="(\d*)">\n(.*)\n<\/page>(.*)$/siU', $res, $match)) {
                $page_number = $match[1] - 1;
                $page_height = $match[2];
                $page_width  = $match[3];
                $zones = $match[4];
                $res = $match[5]; // On reprend pour la suite;

                $tab_lignes = preg_split('/<text /', $zones);
                foreach ($tab_lignes as $une_ligne) {
                    if (preg_match('/top="(\d*)" left="(\d*)" width="(\d*)" height="(\d*)" font="(\d*)">(.*)<\/text>$/', $une_ligne, $match_ligne)) {
                        $zone_top = $match_ligne[1];
                        $zone_left = $match_ligne[2];
                        $zone_width = $match_ligne[3];
                        $zone_height = $match_ligne[4];
                        $zone_font = $match_ligne[5];
                        $zone_text = $match_ligne[6];
                        $zone_text = preg_replace("/<\/?[ib]>/", "", $zone_text);

                        $zone_right = ($page_width - $zone_left - $zone_widht);
                        $zone_bottom = ($page_height - $zone_top - $zone_height);

                        // On crée la zone "globale"
                        $tab_zone = array();
                        $tab_zone['text'] = $zone_text;

                        // On va créer les boxes ...
                        $zone_width_char = strlen($zone_text);
                        $mot_start_char = stripos($zone_text, $q);
                        $mot_width_char = strlen($q);
                        $zone_text = str_ireplace($q, '{{{' . $q . '}}}', $zone_text);

                        $mot_left =  $zone_left + ( ($mot_start_char * $zone_width) / $zone_width_char);<commit_msg>fixed problem with fulltext search function<commit_after><?php
/**
 * The index controller class.
 *
 * @package BookReader
 */
class BookReader_IndexController extends Omeka_Controller_AbstractActionController
{
    /**
     * This function returns the answer to a query with coordinates of the
     * matching words.
     */
    public function fulltextAction()
    {
        $item_id = $this->getRequest()->getParam('item_id');
        $doc = $this->getRequest()->getParam('doc');
        $path = $this->getRequest()->getParam('path');
        $q = $this->getRequest()->getParam('q');
        $q = utf8_encode($q);
        $callback = $this->getRequest()->getParam('callback');

        // On va récupérer le fichier XML de l'item
        $this->getResponse()->clearBody();
        $this->getResponse()->setHeader('Content-Type', 'text/html');
        $item = get_record_by_id('item', $item_id);

        $list = array();
        set_loop_records('files', $item->getFiles());
        foreach (loop('files') as $file) {
            if (strtolower(pathinfo($file->original_filename, PATHINFO_EXTENSION)) == 'xml') {
               $xml_file = escapeshellarg(FILES_DIR . DIRECTORY_SEPARATOR .'original'. DIRECTORY_SEPARATOR . $file->filename);
            }
            elseif ($file->hasThumbnail()) {
                if (preg_match('/(jpg|jpeg|png|gif)/', $file->filename)) {
                    $list[$file->filename] = $file->original_filename;
                }
            }
        }
        // Sorting by original filename if needed, or keep original attached order.
       	$sortOption = get_option('bookreader_sorting_mode');
       	if($sortOption=='1'){
        	uasort($list, 'cmp');
        }

        $widths = array();
        $heights = array();
        foreach ($list as $key => $image) {
            $pathImg = FILES_DIR . DIRECTORY_SEPARATOR . 'fullsize' . DIRECTORY_SEPARATOR . $key;
            list($width, $height, $type, $attr) = getimagesize($pathImg);
            $widths[] = $width;
            $heights[] = $height;
        }

        // On a un fichier XML, on va aller l'interroger pour voir si on a des choses dedans
        if ($xml_file) {
        
        //echo "grep -P -i '<\/?page|$q' $xml_file ";
            $res = shell_exec("grep -P -i '<\/?page|$q' $xml_file");
            $res = preg_replace("/<page[^>]*>\n<\/page>\n/",'',$res);

            $sortie = array();
            $sortie['ia'] = $doc;
            $sortie['q'] = $q;
//          $sortie['page_count'] = 200; // Voir s'il faut vraiment le récupérer
//          $sortie['body_length'] = 140000; // Idem, voir si c'est utilisé
            $sortie['leaf0_missing'] = false; // Kezako ?
            $sortie['matches'] = array();

            // On va parcourir toutes les lignes qui matchent
            while (preg_match('/<page number="(\d*)" [^>]*height="(\d*)" width="(\d*)">\n(.*)\n<\/page>(.*)$/siU', $res, $match)) {
                $page_number = $match[1];
                $page_height = $match[2];
                $page_width  = $match[3];
                $zones = $match[4];
                $res = $match[5]; // On reprend pour la suite;

                $tab_lignes = preg_split('/<text /', $zones);
                foreach ($tab_lignes as $une_ligne) {
                    if (preg_match('/top="(\d*)" left="(\d*)" width="(\d*)" height="(\d*)" font="(\d*)">(.*)<\/text>$/', $une_ligne, $match_ligne)) {
                        $zone_top = $match_ligne[1];
                        $zone_left = $match_ligne[2];
                        $zone_width = $match_ligne[3];
                        $zone_height = $match_ligne[4];
                        $zone_font = $match_ligne[5];
                        $zone_text = $match_ligne[6];
                        $zone_text = preg_replace("/<\/?[ib]>/", "", $zone_text);

                        $zone_right = ($page_width - $zone_left - $zone_width);
                        $zone_bottom = ($page_height - $zone_top - $zone_height);

                        // On crée la zone "globale"
                        $tab_zone = array();
                        $tab_zone['text'] = $zone_text;

                        // On va créer les boxes ...
                        $zone_width_char = strlen($zone_text);
                        $mot_start_char = stripos($zone_text, $q);
                        $mot_width_char = strlen($q);
                        $zone_text = str_ireplace($q, '{{{' . $q . '}}}', $zone_text);

                        $mot_left =  $zone_left + ( ($mot_start_char * $zone_width) / $zone_width_char);
{'fraction_non_alphanumeric': 0.169196675900277, 'fraction_numerical': 0.005207756232686981, 'mean_word_length': 1.8100871731008716, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5913943', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2729, 'n_words': 549}