<?php
-namespace WP_CLI;
+namespace WP_CLI\Iterators;
-class CSVIterator implements \Iterator {
+class CSV implements \Iterator {
const ROW_SIZE = 4096;
--- /dev/null
+<?php
+
+namespace WP_CLI\Iterators;
+
+class Exception extends \RuntimeException {}
+
<?php
-namespace WP_CLI;
+namespace WP_CLI\Iterators;
/**
* Iterates over results of a query, split into many queries via LIMIT and OFFSET
*
* @source https://gist.github.com/4060005
*/
-class QueryIterator implements \Iterator {
+class Query implements \Iterator {
private $limit = 500;
private $query = '';
*
* This will loop over all users, but will retrieve them 100 by 100:
* <code>
- * foreach( new QueryIterator( array( 'query' => 'SELECT * FROM users', 'limit' => 100 ) ) as $user ) {
+ * foreach( new Iterators\Query( array( 'query' => 'SELECT * FROM users', 'limit' => 100 ) ) as $user ) {
* tickle( $user );
* }
* </code>
$this->results = $this->db->get_results( $query );
if ( !$this->results ) {
if ( $this->db->last_error ) {
- throw new QueryIteratorException( 'Database error: '.$this->db->last_error );
+ throw new Iterators\Exception( 'Database error: '.$this->db->last_error );
} else {
return false;
}
}
}
-class QueryIteratorException extends \RuntimeException {}
-
<?php
-namespace WP_CLI;
+namespace WP_CLI\Iterators;
/**
* @source https://gist.github.com/4060005
*/
-class TableIterator extends QueryIterator {
+class Table extends Iterators\Query {
/**
* Creates an iterator over a database table
*
* <code>
- * foreach( new TableIterator( array( 'table' => $wpdb->posts, 'fields' => array( 'ID', 'post_content' ) ) ) as $post ) {
+ * foreach( new Iterators\Table( array( 'table' => $wpdb->posts, 'fields' => array( 'ID', 'post_content' ) ) ) as $post ) {
* count_words_for( $post->ID, $post->post_content );
* }
* </code>
*
* <code>
- * foreach( new TableIterator( array( 'table' => $wpdb->posts, 'where' => 'ID = 8 OR post_status = "publish"' ) ) as $post ) {
+ * foreach( new Iterators\Table( array( 'table' => $wpdb->posts, 'where' => 'ID = 8 OR post_status = "publish"' ) ) as $post ) {
* …
* }
* </code>
}
}
-WP_CLI::add_command( 'role', 'Role_Command' );
\ No newline at end of file
+WP_CLI::add_command( 'role', 'Role_Command' );
'limit' => 1000
);
- $it = new \WP_CLI\TableIterator( $args );
+ $it = new \WP_CLI\Iterators\Table( $args );
$count = 0;
$filename = $args[0];
- foreach ( new \WP_CLI\CSVIterator( $filename ) as $i => $new_user ) {
+ foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $i => $new_user ) {
$defaults = array(
'role' => get_option('default_role'),
'user_pass' => wp_generate_password(),