template arguments, various improvements to handling of recursive inclusion
[mediawiki.git] / includes / Metadata.php
blobebc7164dc20437157905ff2b2fa2243461206177
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.
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.
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
20 define("RDF_TYPE_PREFS", "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1");
22 function wfDublinCoreRdf($article) {
24 $url = dcReallyFullUrl($article->mTitle);
26 if (rdfSetup()) {
27 dcPrologue($url);
28 dcBasics($article);
29 dcEpilogue();
33 function wfCreativeCommonsRdf($article) {
35 if (rdfSetup()) {
36 global $wgRightsUrl;
38 $url = dcReallyFullUrl($article->mTitle);
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";
48 ccSubEpilogue('Work');
50 if (isset($wgRightsUrl)) {
51 $terms = ccGetTerms($wgRightsUrl);
52 if ($terms) {
53 ccSubPrologue('License', $wgRightsUrl);
54 ccLicense($terms);
55 ccSubEpilogue('License');
60 ccEpilogue();
63 /* private */ function rdfSetup() {
64 global $wgOut, $wgRdfMimeType, $_SERVER;
66 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS));
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;
79 /* private */ function dcPrologue($url) {
80 global $wgOutputEncoding;
82 $url = htmlspecialchars( $url );
83 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . ">
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\">
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\">
93 /* private */ function dcEpilogue() {
94 print "
95 </rdf:Description>
96 </rdf:RDF>
100 /* private */ function dcBasics($article) {
101 global $wgLanguageCode, $wgSitename;
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());
112 $contributors = dcContributors($article->mTitle);
114 foreach ($contributors as $cid) {
115 dcPerson('contributor', $cid);
118 dcRights($article);
121 /* private */ function ccPrologue() {
122 global $wgOutputEncoding;
124 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . ">
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#\">
132 /* private */ function ccSubPrologue($type, $url) {
133 $url = htmlspecialchars( $url );
134 echo " <cc:{$type} rdf:about=\"{$url}\">\n";
137 /* private */ function ccSubEpilogue($type) {
138 echo " </cc:{$type}>\n";
141 /* private */ function ccLicense($terms) {
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;
165 /* private */ function ccTerm($term, $name) {
166 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
169 /* private */ function ccEpilogue() {
170 echo "</rdf:RDF>\n";
173 /* private */ function dcElement($name, $value) {
174 $value = htmlspecialchars( $value );
175 print " <dc:{$name}>{$value}</dc:{$name}>\n";
178 /* private */ function dcDate($timestamp) {
179 return substr($timestamp, 0, 4) . "-"
180 . substr($timestamp, 4, 2) . "-"
181 . substr($timestamp, 6, 2);
184 /* private */ function dcReallyFullUrl($title) {
185 return $title->getFullURL();
188 /* private */ function dcPageOrString($name, $page, $str) {
189 $nt = Title::newFromText($page);
191 if (!$nt || $nt->getArticleID() == 0) {
192 dcElement($name, $str);
193 } else {
194 dcPage($name, $nt);
198 /* private */ function dcPage($name, $title) {
199 dcUrl($name, dcReallyFullUrl($title));
202 /* private */ function dcUrl($name, $url) {
203 $url = htmlspecialchars( $url );
204 print " <dc:{$name} rdf:resource=\"{$url}\" />\n";
207 /* private */ function dcPerson($name, $id) {
208 global $wgLang;
210 if ($id == 0) {
211 dcElement($name, wfMsg("anonymous"));
212 } else {
213 $user_name = User::whoIs($id);
214 dcPageOrString($name, $wgLang->getNsText(NS_USER) . ":" . $user_name, $user_name);
218 /* private */ function dcContributors($title) {
220 $contribs = array();
222 $res = wfQuery("SELECT DISTINCT old_user" .
223 " FROM old " .
224 " WHERE old_namespace = " . $title->getNamespace() .
225 " AND old_title = '" . $title->getDBkey() . "'", DB_READ);
227 while ( $line = wfFetchObject( $res ) ) {
228 $contribs[] = $line->old_user;
231 return $contribs;
234 /* Takes an arg, for future enhancement with different rights for
235 different pages. */
237 /* private */ function dcRights($article) {
239 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
241 if (isset($wgRightsPage) &&
242 ($nt = Title::newFromText($wgRightsPage))
243 && ($nt->getArticleID() != 0)) {
244 dcPage('rights', $nt);
245 } else if (isset($wgRightsUrl)) {
246 dcUrl('rights', $wgRightsUrl);
247 } else if (isset($wgRightsText)) {
248 dcElement('rights', $wgRightsText);
252 /* private */ function ccGetTerms($url) {
253 global $wgLicenseTerms;
255 if (isset($wgLicenseTerms)) {
256 return $wgLicenseTerms;
257 } else {
258 $known = getKnownLicenses();
259 return $known[$url];
263 /* private */ function getKnownLicenses() {
265 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
266 'by-nc-sa', 'by-sa', 'nd', 'nd-nc',
267 'nc', 'nc-sa', 'sa');
269 $knownLicenses = array();
271 foreach ($ccLicenses as $license) {
272 $lurl = "http://creativecommons.org/licenses/{$license}/1.0/";
273 $knownLicenses[$lurl] = explode('-', $license);
274 $knownLicenses[$lurl][] = 're';
275 $knownLicenses[$lurl][] = 'di';
276 $knownLicenses[$lurl][] = 'no';
277 if (!in_array('nd', $knownLicenses[$lurl])) {
278 $knownLicenses[$lurl][] = 'de';
282 /* Handle the GPL and LGPL, too. */
284 $knownLicenses["http://creativecommons.org/licenses/GPL/2.0/"] =
285 array('de', 're', 'di', 'no', 'sa', 'sc');
286 $knownLicenses["http://creativecommons.org/licenses/LGPL/2.1/"] =
287 array('de', 're', 'di', 'no', 'sa', 'sc');
288 $knownLicenses["http://www.gnu.org/copyleft/fdl.html"] =
289 array('de', 're', 'di', 'no', 'sa', 'sc');
291 return $knownLicenses;