Passed
Push — master ( 6f44ea...591692 )
by Daimona
01:45
created

PageRiconferma   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 2 1
A getUser() 0 2 1
A __construct() 0 3 1
A getEndTimestamp() 0 11 2
A isVote() 0 3 1
A getUserNum() 0 2 1
A hasOpposition() 0 15 1
A getBaseTitle() 0 2 1
A __toString() 0 2 1
A getContent() 0 5 2
A getNum() 0 3 1
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use BotRiconferme\Request\RequestBase;
6
7
/**
8
 * Represents a single riconferma page
9
 */
10
class PageRiconferma {
11
	/** @var string */
12
	private $title;
13
	/** @var WikiController */
14
	private $controller;
15
	/** @var string */
16
	private $content;
17
18
	/**
19
	 * @param string $title
20
	 * @param WikiController $controller
21
	 */
22
	public function __construct( string $title, WikiController $controller ) {
23
		$this->title = $title;
24
		$this->controller = $controller;
25
	}
26
27
	/**
28
	 * @return string
29
	 */
30
	public function getTitle() : string {
31
		return $this->title;
32
	}
33
34
	/**
35
	 * Get the name of the user from the title
36
	 *
37
	 * @return string
38
	 */
39
	public function getUser() : string {
40
		return explode( '/', $this->title )[2];
41
	}
42
43
	/**
44
	 * Returns the progressive number in the title
45
	 *
46
	 * @return int
47
	 */
48
	public function getNum() : int {
49
		$bits = explode( '/', $this->getTitle() );
50
		return end( $bits );
51
	}
52
53
	/**
54
	 * Get the last part of the title as Username/Num
55
	 *
56
	 * @return string
57
	 */
58
	public function getUserNum() : string {
59
		return explode( '/', $this->getTitle(), 3 )[2];
60
	}
61
62
	/**
63
	 * Strip the part with the progressive number
64
	 *
65
	 * @return string
66
	 */
67
	public function getBaseTitle() : string {
68
		return substr( $this->getTitle(), 0, strrpos( $this->getTitle(), '/' ) );
69
	}
70
71
	/**
72
	 * Get the content of this page
73
	 *
74
	 * @return string
75
	 */
76
	public function getContent() : string {
77
		if ( $this->content === null ) {
78
			$this->content = $this->controller->getPageContent( $this->title );
79
		}
80
		return $this->content;
81
	}
82
83
	/**
84
	 * Whether this page has enough opposing votes
85
	 *
86
	 * @return bool
87
	 */
88
	public function hasOpposition() : bool {
89
		$params = [
90
			'action' => 'query',
91
			'prop' => 'revisions',
92
			'titles' => $this->title,
93
			'rvprop' => 'content',
94
			'rvslots' => 'main',
95
			'rvsection' => 4
96
		];
97
		$res = RequestBase::newFromParams( $params )->execute();
98
		$page = reset( $res->query->pages );
99
		$content = $page->revisions[0]->slots->main->{ '*' };
100
		// Let's hope that this is good enough...
101
		$votes = substr_count( $content, "\n\# *(?![#*])" );
102
		return $votes >= 15;
103
	}
104
105
	/**
106
	 * Whether this page is a vote
107
	 *
108
	 * @return bool
109
	 */
110
	public function isVote() : bool {
111
		$sectionReg = '/<!-- SEZIONE DA UTILIZZARE PER/';
112
		return preg_match( $sectionReg, $this->getContent() ) === false;
113
	}
114
115
	/**
116
	 * Get the end time
117
	 *
118
	 * @return int
119
	 */
120
	public function getEndTimestamp() : int {
121
		if ( $this->isVote() ) {
122
			$matches = [];
123
			$reg = "!La votazione ha inizio il.+ e ha termine.+ '''([^']+)''' alle ore '''([^']+)'''!";
124
			preg_match( $reg, $this->getContent(), $matches );
125
			list( , $day, $hours ) = $matches;
126
			$day = preg_replace( '![^\d \w]!', '', $day );
127
			return WikiController::getTimestampFromLocalTime( $day . " alle " . $hours );
128
		} else {
129
			$created = $this->controller->getPageCreationTS( $this->title );
130
			return $created + 60 * 60 * 24 * 7;
131
		}
132
	}
133
134
	public function __toString() {
135
		return $this->getTitle();
136
	}
137
}
138