r98504 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98503‎ | r98504 | r98505 >
Date:11:25, 30 September 2011
Author:reedy
Status:deferred
Tags:
Comment:
Rename file
Modified paths:
  • /trunk/extensions/MetricsReporting/fetchGoogleSpreadsheet.php (deleted) (history)
  • /trunk/extensions/MetricsReporting/fetchOfflineGoogleSpreadsheet.php (added) (history)

Diff [purge]

Index: trunk/extensions/MetricsReporting/fetchGoogleSpreadsheet.php
@@ -1,174 +0,0 @@
2 -<?php
3 -
4 -require( "MetricsMaintenance.php" );
5 -
6 -class FetchGoogleSpreadsheet extends MetricsMaintenance {
7 - public function __construct() {
8 - parent::__construct();
9 - $this->mDescription = "Grabs and does stuff with a Google Documents Spreadsheet";
10 - $this->addArg( 'spreadsheet', 'URL or something to document' );
11 - }
12 -
13 - public function execute() {
14 - $url = $this->getArg( 0 );
15 -
16 - // Headers
17 - $http = MWHttpRequest::factory( 'https://www.google.com/accounts/ClientLogin',
18 - array(
19 - 'method' => 'POST',
20 - 'postData' => array(
21 - 'accountType' => 'HOSTED_OR_GOOGLE',
22 - 'service' => 'wise', // Spreadsheet service is "wise"
23 - 'Email' => '',
24 - 'Passwd' => '',
25 - 'source' => self::getUserAgent(),
26 - )
27 - )
28 - );
29 - $http->setHeader( 'User-Agent', self::getUserAgent() );
30 -
31 - $res = $http->execute();
32 - if ( $http->getStatus() == 403 ) {
33 - $this->error( '403', true );
34 - }
35 -
36 - $content = $http->getContent();
37 -
38 - $authToken = null;
39 - $pos = strpos( $content, 'Auth' );
40 - if ( $pos !== false ) {
41 - $authToken = rtrim( substr( $content, $pos + strlen( "Auth=" ) ) );
42 - }
43 -
44 - if ( $authToken === null ) {
45 - $this->error( 'No auth token returned. Check your Google Credentials', true );
46 - }
47 - $this->output( "Authorised. Got an authorisation token from Google\n" );
48 -
49 - $cookies = $http->getCookieJar();
50 - $http = $this->buildAuthedRequest( $url, $authToken, $cookies );
51 -
52 - $http->execute();
53 - $content = $http->getContent();
54 -
55 - $reader = new XMLReader();
56 - $reader->XML( $content );
57 -
58 - while ( $reader->read() && $reader->name !== 'entry' );
59 -
60 - $worksheets = array();
61 - while ( $reader->name === 'entry' ) {
62 - $node = new SimpleXMLElement( $reader->readOuterXML() );
63 -
64 - // Worksheet based feed
65 - // $src = (string)$node->link[2]['href'];
66 - //$src = $node->link[2]->attributes()->href;
67 -
68 - // List based feed
69 - $src = (string)$node->content["src"];
70 - // $src = $node->content->attributes()->src;
71 -
72 - // Cell based feed
73 - // $src = (string)$node->link["href"];
74 - // $src = $node->link->attributes()->href;
75 -
76 - //$this->output( 'Worksheet found: ' . $src . "\n" );
77 - $worksheets[] = $src;
78 -
79 - // go to next <entry />
80 - $reader->next( 'entry' );
81 - }
82 -
83 - $reader->close();
84 - $this->output( "\n" );
85 -
86 - foreach( $worksheets as $sheet ) {
87 - $http = $this->buildAuthedRequest( $sheet, $authToken, $cookies );
88 - $http->execute();
89 - $content = $http->getContent();
90 -
91 - $this->output( 'Worksheet: ' . $sheet . "\n" );
92 - $xml = new SimpleXMLElement( $content );
93 -
94 - $this->output( 'Spreadsheet tab title: ' . $xml->title . "\n" );
95 - $this->output( "\n" );
96 -
97 - if ( $xml->title != 'Deployments' ) {
98 - continue;
99 - }
100 - //continue;
101 - $sheetData = array();
102 - // foreach "tab"/worksheet
103 - foreach( $xml->entry as $entry ) {
104 - $namespaces = $entry->getNameSpaces( true );
105 - $gsx = $entry->children( $namespaces['gsx'] );
106 - foreach( get_object_vars( $gsx ) as $key => $value ) {
107 - if ( !isset( $sheetData[$key] ) ) {
108 - $sheetData[$key] = array();
109 - }
110 - $sheetData[$key][] = $value;
111 - //$this->output( "{$key}: {$value}\n" );
112 - }
113 - }
114 - $this->output( "\n" );
115 - $this->getDeploymentFigures( $sheetData );
116 - // var_dump( $sheetData );
117 - $this->output( "\n" );
118 - }
119 -
120 - $this->output( "Finished!\n" );
121 - }
122 -
123 - /**
124 - * @param $data array
125 - */
126 - function getDeploymentFigures( $data ) {
127 - $db = $this->getDb();
128 -
129 - $db->insert( 'offline', $data, __METHOD__, array( 'IGNORE' ) );
130 - }
131 -
132 - /**
133 - * Pretty print xml string
134 - *
135 - * @param $xml string
136 - * @return string
137 - */
138 - function formatXmlString( $xml ) {
139 - $dom = new DOMDocument;
140 - $dom->preserveWhiteSpace = false;
141 - $dom->loadXML( $xml );
142 - $dom->formatOutput = true;
143 - return $dom->saveXml();
144 - }
145 -
146 - /**
147 - * @param $url string
148 - * @param $token string
149 - * @param $cookies CookieJar
150 - * @return MWHttpRequest
151 - */
152 - function buildAuthedRequest( $url, $token, $cookies = null ) {
153 - $http = MWHttpRequest::factory( $url, array(
154 - 'method' => 'GET',
155 - )
156 - );
157 - $http->setHeader( 'User-Agent', self::getUserAgent() );
158 - if ( $cookies !== null ) {
159 - $http->setCookieJar( $cookies );
160 - }
161 - $http->setHeader( 'GData-Version', '3.0' );
162 - $http->setHeader( 'Authorization', "GoogleLogin auth=\"{$token}\"" );
163 - return $http;
164 - }
165 -
166 - /**
167 - * @return string
168 - */
169 - private static function getUserAgent() {
170 - return Http::userAgent() . ' MetricsReporting/' . METRICS_REPORTING_VERSION;
171 - }
172 -}
173 -
174 -$maintClass = "FetchGoogleSpreadsheet";
175 -require_once( DO_MAINTENANCE );
Index: trunk/extensions/MetricsReporting/fetchOfflineGoogleSpreadsheet.php
@@ -0,0 +1,174 @@
 2+<?php
 3+
 4+require( "MetricsMaintenance.php" );
 5+
 6+class FetchGoogleSpreadsheet extends MetricsMaintenance {
 7+ public function __construct() {
 8+ parent::__construct();
 9+ $this->mDescription = "Grabs and does stuff with a Google Documents Spreadsheet";
 10+ $this->addArg( 'spreadsheet', 'URL or something to document' );
 11+ }
 12+
 13+ public function execute() {
 14+ $url = $this->getArg( 0 );
 15+
 16+ // Headers
 17+ $http = MWHttpRequest::factory( 'https://www.google.com/accounts/ClientLogin',
 18+ array(
 19+ 'method' => 'POST',
 20+ 'postData' => array(
 21+ 'accountType' => 'HOSTED_OR_GOOGLE',
 22+ 'service' => 'wise', // Spreadsheet service is "wise"
 23+ 'Email' => '',
 24+ 'Passwd' => '',
 25+ 'source' => self::getUserAgent(),
 26+ )
 27+ )
 28+ );
 29+ $http->setHeader( 'User-Agent', self::getUserAgent() );
 30+
 31+ $res = $http->execute();
 32+ if ( $http->getStatus() == 403 ) {
 33+ $this->error( '403', true );
 34+ }
 35+
 36+ $content = $http->getContent();
 37+
 38+ $authToken = null;
 39+ $pos = strpos( $content, 'Auth' );
 40+ if ( $pos !== false ) {
 41+ $authToken = rtrim( substr( $content, $pos + strlen( "Auth=" ) ) );
 42+ }
 43+
 44+ if ( $authToken === null ) {
 45+ $this->error( 'No auth token returned. Check your Google Credentials', true );
 46+ }
 47+ $this->output( "Authorised. Got an authorisation token from Google\n" );
 48+
 49+ $cookies = $http->getCookieJar();
 50+ $http = $this->buildAuthedRequest( $url, $authToken, $cookies );
 51+
 52+ $http->execute();
 53+ $content = $http->getContent();
 54+
 55+ $reader = new XMLReader();
 56+ $reader->XML( $content );
 57+
 58+ while ( $reader->read() && $reader->name !== 'entry' );
 59+
 60+ $worksheets = array();
 61+ while ( $reader->name === 'entry' ) {
 62+ $node = new SimpleXMLElement( $reader->readOuterXML() );
 63+
 64+ // Worksheet based feed
 65+ // $src = (string)$node->link[2]['href'];
 66+ //$src = $node->link[2]->attributes()->href;
 67+
 68+ // List based feed
 69+ $src = (string)$node->content["src"];
 70+ // $src = $node->content->attributes()->src;
 71+
 72+ // Cell based feed
 73+ // $src = (string)$node->link["href"];
 74+ // $src = $node->link->attributes()->href;
 75+
 76+ //$this->output( 'Worksheet found: ' . $src . "\n" );
 77+ $worksheets[] = $src;
 78+
 79+ // go to next <entry />
 80+ $reader->next( 'entry' );
 81+ }
 82+
 83+ $reader->close();
 84+ $this->output( "\n" );
 85+
 86+ foreach( $worksheets as $sheet ) {
 87+ $http = $this->buildAuthedRequest( $sheet, $authToken, $cookies );
 88+ $http->execute();
 89+ $content = $http->getContent();
 90+
 91+ $this->output( 'Worksheet: ' . $sheet . "\n" );
 92+ $xml = new SimpleXMLElement( $content );
 93+
 94+ $this->output( 'Spreadsheet tab title: ' . $xml->title . "\n" );
 95+ $this->output( "\n" );
 96+
 97+ if ( $xml->title != 'Deployments' ) {
 98+ continue;
 99+ }
 100+ //continue;
 101+ $sheetData = array();
 102+ // foreach "tab"/worksheet
 103+ foreach( $xml->entry as $entry ) {
 104+ $namespaces = $entry->getNameSpaces( true );
 105+ $gsx = $entry->children( $namespaces['gsx'] );
 106+ foreach( get_object_vars( $gsx ) as $key => $value ) {
 107+ if ( !isset( $sheetData[$key] ) ) {
 108+ $sheetData[$key] = array();
 109+ }
 110+ $sheetData[$key][] = $value;
 111+ //$this->output( "{$key}: {$value}\n" );
 112+ }
 113+ }
 114+ $this->output( "\n" );
 115+ $this->getDeploymentFigures( $sheetData );
 116+ // var_dump( $sheetData );
 117+ $this->output( "\n" );
 118+ }
 119+
 120+ $this->output( "Finished!\n" );
 121+ }
 122+
 123+ /**
 124+ * @param $data array
 125+ */
 126+ function getDeploymentFigures( $data ) {
 127+ $db = $this->getDb();
 128+
 129+ $db->insert( 'offline', $data, __METHOD__, array( 'IGNORE' ) );
 130+ }
 131+
 132+ /**
 133+ * Pretty print xml string
 134+ *
 135+ * @param $xml string
 136+ * @return string
 137+ */
 138+ function formatXmlString( $xml ) {
 139+ $dom = new DOMDocument;
 140+ $dom->preserveWhiteSpace = false;
 141+ $dom->loadXML( $xml );
 142+ $dom->formatOutput = true;
 143+ return $dom->saveXml();
 144+ }
 145+
 146+ /**
 147+ * @param $url string
 148+ * @param $token string
 149+ * @param $cookies CookieJar
 150+ * @return MWHttpRequest
 151+ */
 152+ function buildAuthedRequest( $url, $token, $cookies = null ) {
 153+ $http = MWHttpRequest::factory( $url, array(
 154+ 'method' => 'GET',
 155+ )
 156+ );
 157+ $http->setHeader( 'User-Agent', self::getUserAgent() );
 158+ if ( $cookies !== null ) {
 159+ $http->setCookieJar( $cookies );
 160+ }
 161+ $http->setHeader( 'GData-Version', '3.0' );
 162+ $http->setHeader( 'Authorization', "GoogleLogin auth=\"{$token}\"" );
 163+ return $http;
 164+ }
 165+
 166+ /**
 167+ * @return string
 168+ */
 169+ private static function getUserAgent() {
 170+ return Http::userAgent() . ' MetricsReporting/' . METRICS_REPORTING_VERSION;
 171+ }
 172+}
 173+
 174+$maintClass = "FetchGoogleSpreadsheet";
 175+require_once( DO_MAINTENANCE );
Property changes on: trunk/extensions/MetricsReporting/fetchOfflineGoogleSpreadsheet.php
___________________________________________________________________
Added: svn:eol-style
1176 + native

Status & tagging log