-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpecialW4GRBMigrate.php
130 lines (116 loc) · 4.95 KB
/
SpecialW4GRBMigrate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*********************************************************************
**
** This file is part of the W4G Rating Bar extension for MediaWiki
** Copyright (C)2010
** - David Dernoncourt <www.patheticcockroach.com>
** - Franck Dernoncourt <www.francky.me>
**
** Home Page: http://www.wiki4games.com/Wiki4Games:W4G Rating Bar
**
** This program is licensed under the Creative Commons
** Attribution-ShareAlike 4.0 license
** <https://creativecommons.org/licenses/by-sa/4.0/>
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
*********************************************************************/
/**
* Edit those accordingly to your previous Rating Bar v1.1 settings
**/
$W4GRBmigrationDBhost = ''; // Copy value from $ratingbar_dbhost
$W4GRBmigrationDBuser = ''; // Copy value from $ratingbar_dbuser
$W4GRBmigrationDBpass = ''; // Copy value from $ratingbar_dbpass
$W4GRBmigrationDBname = '';// Copy value from $ratingbar_dbname
$W4GRBmigrationTablePrefix = ''; // Copy value from $table_prefix
$W4GRBmigrationTableBase = ''; // Copy value from $ratingbar_tablename
$remove_anonymous_votes = false; // Whether or not you want to remove anonymous votes if you set this to false, you'll want to make sure the primary key of the w4grb_votes table is (uid,pid,ip)
$W4GRBmigrationFollow = 100; // Every $W4GRBmigrationFollow lines inserted you'll get a progress-o-meter message
$W4GRBmigrationTableName=$W4GRBmigrationTablePrefix.$W4GRBmigrationTableBase;
$wgSpecialPages['W4GRBMigrate'] = 'W4GRBMigrate';
class W4GRBMigrate extends UnlistedSpecialPage
{
function __construct()
{
parent::__construct( 'W4GRBMigrate');
# $this->includable( false );
}
function execute( $par )
{
global $wgRequest, $wgOut, $wgUser, $wgDBprefix;
global $W4GRBmigrationDBhost, $W4GRBmigrationDBuser, $W4GRBmigrationDBpass, $W4GRBmigrationDBname, $W4GRBmigrationTableName, $remove_anonymous_votes, $W4GRBmigrationFollow;
error_reporting(E_ALL);
ini_set("display_errors", 'on');
if( wfReadOnly() )
{
$wgOut->readOnlyPage();
return;
}
$this->setHeaders(); # not sure what that's for
$wgOut->disable(); # for raw output
$db_old = mysqli_connect($W4GRBmigrationDBhost, $W4GRBmigrationDBuser, $W4GRBmigrationDBpass, $W4GRBmigrationDBname) or die("Failed to connected to the database");
$query='SELECT * FROM `'.$W4GRBmigrationTableName.'`;';
$db_old_result = mysqli_query($db_old, $query) or die('Failed to select table '.$W4GRBmigrationTableName.' (query: '.$query.mysqli_error($db_old));
$page_obj=new W4GRBPage();
$dbmaster = wfGetDB( DB_MASTER ); # exceptionnally we'll run all on master
# $dbmaster->ignoreErrors('off'); # we need this so that failed queries return false
echo 'Database connection successful, starting vote import...<br/>';
$i=0;
while($db_old_row=mysqli_fetch_array($db_old_result))
{
$i++; # line counter
# Read a line
$uid=$db_old_row['user_id'];
$vote_sent=$db_old_row['rating'];
if(!$page_obj->setFullPageName($db_old_row['page_id'])) # Check if the page name is valid
{
echo 'Error: no page with textual ID '.$db_old_row['page_id'].' => skipping line '.$i.'.<br/>';
continue;
}
$pid=$page_obj->getPID();
$time=$db_old_row['time'];
$ip=$db_old_row['ip'];
# Check if the vote wasn't anonymous
if($uid==0 && $remove_anonymous_votes)
{
echo 'Anonymous vote on page '.$pid.' skipped.<br/>';
continue;
}
if(!$dbmaster->insert('w4grb_votes',
array( 'uid' => $uid,
'pid' => $pid,
'vote' => $vote_sent,
'ip' => $ip,
'time' => $time),
__METHOD__ ))
echo 'Error: couldn't insert line '.$i.' (this is probably multiple anonymous votes on a same page from a same IP)<br/>';
#else echo $dbmaster->lastQuery().'=>OK<br/>';
if(round($i/$W4GRBmigrationFollow)*$W4GRBmigrationFollow==$i) echo $i.' lines processed.<br/>';
}
echo 'All votes imported (a total of '.$i.' lines were read). Generating average ratings table...<br/>';
$result=$dbmaster->select(
$wgDBprefix.'w4grb_votes AS w4grb_votes, '.$wgDBprefix.'page AS page',
'AVG(w4grb_votes.vote) AS avg, COUNT(*) AS n, page.page_id AS pid',
'w4grb_votes.pid=page.page_id',
__METHOD__,
array('GROUP BY' => 'page.page_id')
);
#echo $dbmaster->lastQuery().'=>OK?<br/>';
$i=0;
while($row = $dbmaster->fetchObject($result))
{
$i++;
if(!$dbmaster->insert('w4grb_avg',
array( 'pid' => $row->pid,
'avg' => $row->avg,
'n' => $row->n),
__METHOD__ ))
echo 'Error: couldn't insert average rating for page '.$row->pid.'<br/>';
if(round($i/$W4GRBmigrationFollow)*$W4GRBmigrationFollow==$i) echo $i.' pages processed.<br/>';
}
echo 'All average ratings have been imported (a total of '.$i.' pages were treated).<br/>';
echo 'That's it, all done!';
}
}