Example #1
0
 public function testCompareArrays()
 {
     $a = array(0, 1, 2, 3);
     $b = array(6, 7, 8, 9);
     $this->assertEquals(-1, compareArrays($a, $b));
     $this->assertEquals(1, compareArrays($b, $a));
     $this->assertEquals(0, compareArrays($b, $b));
 }
/**
 * Function used by the SAX xml parser when the parser meets something that's not a tag
 *
 * @param $parser xml parser created with "xml_parser_create()"
 * @param $data "what is not a tag"
 */
function elementData($parser, $data)
{
    global $elementsPile;
    global $itemsPile;
    global $manifestData;
    global $flagTag;
    global $iterator;
    global $dialogBox;
    global $errorFound;
    global $langErrorReadingXMLFile;
    global $zipFile;
    global $errorMsgs, $okMsgs;
    global $pathToManifest;
    global $langErrorOpeningXMLFile;
    global $charset;
    // -----------------------------------
    // when eclass is full utf 8 restore this
    // -----------------------------------------
    $data = trim($data);
    //$data = trim(iconv("utf-8", $charset, $data));
    if (!isset($data)) {
        $data = "";
    }
    switch ($elementsPile[count($elementsPile) - 1]) {
        case "RESOURCE":
            break;
        case "TITLE":
            // $data == '' (empty string) means that title tag contains elements (<langstring> for an exemple), so it's not the title we need
            if ($data != '') {
                if ($flagTag['type'] == "item") {
                    // item title check
                    if (!isset($manifestData['items'][$flagTag['value']]['title'])) {
                        $manifestData['items'][$flagTag['value']]['title'] = "";
                    }
                    $manifestData['items'][$flagTag['value']]['title'] .= $data;
                }
                // get title of package if it was not find in the manifest metadata in the default organization
                if ($elementsPile[sizeof($elementsPile) - 2] == "ORGANIZATION" && $flagTag['type'] == "organization" && $flagTag['value'] == $manifestData['defaultOrganization']) {
                    // if we do not find this title
                    //  - the metadata title has been set as package title
                    //  - if there was nor title for metadata nor for default organization set 'unamed path'
                    // If we are here it means we have found the title in organization, this is the best to chose
                    $manifestData['packageTitle'] = $data;
                }
            }
            break;
        case "ITEM":
            break;
        case "ADLCP:DATAFROMLMS":
            $manifestData['items'][$flagTag['value']]['datafromlms'] = $data;
            break;
            // found a link to another XML file, parse it ...
        // found a link to another XML file, parse it ...
        case "ADLCP:LOCATION":
            if (!$errorFound) {
                $xml_parser = xml_parser_create();
                xml_set_element_handler($xml_parser, "startElement", "endElement");
                xml_set_character_data_handler($xml_parser, "elementData");
                $file = rawurldecode($data);
                //url of secondary manifest files is relative to the position of the base imsmanifest.xml
                // PHP extraction of zip file using zlib
                $unzippingState = $zipFile->extract(PCLZIP_OPT_BY_NAME, $pathToManifest . $file, PCLZIP_OPT_REMOVE_PATH, $pathToManifest);
                if (!($fp = @fopen($file, "r"))) {
                    $file = str_replace("\\", "/", $file);
                    // kanoume mia dokimh mhn tyxon do8hke la8os dir separator, antistrefontas tis ka8etous. ta windows paizoun kai me to unix dir separator
                    $unzippingState = $zipFile->extract(PCLZIP_OPT_BY_NAME, $pathToManifest . $file, PCLZIP_OPT_REMOVE_PATH, $pathToManifest);
                    if (!($fp = @fopen($file, "r"))) {
                        $errorFound = true;
                        array_push($errorMsgs, $langErrorOpeningXMLFile . $pathToManifest . $file);
                    }
                }
                if (!$errorFound) {
                    if (!isset($cache)) {
                        $cache = "";
                    }
                    while ($readdata = str_replace("\n", "", fread($fp, 4096))) {
                        // fix for fread breaking thing
                        // msg from "ml at csite dot com" 02-Jul-2003 02:29 on http://www.php.net/xml
                        // preg expression has been modified to match tag with inner attributes
                        $readdata = $cache . $readdata;
                        if (!feof($fp)) {
                            if (preg_match_all("/<[^\\>]*.>/", $readdata, $regs)) {
                                $lastTagname = $regs[0][count($regs[0]) - 1];
                                $split = false;
                                for ($i = strlen($readdata) - strlen($lastTagname); $i >= strlen($lastTagname); $i--) {
                                    if ($lastTagname == substr($readdata, $i, strlen($lastTagname))) {
                                        $cache = substr($readdata, $i, strlen($readdata));
                                        $readdata = substr($readdata, 0, $i);
                                        $split = true;
                                        break;
                                    }
                                }
                            }
                            if (!$split) {
                                $cache = $readdata;
                            }
                        }
                        // end of fix
                        if (!xml_parse($xml_parser, $readdata, feof($fp))) {
                            // if reading of the xml file in not successfull :
                            // set errorFound, set error msg, break while statement
                            $errorFound = true;
                            array_push($errorMsgs, $langErrorReadingXMLFile . $pathToManifest . $file);
                            break;
                        }
                    }
                    // while $readdata
                }
                //if fopen
                // close file
                @fclose($fp);
            }
            break;
        case "LANGSTRING":
            switch ($flagTag['type']) {
                case "item":
                    // DESCRIPTION
                    // if the langstring tag is a children of a description tag
                    if ($elementsPile[sizeof($elementsPile) - 2] == "DESCRIPTION" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                        if (!isset($manifestData['items'][$flagTag['value']]['description'])) {
                            $manifestData['items'][$flagTag['value']]['description'] = "";
                        }
                        $manifestData['items'][$flagTag['value']]['description'] .= $data;
                    }
                    // title found in metadata of an item (only if we haven't already one title for this sco)
                    if ($manifestData['items'][$flagTag['value']]['title'] == '' || !isset($manifestData['items'][$flagTag['value']]['title'])) {
                        if ($elementsPile[sizeof($elementsPile) - 2] == "TITLE" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                            $manifestData['items'][$flagTag['value']]['title'] .= $data;
                        }
                    }
                    break;
                case "sco":
                    // DESCRIPTION
                    // if the langstring tag is a children of a description tag
                    if ($elementsPile[sizeof($elementsPile) - 2] == "DESCRIPTION" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                        if (isset($manifestData['scos'][$flagTag['value']]['description'])) {
                            $manifestData['scos'][$flagTag['value']]['description'] .= $data;
                        } else {
                            $manifestData['scos'][$flagTag['value']]['description'] = $data;
                        }
                    }
                    // title found in metadata of an item (only if we haven't already one title for this sco)
                    if (!isset($manifestData['scos'][$flagTag['value']]['title']) || $manifestData['scos'][$flagTag['value']]['title'] == '') {
                        if ($elementsPile[sizeof($elementsPile) - 2] == "TITLE" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                            $manifestData['scos'][$flagTag['value']]['title'] = $data;
                        }
                    }
                    break;
                case "asset":
                    // DESCRIPTION
                    // if the langstring tag is a children of a description tag
                    if ($elementsPile[sizeof($elementsPile) - 2] == "DESCRIPTION" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                        if (isset($manifestData['assets'][$flagTag['value']]['description'])) {
                            $manifestData['assets'][$flagTag['value']]['description'] .= $data;
                        } else {
                            $manifestData['assets'][$flagTag['value']]['description'] = $data;
                        }
                    }
                    // title found in metadata of an item (only if we haven't already one title for this sco)
                    if (!isset($manifestData['assets'][$flagTag['value']]['title']) || $manifestData['assets'][$flagTag['value']]['title'] == '') {
                        if ($elementsPile[sizeof($elementsPile) - 2] == "TITLE" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                            if (isset($manifestData['assets'][$flagTag['value']]['title'])) {
                                $manifestData['assets'][$flagTag['value']]['title'] .= $data;
                            } else {
                                $manifestData['assets'][$flagTag['value']]['title'] = $data;
                            }
                        }
                    }
                    break;
                default:
                    // DESCRIPTION
                    $posPackageDesc = array("MANIFEST", "METADATA", "LOM", "GENERAL", "DESCRIPTION");
                    if (compareArrays($posPackageDesc, $elementsPile)) {
                        if (!isset($manifestData['packageDesc'])) {
                            $manifestData['packageDesc'] = "";
                        }
                        $manifestData['packageDesc'] .= $data;
                    }
                    if (!isset($manifestData['packageTitle']) || $manifestData['packageTitle'] == '') {
                        $posPackageTitle = array("MANIFEST", "METADATA", "LOM", "GENERAL", "TITLE");
                        if (compareArrays($posPackageTitle, $elementsPile)) {
                            $manifestData['packageTitle'] = $data;
                        }
                    }
                    break;
            }
            // end switch ( $flagTag['type'] )
            break;
        default:
            break;
    }
    // end switch ($elementsPile[count($elementsPile)-1] )
}
Example #3
0
function compareResults($resultNew, $resultOld)
{
    $resultNew = normalizeResultBuffer($resultNew);
    $resultOld = normalizeResultBuffer($resultOld);
    if ($resultNew == $resultOld) {
        return array();
    }
    $resultNew = xmlToArray($resultNew);
    $resultOld = xmlToArray($resultOld);
    if (!$resultNew && !$resultOld) {
        return array('failed to parse both XMLs');
    }
    if (!$resultNew) {
        return array('failed to parse new XML');
    }
    if (!$resultOld) {
        return array('failed to parse old XML');
    }
    return compareArrays($resultNew, $resultOld, "");
}
Example #4
0
function diffLanguagesMessageFiles(array $fileNames1, array $fileNames2, $messagesDirectoryName, $moduleName)
{
    assert('is_string($messagesDirectoryName)');
    assert('is_dir   ($messagesDirectoryName)');
    $problems = array();
    $directoryName1 = $fileNames1['messageDirectory'];
    $directoryName2 = $fileNames2['messageDirectory'];
    $shortDirectoryName1 = substr($directoryName1, strlen($messagesDirectoryName) + 1);
    $shortDirectoryName2 = substr($directoryName2, strlen($messagesDirectoryName) + 1);
    unset($fileNames1['messageDirectory']);
    unset($fileNames2['messageDirectory']);
    $commonFileNames = array_intersect($fileNames1, $fileNames2);
    $only1FileNames = array_diff($fileNames1, $fileNames2);
    $only2FileNames = array_diff($fileNames2, $fileNames1);
    foreach ($commonFileNames as $fileName) {
        $fullFileName1 = "{$directoryName1}/{$fileName}";
        $fullFileName2 = "{$directoryName2}/{$fileName}";
        $shortFileName1 = substr($fullFileName1, strlen($messagesDirectoryName) + 1);
        $shortFileName2 = substr($fullFileName2, strlen($messagesDirectoryName) + 1);
        $messages1 = (require $fullFileName1);
        if (is_array($messages1)) {
            $messages1 = array_keys($messages1);
            $messages2 = (require $fullFileName2);
            if (is_array($messages2)) {
                $messages2 = array_keys($messages2);
                if ($messages1 != $messages2) {
                    $problems[] = "{$shortFileName1} and {$shortFileName2} do not contain the same messages in '{$moduleName}': " . compareArrays($messages1, $messages2);
                }
            }
        }
    }
    foreach ($only2FileNames as $fileName) {
        $problems[] = "{$fileName} found in {$shortDirectoryName1}.";
    }
    foreach ($only1FileNames as $fileName) {
        $problems[] = "{$fileName} not in {$shortDirectoryName2}.";
    }
    return $problems;
}
<?php

