summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ConfirmEdit/maintenance
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/ConfirmEdit/maintenance
first commit
Diffstat (limited to 'www/wiki/extensions/ConfirmEdit/maintenance')
-rw-r--r--www/wiki/extensions/ConfirmEdit/maintenance/CountFancyCaptchas.php55
-rw-r--r--www/wiki/extensions/ConfirmEdit/maintenance/DeleteOldFancyCaptchas.php92
-rw-r--r--www/wiki/extensions/ConfirmEdit/maintenance/GenerateFancyCaptchas.php263
3 files changed, 410 insertions, 0 deletions
diff --git a/www/wiki/extensions/ConfirmEdit/maintenance/CountFancyCaptchas.php b/www/wiki/extensions/ConfirmEdit/maintenance/CountFancyCaptchas.php
new file mode 100644
index 00000000..05be844b
--- /dev/null
+++ b/www/wiki/extensions/ConfirmEdit/maintenance/CountFancyCaptchas.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Counts the number of fancy captchas remaining.
+ *
+ * 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 2 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+if ( getenv( 'MW_INSTALL_PATH' ) ) {
+ $IP = getenv( 'MW_INSTALL_PATH' );
+} else {
+ $IP = __DIR__ . '/../../..';
+}
+
+require_once "$IP/maintenance/Maintenance.php";
+
+/**
+ * Maintenance script that counts the number of captchas remaining.
+ *
+ * @ingroup Maintenance
+ */
+class CountFancyCaptchas extends Maintenance {
+ public function __construct() {
+ parent::__construct();
+ $this->mDescription = "Counts the number of fancy aptchas in storage";
+ $this->requireExtension( "FancyCaptcha" );
+ }
+
+ public function execute() {
+ $instance = ConfirmEditHooks::getInstance();
+ if ( !( $instance instanceof FancyCaptcha ) ) {
+ $this->error( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
+ }
+
+ $countAct = $instance->getCaptchaCount();
+ $this->output( "Current number of captchas is $countAct.\n" );
+ }
+}
+
+$maintClass = "CountFancyCaptchas";
+require_once RUN_MAINTENANCE_IF_MAIN;
diff --git a/www/wiki/extensions/ConfirmEdit/maintenance/DeleteOldFancyCaptchas.php b/www/wiki/extensions/ConfirmEdit/maintenance/DeleteOldFancyCaptchas.php
new file mode 100644
index 00000000..23745a5c
--- /dev/null
+++ b/www/wiki/extensions/ConfirmEdit/maintenance/DeleteOldFancyCaptchas.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Deletes fancy captchas from storage
+ *
+ * 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 2 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+if ( getenv( 'MW_INSTALL_PATH' ) ) {
+ $IP = getenv( 'MW_INSTALL_PATH' );
+} else {
+ $IP = __DIR__ . '/../../..';
+}
+
+require_once "$IP/maintenance/Maintenance.php";
+
+/**
+ * Maintenance script that deletes old fancy captchas from storage
+ *
+ * @ingroup Maintenance
+ */
+class DeleteOldFancyCaptchas extends Maintenance {
+ public function __construct() {
+ parent::__construct();
+ $this->mDescription = "Deletes old fancy captchas from storage";
+ $this->addOption(
+ "date",
+ 'Delete fancy captchas that were created before this date (e.g. 20170101000000)',
+ true,
+ true
+ );
+ $this->requireExtension( "FancyCaptcha" );
+ }
+
+ public function execute() {
+ $instance = ConfirmEditHooks::getInstance();
+ if ( !( $instance instanceof FancyCaptcha ) ) {
+ $this->error( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
+ }
+
+ $countAct = $instance->getCaptchaCount();
+ $this->output( "Current number of captchas is $countAct.\n" );
+
+ $backend = $instance->getBackend();
+ $dir = $backend->getRootStoragePath() . '/captcha-render';
+
+ $filesToDelete = [];
+ $deleteDate = $this->getOption( 'date' );
+ foreach (
+ $backend->getFileList( [ 'dir' => $dir ] ) as $file
+ ) {
+ $fullPath = $dir . '/' . $file;
+ $timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
+ if ( $timestamp < $deleteDate ) {
+ $filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
+ }
+ }
+
+ $count = count( $filesToDelete );
+
+ if ( !$count ) {
+ $this->output( "No old fancy captchas to delete!\n" );
+ return;
+ }
+
+ $ret = $backend->doQuickOperations( $filesToDelete );
+
+ if ( $ret->isOK() ) {
+ $this->output( "$count old fancy captchas deleted.\n" );
+ } else {
+ $this->output( "Deleting old captchas errored.\n" );
+ $this->output( implode( "\n", $ret->getErrors() ) );
+ }
+ }
+}
+
+$maintClass = "DeleteOldFancyCaptchas";
+require_once RUN_MAINTENANCE_IF_MAIN;
diff --git a/www/wiki/extensions/ConfirmEdit/maintenance/GenerateFancyCaptchas.php b/www/wiki/extensions/ConfirmEdit/maintenance/GenerateFancyCaptchas.php
new file mode 100644
index 00000000..dfe09b76
--- /dev/null
+++ b/www/wiki/extensions/ConfirmEdit/maintenance/GenerateFancyCaptchas.php
@@ -0,0 +1,263 @@
+<?php
+/**
+ * Generate fancy captchas using a python script and copy them into storage.
+ *
+ * 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 2 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Aaron Schulz
+ * @ingroup Maintenance
+ */
+if ( getenv( 'MW_INSTALL_PATH' ) ) {
+ $IP = getenv( 'MW_INSTALL_PATH' );
+} else {
+ $IP = __DIR__ . '/../../..';
+}
+
+require_once "$IP/maintenance/Maintenance.php";
+
+/**
+ * Maintenance script to generate fancy captchas using a python script and copy them into storage.
+ *
+ * @ingroup Maintenance
+ */
+class GenerateFancyCaptchas extends Maintenance {
+ public function __construct() {
+ parent::__construct();
+
+ // See captcha.py for argument usage
+ $this->addOption( "wordlist", 'A list of words', true, true );
+ $this->addOption( "font", "The font to use", true, true );
+ $this->addOption( "font-size", "The font size ", false, true );
+ $this->addOption( "blacklist", "A blacklist of words that should not be used", false, true );
+ $this->addOption( "fill", "Fill the captcha container to N files", true, true );
+ $this->addOption( "verbose", "Show debugging information" );
+ $this->addOption(
+ "oldcaptcha",
+ "Whether to use captcha-old.py which doesn't have OCR fighting improvements"
+ );
+ $this->addOption( "delete", "Delete the old captches" );
+ $this->addOption( "threads", "The number of threads to use to generate the images",
+ false, true );
+ $this->mDescription = "Generate new fancy captchas and move them into storage";
+
+ $this->requireExtension( "FancyCaptcha" );
+ }
+
+ public function execute() {
+ global $wgCaptchaSecret, $wgCaptchaDirectoryLevels;
+
+ $totalTime = -microtime( true );
+
+ $instance = ConfirmEditHooks::getInstance();
+ if ( !( $instance instanceof FancyCaptcha ) ) {
+ $this->error( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
+ }
+ $backend = $instance->getBackend();
+
+ $deleteOldCaptchas = $this->getOption( 'delete' );
+
+ $countGen = (int)$this->getOption( 'fill' );
+ if ( !$deleteOldCaptchas ) {
+ $countAct = $instance->getCaptchaCount();
+ $this->output( "Current number of captchas is $countAct.\n" );
+ $countGen -= $countAct;
+ }
+
+ if ( $countGen <= 0 ) {
+ $this->output( "No need to generate anymore captchas.\n" );
+ return;
+ }
+
+ $tmpDir = wfTempDir() . '/mw-fancycaptcha-' . time() . '-' . wfRandomString( 6 );
+ if ( !wfMkdirParents( $tmpDir ) ) {
+ $this->error( "Could not create temp directory.\n", 1 );
+ }
+
+ $captchaScript = 'captcha.py';
+
+ if ( $this->hasOption( 'oldcaptcha' ) ) {
+ $captchaScript = 'captcha-old.py';
+ }
+
+ $cmd = sprintf( "python %s --key %s --output %s --count %s --dirs %s",
+ wfEscapeShellArg( dirname( __DIR__ ) . '/' . $captchaScript ),
+ wfEscapeShellArg( $wgCaptchaSecret ),
+ wfEscapeShellArg( $tmpDir ),
+ wfEscapeShellArg( $countGen ),
+ wfEscapeShellArg( $wgCaptchaDirectoryLevels )
+ );
+ foreach (
+ [ 'wordlist', 'font', 'font-size', 'blacklist', 'verbose', 'threads' ] as $par
+ ) {
+ if ( $this->hasOption( $par ) ) {
+ $cmd .= " --$par " . wfEscapeShellArg( $this->getOption( $par ) );
+ }
+ }
+
+ $this->output( "Generating $countGen new captchas.." );
+ $retVal = 1;
+ $captchaTime = -microtime( true );
+ wfShellExec( $cmd, $retVal, [], [ 'time' => 0 ] );
+ if ( $retVal != 0 ) {
+ wfRecursiveRemoveDir( $tmpDir );
+ $this->error( "Could not run generation script.\n", 1 );
+ }
+
+ $captchaTime += microtime( true );
+ $this->output( " Done.\n" );
+
+ $this->output(
+ sprintf(
+ "\nGenerated %d captchas in %.1f seconds\n",
+ $countGen,
+ $captchaTime
+ )
+ );
+
+ $filesToDelete = [];
+ if ( $deleteOldCaptchas ) {
+ $this->output( "Getting a list of old captchas to delete..." );
+ $path = $backend->getRootStoragePath() . '/captcha-render';
+ foreach ( $backend->getFileList( [ 'dir' => $path ] ) as $file ) {
+ $filesToDelete[] = [
+ 'op' => 'delete',
+ 'src' => $path . '/' . $file,
+ ];
+ }
+ $this->output( " Done.\n" );
+ }
+
+ $this->output( "Copying the new captchas to storage..." );
+
+ $storeTime = -microtime( true );
+ $iter = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator(
+ $tmpDir,
+ FilesystemIterator::SKIP_DOTS
+ ),
+ RecursiveIteratorIterator::LEAVES_ONLY
+ );
+
+ $captchasGenerated = iterator_count( $iter );
+ $filesToStore = [];
+ /**
+ * @var $fileInfo SplFileInfo
+ */
+ foreach ( $iter as $fileInfo ) {
+ if ( !$fileInfo->isFile() ) {
+ continue;
+ }
+ list( $salt, $hash ) = $instance->hashFromImageName( $fileInfo->getBasename() );
+ $dest = $instance->imagePath( $salt, $hash );
+ $backend->prepare( [ 'dir' => dirname( $dest ) ] );
+ $filesToStore[] = [
+ 'op' => 'store',
+ 'src' => $fileInfo->getPathname(),
+ 'dst' => $dest,
+ ];
+ }
+
+ $ret = $backend->doQuickOperations( $filesToStore );
+
+ $storeTime += microtime( true );
+
+ $storeSuceeded = true;
+ if ( $ret->isOK() ) {
+ $this->output( " Done.\n" );
+ $this->output(
+ sprintf(
+ "\nCopied %d captchas to storage in %.1f seconds\n",
+ $ret->successCount,
+ $storeTime
+ )
+ );
+ if ( !$ret->isGood() ) {
+ $this->output(
+ "Non fatal errors:\n" .
+ Status::wrap( $ret )->getWikiText( null, null, 'en' ) .
+ "\n"
+ );
+ }
+ if ( $ret->failCount ) {
+ $storeSuceeded = false;
+ $this->error( sprintf( "\nFailed to copy %d captchas\n", $ret->failCount ) );
+ }
+ if ( $ret->successCount + $ret->failCount !== $captchasGenerated ) {
+ $storeSuceeded = false;
+ $this->error(
+ sprintf( "Internal error: captchasGenerated: %d, successCount: %d, failCount: %d\n",
+ $captchasGenerated, $ret->successCount, $ret->failCount
+ )
+ );
+ }
+ } else {
+ $storeSuceeded = false;
+ $this->output( "Errored.\n" );
+ $this->error(
+ Status::wrap( $ret )->getWikiText( null, null, 'en' ) .
+ "\n"
+ );
+ }
+
+ if ( $storeSuceeded && $deleteOldCaptchas ) {
+ $numOriginalFiles = count( $filesToDelete );
+ $this->output( "Deleting {$numOriginalFiles} old captchas...\n" );
+ $deleteTime = -microtime( true );
+ $ret = $backend->doQuickOperations( $filesToDelete );
+
+ $deleteTime += microtime( true );
+ if ( $ret->isOK() ) {
+ $this->output( "Done.\n" );
+ $this->output(
+ sprintf(
+ "\nDeleted %d old captchas in %.1f seconds\n",
+ $numOriginalFiles,
+ $deleteTime
+ )
+ );
+ if ( !$ret->isGood() ) {
+ $this->output(
+ "Non fatal errors:\n" .
+ Status::wrap( $ret )->getWikiText( null, null, 'en' ) .
+ "\n"
+ );
+ }
+ } else {
+ $this->output( "Errored.\n" );
+ $this->error(
+ Status::wrap( $ret )->getWikiText( null, null, 'en' ) .
+ "\n"
+ );
+ }
+
+ }
+ $this->output( "Removing temporary files..." );
+ wfRecursiveRemoveDir( $tmpDir );
+ $this->output( " Done.\n" );
+
+ $totalTime += microtime( true );
+ $this->output(
+ sprintf(
+ "\nWhole captchas generation process took %.1f seconds\n",
+ $totalTime
+ )
+ );
+ }
+}
+
+$maintClass = "GenerateFancyCaptchas";
+require_once RUN_MAINTENANCE_IF_MAIN;