MediaWiki r44151 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r44150‎ | r44151 (on ViewVC)‎ | r44152 >
Date:17:25, 2 December 2008
Author:ialex
Status:ok
Tags:
Comment:
* Per aaron: more efficiency for some requests:
** added $options to WebConfiguration::listArchiveVersions() and WebConfiguration::getArchiveVersions() that allow to restrict queries for one wiki and limit the number of results
** introduced WebConfiguration::versionExists() to check if a specific version exists rather than loading all versions and checking if it's in the list
* Might fix problem for settings merging: added array_unique() in WebConfiguration::mergeArrays() for "simple" arrays
Modified paths:

Diff [purge]

Index: trunk/extensions/Configure/Configure.obj.php
===================================================================
--- trunk/extensions/Configure/Configure.obj.php	(revision 44150)
+++ trunk/extensions/Configure/Configure.obj.php	(revision 44151)
@@ -147,7 +147,7 @@
 	 * @return array
 	 */
 	public function getOldSettings( $ts ) {
-		if ($ts == 'default')
+		if ( $ts == 'default' )
 			return array( 'default' => $this->getDefaults() );
 		return $this->mHandler->getOldSettings( $ts );
 	}
@@ -257,15 +257,24 @@
 	}
 
 	/**
-	 * List all archived files that are like conf-{$ts}.ser
+	 * List all archived versions
+	 *
+	 * @param $options Array of options
 	 * @return array of timestamps
 	 */