$names = ['Tina', 'Dana', 'Mike', 'Amy', 'Adam'];
$compare = ['Dean', 'Tina', 'Mel', 'Amy', 'Michael'];
$query = 'Michael';
function valueInArray($search, $array)
{
    $result = array_search($search, $array);
    if ($result === false) {
        return false;
    } else {
        return true;
    }
}
$result = valueInArray($query, $names);
if ($result) {
    echo "The name {$query} was found in the {$result} array index of " . array_search($query, $names) . PHP_EOL;
} else {
    echo "The search was not found." . PHP_EOL;
}
function compareArrays($names, $compare)
{
    $result = array_intersect($names, $compare);
    return count($result);
}
$result = compareArrays($names, $compare);
echo "The two arrays have {$result} elements in common." . PHP_EOL;
// This function uses a foreach loop to check each element in an array against another array.
// It also counts matches and non-matches for the return output.
function compareArrays($newArray, $controlArray)
{
    $match = 0;
    $diff = 0;
    foreach ($newArray as $singleElement) {
        if (lookInArray($singleElement, $controlArray)) {
            $match = $match + 1;
        } else {
            $diff = $diff + 1;
        }
    }
    return "There were {$match} values in common and {$diff} that didn't match between the two arrays." . PHP_EOL;
}
// This function will print the matched names in common between the two arrays.
function compareAndPrintInCommons($newArray, $controlArray)
{
    $matches = [];
    foreach ($newArray as $singleElement) {
        if (lookInArray($singleElement, $controlArray)) {
            array_push($matches, $singleElement);
        }
    }
    foreach ($matches as $matchedNames) {
        echo $matchedNames . " was a matched name." . PHP_EOL;
    }
}
echo compareArrays($names, $compare);
echo compareAndPrintInCommons($names, $compare);
echo combineArrays($names, $compare);
Example #7
0
/**
 * Function used by the SAX xml parser when the parser meets something that's not a tag
 *
 * @param $parser xml parser created with "xml_parser_create()"
 * @param $data "what is not a tag"
 */
