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