Passed
Push — master ( 33a572...6f44ea )
by Daimona
01:49
created

PageRiconferma::getEndTimestamp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 11
rs 9.9332
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
 * 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
	 * Get the last part of the title as Username/Num
45
	 *
46
	 * @return string
47
	 */
48
	public function getUserNum() : string {
49
		return explode( '/', $this->getTitle(), 3 )[2];
50
	}
51
52
	/**
53
	 * Strip the part with the progressive number
54
	 *
55
	 * @return string
56
	 */
57
	public function getBaseTitle() : string {
58
		return substr( $this->getTitle(), 0, strrpos( $this->getTitle(), '/' ) );
59
	}
60
61
	/**
62
	 * Get the content of this page
63
	 *
64
	 * @return string
65
	 */
66
	public function getContent() : string {
67
		if ( $this->content === null ) {
68
			$this->content = $this->controller->getPageContent( $this->title );
69
		}
70
		return $this->content;
71
	}
72
73
	/**
74
	 * Whether this page has enough opposing votes
75
	 *
76
	 * @return bool
77
	 */
78
	public function hasOpposition() : bool {
79
		$params = [
80
			'action' => 'query',
81
			'prop' => 'revisions',
82
			'titles' => $this->title,
83
			'rvprop' => 'content',
84
			'rvslots' => 'main',
85
			'rvsection' => 4
86
		];
87
		$res = RequestBase::newFromParams( $params )->execute();
88
		$page = reset( $res->query->pages );
89
		$content = $page->revisions[0]->slots->main->{ '*' };
90
		// Let's hope that this is good enough...
91
		$votes = substr_count( $content, "\n\# *(?![#*])" );
92
		return $votes >= 15;
93
	}
94
95
	/**
96
	 * Whether this page is a vote
97
	 *
98
	 * @return bool
99
	 */
100
	public function isVote() : bool {
101
		$sectionReg = '/<!-- SEZIONE DA UTILIZZARE PER/';
102
		return preg_match( $sectionReg, $this->getContent() ) === false;
103
	}
104
105
	/**
106
	 * Get the end time
107
	 *
108
	 * @return int
109
	 */
110
	public function getEndTimestamp() : int {
111
		if ( $this->isVote() ) {
112
			$matches = [];
113
			$reg = "!La votazione ha inizio il.+ e ha termine.+ '''([^']+)''' alle ore '''([^']+)'''!";
114
			preg_match( $reg, $this->getContent(), $matches );
115
			list( , $day, $hours ) = $matches;
116
			$day = preg_replace( '![^\d \w]!', '', $day );
117
			return WikiController::getTimestampFromLocalTime( $day . " alle " . $hours );
118
		} else {
119
			$created = $this->controller->getPageCreationTS( $this->title );
120
			return $created + 60 * 60 * 24 * 7;
121
		}
122
	}
123
124
	public function __toString() {
125
		return $this->getTitle();
126
	}
127
}
128