Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
41.18% |
7 / 17 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Utils | |
41.18% |
7 / 17 |
|
0.00% |
0 / 2 |
7.26 | |
0.00% |
0 / 1 |
getDeletedExpiry | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
makeMaintenanceRepository | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\ReadingLists; |
4 | |
5 | use MediaWiki\Logger\LoggerFactory; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\User\User; |
8 | use UnexpectedValueException; |
9 | |
10 | /** |
11 | * Static utility methods. |
12 | */ |
13 | class Utils { |
14 | public const VIRTUAL_DOMAIN = 'virtual-readinglists'; |
15 | |
16 | /** |
17 | * Returns the timestamp at which deleted items expire (can be purged). |
18 | * @return string Timestamp in TS_MW format |
19 | * @throws UnexpectedValueException When the extension is configured incorrectly. |
20 | */ |
21 | public static function getDeletedExpiry() { |
22 | $services = MediaWikiServices::getInstance(); |
23 | $extensionConfig = $services->getConfigFactory()->makeConfig( 'ReadingLists' ); |
24 | $days = $extensionConfig->get( 'ReadingListsDeletedRetentionDays' ); |
25 | $unixTimestamp = strtotime( '-' . $days . ' days' ); |
26 | $timestamp = wfTimestamp( TS_MW, $unixTimestamp ); |
27 | if ( !$timestamp || !$unixTimestamp ) { |
28 | // not really an argument but close enough |
29 | throw new UnexpectedValueException( 'Invalid $wgReadingListsDeletedRetentionDays value: ' |
30 | . $days ); |
31 | } |
32 | return $timestamp; |
33 | } |
34 | |
35 | /** |
36 | * Create a repository for maintenance use. |
37 | * The repo will be associated with a system user. |
38 | * |
39 | * @return ReadingListRepository |
40 | */ |
41 | public static function makeMaintenanceRepository() { |
42 | // TODO: Move this to a service |
43 | $services = MediaWikiServices::getInstance(); |
44 | $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); |
45 | // There isn't really any way for this user to be non-local, but let's be future-proof. |
46 | $centralId = $services->getCentralIdLookupFactory() |
47 | ->getLookup() |
48 | ->centralIdFromLocalUser( $user ); |
49 | $repository = new ReadingListRepository( $centralId, $services->getDBLoadBalancerFactory() ); |
50 | $repository->setLogger( LoggerFactory::getInstance( 'readinglists' ) ); |
51 | return $repository; |
52 | } |
53 | |
54 | } |