function elementData($parser, $data)
{
    global $elementsPile;
    global $itemsPile;
    global $manifestData;
    global $flagTag;
    global $errorFound;
    global $zipFile;
    global $errorMsgs, $okMsgs;
    global $pathToManifest;
    //$data = trim(claro_utf8_decode($data));
    if (!isset($data)) {
        $data = "";
    }
    switch ($elementsPile[count($elementsPile) - 1]) {
        case "RESOURCE":
            //echo "Resource : ".$data;
            break;
        case "TITLE":
            // $data == '' (empty string) means that title tag contains elements (<langstring> for an exemple), so it's not the title we need
            if ($data != '') {
                if ($flagTag['type'] == "item") {
                    if (!isset($manifestData['items'][$flagTag['value']]['title'])) {
                        $manifestData['items'][$flagTag['value']]['title'] = "";
                    }
                    $manifestData['items'][$flagTag['value']]['title'] .= $data;
                }
                // get title of package if it was not find in the manifest metadata in the default organization
                if ($elementsPile[sizeof($elementsPile) - 2] == "ORGANIZATION" && $flagTag['type'] == "organization" && $flagTag['value'] == $manifestData['defaultOrganization']) {
                    // if we do not find this title
                    //  - the metadata title has been set as package title
                    //  - if there was nor title for metadata nor for default organization set 'unnamed path'
                    // If we are here it means we have found the title in organization, this is the best to chose
                    if (!isset($manifestData['packageTitle'])) {
                        $manifestData['packageTitle'] = "";
                    }
                    $manifestData['packageTitle'] .= $data;
                }
            }
            break;
        case "DESCRIPTION":
            if ($data != '') {
                if ($elementsPile[sizeof($elementsPile) - 2] == "ORGANIZATION" && $flagTag['type'] == "organization" && $flagTag['value'] == $manifestData['defaultOrganization']) {
                    $manifestData['packageDesc'] = $data;
                }
            }
            break;
        case "ITEM":
            break;
        case "ADLCP:DATAFROMLMS":
            $manifestData['items'][$flagTag['value']]['datafromlms'] = $data;
            break;
            // found a link to another XML file, parse it ...
        // found a link to another XML file, parse it ...
        case "ADLCP:LOCATION":
            if (!$errorFound) {
                $xml_parser = xml_parser_create();
                xml_set_element_handler($xml_parser, "startElement", "endElement");
                xml_set_character_data_handler($xml_parser, "elementData");
                $file = $data;
                //url of secondary manifest files is relative to the position of the base imsmanifest.xml
                // we try to extract only the required file
                $unzippingState = $zipFile->extract(PCLZIP_OPT_BY_NAME, $pathToManifest . $file, PCLZIP_OPT_REMOVE_PATH, $pathToManifest);
                if ($unzippingState != 0 && file_exists($pathToManifest . $file)) {
                    array_push($okMsgs, get_lang('Secondary manifest found in zip file : ') . $pathToManifest . $file);
                    $readData = file_get_contents($pathToManifest . $file);
                    if (!xml_parse($xml_parser, $readData)) {
                        // if reading of the xml file in not successfull :
                        // set errorFound, set error msg, break while statement
                        $errorFound = true;
                        array_push($errorMsgs, get_lang('Error reading a secondary initialisation file : ') . $pathToManifest . $file);
                    }
                } else {
                    $errorFound = true;
                    array_push($errorMsgs, get_lang('Cannot find secondary initialisation file in the package.<br /> File not found : ') . $pathToManifest . $file);
                }
            }
            break;
        case "LANGSTRING":
            switch ($flagTag['type']) {
                case "item":
                    // DESCRIPTION
                    // if the langstring tag is a children of a description tag
                    if ($elementsPile[sizeof($elementsPile) - 2] == "DESCRIPTION" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                        if (!isset($manifestData['items'][$flagTag['value']]['description'])) {
                            $manifestData['items'][$flagTag['value']]['description'] = "";
                        }
                        $manifestData['items'][$flagTag['value']]['description'] .= $data;
                    }
                    // title found in metadata of an item (only if we haven't already one title for this sco)
                    if ($manifestData['items'][$flagTag['value']]['title'] == '' || !isset($manifestData['items'][$flagTag['value']]['title'])) {
                        if ($elementsPile[sizeof($elementsPile) - 2] == "TITLE" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                            $manifestData['items'][$flagTag['value']]['title'] .= $data;
                        }
                    }
                    break;
                case "sco":
                    // DESCRIPTION
                    // if the langstring tag is a children of a description tag
                    if ($elementsPile[sizeof($elementsPile) - 2] == "DESCRIPTION" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                        if (isset($manifestData['scos'][$flagTag['value']]['description'])) {
                            $manifestData['scos'][$flagTag['value']]['description'] .= $data;
                        } else {
                            $manifestData['scos'][$flagTag['value']]['description'] = $data;
                        }
                    }
                    // title found in metadata of an item (only if we haven't already one title for this sco)
                    if (!isset($manifestData['scos'][$flagTag['value']]['title']) || $manifestData['scos'][$flagTag['value']]['title'] == '') {
                        if ($elementsPile[sizeof($elementsPile) - 2] == "TITLE" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                            $manifestData['scos'][$flagTag['value']]['title'] = $data;
                        }
                    }
                    break;
                case "asset":
                    // DESCRIPTION
                    // if the langstring tag is a children of a description tag
                    if ($elementsPile[sizeof($elementsPile) - 2] == "DESCRIPTION" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                        if (isset($manifestData['assets'][$flagTag['value']]['description'])) {
                            $manifestData['assets'][$flagTag['value']]['description'] .= $data;
                        } else {
                            $manifestData['assets'][$flagTag['value']]['description'] = $data;
                        }
                    }
                    // title found in metadata of an item (only if we haven't already one title for this sco)
                    if (!isset($manifestData['assets'][$flagTag['value']]['title']) || $manifestData['assets'][$flagTag['value']]['title'] == '') {
                        if ($elementsPile[sizeof($elementsPile) - 2] == "TITLE" && $elementsPile[sizeof($elementsPile) - 3] == "GENERAL") {
                            if (isset($manifestData['assets'][$flagTag['value']]['title'])) {
                                $manifestData['assets'][$flagTag['value']]['title'] .= $data;
                            } else {
                                $manifestData['assets'][$flagTag['value']]['title'] = $data;
                            }
                        }
                    }
                    break;
                default:
                    // DESCRIPTION
                    $posPackageDesc = array("MANIFEST", "METADATA", "LOM", "GENERAL", "DESCRIPTION");
                    if (compareArrays($posPackageDesc, $elementsPile)) {
                        if (!isset($manifestData['packageDesc'])) {
                            $manifestData['packageDesc'] = "";
                        }
                        $manifestData['packageDesc'] .= $data;
                    }
                    if (!isset($manifestData['packageTitle']) || $manifestData['packageTitle'] == '') {
                        $posPackageTitle = array("MANIFEST", "METADATA", "LOM", "GENERAL", "TITLE");
                        if (compareArrays($posPackageTitle, $elementsPile)) {
                            $manifestData['packageTitle'] = $data;
                        }
                    }
                    break;
            }
            // end switch ( $flagTag['type'] )
            break;
        default:
            break;
    }
    // end switch ($elementsPile[count($elementsPile)-1] )
}