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