r88682 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88681‎ | r88682 | r88683 >
Date:20:48, 23 May 2011
Author:reedy
Status:ok
Tags:
Comment:
More code improvements

Less hard coding of SQL
Modified paths:
  • /trunk/extensions/CheckUser/CheckUser_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CheckUser/CheckUser_body.php
@@ -121,7 +121,7 @@
122122 protected function preCacheMessages() {
123123 // Precache various messages
124124 if ( !isset( $this->message ) ) {
125 - foreach ( explode( ' ', 'diff hist minoreditletter newpageletter blocklink log' ) as $msg ) {
 125+ foreach ( array( 'diff', 'hist', 'minoreditletter', 'newpageletter', 'blocklink', 'log' ) as $msg ) {
126126 $this->message[$msg] = wfMsgExt( $msg, array( 'escape' ) );
127127 }
128128 }
@@ -560,7 +560,6 @@
561561
562562 $ip_conds = $dbr->makeList( $ip_conds, LIST_AND );
563563 $time_conds = $this->getTimeConds( $period );
564 - $cu_changes = $dbr->tableName( 'cu_changes' );
565564 # Ordered in descent by timestamp. Can cause large filesorts on range scans.
566565 # Check how many rows will need sorting ahead of time to see if this is too big.
567566 # Also, if we only show 5000, too many will be ignored as well.
@@ -587,7 +586,7 @@
588587 # See what is best to do after testing the waters...
589588 if ( isset( $rangecount ) && $rangecount > 5000 ) {
590589 $ret = $dbr->select( 'cu_changes',
591 - array( 'cuc_ip_hex', 'COUNT(*) AS count', 'MIN(cuc_timestamp) AS first', 'MAX(cuc_timestamp AS last' ),
 590+ array( 'cuc_ip_hex', 'COUNT(*) AS count', 'MIN(cuc_timestamp) AS first', 'MAX(cuc_timestamp) AS last' ),
592591 array( $ip_conds, $time_conds ),
593592 __METHOD___,
594593 array(
@@ -639,9 +638,10 @@
640639
641640 $ret = $dbr->select(
642641 'cu_changes',
643 - array( 'cuc_namespace','cuc_title', 'cuc_user', 'cuc_user_text', 'cuc_comment', 'cuc_actiontext',
644 - 'cuc_timestamp', 'cuc_minor', 'cuc_page_id', 'cuc_type', 'cuc_this_oldid',
645 - 'cuc_last_oldid', 'cuc_ip', 'cuc_xff','cuc_agent'
 642+ array(
 643+ 'cuc_namespace','cuc_title', 'cuc_user', 'cuc_user_text', 'cuc_comment', 'cuc_actiontext',
 644+ 'cuc_timestamp', 'cuc_minor', 'cuc_page_id', 'cuc_type', 'cuc_this_oldid',
 645+ 'cuc_last_oldid', 'cuc_ip', 'cuc_xff','cuc_agent'
646646 ),
647647 array( $ip_conds, $time_conds ),
648648 __METHOD__,
@@ -720,7 +720,6 @@
721721 $dbr = wfGetDB( DB_SLAVE );
722722 $user_cond = "cuc_user = '$user_id'";
723723 $time_conds = $this->getTimeConds( $period );
724 - $cu_changes = $dbr->tableName( '' );
725724 # Ordered in descent by timestamp. Causes large filesorts if there are many edits.
726725 # Check how many rows will need sorting ahead of time to see if this is too big.
727726 # If it is, sort by IP,time to avoid the filesort.
@@ -850,7 +849,6 @@
851850
852851 $ip_conds = $dbr->makeList( $ip_conds, LIST_AND );
853852 $time_conds = $this->getTimeConds( $period );
854 - $cu_changes = $dbr->tableName( 'cu_changes' );
855853 $index = $xfor ? 'cuc_xff_hex_time' : 'cuc_ip_hex_time';
856854 # Ordered in descent by timestamp. Can cause large filesorts on range scans.
857855 # Check how many rows will need sorting ahead of time to see if this is too big.
@@ -874,12 +872,21 @@
875873 }
876874 // Are there too many edits?
877875 if ( isset( $rangecount ) && $rangecount > 10000 ) {
878 - $use_index = $dbr->useIndexClause( $index );
879 - $sql = "SELECT cuc_ip_hex, COUNT(*) AS count,
880 - MIN(cuc_timestamp) AS first, MAX(cuc_timestamp) AS last
881 - FROM $cu_changes $use_index WHERE $ip_conds AND $time_conds
882 - GROUP BY cuc_ip_hex ORDER BY cuc_ip_hex LIMIT 5001";
883 - $ret = $dbr->query( $sql, __METHOD__ );
 876+ $ret = $dbr->select(
 877+ 'cu_changes',
 878+ array(
 879+ 'cuc_ip_hex', 'COUNT(*) AS count',
 880+ 'MIN(cuc_timestamp) AS first', 'MAX(cuc_timestamp) AS last'
 881+ ),
 882+ array( $ip_conds, $time_conds ),
 883+ __METHOD__,
 884+ array(
 885+ 'GROUP BY' => 'cuc_ip_hex',
 886+ 'ORDER BY' => 'cuc_ip_hex',
 887+ 'LIMIT' => 5001,
 888+ 'USE INDEX' => $index,
 889+ )
 890+ );
884891 # List out each IP that has edits
885892 $s = '<h5>' . wfMsg( 'checkuser-too-many' ) . '</h5>';
886893 $s .= '<ol>';
@@ -920,18 +927,27 @@
921928
922929 global $wgMemc;
923930 # OK, do the real query...
924 - $use_index = $dbr->useIndexClause( $index );
925 - $sql = "SELECT cuc_user_text, cuc_timestamp, cuc_user, cuc_ip, cuc_agent, cuc_xff
926 - FROM $cu_changes $use_index WHERE $ip_conds AND $time_conds
927 - ORDER BY cuc_timestamp DESC LIMIT 10000";
928 - $ret = $dbr->query( $sql, __METHOD__ );
929931
 932+ $ret = $dbr->select(
 933+ 'cu_changes',
 934+ array(
 935+ 'cuc_user_text', 'cuc_timestamp', 'cuc_user', 'cuc_ip', 'cuc_agent', 'cuc_xff'
 936+ ),
 937+ array( $ip_conds, $time_conds ),
 938+ __METHOD__,
 939+ array(
 940+ 'ORDER BY' => 'cuc_timestamp DESC',
 941+ 'LIMIT' => 10000,
 942+ 'USE INDEX' => $index,
 943+ )
 944+ );
 945+
930946 $users_first = $users_last = $users_edits = $users_ids = array();
931947 if ( !$dbr->numRows( $ret ) ) {
932948 $s = $this->noMatchesMessage( $ip, !$xfor ) . "\n";
933949 } else {
934950 global $wgAuth;
935 - while ( ( $row = $dbr->fetchObject( $ret ) ) != false ) {
 951+ foreach( $ret as $row ) {
936952 if ( !array_key_exists( $row->cuc_user_text, $users_edits ) ) {
937953 $users_last[$row->cuc_user_text] = $row->cuc_timestamp;
938954 $users_edits[$row->cuc_user_text] = 0;

Status & tagging log