This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.php
207 lines (191 loc) · 5.31 KB
/
class.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
<?php
/**
* Main class
*
* @package wmf-tool-tour
*/
class TourTool extends KrToolBaseClass {
protected static $labels = array(
// Pages visible to the public. Editing disabled.
// Part of Wikimedia unified login.
'closed',
// Authorised access only. Editing and reading restricted.
// Not part of Wikimedia unified login.
'private',
// Pages visible to the public. Editing restricted.
// Not part of Wikimedia unified login.
'fishbowl',
);
protected function show() {
global $kgBase;
$kgBase->setHeadTitle( 'Home' );
$kgBase->setLayout( 'header', array(
'titleText' => 'Wiki Tour',
'captionHtml' => 'Manage a tour that passes by all Wikimedia wikis.',
) );
$kgBase->addOut( '<div class="container">' );
$kgBase->addOut( kfAlertHtml( 'info', '<strong>Welcome!</strong> Hello there.' ) );
// TODO: Fetch filter from wiki page config
$filter = array( 'private' => false );
$wikis = $this->getWikis( $filter );
$table = '<table class="table table-striped table-hover" x-data-toggle="table"><thead><tr>'
. '<th>Wiki</th>'
. '<th data-sortable=1>Identifier</th>'
. '<th data-sortable=1>Project</th>';
$labels = array();
foreach ( self::$labels as $label ) {
if ( !( isset( $filter[$label] ) && $filter[$label] === false ) ) {
$labels[] = $label;
$table .= Html::element( 'th', array( 'data-sortable' => true ), ucfirst( $label ) );
}
}
$table .= '<th data-sortable=1>Status</th>';
$table .= '</tr></thead><tbody>';
$stati = array( false, 'done', 'progress' );
foreach ( $wikis as $dbname => $wiki ) {
// TODO: Fetch status from wiki page
$status = $stati[ array_rand( $stati ) ];
$table .= '<tr>'
. Html::rawElement( 'td', array(), Html::element( 'a', array(
'href' => $wiki['url'],
), $wiki['servername'] ) )
. Html::element( 'td', array(), $dbname )
. Html::element( 'td', array(), $wiki['project'] );
foreach ( $labels as $label ) {
$table .= Html::element( 'td', array(), $wiki[$label] ? ucfirst( $label ) : '' );
}
$table .= Html::rawElement( 'td', array(
'class' => $this->getStatusClass( $status ),
),
$this->getStatusField( $status )
. ' '
. htmlspecialchars( $this->getStatusLabel( $status ) )
);
$table .= '</tr>';
}
$table .= '</tbody></table>';
$kgBase->addOut($table);
// Close container
$kgBase->addOut( '</div>' );
}
protected function getStatusField( $status ) {
if ( $status === 'done' ) {
return '<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>';
}
if ( $status === 'progress' ) {
return '<span class="glyphicon glyphicon-retweet" aria-hidden="true"></span>';
}
return '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>';
}
protected function getStatusLabel( $status ) {
if ( $status === 'done' ) {
return 'Done';
}
if ( $status === 'progress' ) {
return 'In-progress';
}
return 'Needs volunteer';
}
protected function getStatusClass( $status ) {
if ( $status === 'done' ) {
return 'success';
}
if ( $status === 'progress' ) {
return 'warning';
}
return 'info';
}
/**
* @return stdClass|bool
*/
protected function fetchSiteMatrix() {
$data = kfApiRequest( 'https://meta.wikimedia.org', array(
'action' => 'sitematrix',
'smlangprop' => 'site',
'smsiteprop' => 'url|dbname|code',
) );
// Sample:
/*
"286": {
"code": "zu",
"name": "isiZulu",
"site": [
{
"url": "http://zu.wikipedia.org",
"dbname": "zuwiki",
"sitename": "Wikipedia"
},
{
"url": "http://zu.wiktionary.org",
..
}
]
},
"specials": [
{
"url": "http://advisory.wikimedia.org",
},
*/
return $data ? $data->sitematrix : false;
}
/**
* @return stdClass
* @throws Exception If fetch failed
*/
protected function getSiteMatrix() {
global $kgCache;
$key = kfCacheKey( 'sitematrix', 2 );
$value = $kgCache->get( $key );
if ( $value === false ) {
$value = $this->fetchSiteMatrix();
if ( $value !== false ) {
$kgCache->set( $key, $value, 3600 );
} else {
$value = null;
// Don't try again for another minute
$kgCache->set( $key, $value, 60 );
}
}
if ( $value === null ) {
throw new Exception( 'Fetch sitematrix failed' );
}
return $value;
}
/**
* @param array $filter Optional boolean filters for 'closed', 'private'
* and/or 'fishbowl'. E.g. "array( 'private' => false )" to omit closed wikis.
* @return array
*/
protected function getWikis( array $filter = array() ) {
$siteMatrix = $this->getSiteMatrix();
$wikis = array();
foreach ( $siteMatrix as $groupKey => $group ) {
if ( $groupKey === 'count' ) {
continue;
}
if ( $groupKey === 'specials' ) {
$sites = $group;
} else {
// Language group
$sites = $group->site;
}
foreach ( $sites as $site ) {
$wiki = array(
'url' => $site->url,
'servername' => preg_replace( '#^https?://#', '', $site->url ),
'project' => $groupKey === 'specials'
? $groupKey
: ( $site->code === 'wiki' ? 'wikipedia' : $site->code ),
);
foreach ( self::$labels as $label ) {
$wiki[ $label ] = isset( $site->$label );
if ( isset( $filter[$label] ) && $filter[$label] !== $wiki[ $label ] ) {
continue 2;
}
}
$wikis[ $site->dbname ] = $wiki;
}
}
return $wikis;
}
}