r61818 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61817‎ | r61818 | r61819 >
Date:21:11, 1 February 2010
Author:pdhanda
Status:resolved (Comments)
Tags:
Comment:
Added a count display. Also the path is now sticky.
Modified paths:
  • /trunk/extensions/CodeReview/ui/CodeRevisionAuthorView.php (modified) (history)
  • /trunk/extensions/CodeReview/ui/CodeRevisionListView.php (modified) (history)
  • /trunk/extensions/CodeReview/ui/CodeRevisionStatusView.php (modified) (history)
  • /trunk/extensions/CodeReview/ui/SpecialCode.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CodeReview/ui/CodeRevisionAuthorView.php
@@ -43,6 +43,10 @@
4444
4545 parent::execute();
4646 }
 47+
 48+ function getSpecializedWhereClause($dbr) {
 49+ return ' AND cr_author = ' . $dbr->addQuotes($this->mAuthor);
 50+ }
4751
4852 }
4953
Index: trunk/extensions/CodeReview/ui/SpecialCode.php
@@ -20,13 +20,13 @@
2121 $wgOut->addStyle( "$wgScriptPath/extensions/CodeReview/codereview.css?$wgCodeReviewStyleVersion" );
2222 # Remove stray slashes
2323 $subpage = preg_replace( '/\/$/', '', $subpage );
24 -
2524 if ( $subpage == '' ) {
2625 $view = new CodeRepoListView();
2726 } else {
2827 $params = explode( '/', $subpage );
2928 switch( count( $params ) ) {
3029 case 1:
 30+ echo "WHY ARE WE HERE ";
3131 $view = new CodeRevisionListView( $params[0] );
3232 break;
3333 case 2:
@@ -119,7 +119,7 @@
120120 return $wgRequest->wasPosted()
121121 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) )
122122 && $wgUser->isAllowed( $permission );
123 - }
 123+ }
124124
125125 abstract function execute();
126126
@@ -131,10 +131,10 @@
132132 return $this->mRepo->authorWikiUser( $author );
133133 }
134134
135 - function authorLink( $author ) {
 135+ function authorLink( $author, $extraParams=array() ) {
136136 $repo = $this->mRepo->getName();
137 - $special = SpecialPage::getTitleFor( 'Code', "$repo/author/$author" );
138 - return $this->mSkin->link( $special, htmlspecialchars( $author ) );
 137+ $special = SpecialPage::getTitleFor( 'Code', "$repo/author/$author" );
 138+ return $this->mSkin->link( $special, htmlspecialchars( $author ), array(), $extraParams);
139139 }
140140
141141 function statusDesc( $status ) {
Index: trunk/extensions/CodeReview/ui/CodeRevisionListView.php
@@ -28,8 +28,18 @@
2929 $this->doBatchChange();
3030 return;
3131 }
32 -
 32+
3333 $this->showForm();
 34+
 35+ //get the total count across all pages
 36+ $dbr =& wfGetDB(DB_SLAVE);
 37+ $revCountRes = $this->getRevCountQuery($dbr);
 38+ $revCount = 0;
 39+ if ($dbr->numRows($revCountRes) > 0){
 40+ $row = $dbr->fetchObject($revCountRes);
 41+ $revCount = $row->rev_count;
 42+ }
 43+
3444 $pager = $this->getPager();
3545
3646 // Build batch change interface as needed
@@ -37,11 +47,15 @@
3848 $wgUser->isAllowed( 'codereview-add-tag' );
3949
4050 $wgOut->addHTML(
 51+ '<table><tr><td>' .
4152 $pager->getNavigationBar() .
4253 $pager->getLimitForm() .
 54+ '</td><td style="padding-left: 2em;">' .
 55+ '&nbsp;Total number of results: <span style="font-weight: bold;">' . $revCount . '</span>' .
 56+ '</td></tr></table>' .
4357 Xml::openElement( 'form',
4458 array( 'action' => $pager->getTitle()->getLocalURL(), 'method' => 'post' )
45 - ) .
 59+ ) .
4660 $pager->getBody() .
4761 $pager->getNavigationBar() .
4862 ( $this->batchForm ? $this->buildBatchInterface( $pager ) : "" ) .
@@ -146,6 +160,33 @@
147161 function getPager() {
148162 return new SvnRevTablePager( $this );
149163 }
 164+
 165+ function getRevCountQuery($dbr) {
 166+ $sql = 'SELECT COUNT(cr_id) AS rev_count' .
 167+ ' FROM ' . $dbr->tableName('code_rev') . ' ';
 168+ // count if code_rev where path matches
 169+ if ( $this->mPath ) {
 170+ $sql .= ' WHERE cr_id in' .
 171+ ' (SELECT cp_rev_id from code_paths' .
 172+ ' WHERE' .
 173+ ' cp_repo_id = ' . $this->mRepo->getId(). ' AND' .
 174+ ' cp_path LIKE ' . $dbr->addQuotes( $this->mPath . '%') . ' AND' .
 175+ // performance
 176+ ' cp_rev_id > ' . ($this->mRepo->getLastStoredRev() - 20000) .
 177+ $this->getSpecializedWhereClause($dbr) .
 178+ ')';
 179+ // No path; count of code_rev
 180+ } else {
 181+ $sql .= ' WHERE cr_repo_id = ' . $this->mRepo->getId() .
 182+ $this->getSpecializedWhereClause($dbr);
 183+ }
 184+ return $dbr->query($sql);
 185+ }
 186+
 187+ function getSpecializedWhereClause($dbr) {
 188+ return '';
 189+ }
 190+
150191 }
151192
152193 // Pager for CodeRevisionListView
@@ -225,11 +266,13 @@
226267 }
227268 return $fields;
228269 }
229 -
 270+
