00001 <?php
00023 abstract class RdfMetaData {
00024 const RDF_TYPE_PREFS = 'application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1';
00025
00030 public function __construct( Article $article ){
00031 $this->mArticle = $article;
00032 }
00033
00034 public abstract function show();
00035
00039 protected function setup() {
00040 global $wgOut, $wgRequest;
00041
00042 $httpaccept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
00043 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) );
00044
00045 if( !$rdftype ){
00046 wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
00047 return false;
00048 } else {
00049 $wgOut->disable();
00050 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
00051 $wgOut->sendCacheControl();
00052 return true;
00053 }
00054 }
00055
00059 protected function reallyFullUrl() {
00060 return $this->mArticle->getTitle()->getFullURL();
00061 }
00062
00063 protected function basics() {
00064 global $wgContLanguageCode, $wgSitename;
00065
00066 $this->element( 'title', $this->mArticle->mTitle->getText() );
00067 $this->pageOrString( 'publisher', wfMsg( 'aboutpage' ), $wgSitename );
00068 $this->element( 'language', $wgContLanguageCode );
00069 $this->element( 'type', 'Text' );
00070 $this->element( 'format', 'text/html' );
00071 $this->element( 'identifier', $this->reallyFullUrl() );
00072 $this->element( 'date', $this->date( $this->mArticle->getTimestamp() ) );
00073
00074 $lastEditor = User::newFromId( $this->mArticle->getUser() );
00075 $this->person( 'creator', $lastEditor );
00076
00077 foreach( $this->mArticle->getContributors() as $user ){
00078 $this->person( 'contributor', $user );
00079 }
00080
00081 $this->rights();
00082 }
00083
00084 protected function element( $name, $value ) {
00085 $value = htmlspecialchars( $value );
00086 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
00087 }
00088
00089 protected function date($timestamp) {
00090 return substr($timestamp, 0, 4) . '-'
00091 . substr($timestamp, 4, 2) . '-'
00092 . substr($timestamp, 6, 2);
00093 }
00094
00095 protected function pageOrString( $name, $page, $str ){
00096 if( $page instanceof Title )
00097 $nt = $page;
00098 else
00099 $nt = Title::newFromText( $page );
00100
00101 if( !$nt || $nt->getArticleID() == 0 ){
00102 $this->element( $name, $str );
00103 } else {
00104 $this->page( $name, $nt );
00105 }
00106 }
00107
00108 protected function page( $name, $title ){
00109 $this->url( $name, $title->getFullUrl() );
00110 }
00111
00112 protected function url($name, $url) {
00113 $url = htmlspecialchars( $url );
00114 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
00115 }
00116
00117 protected function person($name, User $user ){
00118 global $wgContLang;
00119
00120 if( $user->isAnon() ){
00121 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
00122 } else if( $real = $user->getRealName() ) {
00123 $this->element( $name, $real );
00124 } else {
00125 $this->pageOrString( $name, $user->getUserPage(), wfMsg( 'siteuser', $user->getName() ) );
00126 }
00127 }
00128
00133 protected function rights() {
00134 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
00135
00136 if( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
00137 && ($nt->getArticleID() != 0)) {
00138 $this->page('rights', $nt);
00139 } else if( $wgRightsUrl ){
00140 $this->url('rights', $wgRightsUrl);
00141 } else if( $wgRightsText ){
00142 $this->element( 'rights', $wgRightsText );
00143 }
00144 }
00145
00146 protected function getTerms( $url ){
00147 global $wgLicenseTerms;
00148
00149 if( $wgLicenseTerms ){
00150 return $wgLicenseTerms;
00151 } else {
00152 $known = $this->getKnownLicenses();
00153 if( isset( $known[$url] ) ) {
00154 return $known[$url];
00155 } else {
00156 return array();
00157 }
00158 }
00159 }
00160
00161 protected function getKnownLicenses() {
00162 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
00163 'by-nc-sa', 'by-sa');
00164 $ccVersions = array('1.0', '2.0');
00165 $knownLicenses = array();
00166
00167 foreach ($ccVersions as $version) {
00168 foreach ($ccLicenses as $license) {
00169 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
00170 # 2.0 dropped the non-attribs licenses
00171 continue;
00172 }
00173 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
00174 $knownLicenses[$lurl] = explode('-', $license);
00175 $knownLicenses[$lurl][] = 're';
00176 $knownLicenses[$lurl][] = 'di';
00177 $knownLicenses[$lurl][] = 'no';
00178 if (!in_array('nd', $knownLicenses[$lurl])) {
00179 $knownLicenses[$lurl][] = 'de';
00180 }
00181 }
00182 }
00183
00184
00185
00186 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
00187 array('de', 're', 'di', 'no', 'sa', 'sc');
00188 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
00189 array('de', 're', 'di', 'no', 'sa', 'sc');
00190 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
00191 array('de', 're', 'di', 'no', 'sa', 'sc');
00192
00193 return $knownLicenses;
00194 }
00195 }
00196
00197 class DublinCoreRdf extends RdfMetaData {
00198
00199 public function show(){
00200 if( $this->setup() ){
00201 $this->prologue();
00202 $this->basics();
00203 $this->epilogue();
00204 }
00205 }
00206
00210 protected function prologue() {
00211 global $wgOutputEncoding;
00212
00213 $url = htmlspecialchars( $this->reallyFullUrl() );
00214 print <<<PROLOGUE
00215 <?xml version="1.0" encoding="{$wgOutputEncoding}" ?>
00216 <!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">
00217 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
00218 xmlns:dc="http://purl.org/dc/elements/1.1/">
00219 <rdf:Description rdf:about="{$url}">
00220
00221 PROLOGUE;
00222 }
00223
00227 protected function epilogue() {
00228 print <<<EPILOGUE
00229 </rdf:Description>
00230 </rdf:RDF>
00231 EPILOGUE;
00232 }
00233 }
00234
00235 class CreativeCommonsRdf extends RdfMetaData {
00236
00237 public function show(){
00238 if( $this->setup() ){
00239 global $wgRightsUrl;
00240
00241 $url = $this->reallyFullUrl();
00242
00243 $this->prologue();
00244 $this->subPrologue('Work', $url);
00245
00246 $this->basics();
00247 if( $wgRightsUrl ){
00248 $url = htmlspecialchars( $wgRightsUrl );
00249 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
00250 }
00251
00252 $this->subEpilogue('Work');
00253
00254 if( $wgRightsUrl ){
00255 $terms = $this->getTerms( $wgRightsUrl );
00256 if( $terms ){
00257 $this->subPrologue( 'License', $wgRightsUrl );
00258 $this->license( $terms );
00259 $this->subEpilogue( 'License' );
00260 }
00261 }
00262 }
00263
00264 $this->epilogue();
00265 }
00266
00267 protected function prologue() {
00268 global $wgOutputEncoding;
00269 echo <<<PROLOGUE
00270 <?xml version='1.0' encoding="{$wgOutputEncoding}" ?>
00271 <rdf:RDF xmlns:cc="http://web.resource.org/cc/"
00272 xmlns:dc="http://purl.org/dc/elements/1.1/"
00273 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
00274
00275 PROLOGUE;
00276 }
00277
00278 protected function subPrologue( $type, $url ){
00279 $url = htmlspecialchars( $url );
00280 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
00281 }
00282
00283 protected function subEpilogue($type) {
00284 echo "\t</cc:{$type}>\n";
00285 }
00286
00287 protected function license($terms) {
00288
00289 foreach( $terms as $term ){
00290 switch( $term ) {
00291 case 're':
00292 $this->term('permits', 'Reproduction'); break;
00293 case 'di':
00294 $this->term('permits', 'Distribution'); break;
00295 case 'de':
00296 $this->term('permits', 'DerivativeWorks'); break;
00297 case 'nc':
00298 $this->term('prohibits', 'CommercialUse'); break;
00299 case 'no':
00300 $this->term('requires', 'Notice'); break;
00301 case 'by':
00302 $this->term('requires', 'Attribution'); break;
00303 case 'sa':
00304 $this->term('requires', 'ShareAlike'); break;
00305 case 'sc':
00306 $this->term('requires', 'SourceCode'); break;
00307 }
00308 }
00309 }
00310
00311 protected function term( $term, $name ){
00312 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
00313 }
00314
00315 protected function epilogue() {
00316 echo "</rdf:RDF>\n";
00317 }
00318 }