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