Didn't really mean to delete SpecialContributions.php back there, but now that
[mediawiki.git] / includes / Metadata.php
blob4f3c7b15e0e885fc7ed21ce345a2cf2e10c0b3b5
1 <?php
2 /**
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>
21 * @package MediaWiki
24 /**
25 * TODO: Perhaps make this file into a Metadata class, with static methods (declared
26 * as private where indicated), to move these functions out of the global namespace?
28 define('RDF_TYPE_PREFS', "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
30 function wfDublinCoreRdf($article) {
32 $url = dcReallyFullUrl($article->mTitle);
34 if (rdfSetup()) {
35 dcPrologue($url);
36 dcBasics($article);
37 dcEpilogue();
41 function wfCreativeCommonsRdf($article) {
43 if (rdfSetup()) {
44 global $wgRightsUrl;
46 $url = dcReallyFullUrl($article->mTitle);
48 ccPrologue();
49 ccSubPrologue('Work', $url);
50 dcBasics($article);
51 if (isset($wgRightsUrl)) {
52 $url = htmlspecialchars( $wgRightsUrl );
53 print " <cc:license rdf:resource=\"$url\" />\n";
56 ccSubEpilogue('Work');
58 if (isset($wgRightsUrl)) {
59 $terms = ccGetTerms($wgRightsUrl);
60 if ($terms) {
61 ccSubPrologue('License', $wgRightsUrl);
62 ccLicense($terms);
63 ccSubEpilogue('License');
68 ccEpilogue();
71 /**
72 * @private
74 function rdfSetup() {
75 global $wgOut, $_SERVER;
77 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
79 if (!$rdftype) {
80 wfHttpError(406, "Not Acceptable", wfMsg("notacceptable"));
81 return false;
82 } else {
83 $wgOut->disable();
84 header( "Content-type: {$rdftype}" );
85 $wgOut->sendCacheControl();
86 return true;
90 /**
91 * @private
93 function dcPrologue($url) {
94 global $wgOutputEncoding;
96 $url = htmlspecialchars( $url );
97 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
99 <!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\">
101 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
102 xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
103 <rdf:Description rdf:about=\"$url\">
108 * @private
110 function dcEpilogue() {
111 print "
112 </rdf:Description>
113 </rdf:RDF>
118 * @private
120 function dcBasics($article) {
121 global $wgContLanguageCode, $wgSitename;
123 dcElement('title', $article->mTitle->getText());
124 dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename);
125 dcElement('language', $wgContLanguageCode);
126 dcElement('type', 'Text');
127 dcElement('format', 'text/html');
128 dcElement('identifier', dcReallyFullUrl($article->mTitle));
129 dcElement('date', dcDate($article->getTimestamp()));
131 $last_editor = $article->getUser();
133 if ($last_editor == 0) {
134 dcPerson('creator', 0);
135 } else {
136 dcPerson('creator', $last_editor, $article->getUserText(),
137 User::whoIsReal($last_editor));
140 $contributors = $article->getContributors();
142 foreach ($contributors as $user_parts) {
143 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
146 dcRights();
150 * @private
152 function ccPrologue() {
153 global $wgOutputEncoding;
155 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
157 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\"
158 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
159 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
164 * @private
166 function ccSubPrologue($type, $url) {
167 $url = htmlspecialchars( $url );
168 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
172 * @private
174 function ccSubEpilogue($type) {
175 echo " </cc:{$type}>\n";
179 * @private
181 function ccLicense($terms) {
183 foreach ($terms as $term) {
184 switch ($term) {
185 case 're':
186 ccTerm('permits', 'Reproduction'); break;
187 case 'di':
188 ccTerm('permits', 'Distribution'); break;
189 case 'de':
190 ccTerm('permits', 'DerivativeWorks'); break;
191 case 'nc':
192 ccTerm('prohibits', 'CommercialUse'); break;
193 case 'no':
194 ccTerm('requires', 'Notice'); break;
195 case 'by':
196 ccTerm('requires', 'Attribution'); break;
197 case 'sa':
198 ccTerm('requires', 'ShareAlike'); break;
199 case 'sc':
200 ccTerm('requires', 'SourceCode'); break;
206 * @private
208 function ccTerm($term, $name) {
209 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
213 * @private
215 function ccEpilogue() {
216 echo "</rdf:RDF>\n";
220 * @private
222 function dcElement($name, $value) {
223 $value = htmlspecialchars( $value );
224 print " <dc:{$name}>{$value}</dc:{$name}>\n";
228 * @private
230 function dcDate($timestamp) {
231 return substr($timestamp, 0, 4) . '-'
232 . substr($timestamp, 4, 2) . '-'
233 . substr($timestamp, 6, 2);
237 * @private
239 function dcReallyFullUrl($title) {
240 return $title->getFullURL();
244 * @private
246 function dcPageOrString($name, $page, $str) {
247 $nt = Title::newFromText($page);
249 if (!$nt || $nt->getArticleID() == 0) {
250 dcElement($name, $str);
251 } else {
252 dcPage($name, $nt);
257 * @private
259 function dcPage($name, $title) {
260 dcUrl($name, dcReallyFullUrl($title));
264 * @private
266 function dcUrl($name, $url) {
267 $url = htmlspecialchars( $url );
268 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
272 * @private
274 function dcPerson($name, $id, $user_name='', $user_real_name='') {
275 global $wgContLang;
277 if ($id == 0) {
278 dcElement($name, wfMsg('anonymous'));
279 } else if ( !empty($user_real_name) ) {
280 dcElement($name, $user_real_name);
281 } else {
282 # XXX: This shouldn't happen.
283 if( empty( $user_name ) ) {
284 $user_name = User::whoIs($id);
286 dcPageOrString($name, $wgContLang->getNsText(NS_USER) . ':' . $user_name, wfMsg('siteuser', $user_name));
291 * Takes an arg, for future enhancement with different rights for
292 * different pages.
293 * @private
295 function dcRights() {
297 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
299 if (isset($wgRightsPage) &&
300 ($nt = Title::newFromText($wgRightsPage))
301 && ($nt->getArticleID() != 0)) {
302 dcPage('rights', $nt);
303 } else if (isset($wgRightsUrl)) {
304 dcUrl('rights', $wgRightsUrl);
305 } else if (isset($wgRightsText)) {
306 dcElement('rights', $wgRightsText);
311 * @private
313 function ccGetTerms($url) {
314 global $wgLicenseTerms;
316 if (isset($wgLicenseTerms)) {
317 return $wgLicenseTerms;
318 } else {
319 $known = getKnownLicenses();
320 return $known[$url];
325 * @private
327 function getKnownLicenses() {
329 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
330 'by-nc-sa', 'by-sa');
331 $ccVersions = array('1.0', '2.0');
332 $knownLicenses = array();
334 foreach ($ccVersions as $version) {
335 foreach ($ccLicenses as $license) {
336 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
337 # 2.0 dropped the non-attribs licenses
338 continue;
340 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
341 $knownLicenses[$lurl] = explode('-', $license);
342 $knownLicenses[$lurl][] = 're';
343 $knownLicenses[$lurl][] = 'di';
344 $knownLicenses[$lurl][] = 'no';
345 if (!in_array('nd', $knownLicenses[$lurl])) {
346 $knownLicenses[$lurl][] = 'de';
351 /* Handle the GPL and LGPL, too. */
353 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
354 array('de', 're', 'di', 'no', 'sa', 'sc');
355 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
356 array('de', 're', 'di', 'no', 'sa', 'sc');
357 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
358 array('de', 're', 'di', 'no', 'sa', 'sc');
360 return $knownLicenses;