3 * Metadata.php -- provides DublinCore and CreativeCommons metadata
4 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @author Evan Prodromou <evan@wikitravel.org>
23 abstract class RdfMetaData
{
24 const RDF_TYPE_PREFS
= 'application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1';
28 * @param $article Article object
30 public function __construct( Article
$article ){
31 $this->mArticle
= $article;
34 public abstract function show();
39 protected function setup() {
40 global $wgOut, $wgRequest;
42 $httpaccept = isset( $_SERVER['HTTP_ACCEPT'] ) ?
$_SERVER['HTTP_ACCEPT'] : null;
43 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self
::RDF_TYPE_PREFS
) );
46 wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
50 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
51 $wgOut->sendCacheControl();
59 protected function reallyFullUrl() {
60 return $this->mArticle
->getTitle()->getFullURL();
63 protected function basics() {
64 global $wgContLanguageCode, $wgSitename;
66 $this->element( 'title', $this->mArticle
->mTitle
->getText() );
67 $this->pageOrString( 'publisher', wfMsg( 'aboutpage' ), $wgSitename );
68 $this->element( 'language', $wgContLanguageCode );
69 $this->element( 'type', 'Text' );
70 $this->element( 'format', 'text/html' );
71 $this->element( 'identifier', $this->reallyFullUrl() );
72 $this->element( 'date', $this->date( $this->mArticle
->getTimestamp() ) );
74 $lastEditor = User
::newFromId( $this->mArticle
->getUser() );
75 $this->person( 'creator', $lastEditor );
77 foreach( $this->mArticle
->getContributors() as $user ){
78 $this->person( 'contributor', $user );
84 protected function element( $name, $value ) {
85 $value = htmlspecialchars( $value );
86 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
89 protected function date($timestamp) {
90 return substr($timestamp, 0, 4) . '-'
91 . substr($timestamp, 4, 2) . '-'
92 . substr($timestamp, 6, 2);
95 protected function pageOrString( $name, $page, $str ){
96 if( $page instanceof Title
)
99 $nt = Title
::newFromText( $page );
101 if( !$nt ||
$nt->getArticleID() == 0 ){
102 $this->element( $name, $str );
104 $this->page( $name, $nt );
108 protected function page( $name, $title ){
109 $this->url( $name, $title->getFullUrl() );
112 protected function url($name, $url) {
113 $url = htmlspecialchars( $url );
114 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
117 protected function person($name, User
$user ){
120 if( $user->isAnon() ){
121 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
122 } else if( $real = $user->getRealName() ) {
123 $this->element( $name, $real );
125 $this->pageOrString( $name, $user->getUserPage(), wfMsg( 'siteuser', $user->getName() ) );
130 * Takes an arg, for future enhancement with different rights for
133 protected function rights() {
134 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
136 if( $wgRightsPage && ( $nt = Title
::newFromText( $wgRightsPage ) )
137 && ($nt->getArticleID() != 0)) {
138 $this->page('rights', $nt);
139 } else if( $wgRightsUrl ){
140 $this->url('rights', $wgRightsUrl);
141 } else if( $wgRightsText ){
142 $this->element( 'rights', $wgRightsText );
146 protected function getTerms( $url ){
147 global $wgLicenseTerms;
149 if( $wgLicenseTerms ){
150 return $wgLicenseTerms;
152 $known = $this->getKnownLicenses();
153 if( isset( $known[$url] ) ) {
161 protected function getKnownLicenses() {
162 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
163 'by-nc-sa', 'by-sa');
164 $ccVersions = array('1.0', '2.0');
165 $knownLicenses = array();
167 foreach ($ccVersions as $version) {
168 foreach ($ccLicenses as $license) {
169 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
170 # 2.0 dropped the non-attribs licenses
173 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
174 $knownLicenses[$lurl] = explode('-', $license);
175 $knownLicenses[$lurl][] = 're';
176 $knownLicenses[$lurl][] = 'di';
177 $knownLicenses[$lurl][] = 'no';
178 if (!in_array('nd', $knownLicenses[$lurl])) {
179 $knownLicenses[$lurl][] = 'de';
184 /* Handle the GPL and LGPL, too. */
186 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
187 array('de', 're', 'di', 'no', 'sa', 'sc');
188 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
189 array('de', 're', 'di', 'no', 'sa', 'sc');
190 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
191 array('de', 're', 'di', 'no', 'sa', 'sc');
193 return $knownLicenses;
197 class DublinCoreRdf
extends RdfMetaData
{
199 public function show(){
200 if( $this->setup() ){
210 protected function prologue() {
211 global $wgOutputEncoding;
213 $url = htmlspecialchars( $this->reallyFullUrl() );
215 <?xml version="1.0" encoding="{$wgOutputEncoding}" ?>
216 <!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">
217 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
218 xmlns:dc="http://purl.org/dc/elements/1.1/">
219 <rdf:Description rdf:about="{$url}">
227 protected function epilogue() {
235 class CreativeCommonsRdf
extends RdfMetaData
{
237 public function show(){
238 if( $this->setup() ){
241 $url = $this->reallyFullUrl();
244 $this->subPrologue('Work', $url);
248 $url = htmlspecialchars( $wgRightsUrl );
249 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
252 $this->subEpilogue('Work');
255 $terms = $this->getTerms( $wgRightsUrl );
257 $this->subPrologue( 'License', $wgRightsUrl );
258 $this->license( $terms );
259 $this->subEpilogue( 'License' );
267 protected function prologue() {
268 global $wgOutputEncoding;
270 <?xml version='1.0' encoding="{$wgOutputEncoding}" ?>
271 <rdf:RDF xmlns:cc="http://web.resource.org/cc/"
272 xmlns:dc="http://purl.org/dc/elements/1.1/"
273 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
278 protected function subPrologue( $type, $url ){
279 $url = htmlspecialchars( $url );
280 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
283 protected function subEpilogue($type) {
284 echo "\t</cc:{$type}>\n";
287 protected function license($terms) {
289 foreach( $terms as $term ){
292 $this->term('permits', 'Reproduction'); break;
294 $this->term('permits', 'Distribution'); break;
296 $this->term('permits', 'DerivativeWorks'); break;
298 $this->term('prohibits', 'CommercialUse'); break;
300 $this->term('requires', 'Notice'); break;
302 $this->term('requires', 'Attribution'); break;
304 $this->term('requires', 'ShareAlike'); break;
306 $this->term('requires', 'SourceCode'); break;
311 protected function term( $term, $name ){
312 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
315 protected function epilogue() {