Index: trunk/phase3/includes/Metadata.php |
— | — | @@ -0,0 +1,289 @@ |
| 2 | +<?php |
| 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 | + |
| 21 | +define(RDF_TYPE_PREFS, "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1"); |
| 22 | + |
| 23 | +function wfDublinCoreRdf($article) { |
| 24 | + |
| 25 | + $url = dcReallyFullUrl($article->mTitle); |
| 26 | + |
| 27 | + if (rdfSetup()) { |
| 28 | + dcPrologue($url); |
| 29 | + dcBasics($article); |
| 30 | + dcEpilogue(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function wfCreativeCommonsRdf($article) { |
| 35 | + |
| 36 | + if (rdfSetup()) { |
| 37 | + global $wgRightsUrl; |
| 38 | + |
| 39 | + $url = dcReallyFullUrl($article->mTitle); |
| 40 | + |
| 41 | + ccPrologue(); |
| 42 | + ccSubPrologue('Work', $url); |
| 43 | + dcBasics($article); |
| 44 | + if (isset($wgRightsUrl)) { |
| 45 | + print " <cc:license rdf:resource=\"$wgRightsUrl\" />\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 | + print "<?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?> |
| 83 | + |
| 84 | +<!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\"> |
| 85 | + |
| 86 | +<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" |
| 87 | + xmlns:dc=\"http://purl.org/dc/elements/1.1/\"> |
| 88 | + <rdf:Description rdf:about=\"$url\"> |
| 89 | +"; |
| 90 | +} |
| 91 | + |
| 92 | +/* private */ function dcEpilogue() { |
| 93 | + print " |
| 94 | + </rdf:Description> |
| 95 | +</rdf:RDF> |
| 96 | +"; |
| 97 | +} |
| 98 | + |
| 99 | +/* private */ function dcBasics($article) { |
| 100 | + global $wgLanguageCode, $wgSitename; |
| 101 | + |
| 102 | + dcElement('title', $article->mTitle->getText()); |
| 103 | + dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename); |
| 104 | + dcElement('language', $wgLanguageCode); |
| 105 | + dcElement('type', 'Text'); |
| 106 | + dcElement('format', 'text/html'); |
| 107 | + dcElement('identifier', dcReallyFullUrl($article->mTitle)); |
| 108 | + dcElement('date', dcDate($article->getTimestamp())); |
| 109 | + dcPerson('creator', $article->getUser()); |
| 110 | + |
| 111 | + $contributors = dcContributors($article->mTitle); |
| 112 | + |
| 113 | + foreach ($contributors as $cid) { |
| 114 | + dcPerson('contributor', $cid); |
| 115 | + } |
| 116 | + |
| 117 | + dcRights($article); |
| 118 | +} |
| 119 | + |
| 120 | +/* private */ function ccPrologue() { |
| 121 | + global $wgOutputEncoding; |
| 122 | + |
| 123 | + echo "<?xml version='1.0' encoding='{$wgOutputEncoding}' ?> |
| 124 | + |
| 125 | +<rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\" |
| 126 | + xmlns:dc=\"http://purl.org/dc/elements/1.1/\" |
| 127 | + xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"> |
| 128 | +"; |
| 129 | +} |
| 130 | + |
| 131 | +/* private */ function ccSubPrologue($type, $url) { |
| 132 | + echo " <cc:{$type} rdf:about=\"{$url}\">\n"; |
| 133 | +} |
| 134 | + |
| 135 | +/* private */ function ccSubEpilogue($type) { |
| 136 | + echo " </cc:{$type}>\n"; |
| 137 | +} |
| 138 | + |
| 139 | +/* private */ function ccLicense($terms) { |
| 140 | + |
| 141 | + foreach ($terms as $term) { |
| 142 | + switch ($term) { |
| 143 | + case 're': |
| 144 | + ccTerm('permits', "Reproduction"); break; |
| 145 | + case 'di': |
| 146 | + ccTerm('permits', "Distribution"); break; |
| 147 | + case 'de': |
| 148 | + ccTerm('permits', "DerivativeWorks"); break; |
| 149 | + case 'nc': |
| 150 | + ccTerm('prohibits', "CommercialUse"); break; |
| 151 | + case 'no': |
| 152 | + ccTerm('requires', "Notice"); break; |
| 153 | + case 'by': |
| 154 | + ccTerm('requires', "Attribution"); break; |
| 155 | + case 'sa': |
| 156 | + ccTerm('requires', "ShareAlike"); break; |
| 157 | + case 'sc': |
| 158 | + ccTerm('requires', "SourceCode"); break; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/* private */ function ccTerm($term, $name) { |
| 164 | + print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n"; |
| 165 | +} |
| 166 | + |
| 167 | +/* private */ function ccEpilogue() { |
| 168 | + echo "</rdf:RDF>\n"; |
| 169 | +} |
| 170 | + |
| 171 | +/* private */ function dcElement($name, $value) { |
| 172 | + print " <dc:{$name}>{$value}</dc:{$name}>\n"; |
| 173 | +} |
| 174 | + |
| 175 | +/* private */ function dcDate($timestamp) { |
| 176 | + return substr($timestamp, 0, 4) . "-" |
| 177 | + . substr($timestamp, 4, 2) . "-" |
| 178 | + . substr($timestamp, 6, 2); |
| 179 | +} |
| 180 | + |
| 181 | +/* private */ function dcReallyFullUrl($title) { |
| 182 | + $title->getFullURL(); |
| 183 | +} |
| 184 | + |
| 185 | +/* private */ function dcPageOrString($name, $page, $str) { |
| 186 | + $nt = Title::newFromText($page); |
| 187 | + |
| 188 | + if (!$nt || $nt->getArticleID() == 0) { |
| 189 | + dcElement($name, $str); |
| 190 | + } else { |
| 191 | + dcPage($name, $nt); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +/* private */ function dcPage($name, $title) { |
| 196 | + dcUrl($name, dcReallyFullUrl($title)); |
| 197 | +} |
| 198 | + |
| 199 | +/* private */ function dcUrl($name, $url) { |
| 200 | + print " <dc:{$name} rdf:resource=\"{$url}\" />\n"; |
| 201 | +} |
| 202 | + |
| 203 | +/* private */ function dcPerson($name, $id) { |
| 204 | + global $wgLang; |
| 205 | + |
| 206 | + if ($id == 0) { |
| 207 | + dcElement($name, wfMsg("anonymous")); |
| 208 | + } else { |
| 209 | + $user_name = User::whoIs($id); |
| 210 | + dcPageOrString($name, $wgLang->getNsText(NS_USER) . ":" . $user_name, $user_name); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/* private */ function dcContributors($title) { |
| 215 | + |
| 216 | + $contribs = array(); |
| 217 | + |
| 218 | + $res = wfQuery("SELECT DISTINCT old_user" . |
| 219 | + " FROM old " . |
| 220 | + " WHERE old_namespace = " . $title->getNamespace() . |
| 221 | + " AND old_title = '" . $title->getDBkey() . "'", DB_READ); |
| 222 | + |
| 223 | + while ( $line = wfFetchObject( $res ) ) { |
| 224 | + $contribs[] = $line->old_user; |
| 225 | + } |
| 226 | + |
| 227 | + return $contribs; |
| 228 | +} |
| 229 | + |
| 230 | +/* Takes an arg, for future enhancement with different rights for |
| 231 | + different pages. */ |
| 232 | + |
| 233 | +/* private */ function dcRights($article) { |
| 234 | + |
| 235 | + global $wgRightsPage, $wgRightsUrl, $wgRightsText; |
| 236 | + |
| 237 | + if (isset($wgRightsPage) && |
| 238 | + ($nt = Title::newFromText($wgRightsPage)) |
| 239 | + && ($nt->getArticleID() != 0)) { |
| 240 | + dcPage('rights', $nt); |
| 241 | + } else if (isset($wgRightsUrl)) { |
| 242 | + dcUrl('rights', $wgRightsUrl); |
| 243 | + } else if (isset($wgRightsText)) { |
| 244 | + dcElement('rights', $wgRightsText); |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +/* private */ function ccGetTerms($url) { |
| 249 | + global $wgLicenseTerms; |
| 250 | + |
| 251 | + if (isset($wgLicenseTerms)) { |
| 252 | + return $wgLicenseTerms; |
| 253 | + } else { |
| 254 | + $known = getKnownLicenses(); |
| 255 | + return $known[$url]; |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +/* private */ function getKnownLicenses() { |
| 260 | + |
| 261 | + $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc', |
| 262 | + 'by-nc-sa', 'by-sa', 'nd', 'nd-nc', |
| 263 | + 'nc', 'nc-sa', 'sa'); |
| 264 | + |
| 265 | + $knownLicenses = array(); |
| 266 | + |
| 267 | + foreach ($ccLicenses as $license) { |
| 268 | + $lurl = "http://creativecommons.org/licenses/{$license}/1.0/"; |
| 269 | + $knownLicenses[$lurl] = explode('-', $license); |
| 270 | + $knownLicenses[$lurl][] = 're'; |
| 271 | + $knownLicenses[$lurl][] = 'di'; |
| 272 | + $knownLicenses[$lurl][] = 'no'; |
| 273 | + if (!in_array('nd', $knownLicenses[$lurl])) { |
| 274 | + $knownLicenses[$lurl][] = 'de'; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + /* Handle the GPL and LGPL, too. */ |
| 279 | + |
| 280 | + $knownLicenses["http://creativecommons.org/licenses/GPL/2.0/"] = |
| 281 | + array('de', 're', 'di', 'no', 'sa', 'sc'); |
| 282 | + $knownLicenses["http://creativecommons.org/licenses/LGPL/2.1/"] = |
| 283 | + array('de', 're', 'di', 'no', 'sa', 'sc'); |
| 284 | + $knownLicenses["http://www.gnu.org/copyleft/fdl.html"] = |
| 285 | + array('de', 're', 'di', 'no', 'sa', 'sc'); |
| 286 | + |
| 287 | + return $knownLicenses; |
| 288 | +} |
| 289 | + |
| 290 | +?> |
Property changes on: trunk/phase3/includes/Metadata.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 291 | + native |
Added: svn:keywords |
2 | 292 | + Author Date Id Revision |