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