Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Metadata.php

Go to the documentation of this file.
00001 <?php 00002 /* Metadata.php -- provides DublinCore and CreativeCommons metadata 00003 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 00020 define("RDF_TYPE_PREFS", "application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1"); 00021 00022 function wfDublinCoreRdf($article) { 00023 00024 $url = dcReallyFullUrl($article->mTitle); 00025 00026 if (rdfSetup()) { 00027 dcPrologue($url); 00028 dcBasics($article); 00029 dcEpilogue(); 00030 } 00031 } 00032 00033 function wfCreativeCommonsRdf($article) { 00034 00035 if (rdfSetup()) { 00036 global $wgRightsUrl; 00037 00038 $url = dcReallyFullUrl($article->mTitle); 00039 00040 ccPrologue(); 00041 ccSubPrologue('Work', $url); 00042 dcBasics($article); 00043 if (isset($wgRightsUrl)) { 00044 $url = htmlspecialchars( $wgRightsUrl ); 00045 print " <cc:license rdf:resource=\"$url\" />\n"; 00046 } 00047 00048 ccSubEpilogue('Work'); 00049 00050 if (isset($wgRightsUrl)) { 00051 $terms = ccGetTerms($wgRightsUrl); 00052 if ($terms) { 00053 ccSubPrologue('License', $wgRightsUrl); 00054 ccLicense($terms); 00055 ccSubEpilogue('License'); 00056 } 00057 } 00058 } 00059 00060 ccEpilogue(); 00061 } 00062 00063 /* private */ function rdfSetup() { 00064 global $wgOut, $wgRdfMimeType, $_SERVER; 00065 00066 $rdftype = wfNegotiateType(wfAcceptToPrefs($_SERVER['HTTP_ACCEPT']), wfAcceptToPrefs(RDF_TYPE_PREFS)); 00067 00068 if (!$rdftype) { 00069 wfHttpError(406, "Not Acceptable", wfMsg("notacceptable")); 00070 return false; 00071 } else { 00072 $wgOut->disable(); 00073 header( "Content-type: {$rdftype}" ); 00074 $wgOut->sendCacheControl(); 00075 return true; 00076 } 00077 } 00078 00079 /* private */ function dcPrologue($url) { 00080 global $wgOutputEncoding; 00081 00082 $url = htmlspecialchars( $url ); 00083 print "<" . "?xml version=\"1.0\" encoding=\"{$wgOutputEncoding}\" ?" . "> 00084 00085 <!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\"> 00086 00087 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" 00088 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"> 00089 <rdf:Description rdf:about=\"$url\"> 00090 "; 00091 } 00092 00093 /* private */ function dcEpilogue() { 00094 print " 00095 </rdf:Description> 00096 </rdf:RDF> 00097 "; 00098 } 00099 00100 /* private */ function dcBasics($article) { 00101 global $wgLanguageCode, $wgSitename; 00102 00103 dcElement('title', $article->mTitle->getText()); 00104 dcPageOrString('publisher', wfMsg('aboutpage'), $wgSitename); 00105 dcElement('language', $wgLanguageCode); 00106 dcElement('type', 'Text'); 00107 dcElement('format', 'text/html'); 00108 dcElement('identifier', dcReallyFullUrl($article->mTitle)); 00109 dcElement('date', dcDate($article->getTimestamp())); 00110 00111 $last_editor = $article->getUser(); 00112 00113 if ($last_editor == 0) { 00114 dcPerson('creator', 0); 00115 } else { 00116 dcPerson('creator', $last_editor, $article->getUserText(), 00117 User::whoIsReal($last_editor)); 00118 } 00119 00120 $contributors = $article->getContributors(); 00121 00122 foreach ($contributors as $cid => $user_parts) { 00123 dcPerson('contributor', $cid, $user_parts[0], $user_parts[1]); 00124 } 00125 00126 dcRights($article); 00127 } 00128 00129 /* private */ function ccPrologue() { 00130 global $wgOutputEncoding; 00131 00132 echo "<" . "?xml version='1.0' encoding='{$wgOutputEncoding}' ?" . "> 00133 00134 <rdf:RDF xmlns:cc=\"http://web.resource.org/cc/\" 00135 xmlns:dc=\"http://purl.org/dc/elements/1.1/\" 00136 xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"> 00137 "; 00138 } 00139 00140 /* private */ function ccSubPrologue($type, $url) { 00141 $url = htmlspecialchars( $url ); 00142 echo " <cc:{$type} rdf:about=\"{$url}\">\n"; 00143 } 00144 00145 /* private */ function ccSubEpilogue($type) { 00146 echo " </cc:{$type}>\n"; 00147 } 00148 00149 /* private */ function ccLicense($terms) { 00150 00151 foreach ($terms as $term) { 00152 switch ($term) { 00153 case 're': 00154 ccTerm('permits', "Reproduction"); break; 00155 case 'di': 00156 ccTerm('permits', "Distribution"); break; 00157 case 'de': 00158 ccTerm('permits', "DerivativeWorks"); break; 00159 case 'nc': 00160 ccTerm('prohibits', "CommercialUse"); break; 00161 case 'no': 00162 ccTerm('requires', "Notice"); break; 00163 case 'by': 00164 ccTerm('requires', "Attribution"); break; 00165 case 'sa': 00166 ccTerm('requires', "ShareAlike"); break; 00167 case 'sc': 00168 ccTerm('requires', "SourceCode"); break; 00169 } 00170 } 00171 } 00172 00173 /* private */ function ccTerm($term, $name) { 00174 print " <cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n"; 00175 } 00176 00177 /* private */ function ccEpilogue() { 00178 echo "</rdf:RDF>\n"; 00179 } 00180 00181 /* private */ function dcElement($name, $value) { 00182 $value = htmlspecialchars( $value ); 00183 print " <dc:{$name}>{$value}</dc:{$name}>\n"; 00184 } 00185 00186 /* private */ function dcDate($timestamp) { 00187 return substr($timestamp, 0, 4) . "-" 00188 . substr($timestamp, 4, 2) . "-" 00189 . substr($timestamp, 6, 2); 00190 } 00191 00192 /* private */ function dcReallyFullUrl($title) { 00193 return $title->getFullURL(); 00194 } 00195 00196 /* private */ function dcPageOrString($name, $page, $str) { 00197 $nt = Title::newFromText($page); 00198 00199 if (!$nt || $nt->getArticleID() == 0) { 00200 dcElement($name, $str); 00201 } else { 00202 dcPage($name, $nt); 00203 } 00204 } 00205 00206 /* private */ function dcPage($name, $title) { 00207 dcUrl($name, dcReallyFullUrl($title)); 00208 } 00209 00210 /* private */ function dcUrl($name, $url) { 00211 $url = htmlspecialchars( $url ); 00212 print " <dc:{$name} rdf:resource=\"{$url}\" />\n"; 00213 } 00214 00215 /* private */ function dcPerson($name, $id, $user_name="", $user_real_name="") { 00216 global $wgLang; 00217 00218 if ($id == 0) { 00219 dcElement($name, wfMsg("anonymous")); 00220 } else if ( !empty($user_real_name) ) { 00221 dcElement($name, $user_real_name); 00222 } else { 00223 # XXX: This shouldn't happen. 00224 if( empty( $user_name ) ) { 00225 $user_name = User::whoIs($id); 00226 } 00227 dcPageOrString($name, $wgLang->getNsText(NS_USER) . ":" . $user_name, wfMsg("siteuser", $user_name)); 00228 } 00229 } 00230 00231 /* Takes an arg, for future enhancement with different rights for 00232 different pages. */ 00233 00234 /* private */ function dcRights($article) { 00235 00236 global $wgRightsPage, $wgRightsUrl, $wgRightsText; 00237 00238 if (isset($wgRightsPage) && 00239 ($nt = Title::newFromText($wgRightsPage)) 00240 && ($nt->getArticleID() != 0)) { 00241 dcPage('rights', $nt); 00242 } else if (isset($wgRightsUrl)) { 00243 dcUrl('rights', $wgRightsUrl); 00244 } else if (isset($wgRightsText)) { 00245 dcElement('rights', $wgRightsText); 00246 } 00247 } 00248 00249 /* private */ function ccGetTerms($url) { 00250 global $wgLicenseTerms; 00251 00252 if (isset($wgLicenseTerms)) { 00253 return $wgLicenseTerms; 00254 } else { 00255 $known = getKnownLicenses(); 00256 return $known[$url]; 00257 } 00258 } 00259 00260 /* private */ function getKnownLicenses() { 00261 00262 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc', 00263 'by-nc-sa', 'by-sa', 'nd', 'nd-nc', 00264 'nc', 'nc-sa', 'sa'); 00265 00266 $knownLicenses = array(); 00267 00268 foreach ($ccLicenses as $license) { 00269 $lurl = "http://creativecommons.org/licenses/{$license}/1.0/"; 00270 $knownLicenses[$lurl] = explode('-', $license); 00271 $knownLicenses[$lurl][] = 're'; 00272 $knownLicenses[$lurl][] = 'di'; 00273 $knownLicenses[$lurl][] = 'no'; 00274 if (!in_array('nd', $knownLicenses[$lurl])) { 00275 $knownLicenses[$lurl][] = 'de'; 00276 } 00277 } 00278 00279 /* Handle the GPL and LGPL, too. */ 00280 00281 $knownLicenses["http://creativecommons.org/licenses/GPL/2.0/"] = 00282 array('de', 're', 'di', 'no', 'sa', 'sc'); 00283 $knownLicenses["http://creativecommons.org/licenses/LGPL/2.1/"] = 00284 array('de', 're', 'di', 'no', 'sa', 'sc'); 00285 $knownLicenses["http://www.gnu.org/copyleft/fdl.html"] = 00286 array('de', 're', 'di', 'no', 'sa', 'sc'); 00287 00288 return $knownLicenses; 00289 } 00290 00291 ?>

Generated on Tue Jun 29 23:40:05 2004 for Mediawiki by doxygen 1.3.7