Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
XmlTransactionProcessor | |
0.00% |
0 / 58 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
setEndpointURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setTimeout | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setTransactionMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setEnvelope | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doTransaction | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
56 | |||
constructDomFromArray | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
doHttpTransaction | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * -- License -- |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | */ |
21 | |
22 | use MediaWiki\MediaWikiServices; |
23 | |
24 | /** |
25 | * Class that prepares an XML transaction and executes it. Right now it's probably way specific |
26 | * to the SilverPop method. Hopefully easy to extend if required? |
27 | */ |
28 | class XmlTransactionProcessor { |
29 | |
30 | /** @var array[] */ |
31 | private $mTransactionMap = []; |
32 | /** @var array */ |
33 | private $mEnvelope = []; |
34 | /** @var string|int */ |
35 | private $mTimeout = 'default'; |
36 | /** @var string */ |
37 | private $mURL = ''; |
38 | |
39 | /** |
40 | * Set the remote server we will be connecting too |
41 | * @param string $url |
42 | */ |
43 | public function setEndpointURL( $url ) { |
44 | $this->mURL = $url; |
45 | } |
46 | |
47 | /** |
48 | * Maximum cURL operation time |
49 | * @param int $timeout |
50 | */ |
51 | public function setTimeout( $timeout ) { |
52 | $this->mTimeout = $timeout; |
53 | } |
54 | |
55 | /** |
56 | * The transaction map is an array, keyed on transaction name. Each node should have two |
57 | * leaves: 'in' and 'out'. Out should be an array of 'key' which is the XML node name, and 'value' |
58 | * which is the name of the key in the data structure passed to doTransaction(). 'In' should |
59 | * be a function name that will process the return data. The 'in' function will be called in |
60 | * the context of a callbackObj - see doTransaction(). |
61 | * |
62 | * In functions should accept three arguments: |
63 | * - string : Transaction name |
64 | * - DOMDocument : The returned XML object |
65 | * - this : In case the function needs to modify this processor object |
66 | * |
67 | * @param array[] $map |
68 | */ |
69 | public function setTransactionMap( array $map ) { |
70 | $this->mTransactionMap = $map; |
71 | } |
72 | |
73 | /** |
74 | * Wrap the output XML in more nodes! Each entry in the envelope is a new wrapping element. |
75 | * @param array $envelope |
76 | */ |
77 | public function setEnvelope( array $envelope ) { |
78 | $this->mEnvelope = $envelope; |
79 | } |
80 | |
81 | /** |
82 | * Performs an XML transaction! |
83 | * |
84 | * @param string $txnName Name of transaction in transaction map |
85 | * @param mixed $callbackObj Reference to class that holds the callback function |
86 | * @param array $outParams Map of parameter data |
87 | * |
88 | * @return bool If true, the transaction was completely successful. |
89 | */ |
90 | public function doTransaction( $txnName, $callbackObj, $outParams = [] ) { |
91 | global $wgFundraisingEmailUnsubscribeLogXmlTransactions; |
92 | |
93 | Logger::pushLabel( 'XMLTransaction' ); |
94 | $retval = false; |
95 | |
96 | // Brief sanity check |
97 | if ( !array_key_exists( $txnName, $this->mTransactionMap ) ) { |
98 | Logger::log( "Transaction '$txnName' does not exist in map!", LOG_ERR, 'XMLTransaction' ); |
99 | |
100 | // And a further check to ensure that the processing callback function does exist |
101 | } elseif ( !method_exists( $callbackObj, $this->mTransactionMap[$txnName]['in'] ) ) { |
102 | $errstr = "Transaction '$txnName' specifies callback function that does not exist in provided object!"; |
103 | Logger::log( $errstr, LOG_ERR, 'XMLTransaction' ); |
104 | throw new MWException( $errstr ); |
105 | |
106 | // We're sane; process the data |
107 | } else { |
108 | // Construct the DOM tree |
109 | $dom = new DOMDocument( '1.0', 'utf-8' ); |
110 | $root = $dom; |
111 | |
112 | foreach ( $this->mEnvelope as $element ) { |
113 | $el = new DOMElement( $element ); |
114 | $root->appendChild( $el ); |
115 | $root = $el; |
116 | } |
117 | $el = new DOMElement( $txnName ); |
118 | $root->appendChild( $el ); |
119 | $root = $el; |
120 | |
121 | $this->constructDomFromArray( $this->mTransactionMap[$txnName]['out'], $outParams, $root ); |
122 | |
123 | // Send it! |
124 | Logger::log( "Sending transaction '$txnName'" ); |
125 | if ( $wgFundraisingEmailUnsubscribeLogXmlTransactions ) { |
126 | Logger::log( "Transaction content: " . $dom->saveXML() ); |
127 | } |
128 | $retData = $this->doHttpTransaction( $dom->saveXML(), $this->mTimeout ); |
129 | |
130 | // Call the processing hook if everything was OK |
131 | $retDOM = new DOMDocument(); |
132 | if ( ( $retData != false ) && ( $retDOM->loadXML( $retData ) ) ) { |
133 | $function = $this->mTransactionMap[$txnName]['in']; |
134 | $retval = $callbackObj->$function( $txnName, $retDOM, $this ); |
135 | } else { |
136 | Logger::log( "Server did not return useful content: " . json_encode( $retData ) ); |
137 | } |
138 | } |
139 | |
140 | Logger::popLabel(); |
141 | return $retval; |
142 | } |
143 | |
144 | /** |
145 | * Helper function which will create a DOM tree based on an array tree. Will lookup key data |
146 | * in the $data array. |
147 | * |
148 | * @param array $domArray The starting DOM array -- which is not a DOM object |
149 | * @param array $data The source parameter data |
150 | * @param DOMElement|DOMNode &$dom The resulting DOM object |
151 | * |
152 | * @throws MWException |
153 | */ |
154 | protected function constructDomFromArray( array $domArray, $data, &$dom ) { |
155 | foreach ( $domArray as $element => $value ) { |
156 | if ( is_array( $value ) ) { |
157 | $el = new DOMElement( $element ); |
158 | $this->constructDomFromArray( $value, $data, $el ); |
159 | } else { |
160 | if ( array_key_exists( $value, $data ) ) { |
161 | $el = new DOMElement( $element, (string)$data[$value] ); |
162 | } else { |
163 | throw new MWException( "Data key '$value' does not exist! Node '$element' cannot be created." ); |
164 | } |
165 | } |
166 | |
167 | $dom->appendChild( $el ); |
168 | } |
169 | } |
170 | |
171 | /** |
172 | * Actually perform the HTTP transaction. |
173 | * |
174 | * @param string|array $data |
175 | * @param string $timeout |
176 | * |
177 | * @return bool|string |
178 | */ |
179 | protected function doHttpTransaction( $data, $timeout = 'default' ) { |
180 | $options = []; |
181 | $options['method'] = 'POST'; |
182 | $options['timeout'] = $this->mTimeout; |
183 | |
184 | $req = MediaWikiServices::getInstance()->getHttpRequestFactory() |
185 | ->create( $this->mURL, $options ); |
186 | if ( !is_array( $data ) ) { |
187 | $req->setHeader( 'Content-Type', 'text/xml; charset=utf8' ); |
188 | $req->setHeader( 'Content-Length', (string)strlen( $data ) ); |
189 | } |
190 | $req->setData( $data ); |
191 | $status = $req->execute(); |
192 | |
193 | if ( $status->isOK() ) { |
194 | return $req->getContent(); |
195 | } else { |
196 | $errors = json_encode( [ 'curl' => $status->errors, 'request' => $status->getErrorsArray() ] ); |
197 | Logger::log( "Communications failed with : $errors", LOG_ERR, 'XMLTransaction' ); |
198 | return false; |
199 | } |
200 | } |
201 | } |