Passed
Push — master ( c20c86...5d9847 )
by Daimona
01:48
created

TaskDataProvider::shouldAddUser()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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