* (bug 29144) Move action=dublincore and action=creativecommons to extensions
[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 if( !$nt || $nt->getArticleID() == 0 ){
97 $this->element( $name, $str );
98 } else {
99 $this->page( $name, $nt );
100 }
101 }
102
103 protected function page( $name, $title ) {
104 $this->url( $name, $title->getFullUrl() );
105 }
106
107 protected function url($name, $url) {
108 $url = htmlspecialchars( $url );
109 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
110 }
111
112 protected function person( $name, User $user ) {
113 if( $user->isAnon() ){
114 $this->element( $name, wfMsgExt( 'anonymous', array( 'parsemag' ), 1 ) );
115 } else {
116 $real = $user->getRealName();
117 if( $real ) {
118 $this->element( $name, $real );
119 } else {
120 $userName = $user->getName();
121 $this->pageOrString( $name, $user->getUserPage(), wfMsgExt( 'siteuser', 'parsemag', $userName, $userName ) );
122 }
123 }
124 }
125
126 /**
127 * Takes an arg, for future enhancement with different rights for
128 * different pages.
129 */
130 protected function rights() {
131 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
132
133 if( $wgRightsPage && ( $nt = Title::newFromText( $wgRightsPage ) )
134 && ($nt->getArticleID() != 0)) {
135 $this->page('rights', $nt);
136 } else if( $wgRightsUrl ){
137 $this->url('rights', $wgRightsUrl);
138 } else if( $wgRightsText ){
139 $this->element( 'rights', $wgRightsText );
140 }
141 }
142
143 protected function getTerms( $url ){
144 global $wgLicenseTerms;
145
146 if( $wgLicenseTerms ){
147 return $wgLicenseTerms;
148 } else {
149 $known = $this->getKnownLicenses();
150 if( isset( $known[$url] ) ) {
151 return $known[$url];
152 } else {
153 return array();
154 }
155 }
156 }
157
158 protected function getKnownLicenses() {
159 $ccLicenses = array('by', 'by-nd', 'by-nd-nc', 'by-nc',
160 'by-nc-sa', 'by-sa');
161 $ccVersions = array('1.0', '2.0');
162 $knownLicenses = array();
163
164 foreach ($ccVersions as $version) {
165 foreach ($ccLicenses as $license) {
166 if( $version == '2.0' && substr( $license, 0, 2) != 'by' ) {
167 # 2.0 dropped the non-attribs licenses
168 continue;
169 }
170 $lurl = "http://creativecommons.org/licenses/{$license}/{$version}/";
171 $knownLicenses[$lurl] = explode('-', $license);
172 $knownLicenses[$lurl][] = 're';
173 $knownLicenses[$lurl][] = 'di';
174 $knownLicenses[$lurl][] = 'no';
175 if (!in_array('nd', $knownLicenses[$lurl])) {
176 $knownLicenses[$lurl][] = 'de';
177 }
178 }
179 }
180
181 /* Handle the GPL and LGPL, too. */
182
183 $knownLicenses['http://creativecommons.org/licenses/GPL/2.0/'] =
184 array('de', 're', 'di', 'no', 'sa', 'sc');
185 $knownLicenses['http://creativecommons.org/licenses/LGPL/2.1/'] =
186 array('de', 're', 'di', 'no', 'sa', 'sc');
187 $knownLicenses['http://www.gnu.org/copyleft/fdl.html'] =
188 array('de', 're', 'di', 'no', 'sa', 'sc');
189
190 return $knownLicenses;
191 }
192 }
193