diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8013469 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# database credentials and loader of suckless-php and boz-mw +/load.php + +# framework for queries +/suckless-php + +# framework for MediaWiki APIs +/boz-mw diff --git a/bot.php b/bot.php new file mode 100644 index 0000000..69e3b82 --- /dev/null +++ b/bot.php @@ -0,0 +1,89 @@ +createQuery( [ + 'action' => 'query', + 'generator' => 'categorymembers', + 'prop' => 'imageinfo', + 'iiprop' => [ 'url', 'mime' ], + '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; + } + + $image = ( new Query() ) + ->from( 'image' ) + ->whereInt( 'commons_pageid', $page->pageid ) + ->queryRow(); + + if( $image ) { + + // try to update existing image + + foreach( $CONNOTATION_IDs as $CONNOTATION_ID ) { + // try to relate if possible + try { + insert_row( 'image_connotation', [ + 'image_ID' => $image->image_ID, + 'connotation_ID' => $CONNOTATION_ID, + ] ); + echo "Updated\n"; + } catch( Exception $e ) { + echo "Already added\n"; + } + } + + } else { + + // create a new Image + + query( 'START TRANSACTION' ); + + insert_row( 'image', [ + 'image_src' => $imageinfo->url, + 'commons_pageid' => $page->pageid + ] ); + + $inserted = last_inserted_ID(); + + foreach( $CONNOTATION_IDs as $CONNOTATION_ID ) { + insert_row( 'image_connotation', [ + 'image_ID' => $inserted, + 'connotation_ID' => $CONNOTATION_ID, + ] ); + } + + query( 'COMMIT' ); + + echo "created $page->pageid"; + } + } + + +} diff --git a/load-example.php b/load-example.php new file mode 100644 index 0000000..2559dac --- /dev/null +++ b/load-example.php @@ -0,0 +1,6 @@ +