| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
require_once __DIR__ . '/Maintenance.php'; |
| 27 |
|
| 28 |
|
| 29 |
use MediaWiki\MainConfigNames; |
| 30 |
use MediaWiki\SpecialPage\QueryPage; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class UpdateSpecialPages extends Maintenance { |
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addOption( 'list', 'List special page names' ); |
| 41 |
$this->addOption( 'only', 'Only update "page"; case sensitive, ' . |
| 42 |
'check correct case by calling this script with --list. ' . |
| 43 |
'Ex: --only=BrokenRedirects', false, true ); |
| 44 |
$this->addOption( 'override', 'Also update pages that have updates disabled' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function execute() { |
| 48 |
$dbw = $this->getPrimaryDB(); |
| 49 |
$config = $this->getConfig(); |
| 50 |
$specialPageFactory = $this->getServiceContainer()->getSpecialPageFactory(); |
| 51 |
|
| 52 |
$this->doSpecialPageCacheUpdates( $dbw ); |
| 53 |
|
| 54 |
$queryCacheLimit = (int)$config->get( MainConfigNames::QueryCacheLimit ); |
| 55 |
$disabledQueryPages = QueryPage::getDisabledQueryPages( $config ); |
| 56 |
foreach ( QueryPage::getPages() as $page ) { |
| 57 |
[ , $special ] = $page; |
| 58 |
$limit = $page[2] ?? $queryCacheLimit; |
| 59 |
|
| 60 |
|
| 61 |
if ( $this->hasOption( 'list' ) ) { |
| 62 |
$this->output( "$special [QueryPage]\n" ); |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
if ( !$this->hasOption( 'override' ) |
| 67 |
&& isset( $disabledQueryPages[$special] ) |
| 68 |
) { |
| 69 |
$this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) ); |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$specialObj = $specialPageFactory->getPage( $special ); |
| 74 |
if ( !$specialObj ) { |
| 75 |
$this->output( "No such special page: $special\n" ); |
| 76 |
return; |
| 77 |
} |
| 78 |
if ( $specialObj instanceof QueryPage ) { |
| 79 |
$queryPage = $specialObj; |
| 80 |
} else { |
| 81 |
$class = get_class( $specialObj ); |
| 82 |
$this->fatalError( "$class is not an instance of QueryPage.\n" ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $queryPage->getName() ) { |
| 86 |
$this->output( sprintf( '%-30s [QueryPage] ', $special ) ); |
| 87 |
if ( $queryPage->isExpensive() ) { |
| 88 |
$t1 = microtime( true ); |
| 89 |
|
| 90 |
$num = $queryPage->recache( $limit ); |
| 91 |
$t2 = microtime( true ); |
| 92 |
if ( $num === false ) { |
| 93 |
$this->output( "FAILED: database error\n" ); |
| 94 |
} else { |
| 95 |
$this->output( "got $num rows in " ); |
| 96 |
|
| 97 |
$elapsed = $t2 - $t1; |
| 98 |
$hours = intval( $elapsed / 3600 ); |
| 99 |
$minutes = intval( (int)$elapsed % 3600 / 60 ); |
| 100 |
$seconds = $elapsed - $hours * 3600 - $minutes * 60; |
| 101 |
if ( $hours ) { |
| 102 |
$this->output( $hours . 'h ' ); |
| 103 |
} |
| 104 |
if ( $minutes ) { |
| 105 |
$this->output( $minutes . 'm ' ); |
| 106 |
} |
| 107 |
$this->output( sprintf( "%.2fs\n", $seconds ) ); |
| 108 |
} |
| 109 |
|
| 110 |
$this->reopenAndWaitForReplicas(); |
| 111 |
} else { |
| 112 |
|
| 113 |
$cached = $queryPage->getCachedTimestamp(); |
| 114 |
if ( $cached ) { |
| 115 |
$queryPage->deleteAllCachedData(); |
| 116 |
$this->reopenAndWaitForReplicas(); |
| 117 |
$this->output( "cheap, but deleted cached data\n" ); |
| 118 |
} else { |
| 119 |
$this->output( "cheap, skipped\n" ); |
| 120 |
} |
| 121 |
} |
| 122 |
if ( $this->hasOption( 'only' ) ) { |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
private function reopenAndWaitForReplicas() { |
| 136 |
$lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 137 |
$lb = $lbFactory->getMainLB(); |
| 138 |
if ( !$lb->pingAll() ) { |
| 139 |
$this->output( "\n" ); |
| 140 |
do { |
| 141 |
$this->error( "Connection failed, reconnecting in 10 seconds..." ); |
| 142 |
sleep( 10 ); |
| 143 |
$this->waitForReplication(); |
| 144 |
} while ( !$lb->pingAll() ); |
| 145 |
$this->output( "Reconnected\n\n" ); |
| 146 |
} |
| 147 |
$this->waitForReplication(); |
| 148 |
} |
| 149 |
|
| 150 |
public function doSpecialPageCacheUpdates( $dbw ) { |
| 151 |
foreach ( $this->getConfig()->get( MainConfigNames::SpecialPageCacheUpdates ) as $special => $call ) { |
| 152 |
|
| 153 |
if ( $this->hasOption( 'list' ) ) { |
| 154 |
$this->output( "$special [callback]\n" ); |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $special ) { |
| 159 |
if ( !is_callable( $call ) ) { |
| 160 |
$this->error( "Uncallable function $call!" ); |
| 161 |
continue; |
| 162 |
} |
| 163 |
$this->output( sprintf( '%-30s [callback] ', $special ) ); |
| 164 |
$t1 = microtime( true ); |
| 165 |
$call( $dbw ); |
| 166 |
$t2 = microtime( true ); |
| 167 |
|
| 168 |
$this->output( "completed in " ); |
| 169 |
$elapsed = $t2 - $t1; |
| 170 |
$hours = intval( $elapsed / 3600 ); |
| 171 |
$minutes = intval( (int)$elapsed % 3600 / 60 ); |
| 172 |
$seconds = $elapsed - $hours * 3600 - $minutes * 60; |
| 173 |
if ( $hours ) { |
| 174 |
$this->output( $hours . 'h ' ); |
| 175 |
} |
| 176 |
if ( $minutes ) { |
| 177 |
$this->output( $minutes . 'm ' ); |
| 178 |
} |
| 179 |
$this->output( sprintf( "%.2fs\n", $seconds ) ); |
| 180 |
|
| 181 |
$this->reopenAndWaitForReplicas(); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
$maintClass = UpdateSpecialPages::class; |
| 189 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 190 |
|