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