MediaWiki  1.28.1
createCommonPasswordCdb.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
37  public function __construct() {
38  global $IP;
39  parent::__construct();
40  $this->addDescription( 'Generate CDB file of common passwords' );
41  $this->addOption( 'limit', "Max number of passwords to write", false, true, 'l' );
42  $this->addArg( 'inputfile', 'List of passwords (one per line) to use or - for stdin', true );
43  $this->addArg(
44  'output',
45  "Location to write CDB file to (Try $IP/serialized/commonpasswords.cdb)",
46  true
47  );
48  }
49 
50  public function execute() {
51  $limit = (int)$this->getOption( 'limit', PHP_INT_MAX );
52  $langEn = Language::factory( 'en' );
53 
54  $infile = $this->getArg( 0 );
55  if ( $infile === '-' ) {
56  $infile = 'php://stdin';
57  }
58  $outfile = $this->getArg( 1 );
59 
60  if ( !is_readable( $infile ) && $infile !== 'php://stdin' ) {
61  $this->error( "Cannot open input file $infile for reading", 1 );
62  }
63 
64  $file = fopen( $infile, 'r' );
65  if ( $file === false ) {
66  $this->error( "Cannot read input file $infile", 1 );
67  }
68 
69  try {
70  $db = \Cdb\Writer::open( $outfile );
71 
72  $alreadyWritten = [];
73  $skipped = 0;
74  for ( $i = 0; ( $i - $skipped ) < $limit; $i++ ) {
75  if ( feof( $file ) ) {
76  break;
77  }
78  $rawLine = fgets( $file );
79 
80  if ( $rawLine === false ) {
81  $this->error( "Error reading input file" );
82  break;
83  }
84  if ( substr( $rawLine, -1 ) !== "\n" && !feof( $file ) ) {
85  // We're assuming that this just won't happen.
86  $this->error( "fgets did not return whole line at $i??" );
87  }
88  $line = $langEn->lc( trim( $rawLine ) );
89  if ( $line === '' ) {
90  $this->error( "Line number " . ( $i + 1 ) . " is blank?" );
91  $skipped++;
92  continue;
93  }
94  if ( isset( $alreadyWritten[$line] ) ) {
95  $this->output( "Password '$line' already written (line " . ( $i + 1 ) .")\n" );
96  $skipped++;
97  continue;
98  }
99  $alreadyWritten[$line] = true;
100  $db->set( $line, $i + 1 - $skipped );
101  }
102  // All caps, so cannot conflict with potential password
103  $db->set( '_TOTALENTRIES', $i - $skipped );
104  $db->close();
105 
106  $this->output( "Successfully wrote " . ( $i - $skipped ) .
107  " (out of $i) passwords to $outfile\n"
108  );
109  } catch ( \Cdb\Exception $e ) {
110  $this->error( "Error writing cdb file: " . $e->getMessage(), 2 );
111  }
112 
113  }
114 }
115 
116 $maintClass = "GenerateCommonPassword";
117 require_once RUN_MAINTENANCE_IF_MAIN;
$IP
Definition: WebStart.php:58
addArg($arg, $description, $required=true)
Add some args that are needed.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
Maintenance script to create common password cdb database.
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
addDescription($text)
Set the description text.
getOption($name, $default=null)
Get an option, or return the default.
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
$line
Definition: cdb.php:59
error($err, $die=0)
Throw an error to the user.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1046
getArg($argId=0, $default=null)
Get an argument.
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181