Skip the user id lookup when we've already got the name from the old table check...
[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 dcPerson('creator', $article->getUser());
111
112 $contributors = dcContributors($article->mTitle);
113
114 foreach ($contributors as $user_name => $cid) {
115 dcPerson('contributor', $cid, $user_name);
116 }
117
118 dcRights($article);
119 }
120
121 /* private */ function ccPrologue() {
122 global $wgOutputEncoding;
123
124 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
125
126 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\"
127 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
128 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
129 ";
130 }
131
132 /* private */ function ccSubPrologue($type, $url) {
133 $url = htmlspecialchars( $url );
134 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
135 }
136
137 /* private */ function ccSubEpilogue($type) {
138 echo " </cc:{$type}>\n";
139 }
140
141 /* private */ function ccLicense($terms) {
142
143 foreach ($terms as $term) {
144 switch ($term) {
145 case 're':
146 ccTerm('permits', "Reproduction"); break;
147 case 'di':
148 ccTerm('permits', "Distribution"); break;
149 case 'de':
150 ccTerm('permits', "DerivativeWorks"); break;
151 case 'nc':
152 ccTerm('prohibits', "CommercialUse"); break;
153 case 'no':
154 ccTerm('requires', "Notice"); break;
155 case 'by':
156 ccTerm('requires', "Attribution"); break;
157 case 'sa':
158 ccTerm('requires', "ShareAlike"); break;
159 case 'sc':
160 ccTerm('requires', "SourceCode"); break;
161 }
162 }
163 }
164
165 /* private */ function ccTerm($term, $name) {
166 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
167 }
168
169 /* private */ function ccEpilogue() {
170 echo "</rdf:RDF>\n";
171 }
172
173 /* private */ function dcElement($name, $value) {
174 $value = htmlspecialchars( $value );
175 print " <dc:{$name}>{$value}</dc:{$name}>\n";
176 }
177
178 /* private */ function dcDate($timestamp) {
179 return substr($timestamp, 0, 4) . "-"
180 . substr($timestamp, 4, 2) . "-"
181 . substr($timestamp, 6, 2);
182 }
183
184 /* private */ function dcReallyFullUrl($title) {
185 return $title->getFullURL();
186 }
187
188 /* private */ function dcPageOrString($name, $page, $str) {
189 $nt = Title::newFromText($page);
190
191 if (!$nt || $nt->getArticleID() == 0) {
192 dcElement($name, $str);
193 } else {
194 dcPage($name, $nt);
195 }
196 }
197
198 /* private */ function dcPage($name, $title) {
199 dcUrl($name, dcReallyFullUrl($title));
200 }
201
202 /* private */ function dcUrl($name, $url) {
203 $url = htmlspecialchars( $url );
204 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
205 }
206
207 /* private */ function dcPerson($name, $id, $user_name="") {
208 global $wgLang;
209
210 if ($id == 0) {
211 dcElement($name, wfMsg("anonymous"));
212 } else {
213 if( empty( $user_name ) ) {
214 $user_name = User::whoIs($id);
215 }
216 dcPageOrString($name, $wgLang->getNsText(NS_USER) . ":" . $user_name, $user_name);
217 }
218 }
219
220 /* private */ function dcContributors($title) {
221
222 $contribs = array();
223
224 $res = wfQuery("SELECT DISTINCT old_user,old_user_text" .
225 " FROM old " .
226 " WHERE old_namespace = " . $title->getNamespace() .
227 " AND old_title = '" . $title->getDBkey() . "'", DB_READ);
228
229 while ( $line = wfFetchObject( $res ) ) {
230 $contribs[$line->old_user_text] = $line->old_user;
231 }
232
233 return $contribs;
234 }
235
236 /* Takes an arg, for future enhancement with different rights for
237 different pages. */
238
239 /* private */ function dcRights($article) {
240
241 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
242
243 if (isset($wgRightsPage) &&
244 ($nt = Title::newFromText($wgRightsPage))
245 && ($nt->getArticleID() != 0)) {
246 dcPage('rights', $nt);
247 } else if (isset($wgRightsUrl)) {
248 dcUrl('rights', $wgRightsUrl);
249 } else if (isset($wgRightsText)) {
250 dcElement('rights', $wgRightsText);
251 }
252 }
253
254 /* private */ function ccGetTerms($url) {
255 global $wgLicenseTerms;
256
257 if (isset($wgLicenseTerms)) {
258 return $wgLicenseTerms;
259 } else {
260 $known = getKnownLicenses();
261 return $known[$url];
262 }
263 }
264
265 /* private */ function getKnownLicenses() {
266
267 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
268 'by-nc-sa', 'by-sa', 'nd', 'nd-nc',
269 'nc', 'nc-sa', 'sa');
270
271 $knownLicenses = array();
272
273 foreach ($ccLicenses as $license) {
274 $lurl = "http://creativecommons.org/licenses/{$license}/1.0/";
275 $knownLicenses[$lurl] = explode('-', $license);
276 $knownLicenses[$lurl][] = 're';
277 $knownLicenses[$lurl][] = 'di';
278 $knownLicenses[$lurl][] = 'no';
279 if (!in_array('nd', $knownLicenses[$lurl])) {
280 $knownLicenses[$lurl][] = 'de';
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 ?>