4 * Object that represents a user restriction
6 class UserRestriction
{
8 const NAMESPACE = 'namespace';
10 private $mId, $mType, $mNamespace, $mPage, $mSubjectText, $mSubjectId,
11 $mBlockerId, $mBlockerText, $mReason, $mTimestamp, $mExpiry;
13 public static function newFromRow( $row ) {
17 $obj = new UserRestriction();
18 $obj->mId
= $row->ur_id
;
19 $obj->mType
= $row->ur_type
;
20 if( $obj->mType
== self
::PAGE
) {
21 $obj->mPage
= Title
::makeTitle( $row->ur_page_namespace
, $row->ur_page_title
);
22 } elseif( $obj->mType
== self
::NAMESPACE ) {
23 $obj->mNamespace
= $row->ur_namespace
;
25 throw new MWException( "Unknown user restriction type: {$row->ur_type}" );
28 $obj->mSubjectId
= $row->ur_user
;
29 $obj->mSubjectText
= $row->ur_user_text
;
30 $obj->mBlockerId
= $row->ur_by
;
31 $obj->mBlockerText
= $row->ur_by_text
;
32 $obj->mReason
= $row->ur_reason
;
33 $obj->mTimestamp
= wfTimestamp( TS_MW
, $row->ur_timestamp
);
34 $obj->mExpiry
= $row->ur_expiry
;
38 public static function fetchForUser( $user, $forWrite = false ) {
39 $dbr = wfGetDB( $forWrite ? DB_MASTER
: DB_SLAVE
);
41 $query = array( 'ur_user' => $user );
43 $query = array( 'ur_user_text' => $user );
44 $res = $dbr->select( 'user_restrictions', '*', $query, __METHOD__
);
46 foreach( $res as $row ) {
47 $result[] = self
::newFromRow( $row );
52 public static function newFromId( $id, $forWrite = false ) {
53 $dbr = wfGetDB( $forWrite ? DB_MASTER
: DB_SLAVE
);
54 if( !$id ||
!is_numeric( $id ) )
56 $res = $dbr->selectRow( 'user_restrictions', '*', array( 'ur_id' => $id ), __METHOD__
);
57 return self
::newFromRow( $res );
60 public function getId() { return $this->mId
; }
61 public function setId( $v ) { $this->mId
= $v; }
62 public function getType() { return $this->mType
; }
63 public function setType( $v ) { $this->mType
= $v; }
64 public function getNamespace() { return $this->mNamespace
; }
65 public function setNamespace( $v ) { $this->mNamespace
= $v; }
66 public function getPage() { return $this->mPage
; }
67 public function setPage( $v ) { $this->mPage
= $v; }
68 public function getSubjectId() { return $this->mSubjectId
; }
69 public function setSubjectId( $v ) { $this->mSubjectId
= $v; }
70 public function getSubjectText() { return $this->mSubjectText
; }
71 public function setSubjectText( $v ) { $this->mSubjectText
= $v; }
72 public function getBlockerId() { return $this->mBlockerId
; }
73 public function setBlockerId( $v ) { $this->mBlockerId
= $v; }
74 public function getBlockerText() { return $this->mBlockerText
; }
75 public function setBlockerText( $v ) { $this->mBlockerText
= $v; }
76 public function getReason() { return $this->mReason
; }
77 public function setReason( $v ) { $this->mReason
= $v; }
78 public function getTimestamp() { return $this->mTimestamp
; }
79 public function setTimestamp( $v ) { $this->mTimestamp
= $v; }
80 public function getExpiry() { return $this->mExpiry
; }
81 public function setExpiry( $v ) { $this->mExpiry
= $v; }
83 public function isPage() {
84 return $this->mType
== self
::PAGE
;
86 public function isNamespace() {
87 return $this->mType
== self
::NAMESPACE;
90 public function isExpired() {
91 return is_numeric( $this->mExpiry
) && $this->mExpiry
< wfTimestampNow( TS_MW
);
94 public function deleteIfExpired() {
95 if( $this->isExpired() ) {
103 public function delete() {
104 $dbw = wfGetDB( DB_MASTER
);
105 $dbw->delete( 'user_restrictions', array( 'ur_id' => $this->mId
), __METHOD__
);
106 return $dbw->affectedRows();
109 public static function purgeExpired() {
110 $dbw = wfGetDB( DB_MASTER
);
111 $dbw->delete( 'user_restrictions', array( 'ur_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__
);
114 public function commit() {
115 $dbw = wfGetDB( DB_MASTER
);
116 $this->setId( $dbw->nextSequenceValue('user_restrictions_ur_id_val') );
118 'ur_id' => $this->mId
,
119 'ur_type' => $this->mType
,
120 'ur_user' => $this->mSubjectId
,
121 'ur_user_text' => $this->mSubjectText
,
122 'ur_by' => $this->mBlockerId
,
123 'ur_by_text' => $this->mBlockerText
,
124 'ur_reason' => $this->mReason
,
125 'ur_timestamp' => $dbw->timestamp( $this->mTimestamp
),
126 'ur_expiry' => $this->mExpiry
,
128 if( $this->isPage() ) {
129 $row['ur_page_namespace'] = $this->mPage
->getNamespace();
130 $row['ur_page_title'] = $this->mPage
->getDbKey();
132 if( $this->isNamespace() ) {
133 $row['ur_namespace'] = $this->mNamespace
;
135 $dbw->insert( 'user_restrictions', $row, __METHOD__
);
138 public static function formatType( $type ) {
139 return wfMsg( 'userrestrictiontype-' . $type );
143 * Converts expiry which user input to the internal representation.
144 * Returns false if invalid expiry is set, Block::infinity() on empty value,
145 * Block::infinity() on infinity or 14-symbol timestamp
147 public static function convertExpiry( $expiry ) {
149 return Block
::infinity();
150 if( in_array( $expiry, array( 'infinite', 'infinity', 'indefinite' ) ) )
151 return Block
::infinity();
152 $unix = @strtotime
( $expiry );
153 if( !$unix ||
$unix === -1 )
156 return wfTimestamp( TS_MW
, $unix );