-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
153 lines (122 loc) · 3.84 KB
/
bot.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
<?php
# WikiCAPTCHA Question Importer bot
# Copyright (C) 2019 Valerio Bozzolan and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'load.php';
// this script is suitable for Wikimedia commons
$wiki = \wm\Commons::instance();
// the Category name can be provided as first argument
$CATEGORY_NAME = $argv[1] ?? null;
// no Category name no party
while( empty( $CATEGORY_NAME ) ) {
$CATEGORY_NAME = readline( "Digit a Wikimedia Commons category page title (example: Category:Barack Obama): \n" );
}
// get all the existing connotations
$connotations = ( new Query() )
->from( 'connotation' )
->queryGenerator();
// ask the User to connect these photos to some connotations
$CONNOTATION_IDs = [];
foreach( $connotations as $connotation ) {
// cast some attributes
$connotation->integers( 'connotation_ID' );
// read Connotation attributes
$connotation_ID = $connotation->get( 'connotation_ID' );
$connotation_comment = $connotation->get( 'connotation_comment' );
// ask if this Connotation connotates the files in that Category
$line = null;
do {
if( $line !== null ) {
echo "What?\n";
}
$line = readline( "Are photos in '$CATEGORY_NAME' $connotation_comment? (y/n/stop) " );
} while( $line !== 'stop' && $line !== 'y' && $line !== 'n' );
// assign the statement
if( $line !== 'stop' ) {
$CONNOTATION_IDs[ $connotation_ID ] = $line === 'y' ? 'positive' : 'negative';
}
}
$queries =
$wiki->createQuery( [
'action' => 'query',
'generator' => 'categorymembers',
'prop' => 'imageinfo',
'iiprop' => [ 'url', 'mime' ],
'iiurlwidth' => 100,
'gcmtitle' => $CATEGORY_NAME,
] );
foreach( $queries as $query ) {
foreach( $query->query->pages as $page ) {
if( !isset( $page->imageinfo ) ) {
continue;
}
$imageinfo = $page->imageinfo[0] ?? null;
if( !$imageinfo ) {
continue;
}
$mime = $imageinfo->mime ?? null;
if( $mime !== 'image/jpeg' ) {
echo "bad mime $mime\n";
continue;
}
$thumburl = $imageinfo->thumburl;
if( !$thumburl ) {
$continue;
}
$image = ( new Query() )
->from( 'image' )
->whereInt( 'commons_pageid', $page->pageid )
->queryRow();
if( $image ) {
// try to update existing image
( new Query() )
->from( 'image' )
->whereInt( 'image_ID', $image->image_ID )
->update( [
'image_src' => $thumburl,
] );
foreach( $CONNOTATION_IDs as $CONNOTATION_ID => $positive ) {
// try to relate if possible
try {
insert_row( 'image_connotation', [
'image_ID' => $image->image_ID,
'connotation_ID' => $CONNOTATION_ID,
'image_connotation_positive' => $positive,
] );
echo "Updated\n";
} catch( Exception $e ) {
echo "Already added\n";
}
}
} elseif( $CONNOTATION_IDs ) {
// create a new Image
query( 'START TRANSACTION' );
insert_row( 'image', [
'image_src' => $thumburl,
'commons_pageid' => $page->pageid
] );
$inserted = last_inserted_ID();
foreach( $CONNOTATION_IDs as $CONNOTATION_ID => $positive ) {
insert_row( 'image_connotation', [
'image_ID' => $inserted,
'connotation_ID' => $CONNOTATION_ID,
'image_connotation_positive' => $positive,
] );
}
query( 'COMMIT' );
echo "created $page->pageid\n";
}
}
}