RDF and Version Comparison components from v1

This commit is contained in:
fligtar%gmail.com 2006-08-16 03:53:27 +00:00
Родитель 493110600a
Коммит 37c6139420
3 изменённых файлов: 1880 добавлений и 0 удалений

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,85 @@
<?php
require_once("includes/rdf_parser.php");
define('EM_NS', 'http://www.mozilla.org/2004/em-rdf#');
define('MF_RES', 'urn:mozilla:install-manifest');
class RdfComponent extends Object {
/**
* Parses install.rdf using Rdf_parser class
* @param string $manifestData
* @return array $data["manifest"]
*/
function parseInstallManifest($manifestData) {
$data = array();
$rdf = new Rdf_parser();
$rdf->rdf_parser_create(null);
$rdf->rdf_set_user_data($data);
$rdf->rdf_set_statement_handler($this->mfStatementHandler);
$rdf->rdf_set_base("");
if (!$rdf->rdf_parse($manifestData, strlen($manifestData), true)) {
return null;
}
// Set the targetApplication data
$targetArray = array();
if (is_array($data["manifest"]["targetApplication"])) {
foreach ($data["manifest"]["targetApplication"] as $targetApp) {
$id = $data[$targetApp][EM_NS."id"];
$targetArray[$id]["minVersion"] = $data[$targetApp][EM_NS."minVersion"];
$targetArray[$id]["maxVersion"] = $data[$targetApp][EM_NS."maxVersion"];
}
}
$data["manifest"]["targetApplication"] = $targetArray;
$rdf->rdf_parser_free();
return $data["manifest"];
}
/**
* Parses install.rdf for our desired properties
* @param array &$data
* @param string $subjectType
* @param string $subject
* @param string $predicate
* @param int $ordinal
* @param string $objectType
* @param string $object
* @param string $xmlLang
*/
function mfStatementHandler(&$data, $subjectType, $subject, $predicate,
$ordinal, $objectType, $object, $xmlLang) {
// single properties - ignoring: iconURL, optionsURL, aboutURL, and anything not listed
$singleProps = array("id" => 1, "version" => 1, "creator" => 1, "homepageURL" => 1, "updateURL" => 1);
// multiple properties - ignoring: File
$multiProps = array("contributor" => 1, "targetApplication" => 1, "requires" => 1);
// localizable properties
$l10nProps = array("name" => 1, "description" => 1);
// Look for properties on the install manifest itself
if ($subject == MF_RES) {
// we're only really interested in EM properties
$length = strlen(EM_NS);
if (strncmp($predicate, EM_NS, $length) == 0) {
$prop = substr($predicate, $length, strlen($predicate)-$length);
if ($singleProps[$prop]) {
$data["manifest"][$prop] = $object;
}
elseif ($multiProps[$prop]) {
$data["manifest"][$prop][] = $object;
}
elseif ($l10nProps[$prop]) {
$lang = ($xmlLang) ? $xmlLang : "en-US";
$data["manifest"][$prop][$lang] = $object;
}
}
}
else {
// save it anyway
$data[$subject][$predicate] = $object;
}
}

Просмотреть файл

@ -0,0 +1,158 @@
<?php
// ***** BEGIN LICENSE BLOCK *****
// Version: MPL 1.1/GPL 2.0/LGPL 2.1
//
// The contents of this file are subject to the Mozilla Public License Version
// 1.1 (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is Mozilla Update.
//
// Contributor(s):
// Benjamin Smedberg
// Mike Morgan
// Justin Scott
//
// Alternatively, the contents of this file may be used under the terms of
// either the GNU General Public License Version 2 or later (the "GPL"), or
// the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
// in which case the provisions of the GPL or the LGPL are applicable instead
// of those above. If you wish to allow use of your version of this file only
// under the terms of either the GPL or the LGPL, and not to allow others to
// use your version of this file under the terms of the MPL, indicate your
// decision by deleting the provisions above and replace them with the notice
// and other provisions required by the GPL or the LGPL. If you do not delete
// the provisions above, a recipient may use your version of this file under
// the terms of any one of the MPL, the GPL or the LGPL.
//
// ***** END LICENSE BLOCK *****
/**
* Version comparison script. The purpose of this file is to mimic the C++
* version comparison algorithm so version comparisons in VersionCheck.php are
* properly executed with expected results. This could have been accomplished
* by establishing a standardized versioning scheme across all Mozilla
* operations, but although that would make more sense, it's simply too late
* for that now. We cannot escape this.
*
* @package umo
* @subpackage core
*/
class VersionCompareComponent extends Object {
/**
* Parse a version part.
* @return array $r parsed version part.
*/
function NS_ParseVersionPart($p) {
if ($p == '*') {
return array('numA' => 2147483647,
'strB' => '',
'numC' => 0,
'extraD' => '');
}
preg_match('/^([-\d]*)([^-\d]*)([-\d]*)(.*)$/', $p, $m);
$r = array('numA' => intval($m[1]),
'strB' => $m[2],
'numC' => intval($m[3]),
'extraD' => $m[4]);
if ($r['strB'] == '+') {
++$r['numA'];
$r['strB'] = 'pre';
}
return $r;
}
/**
* Compare parsed version parts.
* @param string $an
* @param string $bp
* @return int $r
*/
function NS_cmp($an, $bn) {
if ($an < $bn)
return -1;
if ($an > $bn)
return 1;
return 0;
}
/**
* Recursive string comparison.
* @param string $as
* @param string $bs
* @return int $r
*/
function NS_strcmp($as, $bs) {
if ($as == $bs)
return 0;
if ($as == '')
return 1;
if ($bs == '')
return -1;
return strcmp($as, $bs);
}
/**
* Compare parsed version numbers.
* @param string $ap
* @param string $bp
* @return int $r -1|0|1
*/
function NS_CompareVersionParts($ap, $bp) {
$avp = NS_ParseVersionPart($ap);
$bvp = NS_ParseVersionPart($bp);
$r = NS_cmp($avp['numA'], $bvp['numA']);
if ($r)
return $r;
$r = NS_strcmp($avp['strB'], $bvp['strB']);
if ($r)
return $r;
$r = NS_cmp($avp['numC'], $bvp['numC']);
if ($r)
return $r;
return NS_strcmp($avp['extraD'], $bvp['extraD']);
}
/**
* Master comparison function.
* @param string $a complete version string.
* @param string $b complete version string.
* @return int $r -1|0|1
*/
function NS_CompareVersions($a, $b) {
$al = explode('.', $a);
$bl = explode('.', $b);
while (count($al) || count($bl)) {
$ap = array_shift($al);
$bp = array_shift($bl);
$r = NS_CompareVersionParts($ap, $bp);
if ($r != 0)
return $r;
}
return 0;
}
}
?>