230271 function formatValue( $name, $value ) { } // unused
231272
232273 function formatRevValue( $name, $value, $row ) {
233274 global $wgUser, $wgLang;
 275+ $pathQuery = (empty($this->mView->mPath)) ? array() : array('path' => $this->mView->mPath);
 276+
234277 switch( $name ) {
235278 case 'selectforchange':
236279 $sort = $this->getDefaultSort();
@@ -245,10 +288,12 @@
246289 case 'cr_status':
247290 return $this->mView->mSkin->link(
248291 SpecialPage::getTitleFor( 'Code', $this->mRepo->getName() . '/status/' . $value ),
249 - htmlspecialchars( $this->mView->statusDesc( $value ) )
 292+ htmlspecialchars( $this->mView->statusDesc( $value ) ),
 293+ array(),
 294+ $pathQuery
250295 );
251296 case 'cr_author':
252 - return $this->mView->authorLink( $value );
 297+ return $this->mView->authorLink( $value, $pathQuery );
253298 case 'cr_message':
254299 return $this->mView->messageFragment( $value );
255300 case 'cr_timestamp':
Index: trunk/extensions/CodeReview/ui/CodeRevisionStatusView.php
@@ -9,6 +9,10 @@
1010 function getPager() {
1111 return new SvnRevStatusTablePager( $this, $this->mStatus );
1212 }
 13+
 14+ function getSpecializedWhereClause($dbr) {
 15+ return ' AND cr_status = ' . $dbr->addQuotes($this->mStatus);
 16+ }
1317 }
1418
1519 class SvnRevStatusTablePager extends SvnRevTablePager {

Comments

#Comment by Catrope (talk | contribs)   21:33, 1 February 2010
+		
+		//get the total count across all pages
+		$dbr =& wfGetDB(DB_SLAVE);
+		$revCountRes = $this->getRevCountQuery($dbr);
+		$revCount = 0;		 
+		if ($dbr->numRows($revCountRes) > 0){
+			$row = $dbr->fetchObject($revCountRes);			
+			$revCount = $row->rev_count;
+		}

Please read Manual:Coding conventions, and as a general rule, follow the coding style of the file you're in. There's other instances of inconsistent coding/whitespace style in this commit as well.

Also, there's a selectRow() function in the DatabaseBase class, which is probably what you're looking for here. See the docs at http://svn.wikimedia.org/doc/classDatabaseBase.html

 			$pager->getLimitForm() .
+			'</td><td style="padding-left: 2em;">' . 
+			' Total number of results: <span style="font-weight: bold;">' . $revCount . '</span>' . 
+			'</td></tr></table>' .  

Don't hardcode English text ever. Add a message in CodeReview.i18n.php and use wfMsg( 'messagekey' ) to get the translated message. Messages can be parameterized, so I recommend making the message contain something like "Total number of results: 1" and using wfMsgWikiHtml( 'messagekey', $revCount ); in the code. Also, building HTML with Html:: functions (Xml:: is its predecessor) is preferred.

+	function getRevCountQuery($dbr) {
+		$sql = 'SELECT COUNT(cr_id) AS rev_count' . 
+				' FROM '  . $dbr->tableName('code_rev') . ' ';
+		// count if code_rev where path matches
+		if ( $this->mPath ) {
+			$sql .= ' WHERE cr_id in' .
+						' (SELECT cp_rev_id from code_paths' .
+						' WHERE' .
+						' cp_repo_id = ' . $this->mRepo->getId(). ' AND' .
+						' cp_path LIKE ' . $dbr->addQuotes( $this->mPath . '%') . ' AND' .
+						// performance
+						' cp_rev_id > ' . ($this->mRepo->getLastStoredRev() - 20000) . 
+						$this->getSpecializedWhereClause($dbr) .
+						')';
+		// No path; count of code_rev
+		} else {
+			$sql .= ' WHERE cr_repo_id = ' . $this->mRepo->getId() .
+					$this->getSpecializedWhereClause($dbr);
+		}
+		return $dbr->query($sql);
+	}
+	
+	function getSpecializedWhereClause($dbr) {
+		return '';
+	}
+	

Please don't use raw SQL in your code, unless you need it for stuff like subqueries. I don't see why you need a subquery here, a join should work just fine. The preferred format for doing queries is by calling $dbr->select() (see the DatabaseBase docs I mentioned above) which accepts a bunch of associative arrays as parameters describing the query, and does most escaping for you. Examples of usage are all over the codebase (both core and extensions). getSpecializedWhereClause() can then simply return something like array( 'cr_status' => $this->mStatus ) which you would array_merge() into the WHERE clauses array.

You've also added trailing whitespace here and there.

#Comment by Pdhanda (talk | contribs)   23:16, 1 February 2010

The code style and hardcoded message issue should be fixed now.

I could not eliminate the subselect because of the nature of the join, each rev_id has multiple paths :

mysql> SELECT COUNT(cr_id) AS rev_count FROM `code_rev` WHERE cr_id in (SELECT cp_rev_id from code_paths WHERE cp_repo_id = 1 AND cp_path LIKE '/trunk/extensions/UsabilityInitiative%' AND cp_rev_id > -19987); +-----------+ | rev_count | +-----------+ | 1277 | +-----------+ 1 row in set (0.00 sec)

mysql> SELECT COUNT(cr_id) AS rev_count FROM code_rev, code_paths WHERE cr_id = cp_rev_id AND cp_repo_id = 1 AND cp_path LIKE '/trunk/extensions/UsabilityInitiative%' AND cr_id > -19987; +-----------+ | rev_count | +-----------+ | 5333 | +-----------+ 1 row in set (0.58 sec)

#Comment by MaxSem (talk | contribs)   12:47, 2 February 2010

Don't mark your own commits as resolved, resetting to new.

#Comment by Tim Starling (talk | contribs)   05:33, 16 February 2010
+		if ( $this->mPath ) {
+			$tables[] = 'code_paths';

Inappropriate conversion from string to boolean, will convert "0" etc. to false. Similarly:

+		$pathQuery = (empty($this->mView->mPath)) ? array() : array('path' => $this->mView->mPath);

Inappropriate use of empty(), should probably be !strlen($this->mView->mPath) like in getDefaultSort().

#Comment by Bryan (talk | contribs)   19:35, 23 February 2010

Remarks on this revision appear to no longer apply to HEAD; marking as new.

Status & tagging log