]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/Cite/includes/CiteHooks.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / extensions / Cite / includes / CiteHooks.php
1 <?php
2 /**
3  * Cite extension hooks
4  *
5  * @file
6  * @ingroup Extensions
7  * @copyright 2011-2017 Cite VisualEditor Team and others; see AUTHORS.txt
8  * @license The MIT License (MIT); see MIT-LICENSE.txt
9  */
10
11 class CiteHooks {
12         /**
13          * Convert the content model of a message that is actually JSON to JSON. This
14          * only affects validation and UI when saving and editing, not loading the
15          * content.
16          *
17          * @param Title $title
18          * @param string $model
19          * @return bool
20          */
21         public static function onContentHandlerDefaultModelFor( Title $title, &$model ) {
22                 if (
23                         $title->inNamespace( NS_MEDIAWIKI ) &&
24                         (
25                                 $title->getText() == 'Visualeditor-cite-tool-definition.json' ||
26                                 $title->getText() == 'Cite-tool-definition.json'
27                         )
28                 ) {
29                         $model = CONTENT_MODEL_JSON;
30                 }
31
32                 return true;
33         }
34
35         /**
36          * Conditionally register the unit testing module for the ext.cite.visualEditor module
37          * only if that module is loaded
38          *
39          * @param array $testModules The array of registered test modules
40          * @param ResourceLoader $resourceLoader The reference to the resource loader
41          * @return true
42          */
43         public static function onResourceLoaderTestModules(
44                 array &$testModules,
45                 ResourceLoader &$resourceLoader
46         ) {
47                 $resourceModules = $resourceLoader->getConfig()->get( 'ResourceModules' );
48
49                 if (
50                         isset( $resourceModules[ 'ext.visualEditor.mediawiki' ] ) ||
51                         $resourceLoader->isModuleRegistered( 'ext.visualEditor.mediawiki' )
52                 ) {
53                         $testModules['qunit']['ext.cite.visualEditor.test'] = [
54                                 'scripts' => [
55                                         'modules/ve-cite/tests/ve.dm.citeExample.js',
56                                         'modules/ve-cite/tests/ve.dm.Converter.test.js',
57                                         'modules/ve-cite/tests/ve.dm.InternalList.test.js',
58                                         'modules/ve-cite/tests/ve.dm.Transaction.test.js',
59                                         'modules/ve-cite/tests/ve.ui.MWWikitextStringTransferHandler.test.js',
60                                 ],
61                                 'dependencies' => [
62                                         'ext.cite.visualEditor',
63                                         'ext.visualEditor.test'
64                                 ],
65                                 'localBasePath' => dirname( __DIR__ ),
66                                 'remoteExtPath' => 'Cite',
67                         ];
68                 }
69
70                 return true;
71         }
72
73         /**
74          * Conditionally register resource loader modules that depends on the
75          * VisualEditor MediaWiki extension.
76          *
77          * @param $resourceLoader
78          * @return true
79          */
80         public static function onResourceLoaderRegisterModules( &$resourceLoader ) {
81                 if ( ! class_exists( 'VisualEditorHooks' ) ) {
82                         return true;
83                 }
84
85                 $dir = dirname( __DIR__ ) . DIRECTORY_SEPARATOR;
86
87                 $resourceLoader->register( "ext.cite.visualEditor.core", [
88                         'localBasePath' => $dir . 'modules',
89                         'remoteExtPath' => 'Cite/modules',
90                         "scripts" => [
91                                 "ve-cite/ve.dm.MWReferenceModel.js",
92                                 "ve-cite/ve.dm.MWReferencesListNode.js",
93                                 "ve-cite/ve.dm.MWReferenceNode.js",
94                                 "ve-cite/ve.ce.MWReferencesListNode.js",
95                                 "ve-cite/ve.ce.MWReferenceNode.js",
96                                 "ve-cite/ve.ui.MWReferencesListCommand.js"
97                         ],
98                         "styles" => [
99                                 "ve-cite/ve.ce.MWReferencesListNode.css",
100                                 "ve-cite/ve.ce.MWReferenceNode.css"
101                         ],
102                         "dependencies" => [
103                                 "ext.visualEditor.mwcore"
104                         ],
105                         "messages" => [
106                                 "cite-ve-referenceslist-isempty",
107                                 "cite-ve-referenceslist-isempty-default",
108                                 "cite-ve-referenceslist-missingref",
109                                 "visualeditor-internal-list-diff-default-group-name-mwreference",
110                                 "visualeditor-internal-list-diff-group-name-mwreference"
111                         ],
112                         "targets" => [
113                                 "desktop",
114                                 "mobile"
115                         ]
116                 ] );
117
118                 $resourceLoader->register( "ext.cite.visualEditor.data",
119                         [ "class" => "CiteDataModule" ] );
120
121                 $resourceLoader->register( "ext.cite.visualEditor", [
122                         'localBasePath' => $dir . 'modules',
123                         'remoteExtPath' => 'Cite/modules',
124                         "scripts" => [
125                                 "ve-cite/ve.ui.MWReferenceGroupInputWidget.js",
126                                 "ve-cite/ve.ui.MWReferenceSearchWidget.js",
127                                 "ve-cite/ve.ui.MWReferenceResultWidget.js",
128                                 "ve-cite/ve.ui.MWUseExistingReferenceCommand.js",
129                                 "ve-cite/ve.ui.MWCitationDialog.js",
130                                 "ve-cite/ve.ui.MWReferencesListDialog.js",
131                                 "ve-cite/ve.ui.MWReferenceDialog.js",
132                                 "ve-cite/ve.ui.MWReferenceDialogTool.js",
133                                 "ve-cite/ve.ui.MWCitationDialogTool.js",
134                                 "ve-cite/ve.ui.MWReferenceContextItem.js",
135                                 "ve-cite/ve.ui.MWReferencesListContextItem.js",
136                                 "ve-cite/ve.ui.MWCitationContextItem.js",
137                                 "ve-cite/ve.ui.MWCitationAction.js",
138                                 "ve-cite/ve.ui.MWReference.init.js"
139                         ],
140                         "styles" => [
141                                 "ve-cite/ve.ui.MWReferenceContextItem.css",
142                                 "ve-cite/ve.ui.MWReferenceGroupInputWidget.css",
143                                 "ve-cite/ve.ui.MWReferenceIcons.css",
144                                 "ve-cite/ve.ui.MWReferenceResultWidget.css",
145                                 "ve-cite/ve.ui.MWReferenceSearchWidget.css"
146                         ],
147                         "dependencies" => [
148                                 "oojs-ui.styles.icons-alerts",
149                                 "oojs-ui.styles.icons-interactions",
150                                 "ext.cite.visualEditor.core",
151                                 "ext.cite.visualEditor.data",
152                                 "ext.cite.style",
153                                 "ext.cite.styles",
154                                 "ext.visualEditor.mwtransclusion",
155                                 "ext.visualEditor.mediawiki"
156                         ],
157                         "messages" => [
158                                 "cite-ve-changedesc-ref-group-both",
159                                 "cite-ve-changedesc-ref-group-from",
160                                 "cite-ve-changedesc-ref-group-to",
161                                 "cite-ve-changedesc-reflist-group-both",
162                                 "cite-ve-changedesc-reflist-group-from",
163                                 "cite-ve-changedesc-reflist-group-to",
164                                 "cite-ve-changedesc-reflist-item-id",
165                                 "cite-ve-dialog-reference-editing-reused",
166                                 "cite-ve-dialog-reference-options-group-label",
167                                 "cite-ve-dialog-reference-options-group-placeholder",
168                                 "cite-ve-dialog-reference-options-name-label",
169                                 "cite-ve-dialog-reference-options-responsive-label",
170                                 "cite-ve-dialog-reference-options-section",
171                                 "cite-ve-dialog-reference-placeholder",
172                                 "cite-ve-dialog-reference-title",
173                                 "cite-ve-dialog-reference-useexisting-full-label",
174                                 "cite-ve-dialog-reference-useexisting-label",
175                                 "cite-ve-dialog-reference-useexisting-tool",
176                                 "cite-ve-dialog-referenceslist-contextitem-description-general",
177                                 "cite-ve-dialog-referenceslist-contextitem-description-named",
178                                 "cite-ve-dialog-referenceslist-title",
179                                 "cite-ve-dialogbutton-citation-educationpopup-title",
180                                 "cite-ve-dialogbutton-citation-educationpopup-text",
181                                 "cite-ve-dialogbutton-reference-full-label",
182                                 "cite-ve-dialogbutton-reference-tooltip",
183                                 "cite-ve-dialogbutton-reference-title",
184                                 "cite-ve-dialogbutton-referenceslist-tooltip",
185                                 "cite-ve-reference-input-placeholder",
186                                 "cite-ve-toolbar-group-label",
187                                 "cite-ve-othergroup-item"
188                         ],
189                         "targets" => [
190                                 "desktop",
191                                 "mobile"
192                         ]
193                 ] );
194                 return true;
195         }
196
197         /**
198          * Callback for LinksUpdate hook
199          * Post-output processing of references property, for proper db storage
200          * Deferred to avoid performance overhead when outputting the page
201          *
202          * @param LinksUpdate $linksUpdate
203          */
204         public static function onLinksUpdate( LinksUpdate &$linksUpdate ) {
205                 global $wgCiteStoreReferencesData, $wgCiteCacheRawReferencesOnParse;
206                 if ( !$wgCiteStoreReferencesData ) {
207                         return;
208                 }
209                 $refData = $linksUpdate->getParserOutput()->getExtensionData( Cite::EXT_DATA_KEY );
210                 if ( $refData === null ) {
211                         return;
212                 }
213                 if ( $wgCiteCacheRawReferencesOnParse ) {
214                         // caching
215                         $cache = ObjectCache::getMainWANInstance();
216                         $articleID = $linksUpdate->getTitle()->getArticleID();
217                         $key = $cache->makeKey( Cite::EXT_DATA_KEY, $articleID );
218                         $cache->set( $key, $refData, Cite::CACHE_DURATION_ONPARSE );
219                 }
220                 // JSON encode
221                 $ppValue = FormatJson::encode( $refData, false, FormatJson::ALL_OK );
222                 // GZIP encode references data at maximum compression
223                 $ppValue = gzencode( $ppValue, 9 );
224                 // split the string in smaller parts that can fit into a db blob
225                 $ppValues = str_split( $ppValue, Cite::MAX_STORAGE_LENGTH );
226                 foreach ( $ppValues as $num => $ppValue ) {
227                         $key = 'references-' . intval( $num + 1 );
228                         $linksUpdate->mProperties[$key] = $ppValue;
229                 }
230                 $linksUpdate->getParserOutput()->setExtensionData( Cite::EXT_DATA_KEY, null );
231         }
232
233         /**
234          * Callback for LinksUpdateComplete hook
235          * If $wgCiteCacheRawReferencesOnParse is set to false, purges the cache
236          * when references are modified
237          *
238          * @param LinksUpdate $linksUpdate
239          */
240         public static function onLinksUpdateComplete( LinksUpdate &$linksUpdate ) {
241                 global $wgCiteStoreReferencesData, $wgCiteCacheRawReferencesOnParse;
242                 if ( !$wgCiteStoreReferencesData || $wgCiteCacheRawReferencesOnParse ) {
243                         return;
244                 }
245                 // if we can, avoid clearing the cache when references were not changed
246                 if ( method_exists( $linksUpdate, 'getAddedProperties' )
247                         && method_exists( $linksUpdate, 'getRemovedProperties' )
248                 ) {
249                         $addedProps = $linksUpdate->getAddedProperties();
250                         $removedProps = $linksUpdate->getRemovedProperties();
251                         if ( !isset( $addedProps['references-1'] )
252                                 && !isset( $removedProps['references-1'] )
253                         ) {
254                                 return;
255                         }
256                 }
257                 $cache = ObjectCache::getMainWANInstance();
258                 $articleID = $linksUpdate->getTitle()->getArticleID();
259                 $key = $cache->makeKey( Cite::EXT_DATA_KEY, $articleID );
260                 // delete with reduced hold off period (LinksUpdate uses a master connection)
261                 $cache->delete( $key, WANObjectCache::MAX_COMMIT_DELAY );
262         }
263
264         /**
265          * Adds extra variables to the global config
266          */
267         public static function onResourceLoaderGetConfigVars( array &$vars ) {
268                 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'cite' );
269                 $vars['wgCiteVisualEditorOtherGroup'] = $config->get( 'CiteVisualEditorOtherGroup' );
270                 $vars['wgCiteResponsiveReferences'] = $config->get( 'CiteResponsiveReferences' );
271                 return true;
272         }
273
274         /**
275          * Hook: APIQuerySiteInfoGeneralInfo
276          *
277          * Expose configs via action=query&meta=siteinfo
278          *
279          * @param ApiQuerySiteInfo $api
280          * @param array &$data
281          */
282         public static function onAPIQuerySiteInfoGeneralInfo( ApiQuerySiteInfo $api, array &$data ) {
283                 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'cite' );
284                 $data['citeresponsivereferences'] = $config->get( 'CiteResponsiveReferences' );
285         }
286 }