Merge "Added new MWTimestamp::getRelativeTimestamp for pure relative."
[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 global $wgHiddenPrefs;
120
121 if ( $user->isAnon() ) {
122 $this->element( $name, wfMessage( 'anonymous' )->numParams( 1 )->text() );
123 } else {
124 $real = $user->getRealName();
125 if ( $real && !in_array( 'realname', $wgHiddenPrefs ) ) {
126 $this->element( $name, $real );
127 } else {
128 $userName = $user->getName();
129 $this->pageOrString(
130 $name,
131 $user->getUserPage(),
132 wfMessage( 'siteuser', $userName, $userName )->text()
133 );
134 }
135 }
136 }
137
138 /**
139 * Takes an arg, for future enhancement with different rights for
140 * different pages.
141 */
142 protected function rights() {
143 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
144
145 if ( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
146 && ( $nt->getArticleID() != 0 ) ) {
147 $this->page( 'rights', $nt );
148 } elseif ( $wgRightsUrl ) {
149 $this->url( 'rights', $wgRightsUrl );
150 } elseif ( $wgRightsText ) {
151 $this->element( 'rights', $wgRightsText );
152 }
153 }
154
155 protected function getTerms( $url ) {
156 global $wgLicenseTerms;
157
158 if ( $wgLicenseTerms ) {
159 return $wgLicenseTerms;
160 } else {
161 $known = $this->getKnownLicenses();
162 if ( isset( $known[$url] ) ) {
163 return $known[$url];
164 } else {
165 return array();
166 }
167 }
168 }
169
170 protected function getKnownLicenses() {
171 $ccLicenses = array( 'by', 'by-nd', 'by-nd-nc', 'by-nc',
172 'by-nc-sa', 'by-sa' );
173 $ccVersions = array( '1.0', '2.0' );
174 $knownLicenses = array();
175
176 foreach ( $ccVersions as $version ) {
177 foreach ( $ccLicenses as $license ) {
178 if ( $version == '2.0' && substr( $license, 0, 2 ) != 'by' ) {
179 # 2.0 dropped the non-attribs licenses
180 continue;
181 }
182 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
183 $knownLicenses[$lurl] = explode( '-', $license );
184 $knownLicenses[$lurl][] = 're';
185 $knownLicenses[$lurl][] = 'di';
186 $knownLicenses[$lurl][] = 'no';
187 if ( !in_array( 'nd', $knownLicenses[$lurl] ) ) {
188 $knownLicenses[$lurl][] = 'de';
189 }
190 }
191 }
192
193 /* Handle the GPL and LGPL, too. */
194
195 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
196 array( 'de', 're', 'di', 'no', 'sa', 'sc' );
197 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
198 array( 'de', 're', 'di', 'no', 'sa', 'sc' );
199 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
200 array( 'de', 're', 'di', 'no', 'sa', 'sc' );
201
202 return $knownLicenses;
203 }
204 }