Fix E_NOTICE on line 76 if $_SERVER['HTTP_ACCEPT'] not set
[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 */
22
23 /**
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?
26 */
27 define('RDF_TYPE_PREFS', "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
28
29 function wfDublinCoreRdf($article) {
30
31 $url = dcReallyFullUrl($article->mTitle);
32
33 if (rdfSetup()) {
34 dcPrologue($url);
35 dcBasics($article);
36 dcEpilogue();
37 }
38 }
39
40 function wfCreativeCommonsRdf($article) {
41
42 if (rdfSetup()) {
43 global $wgRightsUrl;
44
45 $url = dcReallyFullUrl($article->mTitle);
46
47 ccPrologue();
48 ccSubPrologue('Work', $url);
49 dcBasics($article);
50 if (isset($wgRightsUrl)) {
51 $url = htmlspecialchars( $wgRightsUrl );
52 print " <cc:license rdf:resource=\"$url\" />\n";
53 }
54
55 ccSubEpilogue('Work');
56
57 if (isset($wgRightsUrl)) {
58 $terms = ccGetTerms($wgRightsUrl);
59 if ($terms) {
60 ccSubPrologue('License', $wgRightsUrl);
61 ccLicense($terms);
62 ccSubEpilogue('License');
63 }
64 }
65 }
66
67 ccEpilogue();
68 }
69
70 /**
71 * @private
72 */
73 function rdfSetup() {
74 global $wgOut, $_SERVER;
75
76 if( !isset($_SERVER['HTTP_ACCEPT']) ) {
77 $_SERVER['HTTP_ACCEPT'] = '';
78 }
79
80 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
81
82 if (!$rdftype) {
83 wfHttpError(406, "Not Acceptable", wfMsg("notacceptable"));
84 return false;
85 } else {
86 $wgOut->disable();
87 header( "Content-type: {$rdftype}; charset=utf-8" );
88 $wgOut->sendCacheControl();
89 return true;
90 }
91 }
92
93 /**
94 * @private
95 */
96 function dcPrologue($url) {
97 global $wgOutputEncoding;
98
99 $url = htmlspecialchars( $url );
100 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
101
102 <!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\">
103
104 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
105 xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
106 <rdf:Description rdf:about=\"$url\">
107 ";
108 }
109
110 /**
111 * @private
112 */
113 function dcEpilogue() {
114 print "
115 </rdf:Description>
116 </rdf:RDF>
117 ";
118 }
119
120 /**
121 * @private
122 */
123 function dcBasics($article) {
124 global $wgContLanguageCode, $wgSitename;
125
126 dcElement('title', $article->mTitle->getText());
127 dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename);
128 dcElement('language', $wgContLanguageCode);
129 dcElement('type', 'Text');
130 dcElement('format', 'text/html');
131 dcElement('identifier', dcReallyFullUrl($article->mTitle));
132 dcElement('date', dcDate($article->getTimestamp()));
133
134 $last_editor = $article->getUser();
135
136 if ($last_editor == 0) {
137 dcPerson('creator', 0);
138 } else {
139 dcPerson('creator', $last_editor, $article->getUserText(),
140 User::whoIsReal($last_editor));
141 }
142
143 $contributors = $article->getContributors();
144
145 foreach ($contributors as $user_parts) {
146 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
147 }
148
149 dcRights();
150 }
151
152 /**
153 * @private
154 */
155 function ccPrologue() {
156 global $wgOutputEncoding;
157
158 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
159
160 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\"
161 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
162 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
163 ";
164 }
165
166 /**
167 * @private
168 */
169 function ccSubPrologue($type, $url) {
170 $url = htmlspecialchars( $url );
171 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
172 }
173
174 /**
175 * @private
176 */
177 function ccSubEpilogue($type) {
178 echo " </cc:{$type}>\n";
179 }
180
181 /**
182 * @private
183 */
184 function ccLicense($terms) {
185
186 foreach ($terms as $term) {
187 switch ($term) {
188 case 're':
189 ccTerm('permits', 'Reproduction'); break;
190 case 'di':
191 ccTerm('permits', 'Distribution'); break;
192 case 'de':
193 ccTerm('permits', 'DerivativeWorks'); break;
194 case 'nc':
195 ccTerm('prohibits', 'CommercialUse'); break;
196 case 'no':
197 ccTerm('requires', 'Notice'); break;
198 case 'by':
199 ccTerm('requires', 'Attribution'); break;
200 case 'sa':
201 ccTerm('requires', 'ShareAlike'); break;
202 case 'sc':
203 ccTerm('requires', 'SourceCode'); break;
204 }
205 }
206 }
207
208 /**
209 * @private
210 */
211 function ccTerm($term, $name) {
212 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
213 }
214
215 /**
216 * @private
217 */
218 function ccEpilogue() {
219 echo "</rdf:RDF>\n";
220 }
221
222 /**
223 * @private
224 */
225 function dcElement($name, $value) {
226 $value = htmlspecialchars( $value );
227 print " <dc:{$name}>{$value}</dc:{$name}>\n";
228 }
229
230 /**
231 * @private
232 */
233 function dcDate($timestamp) {
234 return substr($timestamp, 0, 4) . '-'
235 . substr($timestamp, 4, 2) . '-'
236 . substr($timestamp, 6, 2);
237 }
238
239 /**
240 * @private
241 */
242 function dcReallyFullUrl($title) {
243 return $title->getFullURL();
244 }
245
246 /**
247 * @private
248 */
249 function dcPageOrString($name, $page, $str) {
250 $nt = Title::newFromText($page);
251
252 if (!$nt || $nt->getArticleID() == 0) {
253 dcElement($name, $str);
254 } else {
255 dcPage($name, $nt);
256 }
257 }
258
259 /**
260 * @private
261 */
262 function dcPage($name, $title) {
263 dcUrl($name, dcReallyFullUrl($title));
264 }
265
266 /**
267 * @private
268 */
269 function dcUrl($name, $url) {
270 $url = htmlspecialchars( $url );
271 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
272 }
273
274 /**
275 * @private
276 */
277 function dcPerson($name, $id, $user_name='', $user_real_name='') {
278 global $wgContLang;
279
280 if ($id == 0) {
281 dcElement($name, wfMsg('anonymous'));
282 } else if ( !empty($user_real_name) ) {
283 dcElement($name, $user_real_name);
284 } else {
285 # XXX: This shouldn't happen.
286 if( empty( $user_name ) ) {
287 $user_name = User::whoIs($id);
288 }
289 dcPageOrString($name, $wgContLang->getNsText(NS_USER) . ':' . $user_name, wfMsg('siteuser', $user_name));
290 }
291 }
292
293 /**
294 * Takes an arg, for future enhancement with different rights for
295 * different pages.
296 * @private
297 */
298 function dcRights() {
299
300 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
301
302 if (isset($wgRightsPage) &&
303 ($nt = Title::newFromText($wgRightsPage))
304 && ($nt->getArticleID() != 0)) {
305 dcPage('rights', $nt);
306 } else if (isset($wgRightsUrl)) {
307 dcUrl('rights', $wgRightsUrl);
308 } else if (isset($wgRightsText)) {
309 dcElement('rights', $wgRightsText);
310 }
311 }
312
313 /**
314 * @private
315 */
316 function ccGetTerms($url) {
317 global $wgLicenseTerms;
318
319 if (isset($wgLicenseTerms)) {
320 return $wgLicenseTerms;
321 } else {
322 $known = getKnownLicenses();
323 if( isset( $known[$url] ) ) {
324 return $known[$url];
325 } else {
326 return array();
327 }
328 }
329 }
330
331 /**
332 * @private
333 */
334 function getKnownLicenses() {
335
336 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
337 'by-nc-sa', 'by-sa');
338 $ccVersions = array('1.0', '2.0');
339 $knownLicenses = array();
340
341 foreach ($ccVersions as $version) {
342 foreach ($ccLicenses as $license) {
343 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
344 # 2.0 dropped the non-attribs licenses
345 continue;
346 }
347 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
348 $knownLicenses[$lurl] = explode('-', $license);
349 $knownLicenses[$lurl][] = 're';
350 $knownLicenses[$lurl][] = 'di';
351 $knownLicenses[$lurl][] = 'no';
352 if (!in_array('nd', $knownLicenses[$lurl])) {
353 $knownLicenses[$lurl][] = 'de';
354 }
355 }
356 }
357
358 /* Handle the GPL and LGPL, too. */
359
360 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
361 array('de', 're', 'di', 'no', 'sa', 'sc');
362 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
363 array('de', 're', 'di', 'no', 'sa', 'sc');
364 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
365 array('de', 're', 'di', 'no', 'sa', 'sc');
366
367 return $knownLicenses;
368 }
369
370 ?>