check that $wgArticle is an instance of the Article class in Skin::pageStats() per...
[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 $this->pageOrString( $name, $user->getUserPage(), wfMsg( 'siteuser', $user->getName() ) );
126 }
127 }
128
129 /**
130 * Takes an arg, for future enhancement with different rights for
131 * different pages.
132 */
133 protected function rights() {
134 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
135
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 );
143 }
144 }
145
146 protected function getTerms( $url ){
147 global $wgLicenseTerms;
148
149 if( $wgLicenseTerms ){
150 return $wgLicenseTerms;
151 } else {
152 $known = $this->getKnownLicenses();
153 if( isset( $known[$url] ) ) {
154 return $known[$url];
155 } else {
156 return array();
157 }
158 }
159 }
160
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();
166
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
171 continue;
172 }
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';
180 }
181 }
182 }
183
184 /* Handle the GPL and LGPL, too. */
185
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');
192
193 return $knownLicenses;
194 }
195 }
196
197 class DublinCoreRdf extends RdfMetaData {
198
199 public function show(){
200 if( $this->setup() ){
201 $this->prologue();
202 $this->basics();
203 $this->epilogue();
204 }
205 }
206
207 /**
208 * begin of the page
209 */
210 protected function prologue() {
211 global $wgOutputEncoding;
212
213 $url = htmlspecialchars( $this->reallyFullUrl() );
214 print <<<PROLOGUE
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}">
220
221 PROLOGUE;
222 }
223
224 /**
225 * end of the page
226 */
227 protected function epilogue() {
228 print <<<EPILOGUE
229 </rdf:Description>
230 </rdf:RDF>
231 EPILOGUE;
232 }
233 }
234
235 class CreativeCommonsRdf extends RdfMetaData {
236
237 public function show(){
238 if( $this->setup() ){
239 global $wgRightsUrl;
240
241 $url = $this->reallyFullUrl();
242
243 $this->prologue();
244 $this->subPrologue('Work', $url);
245
246 $this->basics();
247 if( $wgRightsUrl ){
248 $url = htmlspecialchars( $wgRightsUrl );
249 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
250 }
251
252 $this->subEpilogue('Work');
253
254 if( $wgRightsUrl ){
255 $terms = $this->getTerms( $wgRightsUrl );
256 if( $terms ){
257 $this->subPrologue( 'License', $wgRightsUrl );
258 $this->license( $terms );
259 $this->subEpilogue( 'License' );
260 }
261 }
262 }
263
264 $this->epilogue();
265 }
266
267 protected function prologue() {
268 global $wgOutputEncoding;
269 echo <<<PROLOGUE
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#">
274
275 PROLOGUE;
276 }
277
278 protected function subPrologue( $type, $url ){
279 $url = htmlspecialchars( $url );
280 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
281 }
282
283 protected function subEpilogue($type) {
284 echo "\t</cc:{$type}>\n";
285 }
286
287 protected function license($terms) {
288
289 foreach( $terms as $term ){
290 switch( $term ) {
291 case 're':
292 $this->term('permits', 'Reproduction'); break;
293 case 'di':
294 $this->term('permits', 'Distribution'); break;
295 case 'de':
296 $this->term('permits', 'DerivativeWorks'); break;
297 case 'nc':
298 $this->term('prohibits', 'CommercialUse'); break;
299 case 'no':
300 $this->term('requires', 'Notice'); break;
301 case 'by':
302 $this->term('requires', 'Attribution'); break;
303 case 'sa':
304 $this->term('requires', 'ShareAlike'); break;
305 case 'sc':
306 $this->term('requires', 'SourceCode'); break;
307 }
308 }
309 }
310
311 protected function term( $term, $name ){
312 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
313 }
314
315 protected function epilogue() {
316 echo "</rdf:RDF>\n";
317 }
318 }