moving define(mediawiki, true) from index.php to includes/Defines.php
[lhc/web/wiklou.git] / includes / Metadata.php
1 <?php
2 /* Metadata.php -- provides DublinCore and CreativeCommons metadata
3 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 define("RDF_TYPE_PREFS", "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
21
22 function wfDublinCoreRdf($article) {
23
24 $url = dcReallyFullUrl($article->mTitle);
25
26 if (rdfSetup()) {
27 dcPrologue($url);
28 dcBasics($article);
29 dcEpilogue();
30 }
31 }
32
33 function wfCreativeCommonsRdf($article) {
34
35 if (rdfSetup()) {
36 global $wgRightsUrl;
37
38 $url = dcReallyFullUrl($article->mTitle);
39
40 ccPrologue();
41 ccSubPrologue('Work', $url);
42 dcBasics($article);
43 if (isset($wgRightsUrl)) {
44 $url = htmlspecialchars( $wgRightsUrl );
45 print " <cc:license rdf:resource=\"$url\" />\n";
46 }
47
48 ccSubEpilogue('Work');
49
50 if (isset($wgRightsUrl)) {
51 $terms = ccGetTerms($wgRightsUrl);
52 if ($terms) {
53 ccSubPrologue('License', $wgRightsUrl);
54 ccLicense($terms);
55 ccSubEpilogue('License');
56 }
57 }
58 }
59
60 ccEpilogue();
61 }
62
63 /* private */ function rdfSetup() {
64 global $wgOut, $wgRdfMimeType, $_SERVER;
65
66 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
67
68 if (!$rdftype) {
69 wfHttpError(406, "Not Acceptable", wfMsg("notacceptable"));
70 return false;
71 } else {
72 $wgOut->disable();
73 header( "Content-type: {$rdftype}" );
74 $wgOut->sendCacheControl();
75 return true;
76 }
77 }
78
79 /* private */ function dcPrologue($url) {
80 global $wgOutputEncoding;
81
82 $url = htmlspecialchars( $url );
83 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
84
85 <!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\">
86
87 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
88 xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
89 <rdf:Description rdf:about=\"$url\">
90 ";
91 }
92
93 /* private */ function dcEpilogue() {
94 print "
95 </rdf:Description>
96 </rdf:RDF>
97 ";
98 }
99
100 /* private */ function dcBasics($article) {
101 global $wgLanguageCode, $wgSitename;
102
103 dcElement('title', $article->mTitle->getText());
104 dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename);
105 dcElement('language', $wgLanguageCode);
106 dcElement('type', 'Text');
107 dcElement('format', 'text/html');
108 dcElement('identifier', dcReallyFullUrl($article->mTitle));
109 dcElement('date', dcDate($article->getTimestamp()));
110
111 $last_editor = $article->getUser();
112
113 if ($last_editor == 0) {
114 dcPerson('creator', 0);
115 } else {
116 dcPerson('creator', $last_editor, $article->getUserText(),
117 User::whoIsReal($last_editor));
118 }
119
120 $contributors = $article->getContributors();
121
122 foreach ($contributors as $user_parts) {
123 dcPerson('contributor', $user_parts[0], $user_parts[1], $user_parts[2]);
124 }
125
126 dcRights($article);
127 }
128
129 /* private */ function ccPrologue() {
130 global $wgOutputEncoding;
131
132 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
133
134 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\"
135 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
136 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
137 ";
138 }
139
140 /* private */ function ccSubPrologue($type, $url) {
141 $url = htmlspecialchars( $url );
142 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
143 }
144
145 /* private */ function ccSubEpilogue($type) {
146 echo " </cc:{$type}>\n";
147 }
148
149 /* private */ function ccLicense($terms) {
150
151 foreach ($terms as $term) {
152 switch ($term) {
153 case 're':
154 ccTerm('permits', "Reproduction"); break;
155 case 'di':
156 ccTerm('permits', "Distribution"); break;
157 case 'de':
158 ccTerm('permits', "DerivativeWorks"); break;
159 case 'nc':
160 ccTerm('prohibits', "CommercialUse"); break;
161 case 'no':
162 ccTerm('requires', "Notice"); break;
163 case 'by':
164 ccTerm('requires', "Attribution"); break;
165 case 'sa':
166 ccTerm('requires', "ShareAlike"); break;
167 case 'sc':
168 ccTerm('requires', "SourceCode"); break;
169 }
170 }
171 }
172
173 /* private */ function ccTerm($term, $name) {
174 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
175 }
176
177 /* private */ function ccEpilogue() {
178 echo "</rdf:RDF>\n";
179 }
180
181 /* private */ function dcElement($name, $value) {
182 $value = htmlspecialchars( $value );
183 print " <dc:{$name}>{$value}</dc:{$name}>\n";
184 }
185
186 /* private */ function dcDate($timestamp) {
187 return substr($timestamp, 0, 4) . "-"
188 . substr($timestamp, 4, 2) . "-"
189 . substr($timestamp, 6, 2);
190 }
191
192 /* private */ function dcReallyFullUrl($title) {
193 return $title->getFullURL();
194 }
195
196 /* private */ function dcPageOrString($name, $page, $str) {
197 $nt = Title::newFromText($page);
198
199 if (!$nt || $nt->getArticleID() == 0) {
200 dcElement($name, $str);
201 } else {
202 dcPage($name, $nt);
203 }
204 }
205
206 /* private */ function dcPage($name, $title) {
207 dcUrl($name, dcReallyFullUrl($title));
208 }
209
210 /* private */ function dcUrl($name, $url) {
211 $url = htmlspecialchars( $url );
212 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
213 }
214
215 /* private */ function dcPerson($name, $id, $user_name="", $user_real_name="") {
216 global $wgLang;
217
218 if ($id == 0) {
219 dcElement($name, wfMsg("anonymous"));
220 } else if ( !empty($user_real_name) ) {
221 dcElement($name, $user_real_name);
222 } else {
223 # XXX: This shouldn't happen.
224 if( empty( $user_name ) ) {
225 $user_name = User::whoIs($id);
226 }
227 dcPageOrString($name, $wgLang->getNsText(NS_USER) . ":" . $user_name, wfMsg("siteuser", $user_name));
228 }
229 }
230
231 /* Takes an arg, for future enhancement with different rights for
232 different pages. */
233
234 /* private */ function dcRights($article) {
235
236 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
237
238 if (isset($wgRightsPage) &&
239 ($nt = Title::newFromText($wgRightsPage))
240 && ($nt->getArticleID() != 0)) {
241 dcPage('rights', $nt);
242 } else if (isset($wgRightsUrl)) {
243 dcUrl('rights', $wgRightsUrl);
244 } else if (isset($wgRightsText)) {
245 dcElement('rights', $wgRightsText);
246 }
247 }
248
249 /* private */ function ccGetTerms($url) {
250 global $wgLicenseTerms;
251
252 if (isset($wgLicenseTerms)) {
253 return $wgLicenseTerms;
254 } else {
255 $known = getKnownLicenses();
256 return $known[$url];
257 }
258 }
259
260 /* private */ function getKnownLicenses() {
261
262 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
263 'by-nc-sa', 'by-sa');
264 $ccVersions = array('1.0', '2.0');
265 $knownLicenses = array();
266
267 foreach ($ccVersions as $version) {
268 foreach ($ccLicenses as $license) {
269 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
270 # 2.0 dropped the non-attribs licenses
271 continue;
272 }
273 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
274 $knownLicenses[$lurl] = explode('-', $license);
275 $knownLicenses[$lurl][] = 're';
276 $knownLicenses[$lurl][] = 'di';
277 $knownLicenses[$lurl][] = 'no';
278 if (!in_array('nd', $knownLicenses[$lurl])) {
279 $knownLicenses[$lurl][] = 'de';
280 }
281 }
282 }
283
284 /* Handle the GPL and LGPL, too. */
285
286 $knownLicenses["http://creativecommons.org/licenses/GPL/2.0/"] =
287 array('de', 're', 'di', 'no', 'sa', 'sc');
288 $knownLicenses["http://creativecommons.org/licenses/LGPL/2.1/"] =
289 array('de', 're', 'di', 'no', 'sa', 'sc');
290 $knownLicenses["http://www.gnu.org/copyleft/fdl.html"] =
291 array('de', 're', 'di', 'no', 'sa', 'sc');
292
293 return $knownLicenses;
294 }
295
296 ?>