Passed
Push — master ( 0986c5...31e6ff )
by Daimona
01:50
created

TaskDataProvider::addCreatedPages()   A

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;
4
5
use BotRiconferme\Request\RequestBase;
6
7
/**
8
 * Object holding data to be shared between different tasks.
9
 */
10
class TaskDataProvider extends ContextSource {
11
	/** @var array[] */
12
	private $processUsers;
13
	/** @var array[] */
14
	private $allUsers;
15
	/** @var PageRiconferma[] */
16
	private $createdPages = [];
17
18
	/**
19
	 * Get the full content of the JSON users list
20
	 *
21
	 * @return array[]
22
	 */
23
	public function getUsersList() : array {
24
		if ( $this->allUsers === null ) {
25
			$this->getLogger()->debug( 'Retrieving users list' );
26
			$content = $this->getController()->getPageContent( $this->getConfig()->get( 'list-title' ) );
27
			$this->allUsers = json_decode( $content, true );
28
		}
29
30
		return $this->allUsers;
31
	}
32
33
	/**
34
	 * Get a list of users to execute tasks on.
35
	 *
36
	 * @return array[]
37
	 */
38
	public function getUsersToProcess() : array {
39
		if ( $this->processUsers === null ) {
40
			$this->processUsers = [];
41
			foreach ( $this->getUsersList() as $user => $groups ) {
42
				$timestamp = $this->getValidTimestamp( $groups );
43
44
				if ( date( 'd/m', $timestamp ) === date( 'd/m' ) &&
45
					// Don't trigger if the date is actually today
46
					date( 'd/m/Y', $timestamp ) !== date( 'd/m/Y' )
47
				) {
48
					$this->processUsers[ $user ] = $groups;
49
				}
50
			}
51
		}
52
53
		return $this->processUsers;
54
	}
55
56
	/**
57
	 * Get the valid timestamp for the given groups
58
	 *
59
	 * @param array $groups
60
	 * @return int
61
	 */
62
	private function getValidTimestamp( array $groups ) : int {
63
		$checkuser = isset( $groups[ 'checkuser' ] ) ?
64
			\DateTime::createFromFormat( 'd/m/Y', $groups[ 'checkuser' ] )->getTimestamp() :
65
			0;
66
		$bureaucrat = isset( $groups[ 'bureaucrat' ] ) ?
67
			\DateTime::createFromFormat( 'd/m/Y', $groups[ 'bureaucrat' ] )->getTimestamp() :
68
			0;
69
70
		$timestamp = max( $bureaucrat, $checkuser );
71
		if ( $timestamp === 0 ) {
72
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups[ 'sysop' ] )->getTimestamp();
73
		}
74
		return $timestamp;
75
	}
76
77
	/**
78
	 * Get a list of all open procedures
79
	 *
80
	 * @return PageRiconferma[]
81
	 */
82
	public function getOpenPages() : array {
83
		$baseTitle = $this->getConfig()->get( 'ric-main-page' );
84
		$params = [
85
			'action' => 'query',
86
			'prop' => 'templates',
87
			'titles' => $baseTitle,
88
			'tl_namespace' => 4,
89
			'tllimit' => 'max'
90
		];
91
92
		$res = RequestBase::newFromParams( $params )->execute();
93
		$pages = $res->query->pages;
94
		$ret = [];
95
		foreach ( reset( $pages )->templates as $page ) {
96
			if ( preg_match( "!$baseTitle\/[^\/]+\/\d!", $page->title ) !== false ) {
97
				$ret[] = new PageRiconferma( $page->title, $this->getController() );
98
			}
99
		}
100
		return $ret;
101
	}
102
103
	/**
104
	 * Discard an user from the current list
105
	 *
106
	 * @param string $name
107
	 */
108
	public function removeUser( string $name ) {
109
		unset( $this->processUsers[ $name ] );
110
	}
111
112
	/**
113
	 * @return PageRiconferma[]
114
	 */
115
	public function getCreatedPages() : array {
116
		return $this->createdPages;
117
	}
118
119
	/**
120
	 * @param PageRiconferma $page
121
	 */
122
	public function addCreatedPages( PageRiconferma $page ) {
123
		$this->createdPages[] = $page;
124
	}
125
}
126