Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4025213
CentralAuthEditCounter.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
CentralAuthEditCounter.php
View Options
<?php
namespace
MediaWiki\Extension\CentralAuth
;
use
MediaWiki\Extension\CentralAuth\User\CentralAuthUser
;
use
Wikimedia\ObjectCache\WANObjectCache
;
use
Wikimedia\Rdbms\IReadableDatabase
;
use
Wikimedia\Rdbms\RawSQLValue
;
class
CentralAuthEditCounter
{
private
CentralAuthDatabaseManager
$databaseManager
;
private
WANObjectCache
$wanCache
;
public
function
__construct
(
CentralAuthDatabaseManager
$databaseManager
,
WANObjectCache
$wanCache
)
{
$this
->
databaseManager
=
$databaseManager
;
$this
->
wanCache
=
$wanCache
;
}
/**
* Get the global edit count for a user
*
* @return int
*/
public
function
getCount
(
CentralAuthUser
$centralUser
)
{
$userId
=
$centralUser
->
getId
();
$dbr
=
$this
->
databaseManager
->
getCentralReplicaDB
();
$count
=
$this
->
getCountFromDB
(
$dbr
,
$userId
);
if
(
$count
!==
false
)
{
return
$count
;
}
if
(
$this
->
databaseManager
->
isReadOnly
()
)
{
// Don't try DB_PRIMARY since that will throw an exception
return
$this
->
wanCache
->
getWithSetCallback
(
$this
->
wanCache
->
makeGlobalKey
(
'centralauth-editcount'
,
$centralUser
->
getId
()
),
5
*
$this
->
wanCache
::
TTL_MINUTE
,
function
()
use
(
$centralUser
)
{
return
$this
->
getCountFromWikis
(
$centralUser
);
}
);
}
$dbw
=
$this
->
databaseManager
->
getCentralPrimaryDB
();
$count
=
$this
->
getCountFromDB
(
$dbw
,
$userId
);
if
(
$count
!==
false
)
{
return
$count
;
}
$dbw
->
startAtomic
(
__METHOD__
);
// Lock the row
$dbw
->
newInsertQueryBuilder
()
->
insertInto
(
'global_edit_count'
)
->
ignore
()
->
row
(
[
'gec_user'
=>
$userId
,
'gec_count'
=>
0
]
)
->
caller
(
__METHOD__
)
->
execute
();
if
(
!
$dbw
->
affectedRows
()
)
{
// Try one more time after the lock wait
$dbw
->
endAtomic
(
__METHOD__
);
$count
=
$this
->
getCountFromDB
(
$dbw
,
$userId
);
if
(
$count
!==
false
)
{
return
$count
;
}
$dbw
->
startAtomic
(
__METHOD__
);
}
$count
=
$this
->
getCountFromWikis
(
$centralUser
);
$dbw
->
newUpdateQueryBuilder
()
->
update
(
'global_edit_count'
)
->
set
(
[
'gec_count'
=>
$count
]
)
->
where
(
[
'gec_user'
=>
$userId
]
)
->
caller
(
__METHOD__
)
->
execute
();
$dbw
->
endAtomic
(
__METHOD__
);
return
$count
;
}
/**
* Get the count by adding the user_editcount value across all attached wikis
*
* @return int
*/
public
function
getCountFromWikis
(
CentralAuthUser
$centralUser
)
{
$count
=
0
;
foreach
(
$centralUser
->
queryAttached
()
as
$acc
)
{
if
(
isset
(
$acc
[
'editCount'
]
)
)
{
$count
+=
(
int
)
$acc
[
'editCount'
];
}
}
return
$count
;
}
/**
* @return int|false
*/
private
function
getCountFromDB
(
IReadableDatabase
$dbr
,
int
$userId
)
{
$count
=
$dbr
->
newSelectQueryBuilder
()
->
select
(
'gec_count'
)
->
from
(
'global_edit_count'
)
->
where
(
[
'gec_user'
=>
$userId
]
)
->
caller
(
__METHOD__
)
->
fetchField
();
return
is_string
(
$count
)
?
(
int
)
$count
:
$count
;
}
/**
* Increment a global edit count
*
* @param CentralAuthUser $centralUser
* @param int $increment
*/
public
function
increment
(
CentralAuthUser
$centralUser
,
$increment
)
{
if
(
!
$increment
||
$this
->
databaseManager
->
isReadOnly
()
)
{
return
;
}
$dbw
=
$this
->
databaseManager
->
getCentralPrimaryDB
();
$dbw
->
newUpdateQueryBuilder
()
->
update
(
'global_edit_count'
)
->
set
(
[
'gec_count'
=>
new
RawSQLValue
(
'gec_count + '
.
(
int
)
$increment
)
]
)
->
where
(
[
'gec_user'
=>
$centralUser
->
getId
()
]
)
->
caller
(
__METHOD__
)
->
execute
();
// No need to populate when affectedRows() = 0, we can just wait for
// getCount() to be called.
}
public
function
recalculate
(
CentralAuthUser
$centralUser
):
int
{
$dbw
=
$this
->
databaseManager
->
getCentralPrimaryDB
();
$currentCount
=
$this
->
getCountFromDB
(
$dbw
,
$centralUser
->
getId
()
);
if
(
$currentCount
===
false
)
{
// Edit count is not cached, no need to do anything
return
0
;
}
$newCount
=
$this
->
getCountFromWikis
(
$centralUser
);
if
(
$newCount
===
$currentCount
)
{
return
$currentCount
;
}
$dbw
->
newUpdateQueryBuilder
()
->
update
(
'global_edit_count'
)
->
set
(
[
'gec_count'
=>
$newCount
]
)
->
where
(
[
'gec_user'
=>
$centralUser
->
getId
()
]
)
->
caller
(
__METHOD__
)
->
execute
();
return
$newCount
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 14 2026, 20:03 (13 w, 3 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
b4/e8/324c1cf552d552220caa1faf2e07
Default Alt Text
CentralAuthEditCounter.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment