sqlite.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Performs some operations specific to SQLite database backend.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script that performs some operations specific to SQLite database backend.
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class SqliteMaintenance extends Maintenance {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Performs some operations specific to SQLite database backend' );
  33. $this->addOption(
  34. 'vacuum',
  35. 'Clean up database by removing deleted pages. Decreases database file size'
  36. );
  37. $this->addOption( 'integrity', 'Check database for integrity' );
  38. $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
  39. $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
  40. }
  41. /**
  42. * While we use database connection, this simple lie prevents useless --dbpass and
  43. * --dbuser options from appearing in help message for this script.
  44. *
  45. * @return int DB constant
  46. */
  47. public function getDbType() {
  48. return Maintenance::DB_NONE;
  49. }
  50. public function execute() {
  51. // Should work even if we use a non-SQLite database
  52. if ( $this->hasOption( 'check-syntax' ) ) {
  53. $this->checkSyntax();
  54. return;
  55. }
  56. $this->db = $this->getDB( DB_MASTER );
  57. if ( $this->db->getType() != 'sqlite' ) {
  58. $this->error( "This maintenance script requires a SQLite database.\n" );
  59. return;
  60. }
  61. if ( $this->hasOption( 'vacuum' ) ) {
  62. $this->vacuum();
  63. }
  64. if ( $this->hasOption( 'integrity' ) ) {
  65. $this->integrityCheck();
  66. }
  67. if ( $this->hasOption( 'backup-to' ) ) {
  68. $this->backup( $this->getOption( 'backup-to' ) );
  69. }
  70. }
  71. private function vacuum() {
  72. $prevSize = filesize( $this->db->getDbFilePath() );
  73. if ( $prevSize == 0 ) {
  74. $this->error( "Can't vacuum an empty database.\n", true );
  75. }
  76. $this->output( 'VACUUM: ' );
  77. if ( $this->db->query( 'VACUUM' ) ) {
  78. clearstatcache();
  79. $newSize = filesize( $this->db->getDbFilePath() );
  80. $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
  81. $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
  82. } else {
  83. $this->output( 'Error\n' );
  84. }
  85. }
  86. private function integrityCheck() {
  87. $this->output( "Performing database integrity checks:\n" );
  88. $res = $this->db->query( 'PRAGMA integrity_check' );
  89. if ( !$res || $res->numRows() == 0 ) {
  90. $this->error( "Error: integrity check query returned nothing.\n" );
  91. return;
  92. }
  93. foreach ( $res as $row ) {
  94. $this->output( $row->integrity_check );
  95. }
  96. }
  97. private function backup( $fileName ) {
  98. $this->output( "Backing up database:\n Locking..." );
  99. $this->db->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
  100. $ourFile = $this->db->getDbFilePath();
  101. $this->output( " Copying database file $ourFile to $fileName... " );
  102. MediaWiki\suppressWarnings( false );
  103. if ( !copy( $ourFile, $fileName ) ) {
  104. $err = error_get_last();
  105. $this->error( " {$err['message']}" );
  106. }
  107. MediaWiki\suppressWarnings( true );
  108. $this->output( " Releasing lock...\n" );
  109. $this->db->query( 'COMMIT TRANSACTION', __METHOD__ );
  110. }
  111. private function checkSyntax() {
  112. if ( !Sqlite::isPresent() ) {
  113. $this->error( "Error: SQLite support not found\n" );
  114. }
  115. $files = [ $this->getOption( 'check-syntax' ) ];
  116. $files = array_merge( $files, $this->mArgs );
  117. $result = Sqlite::checkSqlSyntax( $files );
  118. if ( $result === true ) {
  119. $this->output( "SQL syntax check: no errors detected.\n" );
  120. } else {
  121. $this->error( "Error: $result\n" );
  122. }
  123. }
  124. }
  125. $maintClass = "SqliteMaintenance";
  126. require_once RUN_MAINTENANCE_IF_MAIN;