MediaWiki  master
createCommonPasswordCdb.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
39  public function __construct() {
40  global $IP;
41  parent::__construct();
42  $this->addDescription( 'Generate CDB file of common passwords' );
43  $this->addOption( 'limit', "Max number of passwords to write", false, true, 'l' );
44  $this->addArg( 'inputfile', 'List of passwords (one per line) to use or - for stdin', true );
45  $this->addArg(
46  'output',
47  "Location to write CDB file to (Try $IP/includes/password/commonpasswords.cdb)",
48  true
49  );
50  }
51 
52  public function execute() {
53  $limit = (int)$this->getOption( 'limit', PHP_INT_MAX );
54  $langEn = Language::factory( 'en' );
55 
56  $infile = $this->getArg( 0 );
57  if ( $infile === '-' ) {
58  $infile = 'php://stdin';
59  }
60  $outfile = $this->getArg( 1 );
61 
62  if ( !is_readable( $infile ) && $infile !== 'php://stdin' ) {
63  $this->fatalError( "Cannot open input file $infile for reading" );
64  }
65 
66  $file = fopen( $infile, 'r' );
67  if ( $file === false ) {
68  $this->fatalError( "Cannot read input file $infile" );
69  }
70 
71  try {
72  $db = \Cdb\Writer::open( $outfile );
73 
74  $alreadyWritten = [];
75  $skipped = 0;
76  for ( $i = 0; ( $i - $skipped ) < $limit; $i++ ) {
77  if ( feof( $file ) ) {
78  break;
79  }
80  $rawLine = fgets( $file );
81 
82  if ( $rawLine === false ) {
83  $this->error( "Error reading input file" );
84  break;
85  }
86  if ( substr( $rawLine, -1 ) !== "\n" && !feof( $file ) ) {
87  // We're assuming that this just won't happen.
88  $this->error( "fgets did not return whole line at $i??" );
89  }
90  $line = $langEn->lc( trim( $rawLine ) );
91  if ( $line === '' ) {
92  $this->error( "Line number " . ( $i + 1 ) . " is blank?" );
93  $skipped++;
94  continue;
95  }
96  if ( isset( $alreadyWritten[$line] ) ) {
97  $this->output( "Password '$line' already written (line " . ( $i + 1 ) . ")\n" );
98  $skipped++;
99  continue;
100  }
101  $alreadyWritten[$line] = true;
102  $db->set( $line, $i + 1 - $skipped );
103  }
104  // All caps, so cannot conflict with potential password
105  $db->set( '_TOTALENTRIES', $i - $skipped );
106  $db->close();
107 
108  $this->output( "Successfully wrote " . ( $i - $skipped ) .
109  " (out of $i) passwords to $outfile\n"
110  );
111  } catch ( \Cdb\Exception $e ) {
112  $this->fatalError( "Error writing cdb file: " . $e->getMessage(), 2 );
113  }
114  }
115 }
116 
118 require_once RUN_MAINTENANCE_IF_MAIN;
getArg( $argId=0, $default=null)
Get an argument.
error( $err, $die=0)
Throw an error to the user.
$IP
Definition: WebStart.php:41
getOption( $name, $default=null)
Get an option, or return the default.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2159
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Maintenance script to create common password cdb database.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
addDescription( $text)
Set the description text.
addArg( $arg, $description, $required=true)
Add some args that are needed.
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:216
output( $out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$line
Definition: cdb.php:59
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.