Example #1
0
function queryXML($mastro = '', $doc = '', $xslt = '', $query = array(), $label = '', $mode = '')
{
    $returnText = '';
    if ($doc != '') {
        // IDENTIFICA IL DOC
        $doc_exploded = explode(DCTL_RESERVED_INFIX, $doc);
        $db_collection = isset($doc_exploded[0]) ? $doc_exploded[0] : '';
        $xml_resource = XMLDB_PATH_BASE . $db_collection;
        global $exist;
        $packageList = array();
        $allowedExt = array();
        // RETRIEVE BY FULL-TEXT
        $terms = isset($query['terms']) ? $query['terms'] : '';
        $terms = preg_match('/(.{3})+/', $terms) ? $terms : '';
        $terms = trim($terms);
        $count_words = 0;
        if (strlen(preg_replace('/' . WS . '[\\?\\*]/', '', $terms)) > 2) {
            $terms = preg_replace('/' . WS . '' . WS . '+/', ' ', $terms);
            $terms = preg_replace('/\\*+/', '*', $terms);
            $terms = preg_replace('/[^a-zA-Z0-9\\?\\*\']' . WS . '/', '?', $terms);
            $terms = preg_replace('/\\?+/', '?', $terms);
            $terms = preg_split('/' . WS . '/', $terms);
            $count_words = count($terms);
            $terms = join(' ', $terms);
        }
        $db_resource = '';
        $doQuery = false;
        $context = '';
        $func = '';
        $highlight = false;
        $high = '';
        $kwicSize = 80;
        $kwic = false;
        if (isset($query['criteria'])) {
            switch ($query['criteria']) {
                case 'text':
                    //let $filtered-q := replace($q, "[&"-*;-`~!@#$%^*()_+-=\[\]\{\}\|';:/.,?(:]", "")
                    $doQuery = $terms != '';
                    $context = '';
                    if ($count_words > 1) {
                        // 					$context = 'tei:div[ancestor-or-self::tei:div[./@xml:id and ./ancestor-or-self::tei:text][position()=last()]]'; // trova SEMPRE una div di primo livello
                        $context = 'tei:div[count(ancestor::tei:div)=0]';
                        // trova SEMPRE una div di primo livello
                        $func .= '[near(., "' . $terms . '", ' . $query['dist'] . ')]';
                        //       $func = '';
                        // 						$func .= '*[near(., "'.$terms.'", '.$query['dist'].')';
                        //       $func .= ' or ';
                        //       $func .= 'near((./tei:div or ./tei:div/following-sibling::tei:div[1]), "'.$terms.'", '.$query['dist'].')]';
                        $kwicSize = $kwicSize * (1 + $query['dist'] / 5);
                    } else {
                        $func = '*[. &= "' . $terms . '"]';
                    }
                    // TROVA TUTTO CON CONTIENE
                    // 					$func = '[text:match-all(.';
                    // 					foreach(explode(' ', $terms) as $term) $func .= ', "'.$term.'"';
                    // 					$func .= ')]';
                    $kwic = true;
                    $highlight = false;
                    $context .= $func;
                    $high = $terms;
                    break;
                case 'db':
                    if ($query['criteria_name'] != '') {
                        $key = isset($query['key']) ? intval($query['key']) != 0 ? sprintf("%06d", $query['key']) : '' : '';
                        $doQuery = $key != '';
                        $context = 'tei:name[@key = "' . $key . '"]';
                        $type_name = array();
                        $type_name[] = 'desc_character';
                        $type_name[] = 'func_character';
                        $type_name[] = 'desc_place';
                        $type_name[] = 'func_place';
                        $type_name[] = 'desc_object';
                        $type_name[] = 'func_object';
                        foreach ($type_name as $type) {
                            if (isset($query[$type])) {
                                $func .= $type . ' ';
                                if (is_array($query[$type])) {
                                    $allowedExt = array_merge($allowedExt, $query[$type]);
                                } else {
                                    $allowedExt = array_merge($allowedExt, explode('+', $query[$type]));
                                }
                            }
                        }
                        if ($func != '') {
                            $func = trim($func);
                            $func = '[@ana |= "' . $func . '"]';
                        }
                        $kwic = true;
                        $context .= $func;
                    }
                    break;
            }
        }
        if ($doQuery) {
            getPackageList($exist, $xml_resource, &$packageList, $allowedExt);
            // FILTER BY DOC
            if (isset($query['set'])) {
                $packageList2 = $packageList;
                $packageList = array();
                foreach ($packageList2 as $package) {
                    foreach ($query['set'] as $item) {
                        if (preg_match('/' . $item . '/', $package['ref'])) {
                            $packageList[] = $package;
                        }
                    }
                }
                if (count($packageList) < 1) {
                    $packageList[] = chr(0);
                }
            }
            $xquery = DCTL_XQUERY_BASE;
            $xquery .= ' declare function tei:highlight($term as xs:string, $node as text(), $args as item()*) as element() { ';
            if ($highlight) {
                $xquery .= ' <span>{$term}</span> ';
            } else {
                $xquery .= ' <span>{$term}</span> ';
            }
            $xquery .= ' }; ';
            $xquery .= ' declare function tei:shrink($nodes as node()*, $width as xs:integer, $args as item()*) as node()* { ';
            $xquery .= ' <span>{$nodes}</span> ';
            $xquery .= ' }; ';
            $xquery .= ' let $highlight := util:function("tei:highlight", 3) ';
            $xquery .= ' let $shrink := util:function("tei:shrink", 3) ';
            $xquery .= ' for $doc in xmldb:document(';
            $iter = -1;
            foreach ($packageList as $package) {
                ++$iter;
                if ($iter > 0) {
                    $xquery .= ', ';
                }
                $xquery .= '"' . $package['path'] . '"';
            }
            $xquery .= '), $node in $doc/tei:TEI/tei:text//' . $context;
            $xquery .= ' ';
            $xquery .= ' let $docNAME := util:document-name($doc) ';
            $xquery .= ' let $block := tei:getBlock($node) ';
            $xquery .= ' let $blockID := $block/@xml:id ';
            $xquery .= ' let $parentID := $node/ancestor-or-self::tei:div[1]/@xml:id ';
            $xquery .= ' let $date := $doc/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:biblFull[contains(@n,\'source\')]/tei:publicationStmt/tei:date[1] ';
            $xquery .= ' let $head := ';
            $xquery .= ' if ($node/ancestor-or-self::tei:div[1]/@rend) ';
            $xquery .= ' then ';
            $xquery .= ' $node/ancestor-or-self::tei:div[1]/@rend ';
            $xquery .= ' else ';
            $xquery .= ' $block/@rend ';
            $xquery .= ' order by $blockID, $date, $docNAME, $head, $node ';
            $xquery .= ' return ';
            $xquery .= ' <tei:div ref="{$docNAME}' . DISTINCT_SEP . '{string($blockID)}' . DISTINCT_SEP . '{string($parentID)}' . DISTINCT_SEP . '{string($node/@xml:id)}' . DISTINCT_SEP . '{local-name($node)}' . DISTINCT_SEP . '{string($node/@ana)}" rend="{string($head)}">';
            if ($kwic) {
                $xquery .= '{text:kwic-display($node//text(), ' . $kwicSize . ', $highlight, () )}';
            } else {
                if ($highlight) {
                    $xquery .= '{text:highlight-matches($node//text(), $highlight, () )}';
                } else {
                    $xquery .= ' {$node} ';
                }
            }
            $xquery .= ' </tei:div>';
            $result = $exist->xquery($xquery);
            //  or dump($exist->getError())
            $resultXML = (array) $result["XML"];
            foreach ($resultXML as $k1 => $node) {
                $db_resource .= $node;
            }
            $db_resource = '<stub>' . $db_resource . '</stub>';
            $where = '';
            $at = '';
            $label = '';
            $returnText .= transformXMLwithXSLT(MASTRO_RETRIEVE, $db_collection, $where, $xslt, $db_resource, $at, $high, $label, $mode);
        } else {
            $returnText .= 'Definisci i criteri di ricerca...';
        }
    }
    return $returnText;
}
Example #2
0
     $label = $label . DISTINCT_SEP . $desc;
     $filter = explode(',', (string) $node[0]['package_kind']);
     $returnText .= getRetrieveNavigator($doc, $where, $what, $label, $filter);
     break;
 default:
     $returnText1 = '';
     $xslt = $what . '.xsl';
     switch ($what) {
         case MASTRO_RETRIEVE:
             $xslt = 'query_by_block.xsl';
             $label = $_REQUEST['terms'];
             $mode = $_REQUEST['criteria'];
             $returnText1 .= queryXML(MASTRO_RETRIEVE, $doc, $xslt, $_REQUEST, $label, $mode);
             break;
         default:
             $returnText1 .= transformXMLwithXSLT(MASTRO_RETRIEVE, $doc, $where, $xslt, $block, '', '', $label);
             break;
     }
     $title = 'Selezione';
     $goto = $title;
     //			generateVerticalString($title, $goto, $db_collection);
     $label = explode(DISTINCT_SEP, $label);
     $label = $label[0];
     $returnText .= '<div class="box">';
     $label = stripslashes($title . ' > ' . $label);
     $returnText .= '<div class="box_head">';
     $returnText .= '<h2 class="page_curr" rel="' . $db_collection . '-' . $goto . '">';
     $returnText .= $label;
     $returnText .= '</h2>';
     $returnText .= '</div>';
     $returnText .= $returnText1;
Example #3
0
                            $returnText .= '</div>';
                            $returnText .= '<div class="widget_body">';
                            $returnText .= $returnText4img;
                            $returnText .= '</div>';
                            $returnText .= '</li>';
                            $returnText .= '</ul>';
                        } else {
                            $returnText .= $returnText4img;
                        }
                    }
                    // //
                    break;
                default:
                    global $cachedID;
                    preloadID($doc, &$cachedID);
                    // load all IDs to cache for next XSLT
                    $xslt = 'body_navigator.xsl';
                    $returnText .= transformXMLwithXSLT(MASTRO_DISPLAY, $doc, $where, $xslt, $block, $at, $high, $label);
                    $doc_ext = explode('_', $doc);
                    $doc_ext = 'body_' . str_ireplace('.xml', '', $doc_ext[1]);
                    $xslt = $doc_ext . '.xsl';
                    $returnText .= transformXMLwithXSLT(MASTRO_DISPLAY, $doc, $where, $xslt, $block, $at, $high, $label);
                    break;
            }
            $returnText .= '</div>';
            break;
    }
}
// return
//echo '<div>'.$returnText.'</div>';
echo $returnText;