-
Notifications
You must be signed in to change notification settings - Fork 1
/
retrieveUsers.php
112 lines (67 loc) · 2.16 KB
/
retrieveUsers.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
<?php
require_once( __DIR__ . '/vendor/autoload.php' );
use \Mediawiki\Api as MwApi;
// Detect commandline args
$conffile = 'config.json';
if ( count( $argv ) > 1 ) {
$conffile = $argv[1];
}
// Detect if files
if ( ! file_exists( $conffile ) ) {
die( "Config file needed" );
}
$confjson = json_decode( file_get_contents( $conffile ), 1 );
$wikiconfig = null;
if ( array_key_exists( "wikipedia", $confjson ) ) {
$wikiconfig = $confjson["wikipedia"];
}
$wpapi = Mwapi\MediawikiApi::newFromApiEndpoint( $wikiconfig["url"] );
// Login
if ( array_key_exists( "user", $wikiconfig ) && array_key_exists( "password", $wikiconfig ) ) {
$wpapi->login( new ApiUser( $wikiconfig["user"], $wikiconfig["password"] ) );
}
// Get a page
if ( array_key_exists( "list", $confjson ) && array_key_exists( "regex", $confjson ) ) {
$params = array( "titles" => $confjson["list"], "prop" => "revisions", "rvlimit" => 1, "rvprop" => "content" );
if ( array_key_exists( "section", $confjson ) ) {
$params["rvsection"] = $confjson["section"];
$params["rvslots"] = "*";
}
$listPage = new Mwapi\SimpleRequest( 'query', $params );
$outcome = $wpapi->postRequest( $listPage );
$text = getWikiText( $outcome );
// TODO: This may be adapted to other pages
$users = getUsers( $text, $confjson["regex"] );
}
function getWikiText( $outcome ) {
$text = null;
if ( array_key_exists( "query", $outcome ) ) {
if ( array_key_exists( "pages", $outcome["query"] ) ) {
foreach ( $outcome["query"]["pages"] as $page ) {
if ( array_key_exists( "revisions", $page ) ) {
if ( count( $page["revisions"] ) > 0 ) {
$rev = $page["revisions"][0];
if ( array_key_exists( "*", $rev ) ) {
$text = $rev["*"];
} else {
if ( array_key_exists( "slots", $rev ) ) {
// Let's assume main
$text = $rev["slots"]["main"]["*"];
}
}
}
}
}
}
}
return $text;
}
function getUsers( $text, $regex ) {
$lines = explode( "\n", $text );
foreach ( $lines as $line ) {
preg_match( $regex, $line, $matches );
if ( count( $matches ) > 1 ) {
echo $matches[1]. "\n";
}
}
}