MediaWiki:Isbn-php

Z WikiSkript

<?php

$sqlsrvr = '';
$sqlbase = '';
$sqluser = '';
$sqlpswd = '';
$dbkey = '';

function getNumerals($string) {
    $returnString = "";
    for ($i = 0; $i < strlen($string); $i++) {
        $ch = substr($string, $i, 1);
        if (ord($ch) >= 48 && ord($ch) <= 57) {
            $returnString .= $ch;
        }
    }
    return $returnString;
}

function getAuthorName($name, $getFirstName)
{
        $nameParts = explode(' ', $name);
        $length = count($nameParts);

        //získá se pole, kde jsou všechny prvky kromě posledního
        for ($i = 0; $i < $length - 1; $i++)
        {
                $firstNameArray[$i] = $nameParts[$i];
        }
        //pole se složí dohromady;
        $firstName = implode(' ', $firstNameArray);
        $surname = $nameParts[$length - 1];

        if ($getFirstName)
                return $firstName;
        else
                return $surname;
}

function getPublisherInfo($info, $type)
{
        $array = explode(' : ', $info);
        $misto = $array[0];
        $publisherAndYear = $array[1];

        $array2 = explode(' c', $publisherAndYear);
        $vydavatel = substr($array2[0], 0, -1);
        $rok = substr($array2[1], 0, -1);

        switch($type)
        {
                case 'misto':
                        return $misto;
                        break;
                case 'vydavatel':
                        return $vydavatel;
                        break;
                case 'rok':
                        return $rok;
                        break;
        }
}

function makeOutput($zDatabaze) {
        global $prijmeni1, $prijmeni2, $prijmeni3, $jmeno1, $jmeno2, $jmeno3, $korporace, $kolektiv;
        global $nazev, $podnazev, $vydani, $misto, $vydavatel, $rok, $isbn, $url, $rozsah, $edice, $svazek;
        if ($prijmeni1 != "" && $jmeno1 != "") {
                $citace['prijmeni1'] = $prijmeni1;
                $citace['jmeno1'] = $jmeno1;
        }

        if ($prijmeni2 != "" && $jmeno2 !="")
        {
                $citace['prijmeni2'] = $prijmeni2;
                $citace['jmeno2'] = $jmeno2;
        }
        if ($prijmeni3 != "" && $jmeno3 != "")
        {
                $citace['prijmeni3'] = $prijmeni3;
                $citace['jmeno3'] = $jmeno3;
        }
        if ($kolektiv)
                $citace['kolektiv'] = "ano";
        if ($nazev != "")
                $citace['titul'] = $nazev;
        if ($podnazev != "")
                $citace['podtitul'] = $podnazev;
        if ($vydani != "")
                $citace['vydani'] = $vydani;
        if ($misto != "")
                $citace['misto'] = $misto;
        if ($vydavatel != "")
                $citace['vydavatel'] = $vydavatel;
        if ($rok != "")
                $citace['rok'] = $rok;
        if ($isbn != "")
                $citace['isbn'] = $isbn;
        if ($url != "")
                $citace['url'] = $url;
        if ($rozsah != "")
                $citace['rozsah'] = $rozsah;
        if ($edice != "")
                $citace['edice'] = $edice;
        if ($svazek != "")
                $citace['svazek'] = $svazek;


        $citace['zDatabaze'] = $zDatabaze;

        return json_encode($citace);
}

