SemanticMediaWiki /
SemanticApprovedRevs
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SMW\ApprovedRevs; |
||
| 4 | |||
| 5 | use MediaWiki\MediaWikiServices; |
||
| 6 | use Onoi\Cache\Cache; |
||
| 7 | use SMW\ApplicationFactory; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @license GNU GPL v2+ |
||
| 11 | * @since 1.0 |
||
| 12 | * |
||
| 13 | * @author mwjames |
||
| 14 | */ |
||
| 15 | class Hooks { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $handlers = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Cache |
||
| 24 | */ |
||
| 25 | private $cache; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @since 1.0 |
||
| 29 | * |
||
| 30 | * @param array $config |
||
| 31 | 2 | */ |
|
| 32 | 2 | public function __construct( $config = [] ) { |
|
| 33 | 2 | $this->registerHandlers( $config ); |
|
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @since 1.0 |
||
| 38 | * |
||
| 39 | * @param Cache $cache |
||
| 40 | 1 | */ |
|
| 41 | 1 | public function setCache( Cache $cache ) { |
|
| 42 | 1 | $this->cache = $cache; |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @since 1.0 |
||
| 47 | */ |
||
| 48 | public static function hasPropertyCollisions( $var ) { |
||
| 49 | |||
| 50 | if ( !isset( $var['sespgEnabledPropertyList'] ) ) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | |||
| 54 | // SESP properties! |
||
| 55 | $list = [ |
||
| 56 | '_APPROVED' => true, |
||
| 57 | '_APPROVEDBY' => true, |
||
| 58 | '_APPROVEDDATE' => true, |
||
| 59 | '_APPROVEDSTATUS' => true |
||
| 60 | ]; |
||
| 61 | |||
| 62 | foreach ( $var['sespgEnabledPropertyList'] as $key ) { |
||
| 63 | if ( isset( $list[$key] ) ) { |
||
| 64 | return $key; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | return false; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @since 1.0 |
||
| 73 | * |
||
| 74 | * @param array &$vars |
||
| 75 | */ |
||
| 76 | public static function initExtension( &$vars ) { |
||
| 77 | $version = 'UNKNOWN' ; |
||
| 78 | |||
| 79 | // See https://phabricator.wikimedia.org/T151136 |
||
| 80 | if ( isset( $credits['version'] ) ) { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 81 | $version = $credits['version']; |
||
| 82 | } |
||
| 83 | |||
| 84 | 1 | define( 'SMW_APPROVED_REVS_VERSION', $version ); |
|
| 85 | |||
| 86 | 1 | /** |
|
| 87 | 1 | * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Config::BeforeCompletion |
|
| 88 | * |
||
| 89 | * @since 1.0 |
||
| 90 | 1 | * |
|
| 91 | * @param array &$config |
||
| 92 | */ |
||
| 93 | $GLOBALS['wgHooks']['SMW::Config::BeforeCompletion'][] = function( &$config ) { |
||
| 94 | |||
| 95 | if ( isset( $config['smwgImportFileDirs'] ) ) { |
||
| 96 | $config['smwgImportFileDirs'] += [ 'sar' => __DIR__ . '/../data/import' ]; |
||
| 97 | 1 | } |
|
| 98 | 1 | ||
| 99 | 1 | return true; |
|
| 100 | }; |
||
| 101 | 1 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @since 1.0 |
||
| 105 | */ |
||
| 106 | 1 | public function register() { |
|
| 107 | 1 | foreach ( $this->handlers as $name => $callback ) { |
|
| 108 | \Hooks::register( $name, $callback ); |
||
| 109 | 1 | } |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | 1 | * @since 1.0 |
|
| 114 | 1 | */ |
|
| 115 | public function deregister() { |
||
| 116 | foreach ( array_keys( $this->handlers ) as $name ) { |
||
| 117 | 1 | MediaWikiServices::getInstance()->getHookContainer()->clear( $name ); |
|
| 118 | |||
| 119 | // Remove registered `wgHooks` hooks that are not cleared by the |
||
| 120 | // previous call |
||
| 121 | if ( isset( $GLOBALS['wgHooks'][$name] ) ) { |
||
| 122 | unset( $GLOBALS['wgHooks'][$name] ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | 1 | ||
| 127 | 1 | /** |
|
| 128 | * @since 1.0 |
||
| 129 | * |
||
| 130 | * @param string $name |
||
| 131 | * |
||
| 132 | * @return boolean |
||
| 133 | */ |
||
| 134 | public function isRegistered( $name ) { |
||
| 135 | return \Hooks::isRegistered( $name ); |
||
| 136 | } |
||
| 137 | 1 | ||
| 138 | 1 | /** |
|
| 139 | * @since 1.0 |
||
| 140 | * |
||
| 141 | * @param string $name |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function getHandlers( $name ) { |
||
| 146 | return \Hooks::getHandlers( $name ); |
||
| 147 | 1 | } |
|
| 148 | |||
| 149 | 1 | /** |
|
| 150 | 1 | * @since 1.0 |
|
| 151 | * |
||
| 152 | * @param Title $title |
||
| 153 | 1 | * @param integer $latestRevID |
|
| 154 | */ |
||
| 155 | public function onIsApprovedRevision( $title, $latestRevID ) { |
||
| 156 | |||
| 157 | $approvedRevsHandler = new ApprovedRevsHandler( |
||
| 158 | new ApprovedRevsFacade() |
||
| 159 | ); |
||
| 160 | |||
| 161 | return $approvedRevsHandler->isApprovedUpdate( $title, $latestRevID ); |
||
| 162 | 1 | } |
|
| 163 | |||
| 164 | 1 | /** |
|
| 165 | 1 | * @since 1.0 |
|
| 166 | * |
||
| 167 | * @param Title $title |
||
| 168 | 1 | * @param Revision|null &$revision |
|
| 169 | */ |
||
| 170 | 1 | public function onChangeRevision( $title, &$revision ) { |
|
| 171 | |||
| 172 | $approvedRevsHandler = new ApprovedRevsHandler( |
||
| 173 | new ApprovedRevsFacade() |
||
| 174 | ); |
||
| 175 | |||
| 176 | $approvedRevsHandler->doChangeRevision( $title, $revision ); |
||
| 177 | |||
| 178 | return true; |
||
| 179 | 1 | } |
|
| 180 | |||
| 181 | 1 | /** |
|
| 182 | 1 | * @since 1.0 |
|
| 183 | * |
||
| 184 | * @param Title $title |
||
| 185 | 1 | * @param integer &$latestRevID |
|
| 186 | */ |
||
| 187 | 1 | public function onOverrideRevisionID( $title, &$latestRevID ) { |
|
| 188 | |||
| 189 | $approvedRevsHandler = new ApprovedRevsHandler( |
||
| 190 | new ApprovedRevsFacade() |
||
| 191 | ); |
||
| 192 | |||
| 193 | $approvedRevsHandler->doChangeRevisionID( $title, $latestRevID ); |
||
| 194 | |||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | 1 | /** |
|
| 199 | * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Property::initProperties |
||
| 200 | 1 | * |
|
| 201 | 1 | * @since 1.0 |
|
| 202 | * |
||
| 203 | 1 | * @param ProertyRegistry $$registry |
|
| 204 | * @param integer &$latestRevID |
||
| 205 | */ |
||
| 206 | public function onInitProperties( $registry ) { |
||
| 207 | |||
| 208 | $propertyRegistry = new PropertyRegistry(); |
||
| 209 | $propertyRegistry->register( $registry ); |
||
| 210 | |||
| 211 | return true; |
||
| 212 | } |
||
| 213 | |||
| 214 | 1 | /** |
|
| 215 | * @see https://www.semantic-mediawiki.org/wiki/Hooks#SMWStore::updateDataBefore |
||
| 216 | 1 | * |
|
| 217 | 1 | * @since 1.0 |
|
| 218 | * |
||
| 219 | * @param ProertyRegistry $$registry |
||
| 220 | 1 | * @param integer &$latestRevID |
|
| 221 | 1 | */ |
|
| 222 | public function onUpdateDataBefore( $store, $semanticData ) { |
||
| 223 | |||
| 224 | 1 | $propertyAnnotator = new PropertyAnnotator( |
|
| 225 | new ServicesFactory() |
||
| 226 | 1 | ); |
|
| 227 | |||
| 228 | $propertyAnnotator->setLogger( |
||
| 229 | ApplicationFactory::getInstance()->getMediaWikiLogger( 'smw-approved-revs' ) |
||
| 230 | ); |
||
| 231 | |||
| 232 | $propertyAnnotator->addAnnotation( $semanticData ); |
||
| 233 | |||
| 234 | return true; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see ?? |
||
| 239 | 1 | * |
|
| 240 | * @since 1.0 |
||
| 241 | 1 | * |
|
| 242 | * @param ParserOutput $output |
||
| 243 | 1 | * @param Title $title |
|
| 244 | * @param integer $rev_id |
||
| 245 | * @param string $content |
||
| 246 | */ |
||
| 247 | public function onApprovedRevsRevisionApproved( $output, $title, $rev_id, $content ) { |
||
| 248 | |||
| 249 | $ttl = 60 * 60; // 1hr |
||
| 250 | |||
| 251 | if ( $this->cache === null ) { |
||
| 252 | 1 | $this->cache = ApplicationFactory::getInstance()->getCache(); |
|
| 253 | 1 | } |
|
| 254 | |||
| 255 | 1 | // Send an event to ParserAfterTidy and allow it to pass the preliminary |
|
| 256 | // test even in cases where the content doesn't contain any SMW related |
||
| 257 | // annotations. It is to ensure that when an agent switches to a blank |
||
| 258 | // version (no SMW related annotations or categories) the update is carried |
||
| 259 | // out and the store is able to remove any remaining annotations. |
||
| 260 | $key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() ); |
||
| 261 | $this->cache->save( $key, $rev_id, $ttl ); |
||
| 262 | |||
| 263 | return true; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @see ?? |
||
| 268 | 1 | * |
|
| 269 | * @since 1.0 |
||
| 270 | 1 | * |
|
| 271 | * @param Parser $parser |
||
| 272 | 1 | * @param Title $title |
|
| 273 | * @param integer $timestamp |
||
| 274 | * @param string $sha1 |
||
| 275 | */ |
||
| 276 | public function onApprovedRevsFileRevisionApproved( $parser, $title, $timestamp, $sha1 ) { |
||
| 277 | 1 | ||
| 278 | 1 | $ttl = 60 * 60; // 1hr |
|
| 279 | |||
| 280 | 1 | if ( $this->cache === null ) { |
|
| 281 | $this->cache = ApplicationFactory::getInstance()->getCache(); |
||
| 282 | } |
||
| 283 | |||
| 284 | // @see onApprovedRevsRevisionApproved for the same reason |
||
| 285 | $key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() ); |
||
| 286 | $this->cache->save( $key, $sha1, $ttl ); |
||
| 287 | |||
| 288 | return true; |
||
| 289 | } |
||
| 290 | |||
| 291 | 1 | /** |
|
| 292 | * @see https://www.semantic-mediawiki.org/wiki/Hooks#... |
||
| 293 | 1 | * |
|
| 294 | 1 | * @since 1.0 |
|
| 295 | * |
||
| 296 | * @param Title $title |
||
| 297 | 1 | * @param File &$file |
|
| 298 | */ |
||
| 299 | 1 | public function onChangeFile( $title, &$file ) { |
|
| 300 | |||
| 301 | $approvedRevsHandler = new ApprovedRevsHandler( |
||
| 302 | 2 | new ApprovedRevsFacade() |
|
| 303 | 2 | ); |
|
| 304 | 2 | ||
| 305 | 2 | $approvedRevsHandler->doChangeFile( $title, $file ); |
|
| 306 | 2 | ||
| 307 | 2 | return true; |
|
| 308 | 2 | } |
|
| 309 | 2 | ||
| 310 | 2 | private function registerHandlers( $config ) { |
|
| 311 | 2 | $this->handlers = [ |
|
| 312 | 'ApprovedRevsRevisionApproved' => [ $this, 'onApprovedRevsRevisionApproved' ], |
||
| 313 | 2 | 'ApprovedRevsFileRevisionApproved' => [ $this, 'onApprovedRevsFileRevisionApproved' ], |
|
| 314 | 'SMW::RevisionGuard::IsApprovedRevision' => [ $this, 'onIsApprovedRevision' ], |
||
| 315 | 'SMW::RevisionGuard::ChangeRevision' => [ $this, 'onChangeRevision' ], |
||
| 316 | 'SMW::RevisionGuard::ChangeRevisionID' => [ $this, 'onOverrideRevisionID' ], |
||
| 317 | 'SMW::RevisionGuard::ChangeFile' => [ $this, 'onChangeFile' ], |
||
| 318 | 'SMW::Property::initProperties' => [ $this, 'onInitProperties' ], |
||
| 319 | 'SMWStore::updateDataBefore' => [ $this, 'onUpdateDataBefore' ], |
||
| 320 | ]; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @since 1.0 |
||
| 325 | */ |
||
| 326 | public static function onExtensionFunction() { |
||
| 327 | |||
| 328 | if ( !defined( 'SMW_VERSION' ) ) { |
||
| 329 | if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) { |
||
| 330 | die( "\nThe 'Semantic Approved Revs' extension requires the 'Semantic MediaWiki' extension to be installed and enabled.\n" ); |
||
|
0 ignored issues
–
show
|
|||
| 331 | } else { |
||
| 332 | die( |
||
|
0 ignored issues
–
show
|
|||
| 333 | '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticApprovedRevs/">Semantic Approved Revs</a> extension' . |
||
| 334 | ' requires the <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> extension to be installed and enabled.<br />' |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | // We expected to check for APPROVED_REVS_VERSION but the extension and |
||
| 340 | // its `extension.json` doesn't set the constant so we have to rely on |
||
| 341 | // active class loading (which is an anti-pattern) to check whether the |
||
| 342 | // extension is enabled or not! |
||
| 343 | if ( !class_exists( 'ApprovedRevs' ) ) { |
||
| 344 | if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) { |
||
| 345 | die( "\nThe 'Semantic Approved Revs' extension requires the 'Approved Revs' extension to be installed and enabled.\n" ); |
||
|
0 ignored issues
–
show
|
|||
| 346 | } else { |
||
| 347 | die( |
||
|
0 ignored issues
–
show
|
|||
| 348 | '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticApprovedRevs/">Semantic Approved Revs</a> extension' . |
||
| 349 | ' requires the <a href="https://www.mediawiki.org/wiki/Extension:Approved_Revs">Approved Revs</a> extension to be installed and enabled.<br />' |
||
| 350 | ); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | if ( defined( 'SESP_VERSION' ) && version_compare( SESP_VERSION, '2.1.0', '<' ) && ( $prop = Hooks::hasPropertyCollisions( $GLOBALS ) ) !== false ) { |
||
| 355 | die( |
||
|
0 ignored issues
–
show
|
|||
| 356 | "\nPlease remove the `$prop` property (defined by the SemanticExtraSpecialProperties extension) and switch to the new SESP version 2.1" . |
||
| 357 | " to avoid collision with the 'Semantic Approved Revs' list of properties.\n" |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | $hooks = new Hooks(); |
||
| 362 | $hooks->register(); |
||
| 363 | } |
||
| 364 | |||
| 365 | } |
||
| 366 |