MediaWiki  1.28.1
DBFileJournal.php
Go to the documentation of this file.
1 <?php
29 class DBFileJournal extends FileJournal {
31  protected $dbw;
32 
33  protected $wiki = false; // string; wiki DB name
34 
41  protected function __construct( array $config ) {
42  parent::__construct( $config );
43 
44  $this->wiki = $config['wiki'];
45  }
46 
53  protected function doLogChangeBatch( array $entries, $batchId ) {
55 
56  try {
57  $dbw = $this->getMasterDB();
58  } catch ( DBError $e ) {
59  $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
60 
61  return $status;
62  }
63 
64  $now = wfTimestamp( TS_UNIX );
65 
66  $data = [];
67  foreach ( $entries as $entry ) {
68  $data[] = [
69  'fj_batch_uuid' => $batchId,
70  'fj_backend' => $this->backend,
71  'fj_op' => $entry['op'],
72  'fj_path' => $entry['path'],
73  'fj_new_sha1' => $entry['newSha1'],
74  'fj_timestamp' => $dbw->timestamp( $now )
75  ];
76  }
77 
78  try {
79  $dbw->insert( 'filejournal', $data, __METHOD__ );
80  if ( mt_rand( 0, 99 ) == 0 ) {
81  $this->purgeOldLogs(); // occasionally delete old logs
82  }
83  } catch ( DBError $e ) {
84  $status->fatal( 'filejournal-fail-dbquery', $this->backend );
85 
86  return $status;
87  }
88 
89  return $status;
90  }
91 
96  protected function doGetCurrentPosition() {
97  $dbw = $this->getMasterDB();
98 
99  return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
100  [ 'fj_backend' => $this->backend ],
101  __METHOD__
102  );
103  }
104 
110  protected function doGetPositionAtTime( $time ) {
111  $dbw = $this->getMasterDB();
112 
113  $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
114 
115  return $dbw->selectField( 'filejournal', 'fj_id',
116  [ 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ],
117  __METHOD__,
118  [ 'ORDER BY' => 'fj_timestamp DESC' ]
119  );
120  }
121 
128  protected function doGetChangeEntries( $start, $limit ) {
129  $dbw = $this->getMasterDB();
130 
131  $res = $dbw->select( 'filejournal', '*',
132  [
133  'fj_backend' => $this->backend,
134  'fj_id >= ' . $dbw->addQuotes( (int)$start ) ], // $start may be 0
135  __METHOD__,
136  array_merge( [ 'ORDER BY' => 'fj_id ASC' ],
137  $limit ? [ 'LIMIT' => $limit ] : [] )
138  );
139 
140  $entries = [];
141  foreach ( $res as $row ) {
142  $item = [];
143  foreach ( (array)$row as $key => $value ) {
144  $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
145  }
146  $entries[] = $item;
147  }
148 
149  return $entries;
150  }
151 
157  protected function doPurgeOldLogs() {
159  if ( $this->ttlDays <= 0 ) {
160  return $status; // nothing to do
161  }
162 
163  $dbw = $this->getMasterDB();
164  $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
165 
166  $dbw->delete( 'filejournal',
167  [ 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ],
168  __METHOD__
169  );
170 
171  return $status;
172  }
173 
180  protected function getMasterDB() {
181  if ( !$this->dbw ) {
182  // Get a separate connection in autocommit mode
183  $lb = wfGetLBFactory()->newMainLB();
184  $this->dbw = $lb->getConnection( DB_MASTER, [], $this->wiki );
185  $this->dbw->clearFlag( DBO_TRX );
186  }
187 
188  return $this->dbw;
189  }
190 }
string $backend
Definition: FileJournal.php:40
insert($table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
getMasterDB()
Get a master connection to the logging DB.
Database error base class.
Definition: DBError.php:26
the array() calling protocol came about after MediaWiki 1.4rc1.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
doGetPositionAtTime($time)
$value
doLogChangeBatch(array $entries, $batchId)
timestamp($ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
const DB_MASTER
Definition: defines.php:23
doGetChangeEntries($start, $limit)
Prior to maintenance scripts were a hodgepodge of code that had no cohesion or formal method of action Beginning maintenance scripts have been cleaned up to use a unified class Directory structure How to run a script How to write your own DIRECTORY STRUCTURE The maintenance directory of a MediaWiki installation contains several all of which have unique purposes HOW TO RUN A SCRIPT Ridiculously just call php someScript php that s in the top level maintenance directory if not default wiki
Definition: maintenance.txt:1
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
delete($table, $conds, $fname=__METHOD__)
DELETE query wrapper.
__construct(array $config)
Construct a new instance from configuration.
$res
Definition: database.txt:21
selectField($table, $var, $cond= '', $fname=__METHOD__, $options=[])
A SELECT wrapper which returns a single field from a single result row.
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
Version of FileJournal that logs to a DB table.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const DBO_TRX
Definition: defines.php:9
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
wfGetLBFactory()
Get the load balancer factory object.
IDatabase $dbw
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
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 $status
Definition: hooks.txt:1046
addQuotes($s)
Adds quotes and backslashes.
select($table, $vars, $conds= '', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
purgeOldLogs()
Purge any old log entries.
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1749
Class for handling file operation journaling.
Definition: FileJournal.php:38