Sat, 17 Feb 2024 15:36:09 +0100
demon.php: ease some db pressure by not querying old entries (7 days).
- replaced some rejects workaround by exclusions
- some moar debug msgs
5
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
1 | #$Id$ |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
2 | # |
0
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
3 | <?PHP |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
4 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
5 | # Wikipedia bot components |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
6 | require_once ( __DIR__.'/vendor/mediawiki-api/vendor/autoload.php' ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
7 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
8 | # "the usual" tools routines, mostly database opening ones; these should probably be replaced by local ones! |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
9 | require_once ( __DIR__.'/public_html/php/common.php' ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
10 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
11 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
12 | class CommonsDelinquent { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
13 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
14 | var $debugging = false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
15 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
16 | function __construct () { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
17 | $this->config = parse_ini_file ( __DIR__.'/bot.cnf' ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
18 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
19 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
20 | // Runs a MySQL query. Optional debugging output, and output of query on error |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
21 | function runQuery ( $db , $sql ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
22 | if ( $this->debugging ) print "$sql\n" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
23 | if(!$result = $db->query($sql)) throw new Exception('There was an error running the query [' . $db->error . ']:'."\n$sql\n"); |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
24 | return $result ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
25 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
26 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
27 | function getCommonsDB () { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
28 | $this->last_db = openDB ( 'commons' , 'wikimedia' ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
29 | return $this->last_db ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
30 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
31 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
32 | function getToolDB () { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
33 | $this->last_db = openToolDB ( 'commonsdelinquent_p' ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
34 | return $this->last_db ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
35 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
36 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
37 | function getDBsafe ( $s ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
38 | if ( !isset ( $this->last_db ) ) die ( "getDBsafe called before database was opened!" ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
39 | return $this->last_db->real_escape_string ( $s ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
40 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
41 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
42 | function isBadWiki ( $wiki ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
43 | if ( $wiki == 'ukwikimedia' ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
44 | if ( preg_match ( '/^wikimania/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
45 | if ( preg_match ( '/strategy/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
46 | if ( preg_match ( '/foundation/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
47 | # if ( preg_match ( '/outreach/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
48 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
49 | if ( preg_match ( '/mxwikimedia/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
50 | if ( preg_match ( '/rswikimedia/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
51 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
52 | if ( preg_match ( '/tenwiki/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
53 | if ( preg_match ( '/stqwiki/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
54 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
55 | if ( preg_match ( '/enwikinews/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
56 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
57 | if ( preg_match ( '/testwikidatawiki/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
58 | # if ( preg_match ( '/^suwiki$/' , $wiki ) ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
59 | if ( preg_match ( '/usability/' , $wiki ) ) return true ; |
5
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
60 | |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
61 | # wikisource |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
62 | if ( preg_match ( '/sourceswiki/' , $wiki ) ) return true ; |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
63 | |
0
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
64 | # SUL LOGIN not working |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
65 | if ( $wiki == 'donatewiki' ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
66 | if ( $wiki == 'idwikimedia' ) return true ; |
5
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
67 | if ( $wiki == 'bdwikimedia' ) return true ; |
0
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
68 | if ( $wiki == 'maiwikimedia' ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
69 | if ( $wiki == 'amwikimedia' ) return true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
70 | if ( $wiki == 'gewikimedia' ) return true ; |
5
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
71 | if ( $wiki == 'azwikimedia' ) return true ; |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
72 | if ( $wiki == 'gurwiki' ) return true ; |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
73 | if ( $wiki == 'gomwiktionary' ) return true; |
0
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
74 | return false ; // Wiki is OK |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
75 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
76 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
77 | function hasLocalFile ( $wiki , $file ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
78 | $ret = false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
79 | # print "OPENING 1: $wiki\n" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
80 | $db = openDBwiki ( $wiki ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
81 | if ( $db === false ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
82 | print "FAILED TO OPEN $wiki - returning false\n" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
83 | return false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
84 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
85 | $this->last_db = $db ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
86 | /// hack by grin, 2021-03-01; getDBsafe may return empty! |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
87 | $sql_name = $this->getDBsafe(str_replace(' ','_',$file)); |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
88 | if( $sql_name == "" ) return false; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
89 | /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
90 | $sql = "SELECT * FROM image WHERE img_name='" . $sql_name . "' LIMIT 1" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
91 | try { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
92 | $result = $this->runQuery ( $this->last_db , $sql ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
93 | while($o = $result->fetch_object()) $ret = true ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
94 | } catch (Exception $e) { |
5
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
95 | echo 'Caught exception: ', $e->getMessage(), "\n"; |
0
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
96 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
97 | return $ret ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
98 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
99 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
100 | function setDone ( $id , $value , $meta = null) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
101 | $db = $this->getToolDB() ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
102 | $sql = "UPDATE event SET done=" . ($value*1) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
103 | if ( isset ( $meta ) ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
104 | if ( !is_array ( $meta ) ) $meta = array ( 'note' => $meta ) ; |
5
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
105 | # clip note to varchar(64) |
ff125aa259db
Clip note to the DB field size (varchar64). Added more exclusions:
Peter Gervai <grin@grin.hu>
parents:
2
diff
changeset
|
106 | if( isset($meta['note']) ) $meta['note'] = substr($meta['note'], 0, 63); |
0
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
107 | foreach ( $meta AS $k => $v ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
108 | $sql .= ",$k='" . $this->getDBsafe($v) . "'" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
109 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
110 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
111 | $sql .= " WHERE id=" . ($id*1) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
112 | if ( $value != 1 ) print "$sql\n" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
113 | $this->runQuery ( $db , $sql ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
114 | $db->close() ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
115 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
116 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
117 | function wiki2server ( $wiki ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
118 | if ( $wiki == 'wikidatawiki' ) return 'www.wikidata.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
119 | if ( $wiki == 'commonswiki' ) return 'commons.wikimedia.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
120 | if ( $wiki == 'mediawikiwiki' ) return 'www.mediawiki.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
121 | if ( $wiki == 'metawiki' ) return 'meta.wikimedia.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
122 | if ( $wiki == 'outreachwiki' ) return 'outreach.wikimedia.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
123 | if ( $wiki == 'incubatorwiki' ) return 'incubator.wikimedia.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
124 | if ( $wiki == 'sourceswiki' ) return 'wikisource.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
125 | if ( $wiki == 'specieswiki' ) return 'species.wikimedia.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
126 | /// fix by grin 2021-03-01: missing close re separator |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
127 | if ( preg_match ( '/(.+)wikimedia/' , $wiki , $m ) ) return $m[1] . ".wikimedia.org" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
128 | if ( preg_match ( '/^(wikimania\d+)wiki$/' , $wiki , $m ) ) return $m[1] . ".wikimedia.org" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
129 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
130 | if ( preg_match ( '/^(.+?)(wik.+)$/' , $wiki , $m ) ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
131 | $server = str_replace('_','-',$m[1]) . "." ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
132 | if ( $server == 'be-x-old.' ) $server = 'be-tarask.' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
133 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
134 | if ( $m[2] == 'wiki' ) $server .= 'wikipedia' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
135 | else $server .= $m[2] ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
136 | $server .= '.org' ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
137 | return $server ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
138 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
139 | return false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
140 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
141 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
142 | function getAPI ( $wiki ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
143 | # TODO check if re-opening same API, cache in object |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
144 | $server = $this->wiki2server ( $wiki ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
145 | if ( $server === false ) return false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
146 | $api = new \Mediawiki\Api\MediawikiApi( 'https://'.$server.'/w/api.php' ); |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
147 | if ( !$api->isLoggedin() ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
148 | print "Logging into https://$server/w/api.php\n"; # --grin 2022-02-05 |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
149 | $x = $api->login( new \Mediawiki\Api\ApiUser( $this->config['name'], $this->config['password'] ) ); |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
150 | if ( !$x ) return false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
151 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
152 | return $api ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
153 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
154 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
155 | function editWiki ( $wiki , $action , $params ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
156 | $api = $this->getAPI ( $wiki ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
157 | if ( $api === false ) return false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
158 | $params['token'] = $api->getToken() ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
159 | $params['bot'] = 1 ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
160 | $x = false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
161 | if ( $this->debugging ) print_r ( $params ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
162 | try { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
163 | $x = $api->postRequest( new \Mediawiki\Api\SimpleRequest( $action, $params ) ); |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
164 | } catch (Exception $e) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
165 | echo 'Caught exception: ', $e->getMessage(), "\n"; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
166 | $this->last_exception = $e->getMessage() ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
167 | $x = false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
168 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
169 | if ( $this->debugging ) print_r ( $x ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
170 | //$api->logout() ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
171 | $params['token'] = $api->getToken() ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
172 | $api->postRequest( new \Mediawiki\Api\SimpleRequest( "logout", $params ) ); |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
173 | return $x ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
174 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
175 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
176 | function editWikidata ( $action , $params ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
177 | $ret = $this->editWiki ( 'wikidatawiki' , $action , $params ) ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
178 | if ( $this->debugging ) { |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
179 | if ( is_array($ret) ) print "RET:" . $ret['success'] . "\n" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
180 | else print "RET: FALSE\n" ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
181 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
182 | if ( is_array($ret) ) return $ret['success'] ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
183 | return false ; |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
184 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
185 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
186 | } |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
187 | |
3b714bbb1347
Add files without passwords and other unwanted fluff.
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
188 | ?> |