Conditions | 36 |
Paths | > 20000 |
Total Lines | 197 |
Code Lines | 134 |
Lines | 38 |
Ratio | 19.29 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
41 | protected function run( ApiPageSet $resultPageSet = null ) { |
||
42 | $db = $this->getDB(); |
||
43 | $params = $this->extractRequestParams( false ); |
||
44 | |||
45 | $result = $this->getResult(); |
||
46 | |||
47 | $this->requireMaxOneParameter( $params, 'user', 'excludeuser' ); |
||
48 | |||
49 | // Namespace check is likely to be desired, but can't be done |
||
50 | // efficiently in SQL. |
||
51 | $miser_ns = null; |
||
52 | $needPageTable = false; |
||
53 | if ( $params['namespace'] !== null ) { |
||
54 | $params['namespace'] = array_unique( $params['namespace'] ); |
||
55 | sort( $params['namespace'] ); |
||
56 | if ( $params['namespace'] != MWNamespace::getValidNamespaces() ) { |
||
57 | $needPageTable = true; |
||
58 | View Code Duplication | if ( $this->getConfig()->get( 'MiserMode' ) ) { |
|
59 | $miser_ns = $params['namespace']; |
||
60 | } else { |
||
61 | $this->addWhere( [ 'page_namespace' => $params['namespace'] ] ); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | |||
66 | $this->addTables( 'revision' ); |
||
67 | if ( $resultPageSet === null ) { |
||
68 | $this->parseParameters( $params ); |
||
69 | $this->addTables( 'page' ); |
||
70 | $this->addJoinConds( |
||
71 | [ 'page' => [ 'INNER JOIN', [ 'rev_page = page_id' ] ] ] |
||
72 | ); |
||
73 | $this->addFields( Revision::selectFields() ); |
||
74 | $this->addFields( Revision::selectPageFields() ); |
||
75 | |||
76 | // Review this depeneding on the outcome of T113901 |
||
77 | $this->addOption( 'STRAIGHT_JOIN' ); |
||
78 | } else { |
||
79 | $this->limit = $this->getParameter( 'limit' ) ?: 10; |
||
80 | $this->addFields( [ 'rev_timestamp', 'rev_id' ] ); |
||
81 | if ( $params['generatetitles'] ) { |
||
82 | $this->addFields( [ 'rev_page' ] ); |
||
83 | } |
||
84 | |||
85 | if ( $needPageTable ) { |
||
86 | $this->addTables( 'page' ); |
||
87 | $this->addJoinConds( |
||
88 | [ 'page' => [ 'INNER JOIN', [ 'rev_page = page_id' ] ] ] |
||
89 | ); |
||
90 | $this->addFieldsIf( [ 'page_namespace' ], (bool)$miser_ns ); |
||
91 | |||
92 | // Review this depeneding on the outcome of T113901 |
||
93 | $this->addOption( 'STRAIGHT_JOIN' ); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | $dir = $params['dir']; |
||
98 | $this->addTimestampWhereRange( 'rev_timestamp', $dir, $params['start'], $params['end'] ); |
||
99 | |||
100 | View Code Duplication | if ( $this->fld_tags ) { |
|
101 | $this->addTables( 'tag_summary' ); |
||
102 | $this->addJoinConds( |
||
103 | [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ] |
||
104 | ); |
||
105 | $this->addFields( 'ts_tags' ); |
||
106 | } |
||
107 | |||
108 | if ( $this->fetchContent ) { |
||
109 | $this->addTables( 'text' ); |
||
110 | $this->addJoinConds( |
||
111 | [ 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ] ] |
||
112 | ); |
||
113 | $this->addFields( 'old_id' ); |
||
114 | $this->addFields( Revision::selectTextFields() ); |
||
115 | } |
||
116 | |||
117 | if ( $params['user'] !== null ) { |
||
118 | $id = User::idFromName( $params['user'] ); |
||
119 | if ( $id ) { |
||
|
|||
120 | $this->addWhereFld( 'rev_user', $id ); |
||
121 | } else { |
||
122 | $this->addWhereFld( 'rev_user_text', $params['user'] ); |
||
123 | } |
||
124 | View Code Duplication | } elseif ( $params['excludeuser'] !== null ) { |
|
125 | $id = User::idFromName( $params['excludeuser'] ); |
||
126 | if ( $id ) { |
||
127 | $this->addWhere( 'rev_user != ' . $id ); |
||
128 | } else { |
||
129 | $this->addWhere( 'rev_user_text != ' . $db->addQuotes( $params['excludeuser'] ) ); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | View Code Duplication | if ( $params['user'] !== null || $params['excludeuser'] !== null ) { |
|
134 | // Paranoia: avoid brute force searches (bug 17342) |
||
135 | if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) { |
||
136 | $bitmask = Revision::DELETED_USER; |
||
137 | } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
||
138 | $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED; |
||
139 | } else { |
||
140 | $bitmask = 0; |
||
141 | } |
||
142 | if ( $bitmask ) { |
||
143 | $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" ); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if ( $params['continue'] !== null ) { |
||
148 | $op = ( $dir == 'newer' ? '>' : '<' ); |
||
149 | $cont = explode( '|', $params['continue'] ); |
||
150 | $this->dieContinueUsageIf( count( $cont ) != 2 ); |
||
151 | $ts = $db->addQuotes( $db->timestamp( $cont[0] ) ); |
||
152 | $rev_id = (int)$cont[1]; |
||
153 | $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] ); |
||
154 | $this->addWhere( "rev_timestamp $op $ts OR " . |
||
155 | "(rev_timestamp = $ts AND " . |
||
156 | "rev_id $op= $rev_id)" ); |
||
157 | } |
||
158 | |||
159 | $this->addOption( 'LIMIT', $this->limit + 1 ); |
||
160 | |||
161 | $sort = ( $dir == 'newer' ? '' : ' DESC' ); |
||
162 | $orderby = []; |
||
163 | // Targeting index rev_timestamp, user_timestamp, or usertext_timestamp |
||
164 | // But 'user' is always constant for the latter two, so it doesn't matter here. |
||
165 | $orderby[] = "rev_timestamp $sort"; |
||
166 | $orderby[] = "rev_id $sort"; |
||
167 | $this->addOption( 'ORDER BY', $orderby ); |
||
168 | |||
169 | $hookData = []; |
||
170 | $res = $this->select( __METHOD__, [], $hookData ); |
||
171 | $pageMap = []; // Maps rev_page to array index |
||
172 | $count = 0; |
||
173 | $nextIndex = 0; |
||
174 | $generated = []; |
||
175 | foreach ( $res as $row ) { |
||
176 | if ( $count === 0 && $resultPageSet !== null ) { |
||
177 | // Set the non-continue since the list of all revisions is |
||
178 | // prone to having entries added at the start frequently. |
||
179 | $this->getContinuationManager()->addGeneratorNonContinueParam( |
||
180 | $this, 'continue', "$row->rev_timestamp|$row->rev_id" |
||
181 | ); |
||
182 | } |
||
183 | if ( ++$count > $this->limit ) { |
||
184 | // We've had enough |
||
185 | $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" ); |
||
186 | break; |
||
187 | } |
||
188 | |||
189 | // Miser mode namespace check |
||
190 | if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) { |
||
191 | continue; |
||
192 | } |
||
193 | |||
194 | if ( $resultPageSet !== null ) { |
||
195 | if ( $params['generatetitles'] ) { |
||
196 | $generated[$row->rev_page] = $row->rev_page; |
||
197 | } else { |
||
198 | $generated[] = $row->rev_id; |
||
199 | } |
||
200 | } else { |
||
201 | $revision = Revision::newFromRow( $row ); |
||
202 | $rev = $this->extractRevisionInfo( $revision, $row ); |
||
203 | |||
204 | if ( !isset( $pageMap[$row->rev_page] ) ) { |
||
205 | $index = $nextIndex++; |
||
206 | $pageMap[$row->rev_page] = $index; |
||
207 | $title = $revision->getTitle(); |
||
208 | $a = [ |
||
209 | 'pageid' => $title->getArticleID(), |
||
210 | 'revisions' => [ $rev ], |
||
211 | ]; |
||
212 | ApiResult::setIndexedTagName( $a['revisions'], 'rev' ); |
||
213 | ApiQueryBase::addTitleInfo( $a, $title ); |
||
214 | $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) && |
||
215 | $result->addValue( [ 'query', $this->getModuleName() ], $index, $a ); |
||
216 | View Code Duplication | } else { |
|
217 | $index = $pageMap[$row->rev_page]; |
||
218 | $fit = $this->processRow( $row, $rev, $hookData ) && |
||
219 | $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev ); |
||
220 | } |
||
221 | if ( !$fit ) { |
||
222 | $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" ); |
||
223 | break; |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | if ( $resultPageSet !== null ) { |
||
229 | if ( $params['generatetitles'] ) { |
||
230 | $resultPageSet->populateFromPageIDs( $generated ); |
||
231 | } else { |
||
232 | $resultPageSet->populateFromRevisionIDs( $generated ); |
||
233 | } |
||
234 | } else { |
||
235 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' ); |
||
236 | } |
||
237 | } |
||
238 | |||
296 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: