Localisation updates for core from Betawiki
[lhc/web/wiklou.git] / includes / UserRestriction.php
1 <?php
2
3 /**
4 * Object that represents a user restriction
5 */
6 class UserRestriction {
7 const PAGE = 'page';
8 const NAMESPACE = 'namespace';
9
10 private $mId, $mType, $mNamespace, $mPage, $mSubjectText, $mSubjectId,
11 $mBlockerId, $mBlockerText, $mReason, $mTimestamp, $mExpiry;
12
13 public static function newFromRow( $row ) {
14 if( !$row )
15 return null;
16
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;
24 } else {
25 throw new MWException( "Unknown user restriction type: {$row->ur_type}" );
26 }
27
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;
35 return $obj;
36 }
37
38 public static function fetchForUser( $user, $forWrite = false ) {
39 $dbr = wfGetDB( $forWrite ? DB_MASTER : DB_SLAVE );
40 if( is_int( $user ) )
41 $query = array( 'ur_user' => $user );
42 else
43 $query = array( 'ur_user_text' => $user );
44 $res = $dbr->select( 'user_restrictions', '*', $query, __METHOD__ );
45 $result = array();
46 foreach( $res as $row ) {
47 $result[] = self::newFromRow( $row );
48 }
49 return $result;
50 }
51
52 public static function newFromId( $id, $forWrite = false ) {
53 $dbr = wfGetDB( $forWrite ? DB_MASTER : DB_SLAVE );
54 if( !$id || !is_numeric( $id ) )
55 return null;
56 $res = $dbr->selectRow( 'user_restrictions', '*', array( 'ur_id' => $id ), __METHOD__ );
57 return self::newFromRow( $res );
58 }
59
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; }
82
83 public function isPage() {
84 return $this->mType == self::PAGE;
85 }
86 public function isNamespace() {
87 return $this->mType == self::NAMESPACE;
88 }
89
90 public function isExpired() {
91 return is_numeric( $this->mExpiry ) && $this->mExpiry < wfTimestampNow( TS_MW );
92 }
93
94 public function deleteIfExpired() {
95 if( $this->isExpired() ) {
96 $this->delete();
97 return true;
98 } else {
99 return false;
100 }
101 }
102
103 public function delete() {
104 $dbw = wfGetDB( DB_MASTER );
105 $dbw->delete( 'user_restrictions', array( 'ur_id' => $this->mId ), __METHOD__ );
106 return $dbw->affectedRows();
107 }
108
109 public static function purgeExpired() {
110 $dbw = wfGetDB( DB_MASTER );
111 $dbw->delete( 'user_restrictions', array( 'ur_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
112 }
113
114 public function commit() {
115 $dbw = wfGetDB( DB_MASTER );
116 $this->setId( $dbw->nextSequenceValue('user_restrictions_ur_id_val') );
117 $row = array(
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,
127 );
128 if( $this->isPage() ) {
129 $row['ur_page_namespace'] = $this->mPage->getNamespace();
130 $row['ur_page_title'] = $this->mPage->getDbKey();
131 }
132 if( $this->isNamespace() ) {
133 $row['ur_namespace'] = $this->mNamespace;
134 }
135 $dbw->insert( 'user_restrictions', $row, __METHOD__ );
136 }
137
138 public static function formatType( $type ) {
139 return wfMsg( 'userrestrictiontype-' . $type );
140 }
141
142 /**
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
146 */
147 public static function convertExpiry( $expiry ) {
148 if( !$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 )
154 return false;
155 else
156 return wfTimestamp( TS_MW, $unix );
157 }
158 }