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