Passed
Push — master ( bac275...6454e3 )
by Daimona
02:17
created

TaskDataProvider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addCreatedPages() 0 2 1
A removeUser() 0 2 1
B getUsersToProcess() 0 23 7
A getPagesToClose() 0 12 4
A getCreatedPages() 0 2 1
A getOpenPages() 0 23 4
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use BotRiconferme\Wiki\Page\Page;
6
use BotRiconferme\Wiki\Page\PageBotList;
7
use BotRiconferme\Wiki\Page\PageRiconferma;
8
use BotRiconferme\Request\RequestBase;
9
10
/**
11
 * Object holding data to be shared between different tasks.
12
 */
13
class TaskDataProvider extends ContextSource {
14
	/** @var array[] */
15
	private $processUsers;
16
	/** @var PageRiconferma[] */
17
	private $createdPages = [];
18
19
	/**
20
	 * Get a list of users to execute tasks on.
21
	 *
22
	 * @return array[]
23
	 */
24
	public function getUsersToProcess() : array {
25
		if ( $this->processUsers === null ) {
26
			$this->processUsers = [];
27
			foreach ( PageBotList::get()->getAdminsList() as $user => $groups ) {
28
				if ( array_intersect_key( $groups, [ 'override-perm', 'override' ] ) ) {
29
					// A one-time override takes precedence
30
					$timestamp = $groups[ 'override' ] ?? $groups[ 'override-perm' ];
31
					$override = true;
32
				} else {
33
					$timestamp = PageBotList::getValidTimestamp( $groups );
34
					$override = false;
35
				}
36
37
				if ( date( 'd/m', $timestamp ) === date( 'd/m' ) &&
38
					// Don't trigger if the date is actually today and it's not an override
39
					( $override || date( 'd/m/Y', $timestamp ) !== date( 'd/m/Y' ) )
40
				) {
41
					$this->processUsers[ $user ] = $groups;
42
				}
43
			}
44
		}
45
46
		return $this->processUsers;
47
	}
48
49
	/**
50
	 * Get a list of all open procedures
51
	 *
52
	 * @return PageRiconferma[]
53
	 */
54
	public function getOpenPages() : array {
55
		static $list = null;
56
		if ( $list === null ) {
57
			$mainTitle = $this->getConfig()->get( 'main-page-title' );
58
			$params = [
59
				'action' => 'query',
60
				'prop' => 'templates',
61
				'titles' => $mainTitle,
62
				'tlnamespace' => 4,
63
				'tllimit' => 'max'
64
			];
65
66
			$pages = RequestBase::newFromParams( $params )->execute()->query->pages;
67
			$reg = ( new Page( $mainTitle ) )->getRegex();
68
			$list = [];
69
			foreach ( reset( $pages )->templates as $page ) {
70
				if ( preg_match( "!$reg\/[^\/]+\/\d!", $page->title ) ) {
71
					$list[] = new PageRiconferma( $page->title );
72
				}
73
			}
74
		}
75
76
		return $list;
77
	}
78
79
	/**
80
	 * Get a list of all procedures to be closed
81
	 *
82
	 * @return PageRiconferma[]
83
	 */
84
	public function getPagesToClose() : array {
85
		static $list = null;
86
		if ( $list === null ) {
87
			$allPages = $this->getOpenPages();
88
			$list = [];
89
			foreach ( $allPages as $page ) {
90
				if ( time() > $page->getEndTimestamp() ) {
91
					$list[] = $page;
92
				}
93
			}
94
		}
95
		return $list;
96
	}
97
98
	/**
99
	 * Discard an user from the current list
100
	 *
101
	 * @param string $name
102
	 */
103
	public function removeUser( string $name ) {
104
		unset( $this->processUsers[ $name ] );
105
	}
106
107
	/**
108
	 * @return PageRiconferma[]
109
	 */
110
	public function getCreatedPages() : array {
111
		return $this->createdPages;
112
	}
113
114
	/**
115
	 * @param PageRiconferma $page
116
	 */
117
	public function addCreatedPages( PageRiconferma $page ) {
118
		$this->createdPages[] = $page;
119
	}
120
}
121