public function asXml()

in pp3/module/Application/src/Application/Pp/Catalog.php [97:234]


    public function asXml($validate, &$validationErrors = null) {
        $implementation = new \DOMImplementation();
        $dtd = $implementation->createDocumentType(
                                    self::MODULE_UPDATES_ELEMENT,
                                    self::CATALOG_DTD,
                                    $this->_dtdPath);

        $xml = $implementation->createDocument('', '', $dtd);
        $modulesEl = $xml->createElement(self::MODULE_UPDATES_ELEMENT);
        $d = new \DateTime('now');
        $modulesEl->setAttribute(self::MODULE_UPDATES_ATTR_timestamp, $d->format('s/i/h/d/m/Y'));

        $validationErrors = array();

        $licenses = array();

        libxml_set_external_entity_loader(
            function ($public, $system, $context) {
                if($public === '-//NetBeans//DTD Autoupdate Catalog 2.8//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-catalog-2_8.dtd";
                } else if($public === '-//NetBeans//DTD Autoupdate Module Info 2.0//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-info-2_0.dtd";
                } else if($public === '-//NetBeans//DTD Autoupdate Module Info 2.2//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-info-2_2.dtd";
                } else if($public === '-//NetBeans//DTD Autoupdate Module Info 2.3//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-info-2_3.dtd";
                } else if($public === '-//NetBeans//DTD Autoupdate Module Info 2.4//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-info-2_4.dtd";
                } else if($public === '-//NetBeans//DTD Autoupdate Module Info 2.5//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-info-2_5.dtd";
                } else if($public === '-//NetBeans//DTD Autoupdate Module Info 2.7//EN') {
                    return __DIR__ . "/../../../../../public/dtd/autoupdate-info-2_7.dtd";
                }
                return null;
            }
        );

        foreach ($this->_items as $item) {
            $this->updateInfoXML($item);
            $this->_pluginVersionRepository->getEntityManager()->refresh($item);

            $infoXMLResource = $item->getInfoXml();

            if(! $infoXMLResource) {
                $validationErrors[$item->getId()] = sprintf('PluginVersion(id: %d) is missing info.xml', $item->getId());
                continue;
            }

            $infoXML = new \DOMDocument();
            $data = stream_get_contents($item->getInfoXml());
            $infoXML->loadXML($data);

            // For infoXML that is missing a doctype, assume a current one
            if(!$infoXML->doctype) {
                $doctype = $implementation->createDocumentType('module', '-//NetBeans//DTD Autoupdate Module Info 2.7//EN', 'http://www.netbeans.org/dtds/autoupdate-info-2_7.dtd');
                $infoXML->insertBefore($doctype, $infoXML->childNodes[0]);
                $infoXML->loadXML($infoXML->saveXML());
            }
            libxml_use_internal_errors(true);
            if (!$infoXML->validate()) {
                $validationErrors[$item->getId()] = libxml_get_errors();
                libxml_clear_errors();
                libxml_use_internal_errors(false);
                continue;
            }
            libxml_use_internal_errors(false);

            $moduleSource = $infoXML->getElementsByTagName(self::MODULE_ELEMENT);

            if(count($moduleSource) != 1) {
                error_log(sprintf('PluginVersion(id: %d) invalid info.xml not exactly one module element', $item->getId()));
                continue;
            }

            $manifestSource = $moduleSource[0]->getElementsByTagName(self::MANIFEST_ELEMENT);
            if(count($manifestSource) != 1) {
                error_log(sprintf('PluginVersion(id: %d) invalid info.xml not exactly one manifest element', $item->getId()));
                continue;
            }

            $licenseSource = $moduleSource[0]->getElementsByTagName(self::LICENSE_ELEMENT);

            $moduleElement = $xml->createElement(self::MODULE_ELEMENT);
            $moduleElement->setAttribute(self::MODULE_ATTR_distribution, rtrim($this->_downloadPath, '/').'/'.$item->getId().'/'.$item->getArtifactFilename());
            $moduleElement->setAttribute(self::MODULE_ATTR_downloadsize, intval($item->getArtifactSize()));
            foreach(self::MODULE_ATTRS as $attr) {
                if($moduleSource[0]->hasAttribute($attr)) {
                    $moduleElement->setAttribute($attr, $moduleSource[0]->getAttribute($attr));
                }
            }

            $manifestElement = $xml->createElement(self::MANIFEST_ELEMENT);
            foreach (self::MANIFEST_ATTRS as $attr) {
                if ($manifestSource[0]->hasAttribute($attr)) {
                    $manifestElement->setAttribute($attr, $manifestSource[0]->getAttribute($attr));
                }
            }

            if(count($licenseSource) == 1 && ($licenseSource[0]->getAttribute(self::LICENSE_ATTR_name))) {
                $licenses[$licenseSource[0]->getAttribute(self::LICENSE_ATTR_name)] = $licenseSource[0]->textContent;
            }

            $moduleElement->appendChild($manifestElement);

            foreach($item->getDigests() as $digest) {
                $messageDigest = $xml->createElement(self::MESSAGEDIGEST_ELEMENT);
                $messageDigest->setAttribute(self::MESSAGEDIGEST_ATTR_algorithm, $digest->getAlgorithm());
                $messageDigest->setAttribute(self::MESSAGEDIGEST_ATTRS_value, $digest->getValue());
                $moduleElement->appendChild($messageDigest);
            }

            $modulesEl->appendChild($moduleElement);
        }

        foreach($licenses as $name => $text) {
            $licenseElement = $xml->createElement(self::LICENSE_ELEMENT);
            $licenseElement->setAttribute(self::LICENSE_ATTR_name, $name);
            $licenseElement->textContent = $text;
            $modulesEl->appendChild($licenseElement);
        }

        $xml->appendChild($modulesEl);

        libxml_use_internal_errors(true);
        if (!$xml->validate()) {
            $validationErrors[-1] = libxml_get_errors();
            libxml_clear_errors(); 
        }
        libxml_use_internal_errors(false);

        if($validate && isset($validationErrors[-1])) {
            $xml->formatOutput = true;
            throw new \Exception('Catalog for '.$this->_version.' is not valid:<br><pre>'.print_r($validationErrors, true) . '</pre><pre><![CDATA[' . $xml->saveXML() .']]></pre>');
        }

        $xml->formatOutput = TRUE;
        return $xml->saveXML();
    }