3 * ARC2 format detection function
5 * @author Benjamin Nowack <bnowack@semsol.com>
6 * @license http://arc.semsol.org/license
11 function ARC2_getFormat($v, $mtype = '', $ext = '') {
13 /* mtype check (atom, rdf/xml, turtle, n3, mp3, jpg) */
14 $r = (!$r && preg_match('/\/atom\+xml/', $mtype)) ? 'atom' : $r;
15 $r = (!$r && preg_match('/\/rdf\+xml/', $mtype)) ? 'rdfxml' : $r;
16 $r = (!$r && preg_match('/\/(x\-)?turtle/', $mtype)) ? 'turtle' : $r;
17 $r = (!$r && preg_match('/\/rdf\+n3/', $mtype)) ? 'n3' : $r;
18 $r = (!$r && preg_match('/\/sparql-results\+xml/', $mtype)) ? 'sparqlxml' : $r;
22 /* starts with angle brackets */
23 preg_match('/^\s*\<[^\s]/s', $v) &&
24 /* has an xmlns:* declaration or a matching pair of tags */
25 (preg_match('/\sxmlns\:?/', $v) || preg_match('/\<([^\s]+).+\<\/\\1\>/s', $v)) &&
26 /* not a typical ntriples/turtle/n3 file */
27 !preg_match('/[\>\"\']\s*\.\s*$/s', $v)
29 while (preg_match('/^\s*\<\?xml[^\r\n]+\?\>\s*/s', $v)) {
30 $v = preg_replace('/^\s*\<\?xml[^\r\n]+\?\>\s*/s', '', $v);
32 while (preg_match('/^\s*\<\!--.+?--\>\s*/s', $v)) {
33 $v = preg_replace('/^\s*\<\!--.+?--\>\s*/s', '', $v);
35 /* doctype checks (html, rdf) */
36 $r = (!$r && preg_match('/^\s*\<\!DOCTYPE\s+html[\s|\>]/is', $v)) ? 'html' : $r;
37 $r = (!$r && preg_match('/^\s*\<\!DOCTYPE\s+[a-z0-9\_\-]\:RDF\s/is', $v)) ? 'rdfxml' : $r;
39 $v = preg_replace('/^\s*\<\!DOCTYPE\s.*\]\>/is', '', $v);
40 $r = (!$r && preg_match('/^\s*\<rss\s+[^\>]*version/s', $v)) ? 'rss' : $r;
41 $r = (!$r && preg_match('/^\s*\<feed\s+[^\>]+http\:\/\/www\.w3\.org\/2005\/Atom/s', $v)) ? 'atom' : $r;
42 $r = (!$r && preg_match('/^\s*\<opml\s/s', $v)) ? 'opml' : $r;
43 $r = (!$r && preg_match('/^\s*\<html[\s|\>]/is', $v)) ? 'html' : $r;
44 $r = (!$r && preg_match('/^\s*\<sparql\s+[^\>]+http\:\/\/www\.w3\.org\/2005\/sparql\-results\#/s', $v)) ? 'sparqlxml' : $r;
45 $r = (!$r && preg_match('/^\s*\<[^\>]+http\:\/\/www\.w3\.org\/2005\/sparql\-results#/s', $v)) ? 'srx' : $r;
46 $r = (!$r && preg_match('/^\s*\<[^\s]*RDF[\s\>]/s', $v)) ? 'rdfxml' : $r;
47 $r = (!$r && preg_match('/^\s*\<[^\>]+http\:\/\/www\.w3\.org\/1999\/02\/22\-rdf/s', $v)) ? 'rdfxml' : $r;
49 $r = !$r ? 'xml' : $r;
52 if (!$r && preg_match('/^[a-z0-9\.\(]*\s*[\{\[].*/s', trim($v))) {
53 /* google social graph api */
54 $r = (!$r && preg_match('/\"canonical_mapping\"/', $v)) ? 'sgajson' : $r;
56 $r = (!$r && preg_match('/\"permalink\"/', $v)) ? 'cbjson' : $r;
58 $r = !$r ? 'json' : $r;
61 $r = (!$r && preg_match('/\@(prefix|base)/i', $v)) ? 'turtle' : $r;
62 $r = (!$r && preg_match('/^(ttl)$/', $ext)) ? 'turtle' : $r;
63 $r = (!$r && preg_match('/^(n3)$/', $ext)) ? 'n3' : $r;
65 $r = (!$r && preg_match('/^\s*(_:|<).+?\s+<[^>]+?>\s+\S.+?\s*\.\s*$/sm', $v)) ? 'ntriples' : $r;
66 $r = (!$r && preg_match('/^(nt)$/', $ext)) ? 'ntriples' : $r;