PurgePage::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PurgePage;
4
5
use Job;
6
use JobQueueGroup;
7
use Title;
8
9
class PurgePage {
10
11
	public static function init() {
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
12
		$GLOBALS[ 'wgExtensionMessagesFiles' ][ 'PurgePageMagic' ] = __DIR__ . '/PurgePage.magic.php';
13
		$GLOBALS[ 'wgJobClasses' ][ 'parsePage' ] = 'PurgePage\\PageParseJob';
14
	}
15
16
	public static function registerParserFunction( \Parser &$parser ) {
17
18
		$parser->setFunctionHook( 'purge', function () {
19
20
			$params = func_get_args();
21
22
			if ( isset( $params[ 1 ] ) ) {
23
24
				$parser = $params[ 0 ];
25
				$pageName = $params[ 1 ];
26
27
				$title = Title::newFromText( $pageName );
28
29
				if ( $title !== null && $title->isContentPage() && $title->exists() ) {
30
					/** @var \ParserOptions $parserOptions */
31
					$parserOptions = $parser->getOptions();
32
					$job           = Job::factory( 'parsePage', $title, [ 'user' => $parserOptions->getUser(), 'lang' => $parserOptions->getUserLang() ] );
33
					JobQueueGroup::singleton()->lazyPush( $job );
34
				}
35
36
37
			}
38
39
		} );
40
41
		return true;
42
	}
43
}
44