* (bug 24563) Entries on Special:WhatLinksHere now have a link to their history
[lhc/web/wiklou.git] / includes / Metadata.php
1 <?php
2 /**
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * @author Evan Prodromou <evan@wikitravel.org>
21 */
22
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';
25
26 /**
27 * Constructor
28 * @param $article Article object
29 */
30 public function __construct( Article $article ){
31 $this->mArticle = $article;
32 }
33
34 public abstract function show();
35
36 /**
37 *
38 */
39 protected function setup() {
40 global $wgOut, $wgRequest;
41
42 $httpaccept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
43 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) );
44
45 if( !$rdftype ){
46 wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
47 return false;
48 } else {
49 $wgOut->disable();
50 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
51 $wgOut->sendCacheControl();
52 return true;
53 }
54 }
55
56 /**
57 *
58 */
59 protected function reallyFullUrl() {
60 return $this->mArticle->getTitle()->getFullURL();
61 }
62
63 protected function basics() {
64 global $wgContLanguageCode, $wgSitename;
65
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() ) );
73
74 $lastEditor = User::newFromId( $this->mArticle->getUser() );
75 $this->person( 'creator', $lastEditor );
76
77 foreach( $this->mArticle->getContributors() as $user ){
78 $this->person( 'contributor', $user );
79 }
80
81 $this->rights();
82 }
83
84 protected function element( $name, $value ) {
85 $value = htmlspecialchars( $value );
86 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
87 }
88
89 protected function date($timestamp) {
90 return substr($timestamp, 0, 4) . '-'
91 . substr($timestamp, 4, 2) . '-'
92 . substr($timestamp, 6, 2);
93 }
94
95 protected function pageOrString( $name, $page, $str ){
96 if( $page instanceof Title )
97 $nt = $page;
98 else
99 $nt = Title::newFromText( $page );
100
101 if( !$nt || $nt->getArticleID() == 0 ){
102 $this->element( $name, $str );
103 } else {
104 $this->page( $name, $nt );
105 }
106 }
107
108 protected function page( $name, $title ){
109 $this->url( $name, $title->getFullUrl() );
110 }
111
112 protected function url($name, $url) {
113 $url = htmlspecialchars( $url );
114 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
115 }
116
117 protected function person($name, User $user ){
118 if( $user->isAnon() ){
119 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
120 } else if( $real = $user->getRealName() ) {
121 $this->element( $name, $real );
122 } else {
123 $userName = $user->getName();
124 $this->pageOrString( $name, $user->getUserPage(), wfMsgExt( 'siteuser', 'parsemag', $userName, $userName ) );
125 }
126 }
127
128 /**
129 * Takes an arg, for future enhancement with different rights for
130 * different pages.
131 */
132 protected function rights() {
133 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
134
135 if( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
136 && ($nt->getArticleID() != 0)) {
137 $this->page('rights', $nt);
138 } else if( $wgRightsUrl ){
139 $this->url('rights', $wgRightsUrl);
140 } else if( $wgRightsText ){
141 $this->element( 'rights', $wgRightsText );
142 }
143 }
144
145 protected function getTerms( $url ){
146 global $wgLicenseTerms;
147
148 if( $wgLicenseTerms ){
149 return $wgLicenseTerms;
150 } else {
151 $known = $this->getKnownLicenses();
152 if( isset( $known[$url] ) ) {
153 return $known[$url];
154 } else {
155 return array();
156 }
157 }
158 }
159
160 protected function getKnownLicenses() {
161 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
162 'by-nc-sa', 'by-sa');
163 $ccVersions = array('1.0', '2.0');
164 $knownLicenses = array();
165
166 foreach ($ccVersions as $version) {
167 foreach ($ccLicenses as $license) {
168 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
169 # 2.0 dropped the non-attribs licenses
170 continue;
171 }
172 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
173 $knownLicenses[$lurl] = explode('-', $license);
174 $knownLicenses[$lurl][] = 're';
175 $knownLicenses[$lurl][] = 'di';
176 $knownLicenses[$lurl][] = 'no';
177 if (!in_array('nd', $knownLicenses[$lurl])) {
178 $knownLicenses[$lurl][] = 'de';
179 }
180 }
181 }
182
183 /* Handle the GPL and LGPL, too. */
184
185 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
186 array('de', 're', 'di', 'no', 'sa', 'sc');
187 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
188 array('de', 're', 'di', 'no', 'sa', 'sc');
189 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
190 array('de', 're', 'di', 'no', 'sa', 'sc');
191
192 return $knownLicenses;
193 }
194 }
195
196 class DublinCoreRdf extends RdfMetaData {
197
198 public function show(){
199 if( $this->setup() ){
200 $this->prologue();
201 $this->basics();
202 $this->epilogue();
203 }
204 }
205
206 /**
207 * begin of the page
208 */
209 protected function prologue() {
210 global $wgOutputEncoding;
211
212 $url = htmlspecialchars( $this->reallyFullUrl() );
213 print <<<PROLOGUE
214 <?xml version="1.0" encoding="{$wgOutputEncoding}" ?>
215 <!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">
216 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
217 xmlns:dc="http://purl.org/dc/elements/1.1/">
218 <rdf:Description rdf:about="{$url}">
219
220 PROLOGUE;
221 }
222
223 /**
224 * end of the page
225 */
226 protected function epilogue() {
227 print <<<EPILOGUE
228 </rdf:Description>
229 </rdf:RDF>
230 EPILOGUE;
231 }
232 }
233
234 class CreativeCommonsRdf extends RdfMetaData {
235
236 public function show(){
237 if( $this->setup() ){
238 global $wgRightsUrl;
239
240 $url = $this->reallyFullUrl();
241
242 $this->prologue();
243 $this->subPrologue('Work', $url);
244
245 $this->basics();
246 if( $wgRightsUrl ){
247 $url = htmlspecialchars( $wgRightsUrl );
248 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
249 }
250
251 $this->subEpilogue('Work');
252
253 if( $wgRightsUrl ){
254 $terms = $this->getTerms( $wgRightsUrl );
255 if( $terms ){
256 $this->subPrologue( 'License', $wgRightsUrl );
257 $this->license( $terms );
258 $this->subEpilogue( 'License' );
259 }
260 }
261 }
262
263 $this->epilogue();
264 }
265
266 protected function prologue() {
267 global $wgOutputEncoding;
268 echo <<<PROLOGUE
269 <?xml version='1.0' encoding="{$wgOutputEncoding}" ?>
270 <rdf:RDF xmlns:cc="http://web.resource.org/cc/"
271 xmlns:dc="http://purl.org/dc/elements/1.1/"
272 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
273
274 PROLOGUE;
275 }
276
277 protected function subPrologue( $type, $url ){
278 $url = htmlspecialchars( $url );
279 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
280 }
281
282 protected function subEpilogue($type) {
283 echo "\t</cc:{$type}>\n";
284 }
285
286 protected function license($terms) {
287
288 foreach( $terms as $term ){
289 switch( $term ) {
290 case 're':
291 $this->term('permits', 'Reproduction'); break;
292 case 'di':
293 $this->term('permits', 'Distribution'); break;
294 case 'de':
295 $this->term('permits', 'DerivativeWorks'); break;
296 case 'nc':
297 $this->term('prohibits', 'CommercialUse'); break;
298 case 'no':
299 $this->term('requires', 'Notice'); break;
300 case 'by':
301 $this->term('requires', 'Attribution'); break;
302 case 'sa':
303 $this->term('requires', 'ShareAlike'); break;
304 case 'sc':
305 $this->term('requires', 'SourceCode'); break;
306 }
307 }
308 }
309
310 protected function term( $term, $name ){
311 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
312 }
313
314 protected function epilogue() {
315 echo "</rdf:RDF>\n";
316 }
317 }