1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../../..'; |
4
|
|
|
require_once $basePath . '/maintenance/Maintenance.php'; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class for handling the rebuilding process of JSON namespaces |
9
|
|
|
* @author Toni Hermoso |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class RebuildJSONData extends Maintenance { |
|
|
|
|
13
|
|
|
public function __construct() { |
14
|
|
|
parent::__construct(); |
15
|
|
|
$this->addDescription( "\n" . |
16
|
|
|
"Script for rebuilding data stored in JSON stores\n" |
17
|
|
|
); |
18
|
|
|
$this->addDefaultParams(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @see Maintenance::addDefaultParams |
23
|
|
|
*/ |
24
|
|
|
protected function addDefaultParams() { |
25
|
|
|
$this->addOption( 'namespace', '<namespace> Namespace index number to be refreshed.', true, true, "ns" ); |
26
|
|
|
$this->addOption( 'dryrun', '<dryRun> If you don\'t really want to refresh information', false, false, "dr" ); |
27
|
|
|
$this->addOption( 'u', 'User to run the script', false, true ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see Maintenance::execute |
32
|
|
|
*/ |
33
|
|
|
public function execute() { |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ( !defined( 'SMW_VERSION' ) || !$GLOBALS['smwgSemanticsEnabled'] ) { |
36
|
|
|
|
37
|
|
|
$this->reportMessage( "\nYou need to have SMW enabled in order to run this maintenance script!\n" ); |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$ns = $this->getOption( "namespace", null ); |
42
|
|
|
$dryRun = $this->getOption( "dryrun", false); |
43
|
|
|
$u = $this->getOption( 'u', false ); |
44
|
|
|
|
45
|
|
|
$reportingInterval = 100; |
46
|
|
|
$dbr = wfGetDB( DB_SLAVE ); |
47
|
|
|
$ns_restrict = "page_namespace > -1"; |
48
|
|
|
$tables = array('page'); |
49
|
|
|
|
50
|
|
|
$seltables = array( 'page_id' ); |
51
|
|
|
// Need to do for NS |
52
|
|
|
if ( $ns > -1 ) { |
53
|
|
|
if ( is_numeric( $ns ) ) { |
54
|
|
|
$ns_restrict = "page_namespace = $ns"; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Default, no user |
59
|
|
|
$user = null; |
60
|
|
|
if ( $u ) { |
61
|
|
|
// $user = User::newSystemUser("SDImport"); |
|
|
|
|
62
|
|
|
$user = User::newFromName( $u ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$res = $dbr->select( $tables, |
67
|
|
|
$seltables, |
68
|
|
|
array( |
69
|
|
|
$ns_restrict ), |
70
|
|
|
__METHOD__ |
71
|
|
|
); |
72
|
|
|
$num = $dbr->numRows( $res ); |
73
|
|
|
$this->output( "$num articles...\n" ); |
74
|
|
|
$i = 0; |
75
|
|
|
foreach ( $res as $row ) { |
76
|
|
|
if ( !( ++$i % $reportingInterval ) ) { |
77
|
|
|
$this->output( "$i\n" ); |
78
|
|
|
wfWaitForSlaves(); // Doubt if necessary |
79
|
|
|
} |
80
|
|
|
if ( ! $dryRun ) { |
81
|
|
|
self::refreshArticle( $row->page_id, $user ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Run fixEditFromArticle for all links on a given page_id (and a user) |
90
|
|
|
* @param $id int The page_id |
91
|
|
|
*/ |
92
|
|
|
public static function refreshArticle( $pageid, $user ) { |
93
|
|
|
|
94
|
|
|
$wikipage = WikiPage::newFromID( $pageid ); |
95
|
|
|
|
96
|
|
|
if ( $wikipage === null ) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Check compatibility. Only if newer versions of MW |
101
|
|
|
if ( method_exists ( $wikipage, "getContent" ) ) { |
102
|
|
|
$contentModel = $wikipage->getContentModel(); |
103
|
|
|
if ( $contentModel === "json" || ! $wikipage->exists() ) { |
104
|
|
|
|
105
|
|
|
// Retrigger import |
106
|
|
|
$statusValue = new StatusValue(); |
107
|
|
|
$statusValue->setOK(true); |
108
|
|
|
$status = new Status(); |
109
|
|
|
$status->wrap( $statusValue ); |
110
|
|
|
|
111
|
|
|
// TODO: To be fixed |
112
|
|
|
SDImportData::saveJSONData( $wikipage, $user, $wikipage->getContent(), "Rebuild JSON", 0, null, null, 2, $wikipage->getRevision(), $status, false ); |
|
|
|
|
113
|
|
|
// $status = $wikipage->doEditContent( $wikipage, "Rebuild", EDIT_FORCE_BOT, false, $user ); |
|
|
|
|
114
|
|
|
// var_dump( $status ); |
|
|
|
|
115
|
|
|
// SDImportData::importJSON( $wikipage->getContent()->getNativeData(), $wikipage->getTitle()->getPrefixedText(), true ); |
|
|
|
|
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
$maintClass = 'RebuildJSONData'; |
126
|
|
|
require_once( DO_MAINTENANCE ); |
127
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.