Some documentation. Let's talk about it in wikitech-l.
[mediawiki.git] / includes / Metadata.php
blobedea045cf8aade3d40b4b9766c935a979ad857c8
1 <?php
2 /**
3 * Metadata.php -- provides DublinCore and CreativeCommons metadata
4 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
5 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * @author Evan Prodromou <evan@wikitravel.org>
23 /**
26 define('RDF_TYPE_PREFS', "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
28 function wfDublinCoreRdf($article) {
30 $url = dcReallyFullUrl($article->mTitle);
32 if (rdfSetup()) {
33 dcPrologue($url);
34 dcBasics($article);
35 dcEpilogue();
39 function wfCreativeCommonsRdf($article) {
41 if (rdfSetup()) {
42 global $wgRightsUrl;
44 $url = dcReallyFullUrl($article->mTitle);
46 ccPrologue();
47 ccSubPrologue('Work', $url);
48 dcBasics($article);
49 if (isset($wgRightsUrl)) {
50 $url = htmlspecialchars( $wgRightsUrl );
51 print " <cc:license rdf:resource=\"$url\" />\n";
54 ccSubEpilogue('Work');
56 if (isset($wgRightsUrl)) {
57 $terms = ccGetTerms($wgRightsUrl);
58 if ($terms) {
59 ccSubPrologue('License', $wgRightsUrl);
60 ccLicense($terms);
61 ccSubEpilogue('License');
66 ccEpilogue();
69 /**
70 * @private
72 function rdfSetup() {
73 global $wgOut, $wgRdfMimeType, $_SERVER;
75 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
77 if (!$rdftype) {
78 wfHttpError(406, "Not Acceptable", wfMsg("notacceptable"));
79 return false;
80 } else {
81 $wgOut->disable();
82 header( "Content-type: {$rdftype}" );
83 $wgOut->sendCacheControl();
84 return true;
88 /**
89 * @private
91 function dcPrologue($url) {
92 global $wgOutputEncoding;
94 $url = htmlspecialchars( $url );
95 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
97 <!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\">
99 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
100 xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
101 <rdf:Description rdf:about=\"$url\">
106 * @private
108 function dcEpilogue() {
109 print "
110 </rdf:Description>
111 </rdf:RDF>
116 * @private
118 function dcBasics($article) {
119 global $wgLanguageCode, $wgSitename;
121 dcElement('title', $article->mTitle->getText());
122 dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename);
123 dcElement('language', $wgLanguageCode);
124 dcElement('type', 'Text');
125 dcElement('format', 'text/html');
126 dcElement('identifier', dcReallyFullUrl($article->mTitle));
127 dcElement('date', dcDate($article->getTimestamp()));
129 $last_editor = $article->getUser();
131 if ($last_editor == 0) {
132 dcPerson('creator', 0);
133 } else {
134 dcPerson('creator', $last_editor, $article->getUserText(),
135 User::whoIsReal($last_editor));
138 $contributors = $article->getContributors();
140 foreach ($contributors as $user_parts) {
141 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
144 dcRights($article);
148 * @private
150 function ccPrologue() {
151 global $wgOutputEncoding;
153 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
155 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\"
156 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
157 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
162 * @private
164 function ccSubPrologue($type, $url) {
165 $url = htmlspecialchars( $url );
166 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
170 * @private
172 function ccSubEpilogue($type) {
173 echo " </cc:{$type}>\n";
177 * @private
179 function ccLicense($terms) {
181 foreach ($terms as $term) {
182 switch ($term) {
183 case 're':
184 ccTerm('permits', 'Reproduction'); break;
185 case 'di':
186 ccTerm('permits', 'Distribution'); break;
187 case 'de':
188 ccTerm('permits', 'DerivativeWorks'); break;
189 case 'nc':
190 ccTerm('prohibits', 'CommercialUse'); break;
191 case 'no':
192 ccTerm('requires', 'Notice'); break;
193 case 'by':
194 ccTerm('requires', 'Attribution'); break;
195 case 'sa':
196 ccTerm('requires', 'ShareAlike'); break;
197 case 'sc':
198 ccTerm('requires', 'SourceCode'); break;
204 * @private
206 function ccTerm($term, $name) {
207 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
211 * @private
213 function ccEpilogue() {
214 echo "</rdf:RDF>\n";
218 * @private
220 function dcElement($name, $value) {
221 $value = htmlspecialchars( $value );
222 print " <dc:{$name}>{$value}</dc:{$name}>\n";
226 * @private
228 function dcDate($timestamp) {
229 return substr($timestamp, 0, 4) . '-'
230 . substr($timestamp, 4, 2) . '-'
231 . substr($timestamp, 6, 2);
235 * @private
237 function dcReallyFullUrl($title) {
238 return $title->getFullURL();
242 * @private
244 function dcPageOrString($name, $page, $str) {
245 $nt = Title::newFromText($page);
247 if (!$nt || $nt->getArticleID() == 0) {
248 dcElement($name, $str);
249 } else {
250 dcPage($name, $nt);
255 * @private
257 function dcPage($name, $title) {
258 dcUrl($name, dcReallyFullUrl($title));
262 * @private
264 function dcUrl($name, $url) {
265 $url = htmlspecialchars( $url );
266 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
270 * @private
272 function dcPerson($name, $id, $user_name='', $user_real_name='') {
273 global $wgLang;
275 if ($id == 0) {
276 dcElement($name, wfMsg('anonymous'));
277 } else if ( !empty($user_real_name) ) {
278 dcElement($name, $user_real_name);
279 } else {
280 # XXX: This shouldn't happen.
281 if( empty( $user_name ) ) {
282 $user_name = User::whoIs($id);
284 dcPageOrString($name, $wgLang->getNsText(NS_USER) . ':' . $user_name, wfMsg('siteuser', $user_name));
289 * Takes an arg, for future enhancement with different rights for
290 * different pages.
291 * @private
293 function dcRights($article) {
295 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
297 if (isset($wgRightsPage) &&
298 ($nt = Title::newFromText($wgRightsPage))
299 && ($nt->getArticleID() != 0)) {
300 dcPage('rights', $nt);
301 } else if (isset($wgRightsUrl)) {
302 dcUrl('rights', $wgRightsUrl);
303 } else if (isset($wgRightsText)) {
304 dcElement('rights', $wgRightsText);
309 * @private
311 function ccGetTerms($url) {
312 global $wgLicenseTerms;
314 if (isset($wgLicenseTerms)) {
315 return $wgLicenseTerms;
316 } else {
317 $known = getKnownLicenses();
318 return $known[$url];
323 * @private
325 function getKnownLicenses() {
327 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
328 'by-nc-sa', 'by-sa');
329 $ccVersions = array('1.0', '2.0');
330 $knownLicenses = array();
332 foreach ($ccVersions as $version) {
333 foreach ($ccLicenses as $license) {
334 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
335 # 2.0 dropped the non-attribs licenses
336 continue;
338 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
339 $knownLicenses[$lurl] = explode('-', $license);
340 $knownLicenses[$lurl][] = 're';
341 $knownLicenses[$lurl][] = 'di';
342 $knownLicenses[$lurl][] = 'no';
343 if (!in_array('nd', $knownLicenses[$lurl])) {
344 $knownLicenses[$lurl][] = 'de';
349 /* Handle the GPL and LGPL, too. */
351 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
352 array('de', 're', 'di', 'no', 'sa', 'sc');
353 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
354 array('de', 're', 'di', 'no', 'sa', 'sc');
355 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
356 array('de', 're', 'di', 'no', 'sa', 'sc');
358 return $knownLicenses;