jenkins-bot has submitted this change and it was merged.
Change subject: Improve elections.php (second attempt)
......................................................................
Improve elections.php (second attempt)
Try I8c2c5bcc66b52335f9dc again.
Change-Id: I44301860cc08a94cbefcc344fae307316243d9ea
---
M Elections/elections.php
1 file changed, 110 insertions(+), 118 deletions(-)
Approvals:
MarcoAurelio: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Elections/elections.php b/Elections/elections.php
index 5137f45..c76a22b 100644
--- a/Elections/elections.php
+++ b/Elections/elections.php
@@ -1,45 +1,40 @@
<?php
-function getPages($titles)
-{
- $URL =
-'https://meta.wikimedia.org/w/api.php?action=query&format=php&prop=revisions&rvprop=content&redirects&titles=';
-
- if (is_array($titles)) {
- foreach ($titles as $t) {
- $URL .= urlencode($t) . '|';
+function getPages( $titles ) {
+ $URL = 'https://meta.wikimedia.org/w/api.php'
+ .
'?action=query&format=php&prop=revisions&rvprop=content&redirects&titles=';
+ if ( is_array( $titles ) ) {
+ foreach ( $titles as $t ) {
+ $URL .= urlencode( $t ) . '|';
}
- $URL = rtrim($URL, '|');
+ $URL = rtrim( $URL, '|' );
} else {
- $URL .= urlencode($titles);
+ $URL .= urlencode( $titles );
}
- echo '<!--', $URL, '-->';
- $ch = curl_init($URL);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Labs Bot -
https://tools.wmflabs.org/stewardbots');
- $result = unserialize(curl_exec($ch));
- curl_close($ch);
-
- $output = array();
- if ($result['query']['pages']) {
- foreach($result['query']['pages'] as $page) {
- $output[] = array('title' => $page['title'],
- 'content' => $page['revisions'][0]['*']);
+ echo "<!-- $URL -->";
+ $ch = curl_init( $URL );
+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
+ curl_setopt( $ch, CURLOPT_USERAGENT, 'Labs Bot -
https://tools.wmflabs.org/stewardbots' );
+ $result = unserialize( curl_exec( $ch ) );
+ curl_close( $ch );
+ $resultPages = $result['query']['pages'];
+ if ( $resultPages ) {
+ $output = [];
+ foreach ( $resultPages as $page ) {
+ $output[] = [
+ 'title' => $page['title'],
+ 'content' => $page['revisions'][0]['*']
+ ];
}
-
return $output;
- } else {
- return False;
}
-}
+ return false;
+}
// To sort the array returned by getPages()
-function titleSort($a , $b) {
- return strnatcasecmp($a['title'], $b['title']);
+function titleSort( $a, $b ) {
+ return strnatcasecmp( $a['title'], $b['title'] );
}
-
-// Page content
-$cacheFile = './cache/elections.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
@@ -63,142 +58,139 @@
<h2>Steward elections</h2>
<p>This page contains an unofficial tally of the votes in the <a
href="//meta.wikimedia.org/wiki/Stewards/Elections_2016">2016 steward
elections</a>.</p>
<?php
+$cacheFile = './cache/elections.php';
+$useCache = false;
// Used cached version?
-if ( file_exists($cacheFile) ) {
- $useCache = True;
- if ( $_GET['action'] == 'purge' && ((time() - filemtime($cacheFile)) > 60)
-|| $_GET['adm']) {
- $useCache = False;
+if ( file_exists( $cacheFile ) ) {
+ $useCache = true;
+ $lastModifiedTime = filemtime( $cacheFile );
+ if ( $_GET['action'] === 'purge' && time() - $lastModifiedTime > 60 ) {
+ // Cache can be purged only once within a minute
+ $useCache = false;
}
-} else {
- $useCache = False;
}
if ( $useCache ) {
- echo '<p style="font-style:italic">Using cached data from ' .
-strftime('%H:%M, %e %B %Y', filemtime($cacheFile)) . ' (UTC), <a href="' .
-$_SERVER['php_self'] . '?action=purge">purge</a>.</p>';
-
- // User tried to purge, but we decided to use the cache anyway.
- if ($_GET['action'] == 'purge') {
- echo '<p style="font-weight:bold;">Note: data can only be purged once
every minute. Please be patient.</p>';
- }
-
- include_once $cacheFile;
+ echo '<p style="font-style:italic">'
+ . 'Using cached data from '
+ . strftime( '%H:%M, %e %B %Y', filemtime( $cacheFile ) ) . ' (UTC), '
+ . '<a href="' . $_SERVER['php_self'] . '?action=purge">purge</a>.'
+ . '<small>Note: Data can only be purged once every
minute.</small></p>';
+ require_once $cacheFile;
} else {
// Start output buffering to regenerate cache
ob_start();
?>
<table class="wikitable sortable">
- <thead>
+ <thead>
<tr>
- <th>#</th>
- <th style="width:300px;">Candidate</th>
- <th>Yes</th>
- <th>No</th>
- <th>Neutral</th>
- <th>Support</th>
- </tr>
- </thead>
+ <th>#</th>
+ <th style="width:300px;">Candidate</th>
+ <th>Yes</th>
+ <th>No</th>
+ <th>Neutral</th>
+ <th>Support</th>
+ </tr>
+ </thead>
<tbody>
<?php
- $pages = getPages('Stewards/Elections_2016');
-
- if($pages) {
+ $pages = getPages( 'Stewards/Elections_2016' );
+ $content = '';
+ if ( $pages ) {
$content = $pages[0]['content'];
}
- preg_match_all('/^\{\{[Ss]e[_ ]candidate[_
]indexer\|2016\|(?<user>[^\|\}]+)(?:\|(?<status>[^\|\}]+))?\}\}/m', $content,
$m);
+
+ preg_match_all(
+ '/^\{\{[Ss]e[_ ]candidate[_
]indexer\|2016\|(?<user>[^\|\}]+)(?:\|(?<status>[^\|\}]+))?\}\}/m',
+ $content, $m
+ );
// Loop users
- $titles = array();
- $count = count($m['user']);
- for($i = 0; $i < $count; $i++) {
+ $titles = [];
+ $count = count( $m['user'] );
+ for ( $i = 0; $i < $count; $i++ ) {
$user = $m['user'][$i];
$status = $m['status'][$i];
- if(!$status || $status == 'yes')
+ if ( !$status || $status == 'yes' ) {
$titles[] = 'Stewards/Elections_2016/Votes/' . $user;
+ }
}
- natcasesort($titles);
+ natcasesort( $titles );
- $titles = array_chunk($titles, 40);
+ $titles = array_chunk( $titles, 40 );
$i = 1;
- foreach($titles as $tchunk) {
- $pages = getPages($tchunk);
- usort($pages, 'titleSort');
+
+ foreach ( $titles as $tchunk ) {
+ $pages = getPages( $tchunk );
+
+ usort( $pages, 'titleSort' );
// Treat pages
- foreach($pages as $page) {
+ foreach ( $pages as $page ) {
$title = $page['title'];
- $user = substr($title, 30);
+ $user = substr( $title, 30 );
$content = $page['content'];
-
- if(!empty($content)) {
+ if ( !empty( $content ) ) {
// Find vote headers
- $offset = array();
- preg_match('/\=\=\=[ ]*?\{\{sr-heading\|yes\}\}[ ]*?\=\=\=/',
-$content, $m, PREG_OFFSET_CAPTURE);
+ $offset = [];
+ preg_match( '/\=\=\=[ ]*?\{\{sr-heading\|yes\}\}[ ]*?\=\=\=/',
$content, $m, PREG_OFFSET_CAPTURE );
$offset['yes'] = $m[0][1];
- preg_match('/\=\=\=[ ]*?\{\{sr-heading\|no\}\}[ ]*?\=\=\=/',
-$content, $m, PREG_OFFSET_CAPTURE);
+ preg_match( '/\=\=\=[ ]*?\{\{sr-heading\|no\}\}[ ]*?\=\=\=/',
$content, $m, PREG_OFFSET_CAPTURE );
$offset['no'] = $m[0][1];
- preg_match('/\=\=\=[ ]*?\{\{sr-heading\|neutral\}\}[
]*?\=\=\=/',
-$content, $m, PREG_OFFSET_CAPTURE);
+ preg_match( '/\=\=\=[ ]*?\{\{sr-heading\|neutral\}\}[
]*?\=\=\=/', $content, $m, PREG_OFFSET_CAPTURE );
$offset['neutral'] = $m[0][1];
// Find votes
$vote = '/^\#[^\:\#].+?$/m';
- $votes = array();
- $votes['yes'] = preg_match_all($vote, substr($content,
-$offset['yes'], $offset['no'] - $offset['yes']), $m);
- $votes['no'] = preg_match_all($vote, substr($content,
-$offset['no'], $offset['neutral'] - $offset['no']), $m);
- $votes['neutral'] = preg_match_all($vote, substr($content,
-$offset['neutral']), $m);
+ $votes = [];
+ $votes['yes'] = preg_match_all( $vote, substr( $content,
$offset['yes'], $offset['no'] - $offset['yes'] ), $m );
+ $votes['no'] = preg_match_all( $vote, substr( $content,
$offset['no'], $offset['neutral'] - $offset['no'] ), $m );
+ $votes['neutral'] = preg_match_all( $vote, substr( $content,
$offset['neutral'] ), $m );
// Math
- $support = $votes['yes'] / ($votes['yes'] + $votes['no']);
- $perc = round($support * 100, 1);
+ $support = $votes['yes'] / ( $votes['yes'] + $votes['no'] );
+ $perc = round( $support * 100, 1 );
- $bgyes = ($votes['yes'] < 30 ? '
-style="background-color:#FF9999"' : '');
- $bgsup = ' style="background-color:' . ($support >= 0.8 ?
-'#99FF99' : '#FF9999') . '"';
-
+ $bgyes = ( $votes['yes'] < 30 ) ?
'style="background-color:#FF9999"' : '';
+ $bgsup = 'style="background-color:' . ( $support >= 0.8 ?
'#99FF99' : '#FF9999' ) . '"';
// Output row
?>
- <tr>
- <td><?=$i;?></td>
- <td><a
href="//meta.wikimedia.org/wiki/Stewards/Elections_2016/Votes/<?=$user;?>"><?=$user;?></a></td>
- <td<?=$bgyes;?>><?=$votes['yes'];?></td>
- <td><?=$votes['no'];?></td>
- <td><?=$votes['neutral'];?></td>
- <td<?=$bgsup;?>><?=$perc;?>%</td>
- </tr>
+ <tr>
+ <td><?=$i;?></td>
+ <td><a
href="//meta.wikimedia.org/wiki/Stewards/Elections_2016/Votes/<?=$user;?>"><?=$user;?></a></td>
+ <td <?=$bgyes;?>><?=$votes['yes'];?></td>
+ <td><?=$votes['no'];?></td>
+ <td><?=$votes['neutral'];?></td>
+ <td <?=$bgsup;?>><?=$perc;?>%</td>
+ </tr>
<?php
- } else {
+ } else {
?>
- <tr>
- <td><?=$i;?></td>
- <td><a
href="//meta.wikimedia.org/wiki/Stewards/Elections_2016/Votes/<?=$user;?>"><?=$user;?></a></td>
- <td colspan="4">Could not get votes. Please <a
href="//phabricator.wikimedia.org/maniphest/task/create/?projects=Tool-Labs-tools-stewardbots">report</a>
this.</td>
- </tr>
+ <tr>
+ <td><?=$i; ?></td>
+ <td><a
href="//meta.wikimedia.org/wiki/Stewards/Elections_2016/Votes/<?=$user;?>"><?=$user;
?></a></td>
+ <td colspan="4">Could not get votes. Please <a
href="//phabricator.wikimedia.org/maniphest/task/create/?projects=Tool-Labs-tools-stewardbots">report</a>
this.</td>
+ </tr>
<?php
}
- $i++;
+ $i++;
}
}
?>
</tbody>
</table>
<?php
- // Save results to cache
- $f = fopen($cacheFile, 'w');
- fwrite($f, ob_get_contents());
- fclose($f);
+ // Save results to cache if possible
+ if ( file_exists( $cacheFile ) ) {
+ $f = fopen( $cacheFile, 'w' );
+ fwrite( $f, ob_get_contents() );
+ fclose( $f );
+ }
// Send the output to the browser
ob_end_flush();
+
}
?>
</div>
@@ -210,17 +202,17 @@
href="//tools.wmflabs.org/stewardbots/Elections/elections.php"
title="Elections"></a>
</div>
<div class="portlet" id="p-navigation">
- <h5>Stewards</h5>
+ <h3>Stewards</h3>
<div class="pBody">
<ul>
- <li><a
ref="//meta.wikimedia.org/wiki/Stewards">Stewards</a></li>
+ <li><a
href="//meta.wikimedia.org/wiki/Stewards">Stewards</a></li>
<li><a
href="//meta.wikimedia.org/wiki/Stewards_policy">Policy</a></li>
<li><a
href="//meta.wikimedia.org/wiki/Steward_handbook">Handbook</a></li>
</ul>
</div>
</div>
<div class="portlet" id="p-navigation2">
- <h5>Steward elections</h5>
+ <h3>Steward elections</h3>
<div class="pBody">
<ul>
<li><a
href="//meta.wikimedia.org/wiki/Stewards/Elections_2016">Elections page</a></li>
@@ -241,7 +233,7 @@
width="88" /></a>
</div>
<ul id="f-list">
- <li id="lastmod">This page was last modified 9 March 2016.</li>
+ <li id="lastmod">This page was last modified 22 March
2016.</li>
<li id="about">This tool was written by <a
href="//meta.wikimedia.org/wiki/User:Erwin">Erwin</a> and is mantained by the
stewardbots project.</li>
</ul>
</div>
--
To view, visit https://gerrit.wikimedia.org/r/280684
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I44301860cc08a94cbefcc344fae307316243d9ea
Gerrit-PatchSet: 2
Gerrit-Project: labs/tools/stewardbots
Gerrit-Branch: master
Gerrit-Owner: Glaisher <[email protected]>
Gerrit-Reviewer: MarcoAurelio <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits