1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File holding the Extensions\Lingo\LingoBackend class |
5
|
|
|
* |
6
|
|
|
* This file is part of the MediaWiki extension Lingo. |
7
|
|
|
* |
8
|
|
|
* @copyright 2011 - 2016, Stephan Gambke |
9
|
|
|
* @license GNU General Public License, version 2 (or any later version) |
10
|
|
|
* |
11
|
|
|
* The Lingo extension is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
13
|
|
|
* Software Foundation; either version 2 of the License, or (at your option) any |
14
|
|
|
* later version. |
15
|
|
|
* |
16
|
|
|
* The Lingo extension is distributed in the hope that it will be useful, but |
17
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
19
|
|
|
* details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License along |
22
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
* @author Stephan Gambke |
25
|
|
|
* @file |
26
|
|
|
* @ingroup Lingo |
27
|
|
|
*/ |
28
|
|
|
namespace Extensions\Lingo; |
29
|
|
|
|
30
|
|
|
use ApprovedRevs; |
31
|
|
|
use Page; |
32
|
|
|
use Parser; |
33
|
|
|
use ParserOptions; |
34
|
|
|
use Revision; |
35
|
|
|
use Title; |
36
|
|
|
use User; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Extensions\Lingo\LingoBasicBackend class. |
40
|
|
|
* |
41
|
|
|
* @ingroup Lingo |
42
|
|
|
*/ |
43
|
|
|
class LingoBasicBackend extends LingoBackend { |
44
|
|
|
|
45
|
|
|
protected $mArticleLines = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Extensions\Lingo\LingoBasicBackend constructor. |
49
|
|
|
* @param LingoMessageLog|null $messages |
50
|
|
|
*/ |
51
|
1 |
|
public function __construct( LingoMessageLog &$messages = null ) { |
52
|
|
|
|
53
|
1 |
|
global $wgexLingoPage, $wgRequest; |
|
|
|
|
54
|
|
|
|
55
|
1 |
|
$page = $wgexLingoPage ? $wgexLingoPage : wfMessage( 'lingo-terminologypagename' )->inContentLanguage()->text(); |
56
|
|
|
|
57
|
1 |
|
parent::__construct( $messages ); |
58
|
|
|
|
59
|
|
|
// Get Terminology page |
60
|
1 |
|
$title = Title::newFromText( $page ); |
61
|
1 |
|
if ( $title->getInterwiki() ) { |
62
|
|
|
$this->getMessageLog()->addError( wfMessage( 'lingo-terminologypagenotlocal', $page )->inContentLanguage()->text() ); |
63
|
|
|
return false; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// FIXME: This is a hack special-casing the submitting of the terminology |
|
|
|
|
67
|
|
|
// page itself. In this case the Revision is not up to date when we get |
68
|
|
|
// here, i.e. $rev->getText() would return outdated Test. |
69
|
|
|
// This hack takes the text directly out of the data from the web request. |
70
|
1 |
|
if ( $wgRequest->getVal( 'action', 'view' ) === 'submit' |
71
|
1 |
|
&& Title::newFromText( $wgRequest->getVal( 'title' ) )->getArticleID() === $title->getArticleID() |
72
|
1 |
|
) { |
73
|
|
|
|
74
|
|
|
$content = $wgRequest->getVal( 'wpTextbox1' ); |
75
|
|
|
|
76
|
|
|
} else { |
77
|
1 |
|
$rev = $this->getRevision( $title ); |
78
|
1 |
|
if ( !$rev ) { |
79
|
1 |
|
$this->getMessageLog()->addWarning( wfMessage( 'lingo-noterminologypage', $page )->inContentLanguage()->text() ); |
80
|
1 |
|
return false; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$content = $rev->getText(); |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$parser = new Parser; |
88
|
|
|
// expand templates and variables in the text, producing valid, static wikitext |
89
|
|
|
// have to use a new anonymous user to avoid any leakage as Lingo is caching only one user-independant glossary |
90
|
|
|
$content = $parser->preprocess( $content, $title, new ParserOptions( new User() ) ); |
91
|
|
|
|
92
|
|
|
$this->mArticleLines = array_reverse( explode( "\n", $content ) ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* This function returns the next element. The element is an array of four |
97
|
|
|
* strings: Term, Definition, Link, Source. For the Extensions\Lingo\LingoBasicBackend Link |
98
|
|
|
* and Source are set to null. If there is no next element the function |
99
|
|
|
* returns null. |
100
|
|
|
* |
101
|
|
|
* @return Array | null |
102
|
|
|
*/ |
103
|
|
|
public function next() { |
104
|
|
|
|
105
|
|
|
wfProfileIn( __METHOD__ ); |
106
|
|
|
|
107
|
|
|
static $term = null; |
108
|
|
|
static $definitions = array(); |
109
|
|
|
static $ret = array(); |
110
|
|
|
|
111
|
|
|
// find next valid line (yes, the assignation is intended) |
112
|
|
|
while ( ( count( $ret ) == 0 ) && ( $entry = each( $this->mArticleLines ) ) ) { |
113
|
|
|
|
114
|
|
|
if ( empty( $entry[ 1 ] ) || ( $entry[ 1 ][ 0 ] !== ';' && $entry[ 1 ][ 0 ] !== ':' ) ) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$chunks = explode( ':', $entry[ 1 ], 2 ); |
119
|
|
|
|
120
|
|
|
// found a new definition? |
121
|
|
|
if ( count( $chunks ) == 2 ) { |
122
|
|
|
|
123
|
|
|
// wipe the data if its a totaly new term definition |
124
|
|
|
if ( !empty( $term ) && count( $definitions ) > 0 ) { |
125
|
|
|
$definitions = array(); |
126
|
|
|
$term = null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$definitions[] = trim( $chunks[ 1 ] ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// found a new term? |
133
|
|
|
if ( count( $chunks ) >= 1 && strlen( $chunks[ 0 ] ) >= 1 ) { |
134
|
|
|
$term = trim( substr( $chunks[ 0 ], 1 ) ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ( $term !== null ) { |
138
|
|
|
foreach ( $definitions as $definition ) { |
139
|
|
|
$ret[] = array( |
140
|
|
|
LingoElement::ELEMENT_TERM => $term, |
141
|
|
|
LingoElement::ELEMENT_DEFINITION => $definition, |
142
|
|
|
LingoElement::ELEMENT_LINK => null, |
143
|
|
|
LingoElement::ELEMENT_SOURCE => null |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
wfProfileOut( __METHOD__ ); |
150
|
|
|
|
151
|
|
|
return array_pop( $ret ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns revision of the terms page. |
156
|
|
|
* |
157
|
|
|
* @param Title $title |
158
|
|
|
* @return Revision |
159
|
|
|
*/ |
160
|
|
|
public function getRevision( $title ) { |
161
|
|
|
global $wgexLingoEnableApprovedRevs; |
|
|
|
|
162
|
|
|
|
163
|
|
|
if ( $wgexLingoEnableApprovedRevs ) { |
164
|
|
|
|
165
|
|
|
if ( defined( 'APPROVED_REVS_VERSION' ) ) { |
166
|
|
|
$rev_id = ApprovedRevs::getApprovedRevID( $title ); |
167
|
|
|
return Revision::newFromId( $rev_id ); |
168
|
|
|
} else { |
169
|
|
|
wfDebug( 'Support for ApprovedRevs is enabled in Lingo. But ApprovedRevs was not found.\n' ); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return Revision::newFromTitle( $title ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Initiates the purging of the cache when the Terminology page was saved or purged. |
178
|
|
|
* |
179
|
|
|
* @param Page $wikipage |
180
|
|
|
* @return Bool |
181
|
|
|
*/ |
182
|
|
|
public static function purgeCache( &$wikipage ) { |
183
|
|
|
|
184
|
|
|
global $wgexLingoPage; |
|
|
|
|
185
|
|
|
$page = $wgexLingoPage ? $wgexLingoPage : wfMessage( 'lingo-terminologypagename' )->inContentLanguage()->text(); |
186
|
|
|
|
187
|
|
|
if ( !is_null( $wikipage ) && ( $wikipage->getTitle()->getText() === $page ) ) { |
188
|
|
|
|
189
|
|
|
LingoParser::purgeCache(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* The basic backend is cache-enabled so this function returns true. |
197
|
|
|
* |
198
|
|
|
* Actual caching is done by the parser, the backend just calls |
199
|
|
|
* Extensions\Lingo\LingoParser::purgeCache when necessary. |
200
|
|
|
* |
201
|
|
|
* @return boolean |
202
|
|
|
*/ |
203
|
|
|
public function useCache() { |
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state