/
Scripto.php
397 lines (352 loc) · 14 KB
/
Scripto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* @package Scripto
* @copyright © 2010-2011, Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* Require the base Scripto exception.
*/
require_once 'Scripto/Exception.php';
/**
* Represents a Scripto application. Serves as a connector object between the
* external system API and MediaWiki API.
*
* @package Scripto
*/
class Scripto
{
/**
* @var Scripto_Adapter_Interface The adapter object for the external
* system.
*/
protected $_adapter;
/**
* @var Scripto_Service_MediaWiki The MediaWiki service object.
*/
protected $_mediawiki;
/**
* @var array
*/
protected $_userInfo;
/**
* Construct the Scripto object.
*
* @param Scripto_Adapter_Interface $adapter The adapter object.
* @param array|Scripto_Service_MediaWiki $mediawiki If an array:
* $mediawiki['api_url']: required; the MediaWiki API URL
* $mediawiki['db_name']: required; the MediaWiki database name
* $mediawiki['pass_cookies']: optional pass cookies to the web browser
* via API client
*/
public function __construct(Scripto_Adapter_Interface $adapter, $mediawiki)
{
// Set the adapter.
$this->_adapter = $adapter;
// Set the MediaWiki service.
if ($mediawiki instanceof Scripto_Service_MediaWiki) {
$this->_mediawiki = $mediawiki;
} else if (is_array($mediawiki)
&& array_key_exists('api_url', $mediawiki)
&& array_key_exists('db_name', $mediawiki)) {
if (!isset($mediawiki['pass_cookies'])) {
$mediawiki['pass_cookies'] = true;
}
require_once 'Scripto/Service/MediaWiki.php';
$this->_mediawiki = new Scripto_Service_MediaWiki($mediawiki['api_url'],
$mediawiki['db_name'],
(bool) $mediawiki['pass_cookies']);
} else {
throw new Scripto_Exception('The provided mediawiki parameter is invalid.');
}
// Set the user information.
$this->setUserInfo();
}
/**
* Check whether the specified document exists in the external system.
*
* @param string|int $id The unique document identifier.
* @return bool
*/
public function documentExists($id)
{
// Query the adapter whether the document exists.
if ($this->_adapter->documentExists($id)) {
return true;
}
return false;
}
/**
* Get a Scripto_Document object.
*
* @param string|int $id The unique document identifier.
* @return Scripto_Document
*/
public function getDocument($id)
{
require_once 'Scripto/Document.php';
return new Scripto_Document($id, $this->_adapter, $this->_mediawiki);
}
/**
* Login via the MediaWiki service.
*
* It is possible to restrict account creation in MediaWiki.
* @link http://www.mediawiki.org/wiki/Manual:Preventing_access#Restrict_account_creation
*
* @param string $username
* @param string $password
*/
public function login($username, $password)
{
$this->_mediawiki->login($username, $password);
$this->setUserInfo();
}
/**
* Logout via the MediaWiki service.
*/
public function logout()
{
$this->_mediawiki->logout();
$this->setUserInfo();
}
/**
* Determine if the current user is logged in.
*
* @return bool
*/
public function isLoggedIn()
{
// Check against the user ID. An anonymous user has an ID of 0.
return (bool) $this->_userInfo['query']['userinfo']['id'];
}
/**
* Determine if the current user can export transcriptions to the external
* system.
*
* @param array $groups The MediaWiki groups allowed to export.
* @return bool
*/
public function canExport(array $groups = array('sysop', 'bureaucrat'))
{
foreach ($groups as $group) {
if (in_array($group, $this->_userInfo['query']['userinfo']['groups'])) {
return true;
}
}
return false;
}
/**
* Determine if the current user can protect the MediaWiki page.
*
* @return bool
*/
public function canProtect()
{
// Users with protect rights can protect pages.
if (in_array('protect', $this->_userInfo['query']['userinfo']['rights'])) {
return true;
}
return false;
}
/**
* Set the current user's information.
*
* Under normal circumstances calling this method directly is unnecessary,
* but is helpful when authenticating after construction and when a login is
* not called, like when hijacking cookies for command line authentication.
*/
public function setUserInfo()
{
$this->_userInfo = $this->_mediawiki->getUserInfo('groups|rights');
}
/**
* Return the name of the current user.
*
* @return string
*/
public function getUserName()
{
return $this->_userInfo['query']['userinfo']['name'];
}
/**
* Get the current user's most recently contributed document pages.
*
* @param int $limit The number of document pages to return.
* @return array
*/
public function getUserDocumentPages($limit = 10)
{
require_once 'Scripto/Document.php';
$limit = (int) $limit;
$userDocumentPages = array();
$documentTitles = array();
$start = null;
do {
$response = $this->_mediawiki->getUserContributions(
$this->_userInfo['query']['userinfo']['name'],
array('ucstart' => $start,
'uclimit' => 100)
);
foreach ($response['query']['usercontribs'] as $value) {
// Filter out duplicate pages.
if (array_key_exists($value['pageid'], $userDocumentPages)) {
continue;
}
// Filter out contributions that are not document pages.
if (Scripto_Document::BASE_TITLE_PREFIX != $value['title'][0]) {
continue;
}
// Set the document ID and page ID.
$document = Scripto_Document::decodeBaseTitle($value['title']);
// Set the document title. Reduce calls to the adapter by
// caching each title and checking if it already exists.
if (array_key_exists($document[0], $documentTitles)) {
$documentTitle = $documentTitles[$document[0]];
} else {
$documentTitle = $this->_adapter->getDocumentTitle($document[0]);
$documentTitles[$document[0]] = $documentTitle;
}
// Build the user document pages, newest properties first.
$userDocumentPages[$value['pageid']] = array(
'revision_id' => $value['revid'],
'mediawiki_title' => $value['title'],
'timestamp' => $value['timestamp'],
'comment' => $value['comment'],
'size' => $value['size'],
'document_id' => $document[0],
'document_page_id' => $document[1],
'document_title' => $documentTitle
);
// Break out of the loops if limit has been reached.
if ($limit == count($userDocumentPages)) {
break 2;
}
}
// Set the query continue, if any.
if (isset($response['query-continue'])) {
$start = $response['query-continue']['usercontribs']['ucstart'];
} else {
$start = null;
}
} while ($start);
return $userDocumentPages;
}
/**
* Get the recent changes.
*
* @link http://www.mediawiki.org/wiki/Manual:Namespace#Built-in_namespaces
* @param int $limit The number of recent changes to return.
* @return array
*/
public function getRecentChanges($limit = 10)
{
require_once 'Scripto/Document.php';
$start = null;
$recentChanges = array();
$documentTitles = array();
// Namespaces to get: ns_index => ns_name
// See http://www.mediawiki.org/wiki/Manual:Namespace#Built-in_namespaces
$namespaces = array('0' => 'Main', '1' => 'Talk');
do {
$response = $this->_mediawiki->getRecentChanges(
array('rcprop' => 'user|comment|timestamp|title|ids|sizes|loginfo|flags',
'rclimit' => '100',
'rcnamespace' => implode('|', array_keys($namespaces)),
'rcstart' => $start)
);
foreach ($response['query']['recentchanges'] as $value) {
// Extract the title, removing the namespace if any.
$title = preg_replace('/^(.+:)?(.+)$/', '$2', $value['title']);
// Filter out contributions that are not document pages.
if (Scripto_Document::BASE_TITLE_PREFIX != $title[0]) {
continue;
}
// Set the document ID and page ID.
$documentIds = Scripto_Document::decodeBaseTitle($title);
// Set the document title. Reduce calls to the adapter by
// caching each title and checking if it already exists.
if (array_key_exists($documentIds[0], $documentTitles)) {
$documentTitle = $documentTitles[$documentIds[0]];
} else {
$documentTitle = $this->_adapter->getDocumentTitle($documentIds[0]);
$documentTitles[$documentIds[0]] = $documentTitle;
}
$recentChanges[] = array(
'type' => $value['type'],
'namespace_index' => $value['ns'],
'namespace_name' => $namespaces[$value['ns']],
'mediawiki_title' => $value['title'],
'rcid' => $value['rcid'],
'page_id' => $value['pageid'],
'revision_id' => $value['revid'],
'old_revision_id' => $value['old_revid'],
'user' => $value['user'],
'old_length' => $value['oldlen'],
'new_length' => $value['newlen'],
'timestamp' => $value['timestamp'],
'comment' => $value['comment'],
'log_id' => isset($value['logid']) ? $value['logid']: null,
'log_type' => isset($value['logtype']) ? $value['logtype']: null,
'log_action' => isset($value['logaction']) ? $value['logaction']: null,
'new' => isset($value['new']) ? true: false,
'minor' => isset($value['minor']) ? true: false,
'document_id' => $documentIds[0],
'document_page_id' => $documentIds[1],
'document_title' => $documentTitle,
);
// Break out of the loops if limit has been reached.
if ($limit == count($recentChanges)) {
break 2;
}
}
// Set the query continue, if any.
if (isset($response['query-continue'])) {
$start = $response['query-continue']['recentchanges']['rcstart'];
} else {
$start = null;
}
} while ($start);
return $recentChanges;
}
/**
* Get the difference between two page revisions.
*
* @param int $from The revision ID from which to diff.
* @param int|string $to The revision to which to diff. Use the revision ID,
* "prev", "next", or "cur".
* @return string
*/
public function getRevisionDiff($fromRevisionId, $toRevisionId = 'prev')
{
return $this->_mediawiki->getRevisionDiff($fromRevisionId, $toRevisionId);
}
/**
* Get properties of the specified page revision.
*
* @param int $revisionId
* @return array
*/
public function getRevision($revisionId)
{
// Get the revision properties.
$response = $this->_mediawiki->getRevisions(
null,
array('revids' => $revisionId,
'rvprop' => 'ids|flags|timestamp|user|comment|size|content')
);
$page = current($response['query']['pages']);
// Parse the wikitext into HTML.
$response = $this->_mediawiki->parse(
array('text' => '__NOEDITSECTION__' . $page['revisions'][0]['*'])
);
$revision = array('revision_id' => $page['revisions'][0]['revid'],
'parent_id' => $page['revisions'][0]['parentid'],
'user' => $page['revisions'][0]['user'],
'timestamp' => $page['revisions'][0]['timestamp'],
'comment' => $page['revisions'][0]['comment'],
'size' => $page['revisions'][0]['size'],
'wikitext' => $page['revisions'][0]['*'],
'html' => $response['parse']['text']['*']);
return $revision;
}
}