3 * Metadata.php -- provides DublinCore and CreativeCommons metadata
4 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @author Evan Prodromou <evan@wikitravel.org>
24 * TODO: Perhaps make this file into a Metadata class, with static methods (declared
25 * as private where indicated), to move these functions out of the global namespace?
27 define('RDF_TYPE_PREFS', "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
29 function wfDublinCoreRdf($article) {
31 $url = dcReallyFullUrl($article->mTitle
);
40 function wfCreativeCommonsRdf($article) {
45 $url = dcReallyFullUrl($article->mTitle
);
48 ccSubPrologue('Work', $url);
50 if (isset($wgRightsUrl)) {
51 $url = htmlspecialchars( $wgRightsUrl );
52 print " <cc:license rdf:resource=\"$url\" />\n";
55 ccSubEpilogue('Work');
57 if (isset($wgRightsUrl)) {
58 $terms = ccGetTerms($wgRightsUrl);
60 ccSubPrologue('License', $wgRightsUrl);
62 ccSubEpilogue('License');
74 global $wgOut, $_SERVER;
76 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
$_SERVER['HTTP_ACCEPT'] : null;
78 $rdftype = wfNegotiateType(wfAcceptToPrefs($httpaccept), wfAcceptToPrefs(RDF_TYPE_PREFS
));
81 wfHttpError(406, "Not Acceptable", wfMsg("notacceptable"));
85 header( "Content-type: {$rdftype}; charset=utf-8" );
86 $wgOut->sendCacheControl();
94 function dcPrologue($url) {
95 global $wgOutputEncoding;
97 $url = htmlspecialchars( $url );
98 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
100 <!DOCTYPE rdf:RDF PUBLIC \"-//DUBLIN CORE//DCMES DTD 2002/07/31//EN\" \"http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd\">
102 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
103 xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
104 <rdf:Description rdf:about=\"$url\">
111 function dcEpilogue() {
121 function dcBasics($article) {
122 global $wgContLanguageCode, $wgSitename;
124 dcElement('title', $article->mTitle
->getText());
125 dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename);
126 dcElement('language', $wgContLanguageCode);
127 dcElement('type', 'Text');
128 dcElement('format', 'text/html');
129 dcElement('identifier', dcReallyFullUrl($article->mTitle
));
130 dcElement('date', dcDate($article->getTimestamp()));
132 $last_editor = $article->getUser();
134 if ($last_editor == 0) {
135 dcPerson('creator', 0);
137 dcPerson('creator', $last_editor, $article->getUserText(),
138 User
::whoIsReal($last_editor));
141 $contributors = $article->getContributors();
143 foreach ($contributors as $user_parts) {
144 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
153 function ccPrologue() {
154 global $wgOutputEncoding;
156 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
158 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\"
159 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
160 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
167 function ccSubPrologue($type, $url) {
168 $url = htmlspecialchars( $url );
169 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
175 function ccSubEpilogue($type) {
176 echo " </cc:{$type}>\n";
182 function ccLicense($terms) {
184 foreach ($terms as $term) {
187 ccTerm('permits', 'Reproduction'); break;
189 ccTerm('permits', 'Distribution'); break;
191 ccTerm('permits', 'DerivativeWorks'); break;
193 ccTerm('prohibits', 'CommercialUse'); break;
195 ccTerm('requires', 'Notice'); break;
197 ccTerm('requires', 'Attribution'); break;
199 ccTerm('requires', 'ShareAlike'); break;
201 ccTerm('requires', 'SourceCode'); break;
209 function ccTerm($term, $name) {
210 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
216 function ccEpilogue() {
223 function dcElement($name, $value) {
224 $value = htmlspecialchars( $value );
225 print " <dc:{$name}>{$value}</dc:{$name}>\n";
231 function dcDate($timestamp) {
232 return substr($timestamp, 0, 4) . '-'
233 . substr($timestamp, 4, 2) . '-'
234 . substr($timestamp, 6, 2);
240 function dcReallyFullUrl($title) {
241 return $title->getFullURL();
247 function dcPageOrString($name, $page, $str) {
248 $nt = Title
::newFromText($page);
250 if (!$nt ||
$nt->getArticleID() == 0) {
251 dcElement($name, $str);
260 function dcPage($name, $title) {
261 dcUrl($name, dcReallyFullUrl($title));
267 function dcUrl($name, $url) {
268 $url = htmlspecialchars( $url );
269 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
275 function dcPerson($name, $id, $user_name='', $user_real_name='') {
279 dcElement($name, wfMsg('anonymous'));
280 } else if ( !empty($user_real_name) ) {
281 dcElement($name, $user_real_name);
283 # XXX: This shouldn't happen.
284 if( empty( $user_name ) ) {
285 $user_name = User
::whoIs($id);
287 dcPageOrString($name, $wgContLang->getNsText(NS_USER
) . ':' . $user_name, wfMsg('siteuser', $user_name));
292 * Takes an arg, for future enhancement with different rights for
296 function dcRights() {
298 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
300 if (isset($wgRightsPage) &&
301 ($nt = Title
::newFromText($wgRightsPage))
302 && ($nt->getArticleID() != 0)) {
303 dcPage('rights', $nt);
304 } else if (isset($wgRightsUrl)) {
305 dcUrl('rights', $wgRightsUrl);
306 } else if (isset($wgRightsText)) {
307 dcElement('rights', $wgRightsText);
314 function ccGetTerms($url) {
315 global $wgLicenseTerms;
317 if (isset($wgLicenseTerms)) {
318 return $wgLicenseTerms;
320 $known = getKnownLicenses();
321 if( isset( $known[$url] ) ) {
332 function getKnownLicenses() {
334 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
335 'by-nc-sa', 'by-sa');
336 $ccVersions = array('1.0', '2.0');
337 $knownLicenses = array();
339 foreach ($ccVersions as $version) {
340 foreach ($ccLicenses as $license) {
341 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
342 # 2.0 dropped the non-attribs licenses
345 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
346 $knownLicenses[$lurl] = explode('-', $license);
347 $knownLicenses[$lurl][] = 're';
348 $knownLicenses[$lurl][] = 'di';
349 $knownLicenses[$lurl][] = 'no';
350 if (!in_array('nd', $knownLicenses[$lurl])) {
351 $knownLicenses[$lurl][] = 'de';
356 /* Handle the GPL and LGPL, too. */
358 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
359 array('de', 're', 'di', 'no', 'sa', 'sc');
360 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
361 array('de', 're', 'di', 'no', 'sa', 'sc');
362 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
363 array('de', 're', 'di', 'no', 'sa', 'sc');
365 return $knownLicenses;