Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.77% |
30 / 31 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| QueueManager | |
96.77% |
30 / 31 |
|
80.00% |
4 / 5 |
6 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| insert | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| deleteByPageId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteByPageIds | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
2.00 | |||
| isPageTriageNamespace | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\PageTriage; |
| 4 | |
| 5 | use MediaWiki\Status\Status; |
| 6 | use Wikimedia\Rdbms\IConnectionProvider; |
| 7 | |
| 8 | /** |
| 9 | * Class for adding, updating and deleting items from a queue of pages awaiting triage. |
| 10 | */ |
| 11 | class QueueManager { |
| 12 | |
| 13 | public function __construct( |
| 14 | private readonly IConnectionProvider $dbProvider, |
| 15 | ) { |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @param QueueRecord $queueRecord |
| 20 | * @return Status OK if the row was added, not OK otherwise. |
| 21 | */ |
| 22 | public function insert( QueueRecord $queueRecord ): Status { |
| 23 | $status = new Status(); |
| 24 | $queueRecordData = $queueRecord->jsonSerialize(); |
| 25 | $dbw = $this->dbProvider->getPrimaryDatabase(); |
| 26 | $dbw->newInsertQueryBuilder() |
| 27 | ->insertInto( 'pagetriage_page' ) |
| 28 | ->row( $queueRecordData ) |
| 29 | ->caller( __METHOD__ ) |
| 30 | // 'IGNORE' is needed for fields like ptrp_tags_updated which will not be |
| 31 | // present on a new record. |
| 32 | ->ignore() |
| 33 | ->execute(); |
| 34 | $status->setOK( $dbw->affectedRows() === 1 ); |
| 35 | return $status; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Delete an item by page ID. |
| 40 | * |
| 41 | * @param int $pageId |
| 42 | * @return Status OK if a page was deleted, not OK otherwise. |
| 43 | */ |
| 44 | public function deleteByPageId( int $pageId ): Status { |
| 45 | return $this->deleteByPageIds( [ $pageId ] ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param int[] $pageIds |
| 50 | * @return Status OK if all pages were deleted, not OK otherwise. |
| 51 | */ |
| 52 | public function deleteByPageIds( array $pageIds ): Status { |
| 53 | $dbw = $this->dbProvider->getPrimaryDatabase(); |
| 54 | $status = new Status(); |
| 55 | if ( !$pageIds ) { |
| 56 | return $status; |
| 57 | } |
| 58 | // TODO: Factor out ArticleMetadata into value object / manager. |
| 59 | $articleMetadata = new ArticleMetadata( $pageIds ); |
| 60 | $dbw->startAtomic( __METHOD__ ); |
| 61 | $dbw->newDeleteQueryBuilder() |
| 62 | ->deleteFrom( 'pagetriage_page' ) |
| 63 | ->where( [ 'ptrp_page_id' => $pageIds ] ) |
| 64 | ->caller( __METHOD__ ) |
| 65 | ->execute(); |
| 66 | $status->setOK( count( $pageIds ) === $dbw->affectedRows() ); |
| 67 | // TODO: Is "ArticleMetadata" used/useful without the core queue data in pagetriage_page? |
| 68 | // if it isn't, we could make QueueManager handle create/update/delete for the page table |
| 69 | // and metadata. |
| 70 | $articleMetadata->deleteMetadata(); |
| 71 | $dbw->endAtomic( __METHOD__ ); |
| 72 | return $status; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check if a namespace is managed by PageTriage. This perhaps belongs better in |
| 77 | * another service. |
| 78 | * |
| 79 | * @param int $namespace |
| 80 | * @return bool True if the article is in a namespace managed by PageTriage. |
| 81 | */ |
| 82 | public function isPageTriageNamespace( int $namespace ): bool { |
| 83 | // TODO: Factor PageTriageUtil::getNamespaces into this service. |
| 84 | return in_array( $namespace, PageTriageUtil::getNamespaces() ); |
| 85 | } |
| 86 | |
| 87 | } |