testPngNativetZtxt requires zlib extension
[lhc/web/wiklou.git] / includes / Metadata.php
1 <?php
2 /**
3 * Base code to format 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @author Evan Prodromou <evan@wikitravel.org>
23 * @file
24 */
25
26 abstract class RdfMetaData {
27 const RDF_TYPE_PREFS = 'application/rdf+xml,text/xml;q=0.7,application/xml;q=0.5,text/rdf;q=0.1';
28
29 /**
30 * Constructor
31 * @param $article Article object
32 */
33 public function __construct( Page $article ) {
34 $this->mArticle = $article;
35 }
36
37 abstract public function show();
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 throw new HttpError( 406, wfMessage( 'notacceptable' ) );
47 }
48
49 $wgOut->disable();
50 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
51 $wgOut->sendCacheControl();
52 return true;
53 }
54
55 protected function reallyFullUrl() {
56 return $this->mArticle->getTitle()->getFullURL();
57 }
58
59 protected function basics() {
60 global $wgLanguageCode, $wgSitename;
61
62 $this->element( 'title', $this->mArticle->getTitle()->getText() );
63 $this->pageOrString( 'publisher', wfMessage( 'aboutpage' )->text(), $wgSitename );
64 $this->element( 'language', $wgLanguageCode );
65 $this->element( 'type', 'Text' );
66 $this->element( 'format', 'text/html' );
67 $this->element( 'identifier', $this->reallyFullUrl() );
68 $this->element( 'date', $this->date( $this->mArticle->getTimestamp() ) );
69
70 $lastEditor = User::newFromId( $this->mArticle->getUser() );
71 $this->person( 'creator', $lastEditor );
72
73 foreach( $this->mArticle->getContributors() as $user ) {
74 $this->person( 'contributor', $user );
75 }
76
77 $this->rights();
78 }
79
80 protected function element( $name, $value ) {
81 $value = htmlspecialchars( $value );
82 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
83 }
84
85 protected function date($timestamp) {
86 return substr($timestamp, 0, 4) . '-'
87 . substr($timestamp, 4, 2) . '-'
88 . substr($timestamp, 6, 2);
89 }
90
91 protected function pageOrString( $name, $page, $str ) {
92 if( $page instanceof Title ) {
93 $nt = $page;
94 } else {
95 $nt = Title::newFromText( $page );
96 }
97
98 if( !$nt || $nt->getArticleID() == 0 ) {
99 $this->element( $name, $str );
100 } else {
101 $this->page( $name, $nt );
102 }
103 }
104
105 /**
106 * @param $name string
107 * @param $title Title
108 */
109 protected function page( $name, $title ) {
110 $this->url( $name, $title->getFullUrl() );
111 }
112
113 protected function url($name, $url) {
114 $url = htmlspecialchars( $url );
115 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
116 }
117
118 protected function person( $name, User $user ) {
119 if( $user->isAnon() ) {
120 $this->element( $name, wfMessage( 'anonymous' )->numParams( 1 )->text() );
121 } else {
122 $real = $user->getRealName();
123 if( $real ) {
124 $this->element( $name, $real );
125 } else {
126 $userName = $user->getName();
127 $this->pageOrString(
128 $name,
129 $user->getUserPage(),
130 wfMessage( 'siteuser', $userName, $userName )->text()
131 );
132 }
133 }
134 }
135
136 /**
137 * Takes an arg, for future enhancement with different rights for
138 * different pages.
139 */
140 protected function rights() {
141 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
142
143 if( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
144 && ( $nt->getArticleID() != 0 ) ) {
145 $this->page( 'rights', $nt );
146 } elseif( $wgRightsUrl ) {
147 $this->url( 'rights', $wgRightsUrl );
148 } elseif( $wgRightsText ) {
149 $this->element( 'rights', $wgRightsText );
150 }
151 }
152
153 protected function getTerms( $url ) {
154 global $wgLicenseTerms;
155
156 if( $wgLicenseTerms ) {
157 return $wgLicenseTerms;
158 } else {
159 $known = $this->getKnownLicenses();
160 if( isset( $known[$url] ) ) {
161 return $known[$url];
162 } else {
163 return array();
164 }
165 }
166 }
167
168 protected function getKnownLicenses() {
169 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
170 'by-nc-sa', 'by-sa');
171 $ccVersions = array('1.0', '2.0');
172 $knownLicenses = array();
173
174 foreach ($ccVersions as $version) {
175 foreach ($ccLicenses as $license) {
176 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
177 # 2.0 dropped the non-attribs licenses
178 continue;
179 }
180 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
181 $knownLicenses[$lurl] = explode('-', $license);
182 $knownLicenses[$lurl][] = 're';
183 $knownLicenses[$lurl][] = 'di';
184 $knownLicenses[$lurl][] = 'no';
185 if (!in_array('nd', $knownLicenses[$lurl])) {
186 $knownLicenses[$lurl][] = 'de';
187 }
188 }
189 }
190
191 /* Handle the GPL and LGPL, too. */
192
193 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
194 array('de', 're', 'di', 'no', 'sa', 'sc');
195 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
196 array('de', 're', 'di', 'no', 'sa', 'sc');
197 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
198 array('de', 're', 'di', 'no', 'sa', 'sc');
199
200 return $knownLicenses;
201 }
202 }