Don't look for pipes in the root node.
[lhc/web/wiklou.git] / includes / Metadata.php
1 <?php
2 /**
3 * Provides DublinCore and CreativeCommons metadata
4 *
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
6 *
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.
11 *
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.
16 *
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
20 *
21 * @author Evan Prodromou <evan@wikitravel.org>
22 * @file
23 */
24
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';
27
28 /**
29 * Constructor
30 * @param $article Article object
31 */
32 public function __construct( Article $article ) {
33 $this->mArticle = $article;
34 }
35
36 public abstract function show();
37
38 /**
39 *
40 */
41 protected function setup() {
42 global $wgOut, $wgRequest;
43
44 $httpaccept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
45 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) );
46
47 if( !$rdftype ){
48 wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
49 return false;
50 } else {
51 $wgOut->disable();
52 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
53 $wgOut->sendCacheControl();
54 return true;
55 }
56 }
57
58 /**
59 *
60 */
61 protected function reallyFullUrl() {
62 return $this->mArticle->getTitle()->getFullURL();
63 }
64
65 protected function basics() {
66 global $wgLanguageCode, $wgSitename;
67
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() ) );
75
76 $lastEditor = User::newFromId( $this->mArticle->getUser() );
77 $this->person( 'creator', $lastEditor );
78
79 foreach( $this->mArticle->getContributors() as $user ){
80 $this->person( 'contributor', $user );
81 }
82
83 $this->rights();
84 }
85
86 protected function element( $name, $value ) {
87 $value = htmlspecialchars( $value );
88 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
89 }
90
91 protected function date($timestamp) {
92 return substr($timestamp, 0, 4) . '-'
93 . substr($timestamp, 4, 2) . '-'
94 . substr($timestamp, 6, 2);
95 }
96
97 protected function pageOrString( $name, $page, $str ) {
98 if( $page instanceof Title )
99 $nt = $page;
100 else
101 $nt = Title::newFromText( $page );
102
103 if( !$nt || $nt->getArticleID() == 0 ){
104 $this->element( $name, $str );
105 } else {
106 $this->page( $name, $nt );
107 }
108 }
109
110 protected function page( $name, $title ) {
111 $this->url( $name, $title->getFullUrl() );
112 }
113
114 protected function url($name, $url) {
115 $url = htmlspecialchars( $url );
116 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
117 }
118
119 protected function person( $name, User $user ) {
120 if( $user->isAnon() ){
121 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
122 } else {
123 $real = $user->getRealName();
124 if( $real ) {
125 $this->element( $name, $real );
126 } else {
127 $userName = $user->getName();
128 $this->pageOrString( $name, $user->getUserPage(), wfMsgExt( 'siteuser', 'parsemag', $userName, $userName ) );
129 }
130 }
131 }
132
133 /**
134 * Takes an arg, for future enhancement with different rights for
135 * different pages.
136 */
137 protected function rights() {
138 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
139
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 );
147 }
148 }
149
150 protected function getTerms( $url ){
151 global $wgLicenseTerms;
152
153 if( $wgLicenseTerms ){
154 return $wgLicenseTerms;
155 } else {
156 $known = $this->getKnownLicenses();
157 if( isset( $known[$url] ) ) {
158 return $known[$url];
159 } else {
160 return array();
161 }
162 }
163 }
164
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();
170
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
175 continue;
176 }
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';
184 }
185 }
186 }
187
188 /* Handle the GPL and LGPL, too. */
189
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');
196
197 return $knownLicenses;
198 }
199 }
200
201 class DublinCoreRdf extends RdfMetaData {
202
203 public function show(){
204 if( $this->setup() ){
205 $this->prologue();
206 $this->basics();
207 $this->epilogue();
208 }
209 }
210
211 /**
212 * begin of the page
213 */
214 protected function prologue() {
215 global $wgOutputEncoding;
216
217 $url = htmlspecialchars( $this->reallyFullUrl() );
218 print <<<PROLOGUE
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}">
224
225 PROLOGUE;
226 }
227
228 /**
229 * end of the page
230 */
231 protected function epilogue() {
232 print <<<EPILOGUE
233 </rdf:Description>
234 </rdf:RDF>
235 EPILOGUE;
236 }
237 }
238
239 class CreativeCommonsRdf extends RdfMetaData {
240
241 public function show(){
242 if( $this->setup() ){
243 global $wgRightsUrl;
244
245 $url = $this->reallyFullUrl();
246
247 $this->prologue();
248 $this->subPrologue('Work', $url);
249
250 $this->basics();
251 if( $wgRightsUrl ){
252 $url = htmlspecialchars( $wgRightsUrl );
253 print "\t\t<cc:license rdf:resource=\"$url\" />\n";
254 }
255
256 $this->subEpilogue('Work');
257
258 if( $wgRightsUrl ){
259 $terms = $this->getTerms( $wgRightsUrl );
260 if( $terms ){
261 $this->subPrologue( 'License', $wgRightsUrl );
262 $this->license( $terms );
263 $this->subEpilogue( 'License' );
264 }
265 }
266 }
267
268 $this->epilogue();
269 }
270
271 protected function prologue() {
272 global $wgOutputEncoding;
273 echo <<<PROLOGUE
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#">
278
279 PROLOGUE;
280 }
281
282 protected function subPrologue( $type, $url ){
283 $url = htmlspecialchars( $url );
284 echo "\t<cc:{$type} rdf:about=\"{$url}\">\n";
285 }
286
287 protected function subEpilogue($type) {
288 echo "\t</cc:{$type}>\n";
289 }
290
291 protected function license($terms) {
292
293 foreach( $terms as $term ){
294 switch( $term ) {
295 case 're':
296 $this->term('permits', 'Reproduction'); break;
297 case 'di':
298 $this->term('permits', 'Distribution'); break;
299 case 'de':
300 $this->term('permits', 'DerivativeWorks'); break;
301 case 'nc':
302 $this->term('prohibits', 'CommercialUse'); break;
303 case 'no':
304 $this->term('requires', 'Notice'); break;
305 case 'by':
306 $this->term('requires', 'Attribution'); break;
307 case 'sa':
308 $this->term('requires', 'ShareAlike'); break;
309 case 'sc':
310 $this->term('requires', 'SourceCode'); break;
311 }
312 }
313 }
314
315 protected function term( $term, $name ){
316 print "\t\t<cc:{$term} rdf:resource=\"http://web.resource.org/cc/{$name}\" />\n";
317 }
318
319 protected function epilogue() {
320 echo "</rdf:RDF>\n";
321 }
322 }