Skip to content

Commit

Permalink
Update IABot
Browse files Browse the repository at this point in the history
Reduces write queries to only 3, per page.
  • Loading branch information
cyberpower678 committed May 1, 2016
1 parent 0891318 commit 3186fe4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
71 changes: 58 additions & 13 deletions IABot/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,37 +151,82 @@ public function updateDBValues() {
$this->checkForUpdatedValues();

$query = "";
$updateQuery = "";
$deleteQuery = "";
$insertQuery = "";
if( !empty( $this->dbValues ) ) {
foreach( $this->dbValues as $id=>$values ) {
if( isset( $values['create'] ) ) {
unset( $values['create'] );
$values = $this->sanitizeValues( $values );
$query .= "INSERT INTO `externallinks_".WIKIPEDIA."` (`";
$query .= implode( "`, `", array_keys( $values ) );
$query .= "`, `pageid`) VALUES ('";
$query .= implode( "', '", $values );
$query .= "', '{$this->commObject->pageid}'); ";
if( empty( $insertQuery ) ) {
$insertQuery = "INSERT IGNORE INTO `externallinks_".WIKIPEDIA."`\n";
$tifields = array();
}
$tiValues[] = $values;
foreach( $values as $field=>$garbage ) {
if( !in_array( $field, $tifields ) ) $tifields[] = $field;
}
} elseif( isset( $values['update'] ) ) {
unset( $values['update'] );
$values = $this->sanitizeValues( $values );
$query .= "UPDATE `externallinks_".WIKIPEDIA."` SET ";
foreach( $values as $column=>$value ) {
if( $column == 'url' ) continue;
$first = false;
$query .= "`$column` = '$value'";
$query .= ", ";
if( empty( $updateQuery ) ) {
$updateQuery = "UPDATE `externallinks_".WIKIPEDIA."`\n";
$tufields = array();
}
$tuValues[] = $values;
foreach( $values as $field=>$garbage ) {
if( $field == 'url' ) continue;
if( !in_array( $field, $tufields ) ) $tufields[] = $field;
}
}
}
if( !empty( $insertQuery ) ) {
$insertQuery .= "\t(`".implode( "`, `", $tifields )."`, `pageid`)\nVALUES\n";
$comma = false;
foreach( $tuValues as $value ) {
if( $comma === true ) $insertQuery .= "),\n";
$insertQuery .= "\t(";
foreach( $tifields as $field ) {
$insertQuery .= "'{$value[$field]}', ";
}
$query .= "`pageid` = '{$this->commObject->pageid}' WHERE `url` = '{$values['url']}'; ";
$insertQuery .= "'{$this->commObject->pageid}'";
$comma = true;
}
$insertQuery .= ");\n";
$query .= $insertQuery;
}
if( !empty( $updateQuery ) ) {
$updateQuery .= "\tSET ";
$urls = array();
foreach( $tufields as $field ) {
$updateQuery .= "`$field` = CASE `url`\n";
foreach( $tuValues as $value ) {
if( isset( $value[$field] ) ) $updateQuery .= "\t\tWHEN '{$value['url']}' THEN '{$value[$field]}'\n";
else $updateQuery .= "\t\tWHEN '{$value['url']}' THEN NULL\n";
}
$updateQuery .= "\tEND,\n\t";
}
$updateQuery .= "`pageid` = CASE `url`\n";
foreach( $tuValues as $value ) {
$urls[] = $value['url'];
$updateQuery .= "\t\tWHEN '{$value['url']}' THEN '{$value['pageid']}'\n";
}
$updateQuery .= "\tEND\n";
$updateQuery .= "WHERE `url` IN ('".implode( "', '", $urls )."');\n";
$query .= $updateQuery;
}
}
if( !empty( $this->cachedPageResults ) ) {
$urls = array();
foreach( $this->cachedPageResults as $id=>$values ) {
$values = $this->sanitizeValues( $values );
if( !isset( $values['nodelete'] ) ) {
$query .= "DELETE FROM `externallinks_".WIKIPEDIA."` WHERE `url` = '{$values['url']}'; ";
$urls[] = $values['url'];
}
}
if( !empty( $urls ) ) $deleteQuery .= "DELETE FROM `externallinks_".WIKIPEDIA."` WHERE `url` IN ('".implode( "', '", $urls )."'); ";
$query .= $deleteQuery;
}
if( $query !== "" ) {
$res = mysqli_multi_query( $this->db, $query );
Expand Down
5 changes: 2 additions & 3 deletions IABot/checkIfDead.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public function checkDeadlinks( $urls, $full = false ) {
}

curl_setopt( $curl_instances[$id], CURLOPT_URL, $url );
curl_setopt( $curl_instances[$id], CURLOPT_HEADER, 1 );
curl_setopt( $curl_instances[$id], CURLOPT_NOBODY, 1 );
if( $full === true ) curl_setopt( $curl_instances[$id], CURLOPT_NOBODY, 0 );
curl_setopt( $curl_instances[$id], CURLOPT_HEADER, 1 );
if( $full !== true ) curl_setopt( $curl_instances[$id], CURLOPT_NOBODY, 1 );
curl_setopt( $curl_instances[$id], CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl_instances[$id], CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl_instances[$id], CURLOPT_TIMEOUT, 30 ); // Set 30 second timeout for the entire curl operation to take place
Expand Down

0 comments on commit 3186fe4

Please sign in to comment.