1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Tasks; |
10
|
|
|
|
11
|
|
|
use DOMDocument; |
12
|
|
|
use DOMElement; |
13
|
|
|
use Waca\API\ApiException; |
14
|
|
|
use Waca\API\IXmlApiAction; |
15
|
|
|
use Waca\WebRequest; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
abstract class XmlApiPageBase extends ApiPageBase implements IXmlApiAction |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* API result document |
22
|
|
|
* @var DOMDocument |
23
|
|
|
*/ |
24
|
|
|
protected $document; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->document = new DOMDocument('1.0'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Main function for this page, when no specific actions are called. |
33
|
|
|
* |
34
|
|
|
* @throws ApiException |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
final protected function main() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (headers_sent()) { |
40
|
|
|
throw new ApiException('Headers have already been sent - this indicates a bug in the application!'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
header("Content-Type: text/xml"); |
44
|
|
|
|
45
|
|
|
// javascript access control |
46
|
|
|
$httpOrigin = WebRequest::origin(); |
47
|
|
|
|
48
|
|
|
if ($httpOrigin !== null) { |
49
|
|
|
$CORSallowed = $this->getSiteConfiguration()->getCrossOriginResourceSharingHosts(); |
50
|
|
|
|
51
|
|
|
if (in_array($httpOrigin, $CORSallowed)) { |
52
|
|
|
header("Access-Control-Allow-Origin: " . $httpOrigin); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$responseData = $this->runApiPage(); |
57
|
|
|
|
58
|
|
|
ob_end_clean(); |
59
|
|
|
print($responseData); |
60
|
|
|
ob_start(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Method that runs API action |
65
|
|
|
* |
66
|
|
|
* @param DOMElement $apiDocument |
67
|
|
|
* |
68
|
|
|
* @return DOMElement |
69
|
|
|
*/ |
70
|
|
|
abstract public function executeApiAction(DOMElement $apiDocument); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
final public function runApiPage() |
76
|
|
|
{ |
77
|
|
|
$apiDocument = $this->document->createElement("api"); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$apiDocument = $this->executeApiAction($apiDocument); |
81
|
|
|
} |
82
|
|
|
catch (ApiException $ex) { |
83
|
|
|
$exception = $this->document->createElement("error"); |
84
|
|
|
$exception->setAttribute("message", $ex->getMessage()); |
85
|
|
|
$apiDocument->appendChild($exception); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->document->appendChild($apiDocument); |
89
|
|
|
|
90
|
|
|
return $this->document->saveXML(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.