function dbisbn($isbn) {

        global $nazev;
        global $podnazev;
        global $prijmeni1; global $prijmeni2; global $prijmeni3;
        global $jmeno1; global $jmeno2; global $jmeno3;
        global $kolektiv;
        global $length;
        global $rok;
        global $misto;
        global $vydavatel;
        global $dbkey;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://isbndb.com/api/books.xml?access_key=" . $dbkey . "&results=details&index1=isbn&value1=" . $isbn);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $data = curl_exec($ch);
        curl_close($ch);

        $xml = simplexml_load_string($data);

        if ($xml->BookList->attributes()->shown_results != '1')
        {
                return 0;
        }

        $nazev = $xml->BookList->BookData->Title;

        $podnazev = $xml->BookList->BookData->TitleLong;
        $authors = explode(',', $xml->BookList->BookData->AuthorsText);
        $length = count($authors);
        if ($length >= 1)
        {
                $prijmeni1 = getAuthorName($authors[0], 0);
                $jmeno1 = getAuthorName($authors[0], 1);
        }
        if ($length >= 2)
        {
                $prijmeni2 = getAuthorName($authors[1], 0);
                $jmeno2 = getAuthorName($authors[1], 1);
        }
        if ($length >= 3)
        {
                $prijmeni3 = getAuthorName($authors[2], 0);
                $jmeno3 = getAuthorName($authors[2], 1);
        }
        if ($length > 3)
                $kolektiv = true;
        else
                $kolektiv = false;

        $publisherText = $xml->BookList->BookData->PublisherText;
        $rok = getPublisherInfo($publisherText, 'rok');
        $misto = getPublisherInfo($publisherText, 'misto');
        $vydavatel = getPublisherInfo($publisherText, 'vydavatel');

        return 1;
}

function getPage($url) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
}

function nk($isbn) {
        global $nazev;
        global $podnazev;
        global $prijmeni1; global $prijmeni2; global $prijmeni3;
        global $jmeno1; global $jmeno2; global $jmeno3;
        global $kolektiv;
        global $vydani;
        global $length;
        global $rok;
        global $misto;
        global $vydavatel;
        global $spatneUdaje;

        $spatneUdaje = 0;
        $data = getPage("http://aleph.nkp.cz/F/?func=find-b&local_base=skc&find_code=ISN&request=" . $isbn);

        //Je záznam v databázi Národní knihovny?
        $pattern = "/Záznam, který jste požadovali, není v databázi./";
        if (preg_match($pattern, $data)) {
                return 0;
        }

        //Je záznam unikátní? Našlo více záznamů? Pokud ano, volím první
        $pattern = "/.*Výsledky dotazu: &nbsp;ISBN\/ISMN\/ISSN=.*/";
        if (preg_match($pattern, $data)) {
                $pattern = "/<[Aa] [Hh][Rr][Ee][Ff]=(.*?)>1<\/[Aa]>/";
                preg_match($pattern, $data, $matches);

                $data = getPage($matches[1]);
        }

        //Název
        $pattern = "/<td.*?Název.*?\n.*?<[Aa].*?>(.*?)<\/[Aa]><\/td>/";
        preg_match($pattern, $data, $matches);
        if ($matches[1]) {
                $splitName = explode("/", $matches[1]);
                if (count($splitName) == 1) {
                        $splitName = explode(":", $matches[1]);
                        $nazev = $splitName[0];
                        if (count($plitName) > 1) {
                                $podnazev = $splitName[1];
                        }
                }
                if (count($splitName) == 2) {
                        $splitName = explode(":", $splitName[0]);
                        $nazev = $splitName[0];
                        if (count($splitName) > 1) {
                                $podnazev = $splitName[1];
                        }
                }
        } else {
                $spatneUdaje = 1;
        }

        //Kolektiv
        $pattern = "/a kolektiv/";
        if (preg_match($pattern, $data)) {
                $kolektiv = true;
        }

        //Autor
        $pattern = "/.*Hlavní záhlaví.*\n.*<[Aa].*>(.*)<\/[Aa]>/";
        preg_match($pattern, $data, $matches);

        $splitName = explode(",", $matches[1]);
        if ($splitName[0]) {
                $prijmeni1 = trim($splitName[0]);
        } else {
                $spatneUdaje = 1;
        }
        if ($splitName[1]) {
                $jmeno1 = trim($splitName[1]);
        } else {
                $spatneUdaje = 1;
        }
        $pattern = "/.*Další původce.*\n.*<[Aa].*>(.*)<\/[Aa]>/";
        preg_match($pattern, $data, $matches);
        if (isset($matches[1])) {
                $splitName = explode(",", $matches[1]);
                if ($prijmeni1)
                        $prijmeni2 = trim($splitName[0]);
                else
                        $prijmeni1 = trim($splitName[0]);
                if ($jmeno1)
                        $jmeno2 = trim($splitName[1]);
                else
                        $jmeno1 = trim($splitName[1]);
        }

        //Vydání
        $pattern = "/<td.*?>Vydání.*?<\/td>.*?<td.*?>(.*?)<\/td>/s";
        preg_match($pattern, $data, $matches);
        if ($matches[1]) {
                $vydani = getNumerals($matches[1]);
                //$vydani = $matches[1];
        } else {
                $spatneUdaje = 1;
        }


        $pattern = "/<td.*?Nakl. údaje.*?\n.*?<td.*?>(.*?):&nbsp;(.*?),&nbsp;(.*?)<\/td>/";
        preg_match($pattern, $data, $matches);
        if ($matches[1]) {
                $misto = trim($matches[1]);
        } else {
                $spatneUdaje = 1;
        }
        if ($matches[2]) {
                $vydavatel = trim($matches[2]);
        } else {
                $spatneUdaje = 1;
        }
        if ($matches[3]) {
                $rok = trim($matches[3]);
        } else {
                $spatneUdaje = 1;
        }

        return 1;
}
function onlyNumbers($isbn) {
        $returnValue = "";
        for ($i = 0; $i < strlen($isbn); $i++) {
                $char = substr($isbn, $i, 1);
                //pokud je znak cislice nebo x (kontrolni znak v isbn10)
                if ((ord($char) >= 48 && ord($char) <= 57) || $char == 'x' || $char == 'X') {
                        $returnValue .= $char;
                }
        }
        return $returnValue;
}

