Changing comments layout preparing for generated documentation with Phpdocumentor
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * @author Evan Prodromou <evan@wikitravel.org>
21 */
22
23 /**
24 *
25 */
26 define('RDF_TYPE_PREFS', "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
27
28 function wfDublinCoreRdf($article) {
29
30 $url = dcReallyFullUrl($article->mTitle);
31
32 if (rdfSetup()) {
33 dcPrologue($url);
34 dcBasics($article);
35 dcEpilogue();
36 }
37 }
38
39 function wfCreativeCommonsRdf($article) {
40
41 if (rdfSetup()) {
42 global $wgRightsUrl;
43
44 $url = dcReallyFullUrl($article->mTitle);
45
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";
52 }
53
54 ccSubEpilogue('Work');
55
56 if (isset($wgRightsUrl)) {
57 $terms = ccGetTerms($wgRightsUrl);
58 if ($terms) {
59 ccSubPrologue('License', $wgRightsUrl);
60 ccLicense($terms);
61 ccSubEpilogue('License');
62 }
63 }
64 }
65
66 ccEpilogue();
67 }
68
69 /**
70 * @private
71 */
72 function rdfSetup() {
73 global $wgOut, $wgRdfMimeType, $_SERVER;
74
75 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
76
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;
85 }
86 }
87
88 /**
89 * @private
90 */
91 function dcPrologue($url) {
92 global $wgOutputEncoding;
93
94 $url = htmlspecialchars( $url );
95 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
96
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\">
98
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\">
102 ";
103 }
104
105 /**
106 * @private
107 */
108 function dcEpilogue() {
109 print "
110 </rdf:Description>
111 </rdf:RDF>
112 ";
113 }
114
115 /**
116 * @private
117 */
118 function dcBasics($article) {
119 global $wgLanguageCode, $wgSitename;
120
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()));
128
129 $last_editor = $article->getUser();
130
131 if ($last_editor == 0) {
132 dcPerson('creator', 0);
133 } else {
134 dcPerson('creator', $last_editor, $article->getUserText(),
135 User::whoIsReal($last_editor));
136 }
137
138 $contributors = $article->getContributors();
139
140 foreach ($contributors as $user_parts) {
141 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
142 }
143
144 dcRights($article);
145 }
146
147 /**
148 * @private
149 */
150 function ccPrologue() {
151 global $wgOutputEncoding;
152
153 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
154
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#\">
158 ";
159 }
160
161 /**
162 * @private
163 */
164 function ccSubPrologue($type, $url) {
165 $url = htmlspecialchars( $url );
166 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
167 }
168
169 /**
170 * @private
171 */
172 function ccSubEpilogue($type) {
173 echo " </cc:{$type}>\n";
174 }
175
176 /**
177 * @private
178 */
179 function ccLicense($terms) {
180
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;
199 }
200 }
201 }
202
203 /**
204 * @private
205 */
206 function ccTerm($term, $name) {
207 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
208 }
209
210 /**
211 * @private
212 */
213 function ccEpilogue() {
214 echo "</rdf:RDF>\n";
215 }
216
217 /**
218 * @private
219 */
220 function dcElement($name, $value) {
221 $value = htmlspecialchars( $value );
222 print " <dc:{$name}>{$value}</dc:{$name}>\n";
223 }
224
225 /**
226 * @private
227 */
228 function dcDate($timestamp) {
229 return substr($timestamp, 0, 4) . '-'
230 . substr($timestamp, 4, 2) . '-'
231 . substr($timestamp, 6, 2);
232 }
233
234 /**
235 * @private
236 */
237 function dcReallyFullUrl($title) {
238 return $title->getFullURL();
239 }
240
241 /**
242 * @private
243 */
244 function dcPageOrString($name, $page, $str) {
245 $nt = Title::newFromText($page);
246
247 if (!$nt || $nt->getArticleID() == 0) {
248 dcElement($name, $str);
249 } else {
250 dcPage($name, $nt);
251 }
252 }
253
254 /**
255 * @private
256 */
257 function dcPage($name, $title) {
258 dcUrl($name, dcReallyFullUrl($title));
259 }
260
261 /**
262 * @private
263 */
264 function dcUrl($name, $url) {
265 $url = htmlspecialchars( $url );
266 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
267 }
268
269 /**
270 * @private
271 */
272 function dcPerson($name, $id, $user_name='', $user_real_name='') {
273 global $wgLang;
274
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);
283 }
284 dcPageOrString($name, $wgLang->getNsText(NS_USER) . ':' . $user_name, wfMsg('siteuser', $user_name));
285 }
286 }
287
288 /**
289 * Takes an arg, for future enhancement with different rights for
290 * different pages.
291 * @private
292 */
293 function dcRights($article) {
294
295 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
296
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);
305 }
306 }
307
308 /**
309 * @private
310 */
311 function ccGetTerms($url) {
312 global $wgLicenseTerms;
313
314 if (isset($wgLicenseTerms)) {
315 return $wgLicenseTerms;
316 } else {
317 $known = getKnownLicenses();
318 return $known[$url];
319 }
320 }
321
322 /**
323 * @private
324 */
325 function getKnownLicenses() {
326
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();
331
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;
337 }
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';
345 }
346 }
347 }
348
349 /* Handle the GPL and LGPL, too. */
350
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');
357
358 return $knownLicenses;
359 }
360
361 ?>