r103246 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103245‎ | r103246 | r103247 >
Date:22:23, 15 November 2011
Author:khorn
Status:ok (Comments)
Tags:
Comment:
Lots more work on the GlobalCollect command-line orphan rectifier.
Modified a couple things in the main GC adapter class where appropriate, and added a subclass (will have to be added to autoload in LocalSettings by itself: Didn't want this loading anywhere by default) just for dealing with orphans.
Added an empty directory where a master list of orphaned order ids, and a bunch of logfiles should be stashed before the script is run.
Modified paths:
  • /trunk/extensions/DonationInterface/gateway_common/gateway.adapter.php (modified) (history)
  • /trunk/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php (modified) (history)
  • /trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php (added) (history)
  • /trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphanlogs (added) (history)
  • /trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphans.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphans.php
@@ -1,4 +1,6 @@
22 <?php
 3+//If you want to use this script, you will have to add the following line to LocalSettings.php:
 4+//$wgAutoloadClasses['GlobalCollectOrphanAdapter'] = $IP . '/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php';
35
46 //TODO: Something that is not specific to anybody's install, here.
57 global $IP;
@@ -8,41 +10,163 @@
911 require_once( "$IP/maintenance/Maintenance.php" );
1012
1113 class GlobalCollectOrphanRectifier extends Maintenance {
 14+
 15+ protected $killfiles = array();
 16+ protected $order_ids = array();
 17+ protected $max_per_execute = 5;
 18+
 19+
1220 function execute(){
1321
14 - $max_per_execute = 50;
 22+ $order_ids = file('orphanlogs/order_ids.txt', FILE_SKIP_EMPTY_LINES);
 23+ foreach ($order_ids as $key=>$val){
 24+ $order_ids[$key] = trim($val);
 25+ }
 26+ foreach ($order_ids as $id){
 27+ $this->order_ids[$id] = $id; //easier to unset this way.
 28+ }
 29+ echo "Order ID count: " . count($this->order_ids) . "\n";
1530
 31+ $files = $this->getAllLogFileNames();
 32+ foreach ($files as $file){
 33+ $file_array = file($file, FILE_SKIP_EMPTY_LINES);
 34+ $payments = $this->findTransactionLines($file_array);
 35+ if (count($payments) === 0){
 36+ $this->killfiles[] = $file;
 37+ }
 38+ }
1639
 40+ $data = array(
 41+ 'wheeee' => 'yes'
 42+// 'order_id' => '1052864192',
 43+// 'i_order_id' => '1052864192',
 44+// 'city' => '',
 45+// 'state' => '',
 46+// 'zip' => '',
 47+// 'country' => 'US',
 48+// 'email' => '',
 49+// 'card_num' => '',
 50+
 51+ );
 52+
 53+ $class_name = 'GlobalCollectOrphanAdapter';
 54+
 55+ $adapter = new $class_name(array('external_data' => $data));
 56+ $adapter->setCurrentTransaction('INSERT_ORDERWITHPAYMENT');
 57+ $var_map = $adapter->defineVarMap();
 58+
 59+ $xml = new DomDocument;
 60+
 61+ foreach ($payments as $key => $payment_data){
 62+ $xml->loadXML($payment_data['xml']);
 63+ $parsed = $adapter->getResponseData($xml);
 64+ $payments[$key]['parsed'] = $parsed;
 65+ $payments[$key]['unstaged'] = $adapter->unstage_data($parsed);
 66+ $payments[$key]['unstaged']['contribution_tracking_id'] = $payments['contribution_tracking_id'];
 67+ $payments[$key]['unstaged']['i_order_id'] = $payments[$key]['unstaged']['order_id'];
 68+ }
 69+ //setCurrentTransaction
 70+ //then load the XML into a DomDocument, and run getResponseData.
 71+
 72+ echo print_r($payments, true);
1773
18 - //load in and chunk the list of XML. Phase 1: From a file
19 - //MUST include contribution_tracking id!
 74+ //careful after this bit.
 75+ die();
2076
21 - //for each chunk, load it into a GC adapter, and have it parse as if it were a response.
2277
23 - //load that response into DonationData, and run the thing that completes the transaction,
24 - //as if we were coming from resultSwitcher.
25 - //(Note: This should only work if we're sitting at one of the designated statuses)
2678
27 - $data = array(
28 - 'order_id' => '1052864192',
29 - 'i_order_id' => '1052864192',
30 - 'city' => '',
31 - 'state' => '',
32 - 'zip' => '',
33 - 'country' => 'US',
34 - 'email' => '',
35 - 'card_num' => '',
36 -
37 - );
38 -
3979 //we may need to unset some hooks out here. Like... recaptcha. Makes no sense.
4080 $adapter = new GlobalCollectAdapter(array('external_data' => $data));
4181 error_log("\n\n\n");
4282 $results = $adapter->do_transaction('Confirm_CreditCard');
 83+
 84+
 85+ //$this->rewriteOrderIds();
 86+ //and don't forget to kill the logs we don't care about anymore.
4387 }
 88+
 89+ function getAllLogFileNames(){
 90+ $files = array();
 91+ if ($handle = opendir(dirname(__FILE__) . '/orphanlogs/')){
 92+ while ( ($file = readdir($handle)) !== false ){
 93+ if (trim($file, '.') != '' && $file != 'order_ids.txt'){
 94+ $files[] = dirname(__FILE__) . '/orphanlogs/' . $file;
 95+ }
 96+ }
 97+ }
 98+ closedir($handle);
 99+ return $files;
 100+ }
 101+
 102+ function findTransactionLines($file){
 103+ $lines = array();
 104+ $orders = array();
 105+ $contrib_id_finder = array();
 106+ foreach ($file as $line_no=>$line_data){
 107+ if (strpos($line_data, '<XML><REQUEST><ACTION>INSERT_ORDERWITHPAYMENT') === 0){
 108+ $lines[$line_no] = $line_data;
 109+ } elseif (strpos($line_data, 'Raw XML Response')){
 110+ $contrib_id_finder[] = $line_data;
 111+ } elseif (strpos(trim($line_data), '<ORDERID>') === 0){
 112+ $contrib_id_finder[] = trim($line_data);
 113+ }
 114+ }
 115+
 116+ foreach ($lines as $line_no=>$line_data){
 117+ if (count($orders) >= $this->max_per_execute){
 118+ continue;
 119+ }
 120+ $pos1 = strpos($line_data, '<ORDERID>') + 9;
 121+ $pos2 = strpos($line_data, '</ORDERID>');
 122+ if ($pos2 > $pos1){
 123+ $tmp = substr($line_data, $pos1, $pos2-$pos1);
 124+ echo "$tmp\n";
 125+ if (isset($this->order_ids[$tmp])){
 126+ $orders[$tmp] = trim($line_data);
 127+ unset($this->order_ids[$tmp]);
 128+ }
 129+ }
 130+ }
 131+
 132+ //reverse the array, so we find the last instance first.
 133+ $contrib_id_finder = array_reverse($contrib_id_finder);
 134+ foreach ($orders as $order_id => $xml){
 135+ $contribution_tracking_id = '';
 136+ $finder = array_search("<ORDERID>$order_id</ORDERID>", $contrib_id_finder);
 137+
 138+ //now search forward (which is actually backward) to the "Raw XML" line, so we can get the contribution_tracking_id
 139+ //TODO: Some kind of (in)sanity check for this. Just because we've found it one step backward doesn't mean...
 140+ //...but it's kind of good. For now.
 141+ $explode_me = false;
 142+ while (!$explode_me){
 143+ ++$finder;
 144+ if (strpos($contrib_id_finder[$finder], "Raw XML Response")){
 145+ $explode_me = $contrib_id_finder[$finder];
 146+ }
 147+ }
 148+ if (strlen($explode_me)){
 149+ $explode_me = explode(': ', $explode_me);
 150+ $contribution_tracking_id = trim($explode_me[1]);
 151+ $orders[$order_id] = array(
 152+ 'xml' => $xml,
 153+ 'contribution_tracking_id' => $contribution_tracking_id,
 154+ );
 155+ }
 156+ }
 157+
 158+ return $orders;
 159+ }
 160+
 161+ function rewriteOrderIds() {
 162+ $file = fopen('orphanlogs/order_ids.txt', 'w');
 163+ $outstanding_orders = implode("\n", $this->order_ids);
 164+ fwrite($file, $outstanding_orders);
 165+ fclose($file);
 166+ }
 167+
44168 }
45169
 170+$maintClass = "GlobalCollectOrphanRectifier";
 171+require_once( "$IP/maintenance/doMaintenance.php" );
46172
47173
48 -$maintClass = "GlobalCollectOrphanRectifier";
49 -require_once( "$IP/maintenance/doMaintenance.php" );
Property changes on: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphanlogs
___________________________________________________________________
Added: svn:ignore
50174 + order_ids.txt
2011-11-11:17:59:48-globalcollect
Index: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php
@@ -0,0 +1,28 @@
 2+<?php
 3+
 4+class GlobalCollectOrphanAdapter extends GlobalCollectAdapter {
 5+ public function unstage_data( $data = array(), $final = true ){
 6+ $unstaged = array();
 7+ foreach ( $data as $key=>$val ){
 8+ if (is_array($val)){
 9+ $unstaged += $this->unstage_data( $val, false );
 10+ } else {
 11+ if (array_key_exists($key, $this->var_map)){
 12+ //run the unstage data functions.
 13+ $unstaged[$this->var_map[$key]] = $val;
 14+ //this would be EXTREMELY bad to put in the regular adapter.
 15+ $this->staged_data[$this->var_map[$key]] = $val;
 16+ } else {
 17+ //$unstaged[$key] = $val;
 18+ }
 19+ }
 20+ }
 21+ if ($final){
 22+ $this->stageData('response');
 23+ }
 24+ foreach ($unstaged as $key=>$val){
 25+ $unstaged[$key] = $this->staged_data[$key];
 26+ }
 27+ return $unstaged;
 28+ }
 29+}
\ No newline at end of file
Property changes on: trunk/extensions/DonationInterface/globalcollect_gateway/scripts/orphan_adapter.php
___________________________________________________________________
Added: svn:eol-style
130 + native
Index: trunk/extensions/DonationInterface/globalcollect_gateway/globalcollect.adapter.php
@@ -1049,6 +1049,7 @@
10501050
10511051 if ( count( $addme ) ){
10521052 $this->addData( $addme );
 1053+ $this->staged_data['order_id'] = $this->staged_data['i_order_id'];
10531054 $logmsg = $this->getData_Raw( 'contribution_tracking_id' ) . ': ';
10541055 $logmsg .= 'CVV Result: ' . $this->getData_Raw( 'cvv_result' );
10551056 $logmsg .= ', AVS Result: ' . $this->getData_Raw( 'avs_result' );
@@ -1596,23 +1597,34 @@
15971598 protected function stage_language( $type = 'request' ) {
15981599 $language = strtolower( $this->staged_data['language'] );
15991600
1600 - $count = 0;
1601 - //Count's just there making sure we don't get stuck here.
1602 - while ( !in_array( $language, $this->getAvailableLanguages() ) && $count < 3 ){
1603 - // Get the fallback language
1604 - $language = Language::getFallbackFor( $language );
1605 - $count += 1;
 1601+ switch ( $type ) {
 1602+ case 'request':
 1603+ $count = 0;
 1604+ //Count's just there making sure we don't get stuck here.
 1605+ while ( !in_array( $language, $this->getAvailableLanguages() ) && $count < 3 ){
 1606+ // Get the fallback language
 1607+ $language = Language::getFallbackFor( $language );
 1608+ $count += 1;
 1609+ }
 1610+
 1611+ if ( !in_array( $language, $this->getAvailableLanguages() ) ){
 1612+ $language = 'en';
 1613+ }
 1614+
 1615+ if ( $language === 'zh' ) { //Handles GC's mutant Chinese code.
 1616+ $language = 'sc';
 1617+ }
 1618+
 1619+ break;
 1620+ case 'response':
 1621+ if ( $language === 'sc' ){
 1622+ $language = 'zh';
 1623+ }
 1624+ break;
16061625 }
16071626
1608 - if ( !in_array( $language, $this->getAvailableLanguages() ) ){
1609 - $language = 'en';
1610 - }
1611 -
1612 - if ( $language === 'zh' ) { //Handles GC's mutant Chinese code.
1613 - $language = 'sc';
1614 - }
1615 -
16161627 $this->staged_data['language'] = $language;
 1628+
16171629 }
16181630
16191631 /**
Index: trunk/extensions/DonationInterface/gateway_common/gateway.adapter.php
@@ -1530,7 +1530,9 @@
15311531 }
15321532
15331533 // Format the staged data
1534 - $this->formatStagedData();
 1534+ if ($type === 'request'){
 1535+ $this->formatStagedData();
 1536+ }
15351537
15361538 }
15371539
@@ -1667,7 +1669,7 @@
16681670 * @return mixed Transaction results status, or false if not set.
16691671 */
16701672 public function getTransactionStatus() {
1671 - if ( array_key_exists( 'status', $this->transaction_results ) ) {
 1673+ if ( is_array( $this->transaction_results ) && array_key_exists( 'status', $this->transaction_results ) ) {
16721674 return $this->transaction_results['status'];
16731675 } else {
16741676 return false;

Follow-up revisions

RevisionCommit summaryAuthorDate
r103512MFT r103506, r103501, r103499, r103491, r103416, r103385, r103288, r103246, r...khorn21:54, 17 November 2011
r103513MFT r103506, r103501, r103499, r103491, r103416, r103385, r103288, r103246, r...khorn21:57, 17 November 2011
r103847MFT r102338, r102681, r102685, r102810, r102828, r102829, r102832, r102836, r...khorn22:30, 21 November 2011
r103848MFT r102338, r102681, r102685, r102810, r102828, r102829, r102832, r102836, r...khorn22:31, 21 November 2011

Comments

#Comment by Kaldari (talk | contribs)   20:05, 17 November 2011
+			$pos1 = strpos($line_data, '<ORDERID>') + 9;
+			$pos2 = strpos($line_data, '</ORDERID>');
+			if ($pos2 > $pos1){
...

Might be good to explain what you're doing here.

Also, using continues makes code harder to read IMO, might want to just stick the statements below it within the if statement and reverse the operator:
if (count($orders) < $this->max_per_execute){

Status & tagging log