3 * Provides DublinCore and CreativeCommons metadata
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 * @author Evan Prodromou <evan@wikitravel.org>
25 abstract class RdfMetaData
{
26 const RDF_TYPE_PREFS
= 'application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1';
30 * @param $article Article object
32 public function __construct( Article
$article ) {
33 $this->mArticle
= $article;
36 public abstract function show();
41 protected function setup() {
42 global $wgOut, $wgRequest;
44 $httpaccept = isset( $_SERVER['HTTP_ACCEPT'] ) ?
$_SERVER['HTTP_ACCEPT'] : null;
45 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self
::RDF_TYPE_PREFS
) );
48 wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
52 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
53 $wgOut->sendCacheControl();
61 protected function reallyFullUrl() {
62 return $this->mArticle
->getTitle()->getFullURL();
65 protected function basics() {
66 global $wgLanguageCode, $wgSitename;
68 $this->element( 'title', $this->mArticle
->mTitle
->getText() );
69 $this->pageOrString( 'publisher', wfMsg( 'aboutpage' ), $wgSitename );
70 $this->element( 'language', $wgLanguageCode );
71 $this->element( 'type', 'Text' );
72 $this->element( 'format', 'text/html' );
73 $this->element( 'identifier', $this->reallyFullUrl() );
74 $this->element( 'date', $this->date( $this->mArticle
->getTimestamp() ) );
76 $lastEditor = User
::newFromId( $this->mArticle
->getUser() );
77 $this->person( 'creator', $lastEditor );
79 foreach( $this->mArticle
->getContributors() as $user ){
80 $this->person( 'contributor', $user );
86 protected function element( $name, $value ) {
87 $value = htmlspecialchars( $value );
88 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
91 protected function date($timestamp) {
92 return substr($timestamp, 0, 4) . '-'
93 . substr($timestamp, 4, 2) . '-'
94 . substr($timestamp, 6, 2);
97 protected function pageOrString( $name, $page, $str ) {
98 if( $page instanceof Title
)
101 $nt = Title
::newFromText( $page );
103 if( !$nt ||
$nt->getArticleID() == 0 ){
104 $this->element( $name, $str );
106 $this->page( $name, $nt );
110 protected function page( $name, $title ) {
111 $this->url( $name, $title->getFullUrl() );
114 protected function url($name, $url) {
115 $url = htmlspecialchars( $url );
116 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
119 protected function person( $name, User
$user ) {
120 if( $user->isAnon() ){
121 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
123 $real = $user->getRealName();
125 $this->element( $name, $real );
127 $userName = $user->getName();
128 $this->pageOrString( $name, $user->getUserPage(), wfMsgExt( 'siteuser', 'parsemag', $userName, $userName ) );
134 * Takes an arg, for future enhancement with different rights for
137 protected function rights() {
138 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
140 if( $wgRightsPage && ( $nt = Title
::newFromText( $wgRightsPage ) )
141 && ($nt->getArticleID() != 0)) {
142 $this->page('rights', $nt);
143 } else if( $wgRightsUrl ){
144 $this->url('rights', $wgRightsUrl);
145 } else if( $wgRightsText ){
146 $this->element( 'rights', $wgRightsText );
150 protected function getTerms( $url ){
151 global $wgLicenseTerms;
153 if( $wgLicenseTerms ){
154 return $wgLicenseTerms;
156 $known = $this->getKnownLicenses();
157 if( isset( $known[$url] ) ) {
165 protected function getKnownLicenses() {
166 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
167 'by-nc-sa', 'by-sa');
168 $ccVersions = array('1.0', '2.0');
169 $knownLicenses = array();
171 foreach ($ccVersions as $version) {
172 foreach ($ccLicenses as $license) {
173 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
174 # 2.0 dropped the non-attribs licenses
177 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
178 $knownLicenses[$lurl] = explode('-', $license);
179 $knownLicenses[$lurl][] = 're';
180 $knownLicenses[$lurl][] = 'di';
181 $knownLicenses[$lurl][] = 'no';
182 if (!in_array('nd', $knownLicenses[$lurl])) {
183 $knownLicenses[$lurl][] = 'de';
188 /* Handle the GPL and LGPL, too. */
190 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
191 array('de', 're', 'di', 'no', 'sa', 'sc');
192 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
193 array('de', 're', 'di', 'no', 'sa', 'sc');
194 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
195 array('de', 're', 'di', 'no', 'sa', 'sc');
197 return $knownLicenses;
201 class DublinCoreRdf
extends RdfMetaData
{
203 public function show(){
204 if( $this->setup() ){
214 protected function prologue() {
215 global $wgOutputEncoding;
217 $url = htmlspecialchars( $this->reallyFullUrl() );
219 <?xml version="1.0" encoding="{$wgOutputEncoding}" ?>
220 <!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">
221 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
222 xmlns:dc="http://purl.org/dc/elements/1.1/">
223 <rdf:Description rdf:about="{$url}">
231 protected function epilogue() {
239 class CreativeCommonsRdf
extends RdfMetaData
{
241 public function show(){
242 if( $this->setup() ){
245 $url = $this->reallyFullUrl();
248 $this->subPrologue('Work', $url);
252 $url = htmlspecialchars( $wgRightsUrl );
253 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
256 $this->subEpilogue('Work');
259 $terms = $this->getTerms( $wgRightsUrl );
261 $this->subPrologue( 'License', $wgRightsUrl );
262 $this->license( $terms );
263 $this->subEpilogue( 'License' );
271 protected function prologue() {
272 global $wgOutputEncoding;
274 <?xml version='1.0' encoding="{$wgOutputEncoding}" ?>
275 <rdf:RDF xmlns:cc="http://web.resource.org/cc/"
276 xmlns:dc="http://purl.org/dc/elements/1.1/"
277 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
282 protected function subPrologue( $type, $url ){
283 $url = htmlspecialchars( $url );
284 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
287 protected function subEpilogue($type) {
288 echo "\t</cc:{$type}>\n";
291 protected function license($terms) {
293 foreach( $terms as $term ){
296 $this->term('permits', 'Reproduction'); break;
298 $this->term('permits', 'Distribution'); break;
300 $this->term('permits', 'DerivativeWorks'); break;
302 $this->term('prohibits', 'CommercialUse'); break;
304 $this->term('requires', 'Notice'); break;
306 $this->term('requires', 'Attribution'); break;
308 $this->term('requires', 'ShareAlike'); break;
310 $this->term('requires', 'SourceCode'); break;
315 protected function term( $term, $name ){
316 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
319 protected function epilogue() {