Skip to content

Commit

Permalink
Update popularpages bot to work off of API instead of DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Niharika29 committed Mar 22, 2017
1 parent 60e3eb7 commit 6231532
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 68 deletions.
94 changes: 37 additions & 57 deletions ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,7 @@ public function getMonthlyPageviews( $pages, $start, $end ) {
* @param $page string Page link
*/
public function updateIndex( $page ) {
$creds = parse_ini_file( 'config.ini' );
$link = mysqli_connect( $creds['dbhost'], $creds['dbuser'], $creds['dbpass'], $creds['dbname'] );
$query = "SELECT * FROM checklist";
$data = mysqli_query( $link, $query );
$config = $this->getJSONConfig();
$output = '';
if ( $data->num_rows > 0 ) {
$output .= '
$output = '
The table below is the wikitext-table representation of the config used for generating Popular pages for wikiprojects. The actual config can be found at [[User:Community Tech bot/Popular pages config.json]].
\'\'\'Please do not edit this page\'\'\'. All edits will be overwritten the next time bot updates this page.
Expand All @@ -208,22 +201,29 @@ public function updateIndex( $page ) {
!Limit
!Updated on
';
while ( $row = $data->fetch_assoc() ) {
if ( $config["Wikipedia:WikiProject " . $row['project']] ) {
$config["Wikipedia:WikiProject " . $row['project']]['Updated'] = $row['updated'];
}
$projects = $this->getJSONConfig();
foreach ( $projects as $project => $info ) {
$params = [
'prop' => 'revisions',
'titles' => $info['Report'],
'rvprop' => 'timestamp',
'rvuser' => 'Community Tech bot',

This comment has been minimized.

Copy link
@kaldari

kaldari Mar 23, 2017

Collaborator

This should be $creds['botuser']

'rvlimit' => 1
];
$res = $this->apiQuery( $params );
$timestamp = '';
if ( isset( $res['query']['pages'][0]['revisions'][0]['timestamp'] ) ) {
$timestamp = date( 'Y-m-d', strtotime( $res['query']['pages'][0]['revisions'][0]['timestamp'] ) );
}
foreach ( $config as $project => $info ) {
$output .= '
$output .= '
|-
|[[' . $project . ']]
|[[' . $info['Report'] . ']]
|' . $info['Limit'] . '
|' . $info['Updated'] . '
|' . $timestamp . '
';
}
$this->setText( $page, $output );
}
$this->setText( $page, $output );
}

/**
Expand Down Expand Up @@ -285,53 +285,33 @@ public function getJSONConfig( $page = 'User:Community Tech bot/Popular pages co
}

/**
* Get projects which have not been updated for current cycle
* Get projects which have not been updated for current cycle using the API
*
* @return array List of projects which were updated more than 25 days ago from current date
*/
public function getStaleProjects() {
$creds = parse_ini_file( 'config.ini' );
$link = mysqli_connect( $creds['dbhost'], $creds['dbuser'], $creds['dbpass'], $creds['dbname'] );
$query = "SELECT * FROM checklist";
$data = mysqli_query( $link, $query );
$notUpdated = [];

if ( $data->num_rows > 0 ) {
while ( $row = $data->fetch_assoc() ) {
$project = $row['project'];
$lastUpdate = $row['updated'];
if ( !isset( $lastUpdate ) ) {
$notUpdated[] = $project;
continue;
} else {
$dateDiff = date_diff( new DateTime(), new DateTime( $lastUpdate ), true );
if ( (int)$dateDiff->format( '%d' ) > 25 ) {
// We found a project not updated for current month yet, add it to the array of projects not updated
$notUpdated[] = $project;
}
$staleProjects = [];
$projects = $this->getJSONConfig();
foreach ( $projects as $project => $info ) {
$params = [
'prop' => 'revisions',
'titles' => $info['Report'],
'rvprop' => 'timestamp',
'rvuser' => 'Community Tech bot',

This comment has been minimized.

Copy link
@kaldari

kaldari Mar 23, 2017

Collaborator

Same here.

'rvlimit' => 1
];
$res = $this->apiQuery( $params );
if ( isset( $res['query']['pages'][0]['revisions'][0]['timestamp'] ) ) {
$timestamp = $res['query']['pages'][0]['revisions'][0]['timestamp'];
// If the report is over 25 days old, we consider it to be stale
if ( date_diff( new DateTime( $timestamp ), new DateTime() )->format( '%d') > 25 ) {
$staleProjects[] = $info['Name'];
}
} else {
$staleProjects[] = $info['Name'];
}
}

return $notUpdated;
}

/**
* Update timestamp of report update for a project in the DB
*
* @param $project string Project name to against timestamp against
*/
public function updateDB( $project ) {
$creds = parse_ini_file( 'config.ini' );
$link = mysqli_connect( $creds['dbhost'], $creds['dbuser'], $creds['dbpass'], $creds['dbname'] );
$date = date( 'Y-m-d' );
$query = "UPDATE checklist SET updated = '" . (string)$date . "' WHERE project = '" . $project ."'";
$res = mysqli_query( $link, $query );
if ( $res ) {
logToFile( 'Database updated' );
} else {
logToFile( 'Database update failed!' );
}
return $staleProjects;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions UpdateReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ class UpdateReports {

protected $api;

/**
* UpdateReports constructor.
* If a projects array is provided, the constructor fetches the config info for those projects and updates them
*
* @param null|array $projects List of projects to update
*/
public function __construct( $projects = null ) {
$this->api = new ApiHelper();
logToFile( 'Running new cycle. Fetching config.' );
Expand All @@ -19,6 +25,11 @@ public function __construct( $projects = null ) {
$this->updateReports( $config );
}

/**
* Update popular pages reports. Primary execution point.
*
* @param $config array The JSON config from the wiki page
*/
public function updateReports( $config ) {
foreach ( $config as $project => $info ) {
logToFile( 'Beginning to process: ' . $project );
Expand All @@ -32,7 +43,7 @@ public function updateReports( $config ) {
logToFile( 'Incomplete data in config. Aborting.' );
continue;
}
$pages = $this->api->getProjectPages( $info['Name'] ); // Returns { \'title\' => array( \'class\' => \'\', \'importance\' => \'\' ),... }
$pages = $this->api->getProjectPages( $info['Name'] ); // Returns { 'title' => ['class'=>'', 'importance'=>''],...}
if ( empty( $pages ) ) {
continue;
}
Expand Down Expand Up @@ -91,8 +102,6 @@ public function updateReports( $config ) {
// Update complete page
$this->api->setText( $info['Report'], $output );
}
// Update database for the project
$this->api->updateDB( $info['Name'] );
logToFile( 'Finished processing: ' . $project );
}
// Update index page
Expand Down
8 changes: 0 additions & 8 deletions checklist.sql

This file was deleted.

0 comments on commit 6231532

Please sign in to comment.