TaskDataProvider::removeUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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