Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 117 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
SpecialCargoRecreateData | |
0.00% |
0 / 117 |
|
0.00% |
0 / 3 |
420 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 101 |
|
0.00% |
0 / 1 |
342 | |||
getNumPagesThatCallTemplate | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | use MediaWiki\Linker\LinkTarget; |
4 | use MediaWiki\MediaWikiServices; |
5 | |
6 | /** |
7 | * Displays an interface to let users recreate data via the Cargo |
8 | * extension. |
9 | * |
10 | * @author Yaron Koren |
11 | * @ingroup Cargo |
12 | */ |
13 | |
14 | class SpecialCargoRecreateData extends UnlistedSpecialPage { |
15 | public $mTemplateTitle; |
16 | public $mTableName; |
17 | public $mIsDeclared; |
18 | |
19 | public function __construct( $templateTitle = null, $tableName = null, $isDeclared = false ) { |
20 | parent::__construct( 'RecreateCargoData', 'recreatecargodata' ); |
21 | $this->mTemplateTitle = $templateTitle; |
22 | $this->mTableName = $tableName; |
23 | $this->mIsDeclared = $isDeclared; |
24 | } |
25 | |
26 | public function execute( $query = null ) { |
27 | global $cgScriptPath; |
28 | |
29 | $this->checkPermissions(); |
30 | |
31 | $out = $this->getOutput(); |
32 | $out->enableOOUI(); |
33 | $this->setHeaders(); |
34 | |
35 | if ( $this->mTableName == null ) { |
36 | $this->mTableName = $query; |
37 | } |
38 | $tableExists = CargoUtils::tableFullyExists( $this->mTableName ); |
39 | if ( !$tableExists ) { |
40 | $out->setPageTitle( $this->msg( 'cargo-createdatatable' )->parse() ); |
41 | } |
42 | |
43 | // Disable page if "replacement table" exists. |
44 | $possibleReplacementTable = $this->mTableName . '__NEXT'; |
45 | if ( CargoUtils::tableFullyExists( $this->mTableName ) && CargoUtils::tableFullyExists( $possibleReplacementTable ) ) { |
46 | $text = $this->msg( 'cargo-recreatedata-replacementexists', $this->mTableName, $possibleReplacementTable )->parse(); |
47 | $ctURL = SpecialPage::getTitleFor( 'CargoTables' )->getFullURL(); |
48 | $viewURL = $ctURL . '/' . $this->mTableName; |
49 | $viewURL .= strpos( $viewURL, '?' ) ? '&' : '?'; |
50 | $viewURL .= "_replacement"; |
51 | $viewReplacementText = $this->msg( 'cargo-cargotables-viewreplacementlink' )->parse(); |
52 | |
53 | $text .= ' (' . Xml::element( 'a', [ 'href' => $viewURL ], $viewReplacementText ) . ')'; |
54 | $out->addHTML( $text ); |
55 | return true; |
56 | } |
57 | |
58 | $specialTableNames = CargoUtils::specialTableNames(); |
59 | if ( !$this->mTemplateTitle && !in_array( $this->mTableName, $specialTableNames ) ) { |
60 | // TODO - show an error message. |
61 | return true; |
62 | } |
63 | |
64 | $out->addModules( 'ext.cargo.recreatedata' ); |
65 | |
66 | $templateData = []; |
67 | $dbr = CargoUtils::getMainDBForRead(); |
68 | if ( $this->mTemplateTitle === null ) { |
69 | if ( $this->mTableName == '_pageData' ) { |
70 | $conds = null; |
71 | } elseif ( $this->mTableName == '_fileData' ) { |
72 | $conds = 'page_namespace = ' . NS_FILE; |
73 | } elseif ( $this->mTableName == '_bpmnData' ) { |
74 | $conds = 'page_namespace = ' . FD_NS_BPMN; |
75 | } else { // if ( $this->mTableName == '_ganttData' ) { |
76 | $conds = 'page_namespace = ' . FD_NS_GANTT; |
77 | } |
78 | $numTotalPages = $dbr->selectField( 'page', 'COUNT(*)', $conds, __METHOD__ ); |
79 | } else { |
80 | $numTotalPages = null; |
81 | $templateData[] = [ |
82 | 'name' => $this->mTemplateTitle->getText(), |
83 | 'numPages' => $this->getNumPagesThatCallTemplate( $dbr, $this->mTemplateTitle ) |
84 | ]; |
85 | } |
86 | |
87 | if ( $this->mIsDeclared ) { |
88 | // Get all attached templates. |
89 | $res = $dbr->select( 'page_props', |
90 | [ |
91 | 'pp_page' |
92 | ], |
93 | [ |
94 | 'pp_value' => $this->mTableName, |
95 | 'pp_propname' => 'CargoAttachedTable' |
96 | ], |
97 | __METHOD__ |
98 | ); |
99 | foreach ( $res as $row ) { |
100 | $templateID = $row->pp_page; |
101 | $attachedTemplateTitle = Title::newFromID( $templateID ); |
102 | $numPages = $this->getNumPagesThatCallTemplate( $dbr, $attachedTemplateTitle ); |
103 | $attachedTemplateName = $attachedTemplateTitle->getText(); |
104 | $templateData[] = [ |
105 | 'name' => $attachedTemplateName, |
106 | 'numPages' => $numPages |
107 | ]; |
108 | } |
109 | } |
110 | |
111 | $ct = SpecialPage::getTitleFor( 'CargoTables' ); |
112 | $viewTableURL = $ct->getLocalURL() . '/' . $this->mTableName; |
113 | |
114 | // Store all the necesssary data on the page. |
115 | $text = Html::element( 'div', [ |
116 | 'hidden' => 'true', |
117 | 'id' => 'recreateDataData', |
118 | // 'cargoscriptpath' is not data- |
119 | // specific, but this seemed like the |
120 | // easiest way to pass it over without |
121 | // interfering with any other pages. |
122 | 'cargoscriptpath' => $cgScriptPath, |
123 | 'tablename' => $this->mTableName, |
124 | 'isspecialtable' => ( $this->mTemplateTitle == null ), |
125 | 'isdeclared' => $this->mIsDeclared, |
126 | 'totalpages' => $numTotalPages, |
127 | 'viewtableurl' => $viewTableURL |
128 | ], json_encode( $templateData ) ); |
129 | |
130 | // Simple form. |
131 | $text .= '<div id="recreateDataCanvas">' . "\n"; |
132 | if ( $tableExists ) { |
133 | $checkBox = new OOUI\FieldLayout( |
134 | new OOUI\CheckboxInputWidget( [ |
135 | 'name' => 'createReplacement', |
136 | 'selected' => true, |
137 | 'value' => 1, |
138 | ] ), |
139 | [ |
140 | 'label' => $this->msg( 'cargo-recreatedata-createreplacement' )->parse(), |
141 | 'align' => 'inline', |
142 | 'infusable' => true, |
143 | ] |
144 | ); |
145 | $text .= Html::rawElement( 'p', null, $checkBox ); |
146 | } |
147 | |
148 | if ( $this->mTemplateTitle == null ) { |
149 | $msg = $tableExists ? 'cargo-recreatedata-recreatetable' : 'cargo-recreatedata-createtable'; |
150 | $text .= Html::element( 'p', null, $this->msg( $msg, $this->mTableName )->parse() ); |
151 | } else { |
152 | $msg = $tableExists ? 'cargo-recreatedata-desc' : 'cargo-recreatedata-createdata'; |
153 | $text .= Html::element( 'p', null, $this->msg( $msg )->parse() ); |
154 | } |
155 | |
156 | $text .= new OOUI\ButtonInputWidget( [ |
157 | 'id' => 'cargoSubmit', |
158 | 'label' => $this->msg( 'ok' )->parse(), |
159 | 'flags' => [ 'primary', 'progressive' ] |
160 | ] ); |
161 | $text .= "\n</div>"; |
162 | |
163 | $out->addHTML( $text ); |
164 | |
165 | return true; |
166 | } |
167 | |
168 | public function getNumPagesThatCallTemplate( $dbr, LinkTarget $templateTitle ) { |
169 | $conds = [ "tl_from=page_id" ]; |
170 | $linkTargetLookup = MediaWikiServices::getInstance()->getLinkTargetLookup(); |
171 | $conds['tl_target_id'] = $linkTargetLookup->getLinkTargetId( $templateTitle ); |
172 | |
173 | $res = $dbr->select( |
174 | [ 'page', 'templatelinks' ], |
175 | 'COUNT(*) AS total', |
176 | $conds, |
177 | __METHOD__, |
178 | [] |
179 | ); |
180 | $row = $res->fetchRow(); |
181 | return intval( $row['total'] ); |
182 | } |
183 | |
184 | } |