Merge "Fixing documentation for memcached."
[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 Page $page
32 */
33 public function __construct( Page $page ) {
34 $this->mArticle = $page;
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(
44 wfAcceptToPrefs( $httpaccept ),
45 wfAcceptToPrefs( self::RDF_TYPE_PREFS )
46 );
47
48 if ( !$rdftype ) {
49 throw new HttpError( 406, wfMessage( 'notacceptable' ) );
50 }
51
52 $wgOut->disable();
53 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
54 $wgOut->sendCacheControl();
55 return true;
56 }
57
58 protected function reallyFullUrl() {
59 return $this->mArticle->getTitle()->getFullURL();
60 }
61
62 protected function basics() {
63 global $wgLanguageCode, $wgSitename;
64
65 $this->element( 'title', $this->mArticle->getTitle()->getText() );
66 $this->pageOrString( 'publisher', wfMessage( 'aboutpage' )->text(), $wgSitename );
67 $this->element( 'language', $wgLanguageCode );
68 $this->element( 'type', 'Text' );
69 $this->element( 'format', 'text/html' );
70 $this->element( 'identifier', $this->reallyFullUrl() );
71 $this->element( 'date', $this->date( $this->mArticle->getTimestamp() ) );
72
73 $lastEditor = User::newFromId( $this->mArticle->getUser() );
74 $this->person( 'creator', $lastEditor );
75
76 foreach ( $this->mArticle->getContributors() as $user ) {
77 $this->person( 'contributor', $user );
78 }
79
80 $this->rights();
81 }
82
83 protected function element( $name, $value ) {
84 $value = htmlspecialchars( $value );
85 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
86 }
87
88 protected function date( $timestamp ) {
89 return substr( $timestamp, 0, 4 ) . '-'
90 . substr( $timestamp, 4, 2 ) . '-'
91 . substr( $timestamp, 6, 2 );
92 }
93
94 protected function pageOrString( $name, $page, $str ) {
95 if ( $page instanceof Title ) {
96 $nt = $page;
97 } else {
98 $nt = Title::newFromText( $page );
99 }
100
101 if ( !$nt || $nt->getArticleID() == 0 ) {
102 $this->element( $name, $str );
103 } else {
104 $this->page( $name, $nt );
105 }
106 }
107
108 /**
109 * @param string $name
110 * @param Title $title
111 */
112 protected function page( $name, $title ) {
113 $this->url( $name, $title->getFullURL() );
114 }
115
116 protected function url( $name, $url ) {
117 $url = htmlspecialchars( $url );
118 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
119 }
120
121 protected function person( $name, User $user ) {
122 global $wgHiddenPrefs;
123
124 if ( $user->isAnon() ) {
125 $this->element( $name, wfMessage( 'anonymous' )->numParams( 1 )->text() );
126 } else {
127 $real = $user->getRealName();
128 if ( $real && !in_array( 'realname', $wgHiddenPrefs ) ) {
129 $this->element( $name, $real );
130 } else {
131 $userName = $user->getName();
132 $this->pageOrString(
133 $name,
134 $user->getUserPage(),
135 wfMessage( 'siteuser', $userName, $userName )->text()
136 );
137 }
138 }
139 }
140
141 /**
142 * Takes an arg, for future enhancement with different rights for
143 * different pages.
144 */
145 protected function rights() {
146 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
147
148 if ( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
149 && ( $nt->getArticleID() != 0 ) ) {
150 $this->page( 'rights', $nt );
151 } elseif ( $wgRightsUrl ) {
152 $this->url( 'rights', $wgRightsUrl );
153 } elseif ( $wgRightsText ) {
154 $this->element( 'rights', $wgRightsText );
155 }
156 }
157
158 protected function getTerms( $url ) {
159 global $wgLicenseTerms;
160
161 if ( $wgLicenseTerms ) {
162 return $wgLicenseTerms;
163 } else {
164 $known = $this->getKnownLicenses();
165 if ( isset( $known[$url] ) ) {
166 return $known[$url];
167 } else {
168 return array();
169 }
170 }
171 }
172
173 protected function getKnownLicenses() {
174 $ccLicenses = array( 'by', 'by-nd', 'by-nd-nc', 'by-nc',
175 'by-nc-sa', 'by-sa' );
176 $ccVersions = array( '1.0', '2.0' );
177 $knownLicenses = array();
178
179 foreach ( $ccVersions as $version ) {
180 foreach ( $ccLicenses as $license ) {
181 if ( $version == '2.0' && substr( $license, 0, 2 ) != 'by' ) {
182 # 2.0 dropped the non-attribs licenses
183 continue;
184 }
185 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
186 $knownLicenses[$lurl] = explode( '-', $license );
187 $knownLicenses[$lurl][] = 're';
188 $knownLicenses[$lurl][] = 'di';
189 $knownLicenses[$lurl][] = 'no';
190 if ( !in_array( 'nd', $knownLicenses[$lurl] ) ) {
191 $knownLicenses[$lurl][] = 'de';
192 }
193 }
194 }
195
196 /* Handle the GPL and LGPL, too. */
197
198 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
199 array( 'de', 're', 'di', 'no', 'sa', 'sc' );
200 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
201 array( 'de', 're', 'di', 'no', 'sa', 'sc' );
202 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
203 array( 'de', 're', 'di', 'no', 'sa', 'sc' );
204
205 return $knownLicenses;
206 }
207 }