Remove various unused parameters
[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 global $wgContLang;
119
120 if( $user->isAnon() ){
121 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
122 } else if( $real = $user->getRealName() ) {
123 $this->element( $name, $real );
124 } else {
125 $userName = $user->getName();
126 $this->pageOrString( $name, $user->getUserPage(), wfMsgExt( 'siteuser', 'parsemag', $userName, $userName ) );
127 }
128 }
129
130 /**
131 * Takes an arg, for future enhancement with different rights for
132 * different pages.
133 */
134 protected function rights() {
135 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
136
137 if( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
138 && ($nt->getArticleID() != 0)) {
139 $this->page('rights', $nt);
140 } else if( $wgRightsUrl ){
141 $this->url('rights', $wgRightsUrl);
142 } else if( $wgRightsText ){
143 $this->element( 'rights', $wgRightsText );
144 }
145 }
146
147 protected function getTerms( $url ){
148 global $wgLicenseTerms;
149
150 if( $wgLicenseTerms ){
151 return $wgLicenseTerms;
152 } else {
153 $known = $this->getKnownLicenses();
154 if( isset( $known[$url] ) ) {
155 return $known[$url];
156 } else {
157 return array();
158 }
159 }
160 }
161
162 protected function getKnownLicenses() {
163 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
164 'by-nc-sa', 'by-sa');
165 $ccVersions = array('1.0', '2.0');
166 $knownLicenses = array();
167
168 foreach ($ccVersions as $version) {
169 foreach ($ccLicenses as $license) {
170 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
171 # 2.0 dropped the non-attribs licenses
172 continue;
173 }
174 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
175 $knownLicenses[$lurl] = explode('-', $license);
176 $knownLicenses[$lurl][] = 're';
177 $knownLicenses[$lurl][] = 'di';
178 $knownLicenses[$lurl][] = 'no';
179 if (!in_array('nd', $knownLicenses[$lurl])) {
180 $knownLicenses[$lurl][] = 'de';
181 }
182 }
183 }
184
185 /* Handle the GPL and LGPL, too. */
186
187 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
188 array('de', 're', 'di', 'no', 'sa', 'sc');
189 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
190 array('de', 're', 'di', 'no', 'sa', 'sc');
191 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
192 array('de', 're', 'di', 'no', 'sa', 'sc');
193
194 return $knownLicenses;
195 }
196 }
197
198 class DublinCoreRdf extends RdfMetaData {
199
200 public function show(){
201 if( $this->setup() ){
202 $this->prologue();
203 $this->basics();
204 $this->epilogue();
205 }
206 }
207
208 /**
209 * begin of the page
210 */
211 protected function prologue() {
212 global $wgOutputEncoding;
213
214 $url = htmlspecialchars( $this->reallyFullUrl() );
215 print <<<PROLOGUE
216 <?xml version="1.0" encoding="{$wgOutputEncoding}" ?>
217 <!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">
218 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
219 xmlns:dc="http://purl.org/dc/elements/1.1/">
220 <rdf:Description rdf:about="{$url}">
221
222 PROLOGUE;
223 }
224
225 /**
226 * end of the page
227 */
228 protected function epilogue() {
229 print <<<EPILOGUE
230 </rdf:Description>
231 </rdf:RDF>
232 EPILOGUE;
233 }
234 }
235
236 class CreativeCommonsRdf extends RdfMetaData {
237
238 public function show(){
239 if( $this->setup() ){
240 global $wgRightsUrl;
241
242 $url = $this->reallyFullUrl();
243
244 $this->prologue();
245 $this->subPrologue('Work', $url);
246
247 $this->basics();
248 if( $wgRightsUrl ){
249 $url = htmlspecialchars( $wgRightsUrl );
250 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
251 }
252
253 $this->subEpilogue('Work');
254
255 if( $wgRightsUrl ){
256 $terms = $this->getTerms( $wgRightsUrl );
257 if( $terms ){
258 $this->subPrologue( 'License', $wgRightsUrl );
259 $this->license( $terms );
260 $this->subEpilogue( 'License' );
261 }
262 }
263 }
264
265 $this->epilogue();
266 }
267
268 protected function prologue() {
269 global $wgOutputEncoding;
270 echo <<<PROLOGUE
271 <?xml version='1.0' encoding="{$wgOutputEncoding}" ?>
272 <rdf:RDF xmlns:cc="http://web.resource.org/cc/"
273 xmlns:dc="http://purl.org/dc/elements/1.1/"
274 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
275
276 PROLOGUE;
277 }
278
279 protected function subPrologue( $type, $url ){
280 $url = htmlspecialchars( $url );
281 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
282 }
283
284 protected function subEpilogue($type) {
285 echo "\t</cc:{$type}>\n";
286 }
287
288 protected function license($terms) {
289
290 foreach( $terms as $term ){
291 switch( $term ) {
292 case 're':
293 $this->term('permits', 'Reproduction'); break;
294 case 'di':
295 $this->term('permits', 'Distribution'); break;
296 case 'de':
297 $this->term('permits', 'DerivativeWorks'); break;
298 case 'nc':
299 $this->term('prohibits', 'CommercialUse'); break;
300 case 'no':
301 $this->term('requires', 'Notice'); break;
302 case 'by':
303 $this->term('requires', 'Attribution'); break;
304 case 'sa':
305 $this->term('requires', 'ShareAlike'); break;
306 case 'sc':
307 $this->term('requires', 'SourceCode'); break;
308 }
309 }
310 }
311
312 protected function term( $term, $name ){
313 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
314 }
315
316 protected function epilogue() {
317 echo "</rdf:RDF>\n";
318 }
319 }