-	public function listArchiveVersions() {
-		return $this->mHandler->listArchiveVersions();
+	public function listArchiveVersions( $options = array() ) {
+		return $this->mHandler->listArchiveVersions( $options );
 	}
-	
-	public function getArchiveVersions() {
-		return $this->mHandler->getArchiveVersions();
+
+	/**
+	 * Same as listArchiveVersions(), but with more information about each
+	 * version
+	 *
+	 * @param $options Array of options
+	 * @return Array of versions
+	 */
+	public function getArchiveVersions( $options = array() ) {
+		return $this->mHandler->getArchiveVersions( $options );
 	}
 
 	/**
@@ -284,6 +293,16 @@
 	}
 
 	/**
+	 * Return a bool whether the version exists
+	 *
+	 * @param $ts version
+	 * @return bool
+	 */
+	public function versionExists( $ts ) {
+		return $this->mHandler->versionExists( $ts );
+	}
+
+	/**
 	 * Get the wiki in use
 	 *
 	 * @return String
@@ -327,6 +346,8 @@
 				}
 			}
 		}
+		if ( $canAdd )
+			$out = array_unique( $out );
 		return $out;
 	}
 }
Index: trunk/extensions/Configure/Configure.page.php
===================================================================
--- trunk/extensions/Configure/Configure.page.php	(revision 44150)
+++ trunk/extensions/Configure/Configure.page.php	(revision 44151)
@@ -305,19 +305,19 @@
 		global $wgConf, $wgOut, $wgRequest, $wgLang;
 
 		if ( $version = $wgRequest->getVal( 'version' ) ) {
-			$versions = $wgConf->listArchiveVersions();
-			if ( in_array( $version, $versions ) || $version == 'default' ) {
+			if ( $version == 'default' || $wgConf->versionExists( $version ) ) {
 				$conf = $wgConf->getOldSettings( $version );
 				
-				if ($version == 'default') { ## Hacky special case.
+				if ( $version == 'default' ) { ## Hacky special case.
 					$conf[$this->mWiki] = $conf['default'];
 				}
-				
-				$this->conf = $conf[$this->mWiki];
+
 				if ( !isset( $conf[$this->mWiki] ) ) {
-					$wgOut->addHtml( Xml::tags( 'div', array( 'class' => 'errorbox' ), wfMsgExt( 'configure-old-not-available', 'parseinline', $version ) ) );
+					$wgOut->wrapWikiMsg( '<div class="errorbox">$1</div>',
+						array( 'configure-old-not-available', $version ) );
 					return false;
 				}
+				$this->conf = $conf[$this->mWiki];
 				$current = null;
 				foreach ( $this->conf as $name => $value ) {
 					if ( $this->canBeMerged( $name, $value ) ) {
@@ -328,7 +328,7 @@
 				}
 				$wgOut->addWikiMsg( 'configure-edit-old', $wgLang->timeAndDate( $version ) );
 			} else {
-				$wgOut->addWikiText( '<div class="errorbox">$1</div>',
+				$wgOut->wrapWikiMsg( '<div class="errorbox">$1</div>',
 					array( 'configure-old-not-available', $version ) );
 				return false;
 			}
@@ -344,47 +344,44 @@
 	protected function buildOldVersionSelect() {
 		global $wgConf, $wgLang, $wgUser;
 
-		$showAllLink = false;
 		$count = 0;
 		$links = array();
 
-		$versions = $wgConf->getArchiveVersions();
+		$versions = $wgConf->getArchiveVersions( array( 'wiki' => $this->mWiki, 'limit' => 11 ) );
 		$skin = $wgUser->getSkin();
 		$title = $this->getTitle();
 		$prev = null;
 
-		ksort($versions); ## Put in ascending order for now.
+		ksort( $versions ); ## Put in ascending order for now.
 
 		foreach ( $versions as $data ) {
 			$ts = $data['timestamp'];
-			if ( in_array( $this->mWiki, $wgConf->getWikisInVersion( $ts ) ) ) {
-				$count++;
-				$link = $skin->makeKnownLinkObj( $title, $wgLang->timeAndDate( $ts ), "version=$ts" );
-				$diffLink = '';
-				if ($prev)
-					$diffLink =  '(' . $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'ViewConfig' ), wfMsg( 'configure-old-changes' ), "version=$ts&diff=$prev" ) . ')';
+			$count++;
+			$link = $skin->makeKnownLinkObj( $title, $wgLang->timeAndDate( $ts ), "version=$ts" );
+			$diffLink = '';
+			if ( $prev )
+				$diffLink =  '(' . $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'ViewConfig' ), wfMsg( 'configure-old-changes' ), "version=$ts&diff=$prev" ) . ')';
 
-				## Make user link...
+			## Make user link...
+			$userLink = '';
+			if( !$data['userwiki'] || !$data['username'] ) {
 				$userLink = '';
-				if( !$data['userwiki'] || !$data['username'] ) {
-					$userLink = '';
-				} else if ( $data['userwiki'] == wfWikiId() ) {
-					$userLink = $skin->link( Title::makeTitle( NS_USER, $data['username'] ), $data['username'] );
-				} elseif ( class_exists( 'WikiMap' ) && ($wiki = WikiMap::getWiki( $data['userwiki'] ) ) ) {
-					$userLink = $skin->makeExternalLink( $wiki->getUrl( 'User:'.$data['username'] ), $data['username'].'@'.$data['userwiki'] );
-				} else {
-					## Last-ditch
-					$userLink = $data['username'].'@'.$data['userwiki'];
-				}
-				
-				$comment = $data['reason'] ? $skin->commentBlock( $data['reason'] ) : '';
+			} else if ( $data['userwiki'] == wfWikiId() ) {
+				$userLink = $skin->link( Title::makeTitle( NS_USER, $data['username'] ), $data['username'] );
+			} elseif ( class_exists( 'WikiMap' ) && ($wiki = WikiMap::getWiki( $data['userwiki'] ) ) ) {
+				$userLink = $skin->makeExternalLink( $wiki->getUrl( 'User:'.$data['username'] ), $data['username'].'@'.$data['userwiki'] );
+			} else {
+				## Last-ditch
+				$userLink = $data['username'].'@'.$data['userwiki'];
+			}
 
-				$text = wfMsgExt( 'configure-old-summary', array( 'replaceafter', 'parseinline'), array( $link, $userLink, $diffLink, $comment ) );
+			$comment = $data['reason'] ? $skin->commentBlock( $data['reason'] ) : '';
 
-				$prev = $ts;
+			$text = wfMsgExt( 'configure-old-summary', array( 'replaceafter', 'parseinline' ), array( $link, $userLink, $diffLink, $comment ) );
 
-				$links[] = $text;
-			}
+			$prev = $ts;
+
+			$links[] = $text;
 		}
 
 		## Reset into descending order
@@ -403,7 +400,7 @@
 		}
 		$link = SpecialPage::getTitleFor( 'ViewConfig' );
 		$text .= Xml::tags( 'p', null, $skin->makeKnownLinkObj( $link, wfMsgHtml( 'configure-view-all-versions' ) ) );
-		$text .= Xml::tags( 'p', null, $skin->link( SpecialPage::getTitleFor( 'ViewConfig' ), wfMsgHtml( 'configure-view-default' ), array(), array( 'version' => 'default' ) ) );
+		$text .= Xml::tags( 'p', null, $skin->makeKnownLinkObj( $link, wfMsgHtml( 'configure-view-default' ), 'version=default' ) );
 
 		$text .= '</fieldset>';
 		return $text;
Index: trunk/extensions/Configure/Configure.handler-files.php
===================================================================
--- trunk/extensions/Configure/Configure.handler-files.php	(revision 44150)
+++ trunk/extensions/Configure/Configure.handler-files.php	(revision 44151)
@@ -114,7 +114,7 @@
 	 * List all archived files that are like conf-{$ts}.php
 	 * @return array of timestamps
 	 */
-	public function getArchiveVersions() {
+	public function getArchiveVersions( $options = array() ) {
 		if ( !$dir = opendir( $this->mDir ) )
 			return array();
 		$files = array();
@@ -124,7 +124,10 @@
 				## Read the data.
 				require( $this->mDir."/$file" );
 				
-				if (isset( $settings['__metadata'] )) {
+				if( isset( $options['wiki'] ) && !isset( $settings[$options['wiki']] ) )
+					continue;
+				
+				if ( isset( $settings['__metadata'] ) ) {
 					$metadata = $settings['__metadata'];
 					
 					$files[$m[1]] = array( 'username' => $metadata['user_name'], 
@@ -135,10 +138,34 @@
 			}
 		}
 		krsort( $files, SORT_NUMERIC );
+		if( isset( $options['limit'] ) && count( $files ) > $options['limit'] )
+			$files = array_slice( $files, 0, $options['limit'] );
+
 		return $files;
 	}
 
 	/**
+	 * Same as listArchiveVersions(), but with more information about each
+	 * version
+	 *
+	 * @param $options Array of options
+	 * @return Array of versions
+	 */
+	public function listArchiveVersions( $options = array() ) {
+		return array_keys( $this->getArchiveVersions( $options ) );
+	}
+
+	/**
+	 * Return a bool whether the version exists
+	 *
+	 * @param $ts version
+	 * @return bool
+	 */
+	public function versionExists( $ts ) {
+		return file_exists( $this->getArchiveFileName( $ts ) );
+	}
+
+	/**
 	 * Do some checks
 	 */
 	public function doChecks() {
@@ -186,7 +213,7 @@
 		
 		## Hack hack hack
 		## Basically, if the file already exists, pretend that the setting change was made in a second's time.
-		if ($ts_orig === null && file_exists($file))
+		if ( $ts_orig === null && file_exists( $file ) )
 			return $this->getArchiveFileName( $ts+1 );
 			
 		return $file;
@@ -200,15 +227,4 @@
 	public function getDir() {
 		return $this->mDir;
 	}
-	
-	public function listArchiveVersions() {
-		$versions = $this->getArchiveVersions();
-		$ret = array();
-		
-		foreach( $versions as $data ) {
-			$ret[] = $data['timestamp'];
-		}
-		
-		return $ret;
-	}
 }
Index: trunk/extensions/Configure/Configure.handler-db.php
===================================================================
--- trunk/extensions/Configure/Configure.handler-db.php	(revision 44150)
+++ trunk/extensions/Configure/Configure.handler-db.php	(revision 44151)
@@ -181,9 +181,11 @@
 
 	/**
 	 * Save a new configuration
+	 *
 	 * @param $settings array of settings
 	 * @param $wiki String: wiki name or true for all
-	 * @param $ts
+	 * @param $ts 14 chars timestamps
+	 * @param $reason String: Reason, as given by the user.
 	 * @return bool true on success
 	 */
 	public function saveNewSettings( $settings, $wiki, $ts = false, $reason = '' ) {
@@ -245,14 +247,22 @@
 	 * FIXME: timestamp not unique
 	 * @return array of timestamps
 	 */
-	public function getArchiveVersions() {
-		$db = $this->getSlaveDB();
-		$ret = $db->select(
+	public function getArchiveVersions( $options = array() ) {
+		$dbOpts = array( 'ORDER BY' => 'cv_timestamp DESC' );
+		$where = array();
+
+		if ( isset( $options['limit'] ) )
+			$dbOpts['LIMIT'] = $options['limit'];
+		if ( isset( $options['wiki'] ) )
+			$where['cv_wiki'] = $options['wiki'];
+
+		$dbr = $this->getSlaveDB();
+		$ret = $dbr->select(
 			array( 'config_version' ),
 			array( 'cv_timestamp', 'cv_user_text', 'cv_user_wiki', 'cv_reason' ),
-			array(),
+			$where,
 			__METHOD__,
-			array( 'ORDER BY' => 'cv_timestamp DESC' )
+			$dbOpts
 		);
 		$arr = array();
 		foreach ( $ret as $row ) {
@@ -262,6 +272,28 @@
 	}
 
 	/**
+	 * Same as listArchiveVersions(), but with more information about each
+	 * version
+	 *
+	 * @param $options Array of options
+	 * @return Array of versions
+	 */
+	public function listArchiveVersions( $options = array() ) {
+		return array_keys( $this->getArchiveVersions( $options ) );
+	}
+
+	/**
+	 * Return a bool whether the version exists
+	 *
+	 * @param $ts version
+	 * @return bool
+	 */
+	public function versionExists( $ts ) {
+		$dbr = $this->getSlaveDB();
+		return (bool)$dbr->selectField( 'config_version', '1', array( 'cv_timestamp' => $ts ), __METHOD__ );
+	}
+
+	/**
 	 * Do some checks
 	 */
 	public function doChecks() {
@@ -313,8 +345,4 @@
 			'wgMemCachedServers',
 		);
 	}
-	
-	public function listArchiveVersions() {
-		return array_keys( $this->getArchiveVersions() );
-	}
 }
Index: trunk/extensions/Configure/Configure.handler.php
===================================================================
--- trunk/extensions/Configure/Configure.handler.php	(revision 44150)
+++ trunk/extensions/Configure/Configure.handler.php	(revision 44151)
@@ -44,13 +44,41 @@
 
 	/**
 	 * Save a new configuration
+	 *
 	 * @param $settings array of settings
-	 * @param $wiki String: wiki name or false to use the current one
+	 * @param $wiki String: wiki name or true for all
+	 * @param $ts 14 chars timestamps
+	 * @param $reason String: Reason, as given by the user.
 	 * @return bool true on success
 	 */
-	public function saveNewSettings( $settings, $wiki );
+	public function saveNewSettings( $settings, $wiki, $ts = false, $reason = '' );
 
 	/**
+	 * List all archived versions
+	 *
+	 * @param $options Array of options
+	 * @return array of timestamps
+	 */
+	public function getArchiveVersions( $options = array() );
+
+	/**
+	 * Same as listArchiveVersions(), but with more information about each
+	 * version
+	 *
+	 * @param $options Array of options
+	 * @return Array of versions
+	 */
+	public function listArchiveVersions( $options = array() );
+	
+	/**
+	 * Return a bool whether the version exists
+	 *
+	 * @param $ts version
+	 * @return bool
+	 */
+	public function versionExists( $ts );
+
+	/**
 	 * Do some checks
 	 * @return array
 	 */
@@ -61,8 +89,4 @@
 	 * @return array
 	 */
 	public function getUneditableSettings();
-	
-	public function getArchiveVersions();
-	
-	public function listArchiveVersions();
 }

Status & tagging log

Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox