Add parser tests for bug 52468 and bug 52363.
[mediawiki.git] / includes / Metadata.php
blob37df489c9d19039d152fd17fbe6a1491f0d9d04c
1 <?php
2 /**
3 * Base code to format metadata.
5 * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
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.
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.
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
22 * @author Evan Prodromou <evan@wikitravel.org>
23 * @file
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';
29 /**
30 * Constructor
31 * @param $article Article object
33 public function __construct( Page $article ) {
34 $this->mArticle = $article;
37 abstract public function show();
39 protected function setup() {
40 global $wgOut, $wgRequest;
42 $httpaccept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
43 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) );
45 if ( !$rdftype ) {
46 throw new HttpError( 406, wfMessage( 'notacceptable' ) );
49 $wgOut->disable();
50 $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
51 $wgOut->sendCacheControl();
52 return true;
55 protected function reallyFullUrl() {
56 return $this->mArticle->getTitle()->getFullURL();
59 protected function basics() {
60 global $wgLanguageCode, $wgSitename;
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() ) );
70 $lastEditor = User::newFromId( $this->mArticle->getUser() );
71 $this->person( 'creator', $lastEditor );
73 foreach ( $this->mArticle->getContributors() as $user ) {
74 $this->person( 'contributor', $user );
77 $this->rights();
80 protected function element( $name, $value ) {
81 $value = htmlspecialchars( $value );
82 print "\t\t<dc:{$name}>{$value}</dc:{$name}>\n";
85 protected function date( $timestamp ) {
86 return substr( $timestamp, 0, 4 ) . '-'
87 . substr( $timestamp, 4, 2 ) . '-'
88 . substr( $timestamp, 6, 2 );
91 protected function pageOrString( $name, $page, $str ) {
92 if ( $page instanceof Title ) {
93 $nt = $page;
94 } else {
95 $nt = Title::newFromText( $page );
98 if ( !$nt || $nt->getArticleID() == 0 ) {
99 $this->element( $name, $str );
100 } else {
101 $this->page( $name, $nt );
106 * @param $name string
107 * @param $title Title
109 protected function page( $name, $title ) {
110 $this->url( $name, $title->getFullURL() );
113 protected function url( $name, $url ) {
114 $url = htmlspecialchars( $url );
115 print "\t\t<dc:{$name} rdf:resource=\"{$url}\" />\n";
118 protected function person( $name, User $user ) {
119 global $wgHiddenPrefs;
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()
139 * Takes an arg, for future enhancement with different rights for
140 * different pages.
142 protected function rights() {
143 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
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 );
155 protected function getTerms( $url ) {
156 global $wgLicenseTerms;
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();
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();
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;
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';
193 /* Handle the GPL and LGPL, too. */
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' );
202 return $knownLicenses;