function wikiskriptadb($isbnid) {
        global $sqlsrvr, $sqlbase, $sqluser, $sqlpswd;
        global $prijmeni1, $prijmeni2, $prijmeni3, $jmeno1, $jmeno2, $jmeno3, $korporace, $kolektiv;
        global $nazev, $podnazev, $vydani, $misto, $vydavatel, $rok, $isbn, $url, $rozsah, $edice, $svazek;

        if (substr($isbnid, -1, 1) == 'x' || substr($isbnid, -1, 1) == 'x') {
                $isbnid = substr($isbn, 0, -1);
        }

        $con = mysql_connect($sqlsrvr, $sqluser, $sqlpswd);
        if (!$con) {
                return False;
        }
        mysql_select_db($sqlbase, $con);

        $sql = 'SELECT * FROM `' . $sqlbase . '`.`ws_isbn` WHERE `isbnid`=\'' . $isbnid . '\';';
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        mysql_close($con);

        if ($row['isbn']) {
                $prijmeni1 = $row['prijmeni1'];
                $prijmeni2 = $row['prijmeni2'];
                $prijmeni3 = $row['prijmeni3'];
                $jmeno1 = $row['jmeno1'];
                $jmeno2 = $row['jmeno2'];
                $jmeno3 = $row['jmeno3'];
                if ($row['kolektiv']) {
                        $kolektiv = true;
                }
                $nazev = $row['titul'];
                $podnazev = $row['podnazev'];
                $url = $row['url'];
                $rozsah = $row['rozsah'];
                $edice = $row['edice'];
                $svazek = $row['svazek'];
                $isbn = $row['isbn'];
                $rok = $row['rok'];
                $misto = $row['misto'];
                $vydavatel = $row['vydavatel'];
                $vydani = $row['vydani'];
                $korporace = $row[''];

                return True;
        } else {
                return False;
        }
}
if (isset($_GET['isbn'])) {
        if (isset($_GET['pouzijDatabazi'])) {
                if ($_GET['pouzijDatabazi'] == "false") {
                        dbisbn($_GET['isbn']);
                        nk($_GET['isbn']);
                        echo makeOutput($zDatabaze = False);
                }
        } else {
                 
                if (wikiskriptadb(onlyNumbers($_GET['isbn']))){
                        echo makeOutput($zDatabaze = True);
                } else {
                        dbisbn($_GET['isbn']);
                        nk($_GET['isbn']);
                        echo makeOutput($zDatabaze = False);
                }
        }
}
?>