b48ced0de9a9b4c02f7a35afc77eb26a4be91a95
[lhc/web/wiklou.git] / includes / Metadata.php
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.
10 *
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.
15 *
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
19 *
20 * @author Evan Prodromou <evan@wikitravel.org>
21 * @package MediaWiki
22 */
23
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?
27 */
28 define('RDF_TYPE_PREFS', "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
29
30 function wfDublinCoreRdf($article) {
31
32 $url = dcReallyFullUrl($article->mTitle);
33
34 if (rdfSetup()) {
35 dcPrologue($url);
36 dcBasics($article);
37 dcEpilogue();
38 }
39 }
40
41 function wfCreativeCommonsRdf($article) {
42
43 if (rdfSetup()) {
44 global $wgRightsUrl;
45
46 $url = dcReallyFullUrl($article->mTitle);
47
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";
54 }
55
56 ccSubEpilogue('Work');
57
58 if (isset($wgRightsUrl)) {
59 $terms = ccGetTerms($wgRightsUrl);
60 if ($terms) {
61 ccSubPrologue('License', $wgRightsUrl);
62 ccLicense($terms);
63 ccSubEpilogue('License');
64 }
65 }
66 }
67
68 ccEpilogue();
69 }
70
71 /**
72 * @private
73 */
74 function rdfSetup() {
75 global $wgOut, $_SERVER;
76
77 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
78
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;
87 }
88 }
89
90 /**
91 * @private
92 */
93 function dcPrologue($url) {
94 global $wgOutputEncoding;
95
96 $url = htmlspecialchars( $url );
97 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
98
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\">
100
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\">
104 ";
105 }
106
107 /**
108 * @private
109 */
110 function dcEpilogue() {
111 print "
112 </rdf:Description>
113 </rdf:RDF>
114 ";
115 }
116
117 /**
118 * @private
119 */
120 function dcBasics($article) {
121 global $wgContLanguageCode, $wgSitename;
122
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()));
130
131 $last_editor = $article->getUser();
132
133 if ($last_editor == 0) {
134 dcPerson('creator', 0);
135 } else {
136 dcPerson('creator', $last_editor, $article->getUserText(),
137 User::whoIsReal($last_editor));
138 }
139
140 $contributors = $article->getContributors();
141
142 foreach ($contributors as $user_parts) {
143 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
144 }
145
146 dcRights();
147 }
148
149 /**
150 * @private
151 */
152 function ccPrologue() {
153 global $wgOutputEncoding;
154
155 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
156
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#\">
160 ";
161 }
162
163 /**
164 * @private
165 */
166 function ccSubPrologue($type, $url) {
167 $url = htmlspecialchars( $url );
168 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
169 }
170
171 /**
172 * @private
173 */
174 function ccSubEpilogue($type) {
175 echo " </cc:{$type}>\n";
176 }
177
178 /**
179 * @private
180 */
181 function ccLicense($terms) {
182
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;
201 }
202 }
203 }
204
205 /**
206 * @private
207 */
208 function ccTerm($term, $name) {
209 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
210 }
211
212 /**
213 * @private
214 */
215 function ccEpilogue() {
216 echo "</rdf:RDF>\n";
217 }
218
219 /**
220 * @private
221 */
222 function dcElement($name, $value) {
223 $value = htmlspecialchars( $value );
224 print " <dc:{$name}>{$value}</dc:{$name}>\n";
225 }
226
227 /**
228 * @private
229 */
230 function dcDate($timestamp) {
231 return substr($timestamp, 0, 4) . '-'
232 . substr($timestamp, 4, 2) . '-'
233 . substr($timestamp, 6, 2);
234 }
235
236 /**
237 * @private
238 */
239 function dcReallyFullUrl($title) {
240 return $title->getFullURL();
241 }
242
243 /**
244 * @private
245 */
246 function dcPageOrString($name, $page, $str) {
247 $nt = Title::newFromText($page);
248
249 if (!$nt || $nt->getArticleID() == 0) {
250 dcElement($name, $str);
251 } else {
252 dcPage($name, $nt);
253 }
254 }
255
256 /**
257 * @private
258 */
259 function dcPage($name, $title) {
260 dcUrl($name, dcReallyFullUrl($title));
261 }
262
263 /**
264 * @private
265 */
266 function dcUrl($name, $url) {
267 $url = htmlspecialchars( $url );
268 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
269 }
270
271 /**
272 * @private
273 */
274 function dcPerson($name, $id, $user_name='', $user_real_name='') {
275 global $wgContLang;
276
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);
285 }
286 dcPageOrString($name, $wgContLang->getNsText(NS_USER) . ':' . $user_name, wfMsg('siteuser', $user_name));
287 }
288 }
289
290 /**
291 * Takes an arg, for future enhancement with different rights for
292 * different pages.
293 * @private
294 */
295 function dcRights() {
296
297 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
298
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);
307 }
308 }
309
310 /**
311 * @private
312 */
313 function ccGetTerms($url) {
314 global $wgLicenseTerms;
315
316 if (isset($wgLicenseTerms)) {
317 return $wgLicenseTerms;
318 } else {
319 $known = getKnownLicenses();
320 if( isset( $known[$url] ) ) {
321 return $known[$url];
322 } else {
323 return array();
324 }
325 }
326 }
327
328 /**
329 * @private
330 */
331 function getKnownLicenses() {
332
333 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
334 'by-nc-sa', 'by-sa');
335 $ccVersions = array('1.0', '2.0');
336 $knownLicenses = array();
337
338 foreach ($ccVersions as $version) {
339 foreach ($ccLicenses as $license) {
340 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
341 # 2.0 dropped the non-attribs licenses
342 continue;
343 }
344 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
345 $knownLicenses[$lurl] = explode('-', $license);
346 $knownLicenses[$lurl][] = 're';
347 $knownLicenses[$lurl][] = 'di';
348 $knownLicenses[$lurl][] = 'no';
349 if (!in_array('nd', $knownLicenses[$lurl])) {
350 $knownLicenses[$lurl][] = 'de';
351 }
352 }
353 }
354
355 /* Handle the GPL and LGPL, too. */
356
357 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
358 array('de', 're', 'di', 'no', 'sa', 'sc');
359 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
360 array('de', 're', 'di', 'no', 'sa', 'sc');
361 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
362 array('de', 're', 'di', 'no', 'sa', 'sc');
363
364 return $knownLicenses;
365 }
366
367 ?>