r14924 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r14923‎ | r14924 | r14925 >
Date:06:17, 23 June 2006
Author:tstarling
Status:old
Tags:
Comment:
SpecialContributions: fixed style, fixed sloppy handling of empty query results, fixed inefficient code.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/SpecialContributions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialContributions.php
@@ -5,124 +5,139 @@
66 */
77
88 /** @package MediaWiki */
9 -class contribs_finder {
 9+class ContribsFinder {
1010 var $username, $offset, $limit, $namespace;
1111 var $dbr;
1212
13 - function contribs_finder($username) {
 13+ function ContribsFinder( $username ) {
1414 $this->username = $username;
1515 $this->namespace = false;
16 - $this->dbr =& wfGetDB(DB_SLAVE);
 16+ $this->dbr =& wfGetDB( DB_SLAVE );
1717 }
1818
19 - function set_namespace($ns) {
 19+ function setNamespace( $ns ) {
2020 $this->namespace = $ns;
2121 }
2222
23 - function set_limit($limit) {
 23+ function setLimit( $limit ) {
2424 $this->limit = $limit;
2525 }
2626
27 - function set_offset($offset) {
 27+ function setOffset( $offset ) {
2828 $this->offset = $offset;
2929 }
3030
31 - function get_edit_limit($dir) {
32 - list($index, $usercond) = $this->get_user_cond();
33 - $nscond = $this->get_namespace_cond();
34 - $use_index = $this->dbr->useIndexClause($index);
35 - extract($this->dbr->tableNames('revision', 'page'));
 31+ function getEditLimit( $dir ) {
 32+ list( $index, $usercond ) = $this->getUserCond();
 33+ $nscond = $this->getNamespaceCond();
 34+ $use_index = $this->dbr->useIndexClause( $index );
 35+ extract( $this->dbr->tableNames( 'revision', 'page' ) );
3636 $sql = "SELECT rev_timestamp " .
3737 " FROM $page,$revision $use_index " .
3838 " WHERE rev_page=page_id AND $usercond $nscond" .
3939 " ORDER BY rev_timestamp $dir LIMIT 1";
4040
41 - $res = $this->dbr->query($sql, "contribs_finder::get_edit_limit");
42 - while ($o = $this->dbr->fetchObject($res))
43 - $row = $o;
44 - return $row->rev_timestamp;
 41+ $res = $this->dbr->query( $sql, __METHOD__ );
 42+ $row = $this->dbr->fetchObject( $res );
 43+ if ( $row ) {
 44+ return $row->rev_timestamp;
 45+ } else {
 46+ return false;
 47+ }
4548 }
4649
47 - function get_edit_limits() {
 50+ function getEditLimits() {
4851 return array(
49 - $this->get_edit_limit("ASC"),
50 - $this->get_edit_limit("DESC")
 52+ $this->getEditLimit( "ASC" ),
 53+ $this->getEditLimit( "DESC" )
5154 );
5255 }
5356
54 - function get_user_cond() {
 57+ function getUserCond() {
5558 $condition = '';
5659
57 - if ($this->username == 'newbies') {
58 - $max = $this->dbr->selectField('user', 'max(user_id)', false, 'make_sql');
 60+ if ( $this->username == 'newbies' ) {
 61+ $max = $this->dbr->selectField( 'user', 'max(user_id)', false, 'make_sql' );
5962 $condition = '>' . (int)($max - $max / 100);
6063 }
6164
62 - if ($condition == '') {
63 - $condition = ' rev_user_text=' . $this->dbr->addQuotes($this->username);
 65+ if ( $condition == '' ) {
 66+ $condition = ' rev_user_text=' . $this->dbr->addQuotes( $this->username );
6467 $index = 'usertext_timestamp';
6568 } else {
6669 $condition = ' rev_user '.$condition ;
6770 $index = 'user_timestamp';
6871 }
69 - return array($index, $condition);
 72+ return array( $index, $condition );
7073 }
7174
72 - function get_namespace_cond() {
73 - if ($this->namespace !== false)
 75+ function getNamespaceCond() {
 76+ if ( $this->namespace !== false )
7477 return ' AND page_namespace = ' . (int)$this->namespace;
7578 return '';
7679 }
7780
78 - function get_previous_offset_for_paging() {
79 - list($index, $usercond) = $this->get_user_cond();
80 - $nscond = $this->get_namespace_cond();
 81+ function getPreviousOffsetForPaging() {
 82+ list( $index, $usercond ) = $this->getUserCond();
 83+ $nscond = $this->getNamespace_cond();
8184
82 - $use_index = $this->dbr->useIndexClause($index);
83 - extract($this->dbr->tableNames('page', 'revision'));
 85+ $use_index = $this->dbr->useIndexClause( $index );
 86+ extract( $this->dbr->tableNames( 'page', 'revision' ) );
8487
8588 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
8689 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " .
8790 $usercond . $nscond;
8891 $sql .= " ORDER BY rev_timestamp ASC";
89 - $sql = $this->dbr->limitResult($sql, $this->limit, 0);
90 - $res = $this->dbr->query($sql);
91 - $rows = array();
92 - while ($obj = $this->dbr->fetchObject($res))
93 - $rows[] = $obj;
94 - $this->dbr->freeResult($res);
95 - return $rows[count($rows) - 1]->rev_timestamp;
 92+ $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
 93+ $res = $this->dbr->query( $sql );
 94+
 95+ $numRows = $this->dbr->numRows( $res );
 96+ if ( $numRows ) {
 97+ $this->dbr->dataSeek( $res, $numRows - 1 );
 98+ $row = $this->dbr->fetchObject( $res );
 99+ $offset = $row->rev_timestamp;
 100+ } else {
 101+ $offset = false;
 102+ }
 103+ $this->dbr->freeResult( $res );
 104+ return $offset;
96105 }
97106
98 - function get_first_offset_for_paging() {
99 - list($index, $usercond) = $this->get_user_cond();
100 - $use_index = $this->dbr->useIndexClause($index);
101 - extract($this->dbr->tableNames('page', 'revision'));
102 - $nscond = $this->get_namespace_cond();
 107+ function getFirstOffsetForPaging() {
 108+ list( $index, $usercond ) = $this->getUserCond();
 109+ $use_index = $this->dbr->useIndexClause( $index );
 110+ extract( $this->dbr->tableNames( 'page', 'revision' ) );
 111+ $nscond = $this->getNamespaceCond();
103112 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
104113 "WHERE page_id = rev_page AND " .
105114 $usercond . $nscond;
106115 $sql .= " ORDER BY rev_timestamp ASC";
107 - $sql = $this->dbr->limitResult($sql, $this->limit, 0);
108 - $res = $this->dbr->query($sql);
109 - $rows = array();
110 - while ($obj = $this->dbr->fetchObject($res))
111 - $rows[] = $obj;
112 - $this->dbr->freeResult($res);
113 - return $rows[count($rows) - 1]->rev_timestamp;
 116+ $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
 117+ $res = $this->dbr->query( $sql );
 118+
 119+ $numRows = $this->dbr->numRows( $res );
 120+ if ( $numRows ) {
 121+ $this->dbr->dataSeek( $res, $numRows - 1 );
 122+ $row = $this->dbr->fetchObject( $res );
 123+ $offset = $row->rev_timestamp;
 124+ } else {
 125+ $offset = false;
 126+ }
 127+ $this->dbr->freeResult( $res );
 128+ return $offset;
114129 }
115130
116 - /* private */ function make_sql() {
 131+ /* private */ function makeSql() {
117132 $userCond = $condition = $index = $offsetQuery = '';
118133
119 - extract($this->dbr->tableNames('page', 'revision'));
120 - list($index, $userCond) = $this->get_user_cond();
 134+ extract( $this->dbr->tableNames( 'page', 'revision' ) );
 135+ list( $index, $userCond ) = $this->getUserCond();
121136
122 - if ($this->offset)
 137+ if ( $this->offset )
123138 $offsetQuery = "AND rev_timestamp <= '{$this->offset}'";
124139
125 - $nscond = $this->get_namespace_cond();
126 - $use_index = $this->dbr->useIndexClause($index);
 140+ $nscond = $this->getNamespaceCond();
 141+ $use_index = $this->dbr->useIndexClause( $index );
127142 $sql = "SELECT
128143 page_namespace,page_title,page_is_new,page_latest,
129144 rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text,
@@ -130,16 +145,16 @@
131146 FROM $page,$revision $use_index
132147 WHERE page_id=rev_page AND $userCond $nscond $offsetQuery
133148 ORDER BY rev_timestamp DESC";
134 - $sql = $this->dbr->limitResult($sql, $this->limit, 0);
 149+ $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
135150 return $sql;
136151 }
137152
138153 function find() {
139154 $contribs = array();
140 - $res = $this->dbr->query($this->make_sql(), 'contribs_finder::find');
141 - while ($c = $this->dbr->fetchObject($res))
 155+ $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
 156+ while ( $c = $this->dbr->fetchObject( $res ) )
142157 $contribs[] = $c;
143 - $this->dbr->freeResult($res);
 158+ $this->dbr->freeResult( $res );
144159 return $contribs;
145160 }
146161 };
@@ -155,14 +170,14 @@
156171 global $wgUser, $wgOut, $wgLang, $wgRequest;
157172 $fname = 'wfSpecialContributions';
158173
159 - $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
160 - if (!strlen($target)) {
161 - $wgOut->showErrorPage('notargettitle', 'notargettext');
 174+ $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
 175+ if ( !strlen( $target ) ) {
 176+ $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
162177 return;
163178 }
164179
165180 $nt = Title::newFromURL( $target );
166 - if (!$nt) {
 181+ if ( !$nt ) {
167182 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
168183 return;
169184 }
@@ -170,112 +185,118 @@
171186 $options = array();
172187
173188 list( $options['limit'], $options['offset']) = wfCheckLimits();
174 - $options['offset'] = $wgRequest->getVal('offset');
 189+ $options['offset'] = $wgRequest->getVal( 'offset' );
175190 /* Offset must be an integral. */
176 - if (!strlen($options['offset']) || !preg_match('/^[0-9]+$/', $options['offset']))
 191+ if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
177192 $options['offset'] = '';
178193
179 - $title = Title::makeTitle(NS_SPECIAL, 'Contributions');
 194+ $title = Title::makeTitle( NS_SPECIAL, 'Contributions' );
180195 $options['target'] = $target;
181196
182 - $nt =& Title::makeTitle(NS_USER, $nt->getDBkey());
183 - $finder = new contribs_finder(($target == 'newbies') ? 'newbies' : $nt->getText());
184 - $finder->set_limit($options['limit']);
185 - $finder->set_offset($options['offset']);
 197+ $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
 198+ $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
 199+ $finder->setLimit( $options['limit'] );
 200+ $finder->setOffset( $options['offset'] );
186201
187 - if (($ns = $wgRequest->getVal('namespace', null)) !== null && $ns !== '') {
 202+ if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
188203 #$options['namespace'] = intval( $ns );
189 - $finder->set_namespace($options['namespace']);
 204+ $finder->setNamespace( $options['namespace'] );
190205 } else {
191206 $options['namespace'] = '';
192207 }
193208
194 - if ($wgUser->isAllowed('rollback') && $wgRequest->getBool( 'bot' )) {
 209+ if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
195210 $options['bot'] = '1';
196211 }
197212
198 - if ($wgRequest->getText('go') == 'prev') {
199 - $options['offset'] = $finder->get_previous_offset_for_paging();
200 - $prevurl = $title->getLocalURL(wfArrayToCGI( $options ));
201 - $wgOut->redirect($prevurl);
202 - return;
 213+ if ( $wgRequest->getText( 'go' ) == 'prev' ) {
 214+ $offset = $finder->getPreviousOffsetForPaging();
 215+ if ( $offset !== false ) {
 216+ $options['offset'] = $offset;
 217+ $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
 218+ $wgOut->redirect( $prevurl );
 219+ return;
 220+ }
203221 }
204222
205 - if ($wgRequest->getText('go') == 'first' && $target != 'newbies') {
206 - $options['offset'] = $finder->get_first_offset_for_paging();
207 - $prevurl = $title->getLocalURL(wfArrayToCGI( $options ));
208 - $wgOut->redirect($prevurl);
209 - return;
 223+ if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
 224+ $offset = $finder->getFirstOffsetForPaging();
 225+ if ( $offset !== false ) {
 226+ $options['offset'] = $offset;
 227+ $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
 228+ $wgOut->redirect( $prevurl );
 229+ return;
 230+ }
210231 }
211232
212 - if ($target == 'newbies') {
 233+ if ( $target == 'newbies' ) {
213234 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
214235 } else {
215 - $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub($nt) ) );
 236+ $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
216237 }
217238
218 - $id = User::idFromName($nt->getText());
219 - wfRunHooks('SpecialContributionsBeforeMainOutput', $id );
 239+ $id = User::idFromName( $nt->getText() );
 240+ wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
220241
221 - $wgOut->addHTML(contributionsForm($options));
 242+ $wgOut->addHTML( contributionsForm( $options) );
222243
223244 $contribs = $finder->find();
224245
225 - if (count($contribs) == 0) {
 246+ if ( count( $contribs ) == 0) {
226247 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
227248 return;
228249 }
229250
230 - list($early, $late) = $finder->get_edit_limits();
231 - $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0;
232 - $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp);
233 - $atend = (!count($contribs) || $early == $lastts);
 251+ list( $early, $late ) = $finder->getEditLimits();
 252+ $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
 253+ $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
 254+ $atend = ( !count( $contribs ) || $early == $lastts );
234255
235256 // These four are defaults
236 - $newestlink = wfMsgHtml('sp-contributions-newest');
237 - $oldestlink = wfMsgHtml('sp-contributions-oldest');
238 - $newerlink = wfMsgHtml('sp-contributions-newer', $options['limit']);
239 - $olderlink = wfMsgHtml('sp-contributions-older', $options['limit']);
 257+ $newestlink = wfMsgHtml( 'sp-contributions-newest' );
 258+ $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
 259+ $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
 260+ $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
240261
241 - if (!$atstart) {
242 - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('offset' => ''), $options));
 262+ if ( !$atstart ) {
 263+ $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
243264 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
244 - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('go' => 'prev'), $options));
 265+ $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
245266 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
246267 }
247268
248 - if (!$atend) {
249 - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('go' => 'first'), $options));
 269+ if ( !$atend ) {
 270+ $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
250271 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
251 - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('offset' => $lastts), $options));
 272+ $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
252273 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
253274 }
254275
255 - if ($target == 'newbies') {
 276+ if ( $target == 'newbies' ) {
256277 $firstlast ="($newestlink)";
257278 } else {
258279 $firstlast = "($newestlink | $oldestlink)";
259280 }
260281
261282 $urls = array();
262 - foreach (array(20, 50, 100, 250, 500) as $num) {
263 - $stuff = $title->escapeLocalURL(wfArrayToCGI(array('limit' => $num), $options));
264 - $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum($num)."</a>";
 283+ foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
 284+ $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
 285+ $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
265286 }
266 - $bits = implode($urls, ' | ');
 287+ $bits = implode( $urls, ' | ' );
267288
268 - $prevnextbits = $firstlast .' '. wfMsgHtml('viewprevnext', $newerlink, $olderlink, $bits);
 289+ $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
269290
270 - $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
 291+ $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
271292
272293 $wgOut->addHTML( "<ul>\n" );
273294
274295 $sk = $wgUser->getSkin();
275 - foreach ($contribs as $contrib)
276 - $wgOut->addHTML(ucListEdit($sk, $contrib));
 296+ foreach ( $contribs as $contrib )
 297+ $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
277298
278299 $wgOut->addHTML( "</ul>\n" );
279 - $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
 300+ $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
280301 }
281302
282303 /**
@@ -286,7 +307,7 @@
287308 global $wgSysopUserBans, $wgLang, $wgUser;
288309
289310 $sk = $wgUser->getSkin();
290 - $id = User::idFromName($nt->getText());
 311+ $id = User::idFromName( $nt->getText() );
291312
292313 if ( 0 == $id ) {
293314 $ul = $nt->getText();
@@ -323,17 +344,17 @@
324345 $f = "<form method='get' action=\"$wgScript\">\n";
325346 foreach ( $options as $name => $value ) {
326347 if( $name === 'namespace') continue;
327 - $f .= "\t" . wfElement('input', array(
 348+ $f .= "\t" . wfElement( 'input', array(
328349 'name' => $name,
329350 'type' => 'hidden',
330 - 'value' => $value)) . "\n";
 351+ 'value' => $value ) ) . "\n";
331352 }
332353
333 - $f .= '<p>' . wfMsgHtml('namespace') . ' ' .
 354+ $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
334355 HTMLnamespaceselector( $options['namespace'], '' ) .
335 - wfElement('input', array(
 356+ wfElement( 'input', array(
336357 'type' => 'submit',
337 - 'value' => wfMsg('allpagessubmit'))
 358+ 'value' => wfMsg( 'allpagessubmit' ) )
338359 ) .
339360 "</p></form>\n";
340361
@@ -381,7 +402,7 @@
382403 $difftext .= $messages['newarticle'];
383404 }
384405
385 - if( $wgUser->isAllowed('rollback') ) {
 406+ if( $wgUser->isAllowed( 'rollback' ) ) {
386407 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
387408 $extraRollback .= '&token=' . urlencode(
388409 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
@@ -399,7 +420,7 @@
400421 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
401422
402423 $comment = $sk->revComment( $rev );
403 - $d = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true );
 424+ $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
404425
405426 if( $rev->isDeleted( Revision::MW_REV_DELETED_TEXT ) ) {
406427 $d = '<span class="history-deleted">' . $d . '</span>';
Index: trunk/phase3/includes/AutoLoader.php
@@ -150,7 +150,7 @@
151151 'BrokenRedirectsPage' => 'SpecialBrokenRedirects.php',
152152 'CategoriesPage' => 'SpecialCategories.php',
153153 'EmailConfirmation' => 'SpecialConfirmemail.php',
154 - 'contribs_finder' => 'SpecialContributions.php',
 154+ 'ContribsFinder' => 'SpecialContributions.php',
155155 'DeadendPagesPage' => 'SpecialDeadendpages.php',
156156 'DisambiguationsPage' => 'SpecialDisambiguations.php',
157157 'DoubleRedirectsPage' => 'SpecialDoubleRedirects.php',

Status & tagging log