| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Init the user_editcount database field based on the number of rows in the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * revision table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This program is free software; you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * the Free Software Foundation; either version 2 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * You should have received a copy of the GNU General Public License along | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * with this program; if not, write to the Free Software Foundation, Inc., | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * http://www.gnu.org/copyleft/gpl.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @ingroup Maintenance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | require_once __DIR__ . '/Maintenance.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  | class InitEditCount extends Maintenance { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	public function __construct() { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 		parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 		$this->addOption( 'quick', 'Force the update to be done in a single query' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		$this->addOption( 'background', 'Force replication-friendly mode; may be inefficient but | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 		avoids locking tables or lagging slaves with large updates; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 		calculates counts on a slave if possible. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | Background mode will be automatically used if multiple servers are listed | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | in the load balancer, usually indicating a replication environment.' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		$this->addDescription( 'Batch-recalculate user_editcount fields from the revision table' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	public function execute() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$dbw = $this->getDB( DB_MASTER ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$user = $dbw->tableName( 'user' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$revision = $dbw->tableName( 'revision' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$dbver = $dbw->getServerVersion(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		// Autodetect mode... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		if ( $this->hasOption( 'background' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			$backgroundMode = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		} elseif ( $this->hasOption( 'quick' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			$backgroundMode = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			$backgroundMode = wfGetLB()->getServerCount() > 1; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		if ( $backgroundMode ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$this->output( "Using replication-friendly background mode...\n" ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			$dbr = $this->getDB( DB_SLAVE ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			$chunkSize = 100; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			$lastUser = $dbr->selectField( 'user', 'MAX(user_id)', '', __METHOD__ ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$start = microtime( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$migrated = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			for ( $min = 0; $min <= $lastUser; $min += $chunkSize ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 				$max = $min + $chunkSize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				$result = $dbr->query( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 					"SELECT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 						user_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 						COUNT(rev_user) AS user_editcount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 					FROM $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 					LEFT OUTER JOIN $revision ON user_id=rev_user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 					WHERE user_id > $min AND user_id <= $max | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 					GROUP BY user_id", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 					__METHOD__ ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 				foreach ( $result as $row ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 					$dbw->update( 'user', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 						[ 'user_editcount' => $row->user_editcount ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 						[ 'user_id' => $row->user_id ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 						__METHOD__ ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 					++$migrated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				$delta = microtime( true ) - $start; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				$rate = ( $delta == 0.0 ) ? 0.0 : $migrated / $delta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				$this->output( sprintf( "%s %d (%0.1f%%) done in %0.1f secs (%0.3f accounts/sec).\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 					wfWikiID(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 					$migrated, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 					min( $max, $lastUser ) / $lastUser * 100.0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 					$delta, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 					$rate ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 				wfWaitForSlaves(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			$this->output( "Using single-query mode...\n" ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$sql = "UPDATE $user SET user_editcount=(SELECT COUNT(*) FROM $revision WHERE rev_user=user_id)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$dbw->query( $sql ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$this->output( "Done!\n" ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | $maintClass = "InitEditCount"; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | require_once RUN_MAINTENANCE_IF_MAIN; | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.