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