MediaWiki  master
categoryChangesAsRdfTest.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
11  public function setUp() {
12  parent::setUp();
13  $this->setMwGlobals( [
14  'wgServer' => 'http://acme.test',
15  'wgCanonicalServer' => 'http://acme.test',
16  'wgArticlePath' => '/wiki/$1',
17  ] );
18  }
19 
20  public function provideCategoryData() {
21  return [
22  'delete category' => [
23  __DIR__ . "/../data/categoriesrdf/delete.sparql",
24  'getDeletedCatsIterator',
25  'handleDeletes',
26  [
27  (object)[ 'rc_title' => 'Test', 'rc_cur_id' => 1, '_processed' => 1 ],
28  (object)[ 'rc_title' => 'Test 2', 'rc_cur_id' => 2, '_processed' => 2 ],
29  ],
30  ],
31  'move category' => [
32  __DIR__ . "/../data/categoriesrdf/move.sparql",
33  'getMovedCatsIterator',
34  'handleMoves',
35  [
36  (object)[
37  'rc_title' => 'Test',
38  'rc_cur_id' => 4,
39  'page_title' => 'MovedTo',
40  'page_namespace' => NS_CATEGORY,
41  '_processed' => 4,
42  'pp_propname' => null,
43  'cat_pages' => 10,
44  'cat_subcats' => 2,
45  'cat_files' => 1,
46  ],
47  (object)[
48  'rc_title' => 'MovedTo',
49  'rc_cur_id' => 4,
50  'page_title' => 'MovedAgain',
51  'page_namespace' => NS_CATEGORY,
52  'pp_propname' => 'hiddencat',
53  'cat_pages' => 10,
54  'cat_subcats' => 2,
55  'cat_files' => 1,
56  ],
57  (object)[
58  'rc_title' => 'Test 2',
59  'rc_cur_id' => 5,
60  'page_title' => 'AlsoMoved',
61  'page_namespace' => NS_CATEGORY,
62  '_processed' => 5,
63  'pp_propname' => null,
64  'cat_pages' => 10,
65  'cat_subcats' => 2,
66  'cat_files' => 1,
67  ],
68  (object)[
69  'rc_title' => 'Test 3',
70  'rc_cur_id' => 6,
71  'page_title' => 'MovedOut',
72  'page_namespace' => NS_MAIN,
73  'pp_propname' => null,
74  'cat_pages' => 10,
75  'cat_subcats' => 2,
76  'cat_files' => 1,
77  ],
78  (object)[
79  'rc_title' => 'Test 4',
80  'rc_cur_id' => 7,
81  'page_title' => 'Already Done',
82  'page_namespace' => NS_CATEGORY,
83  'pp_propname' => null,
84  'cat_pages' => 10,
85  'cat_subcats' => 2,
86  'cat_files' => 1,
87  ],
88  ],
89  [ 7 => true ],
90  ],
91  'restore deleted category' => [
92  __DIR__ . "/../data/categoriesrdf/restore.sparql",
93  'getRestoredCatsIterator',
94  'handleRestores',
95  [
96  (object)[
97  'rc_title' => 'Restored cat',
98  'rc_cur_id' => 10,
99  '_processed' => 10,
100  'pp_propname' => null,
101  'cat_pages' => 10,
102  'cat_subcats' => 2,
103  'cat_files' => 1,
104  ],
105  (object)[
106  'rc_title' => 'Restored again',
107  'rc_cur_id' => 10,
108  'pp_propname' => null,
109  'cat_pages' => 10,
110  'cat_subcats' => 2,
111  'cat_files' => 1,
112  ],
113  (object)[
114  'rc_title' => 'Already seen',
115  'rc_cur_id' => 11,
116  'pp_propname' => null,
117  'cat_pages' => 10,
118  'cat_subcats' => 2,
119  'cat_files' => 1,
120  ],
121  ],
122  [ 11 => true ],
123  ],
124  'new page' => [
125  __DIR__ . "/../data/categoriesrdf/new.sparql",
126  'getNewCatsIterator',
127  'handleAdds',
128  [
129  (object)[
130  'rc_title' => 'New category',
131  'rc_cur_id' => 20,
132  '_processed' => 20,
133  'pp_propname' => null,
134  'cat_pages' => 10,
135  'cat_subcats' => 2,
136  'cat_files' => 1,
137  ],
138  (object)[
139  'rc_title' => 'Новая категория 😃',
140  'rc_cur_id' => 21,
141  '_processed' => 21,
142  'pp_propname' => 'hiddencat',
143  'cat_pages' => 10,
144  'cat_subcats' => 2,
145  'cat_files' => 1,
146  ],
147  (object)[
148  'rc_title' => 'Processed already',
149  'rc_cur_id' => 22,
150  ],
151  ],
152  [ 22 => true ],
153  ],
154  'edit category' => [
155  __DIR__ . "/../data/categoriesrdf/edit.sparql",
156  'getChangedCatsIterator',
157  'handleEdits',
158  [
159  (object)[
160  'rc_title' => 'Changed category',
161  'rc_cur_id' => 30,
162  '_processed' => 30,
163  'pp_propname' => null,
164  'cat_pages' => 10,
165  'cat_subcats' => 2,
166  'cat_files' => 1,
167  ],
168  (object)[
169  'rc_title' => 'Changed again',
170  'rc_cur_id' => 30,
171  'pp_propname' => null,
172  'cat_pages' => 12,
173  'cat_subcats' => 2,
174  'cat_files' => 1,
175  ],
176  (object)[
177  'rc_title' => 'Processed already',
178  'rc_cur_id' => 31,
179  'pp_propname' => null,
180  'cat_pages' => 10,
181  'cat_subcats' => 2,
182  'cat_files' => 1,
183  ],
184  ],
185  [ 31 => true ],
186  ],
187  // TODO: not sure how to test categorization changes, it uses the database select...
188  ];
189  }
190 
197  public function getCategoryLinksIterator( $dbr, array $ids ) {
198  $res = [];
199  foreach ( $ids as $pageid ) {
200  $res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
201  }
202  return $res;
203  }
204 
213  public function testSparqlUpdate( $testFileName, $iterator, $handler, $result,
214  array $preProcessed = [] ) {
215  $dumpScript =
216  $this->getMockBuilder( CategoryChangesAsRdf::class )
217  ->setMethods( [ $iterator, 'getCategoryLinksIterator' ] )
218  ->getMock();
219 
220  $dumpScript->expects( $this->any() )
221  ->method( 'getCategoryLinksIterator' )
222  ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
223 
224  $dumpScript->expects( $this->once() )
225  ->method( $iterator )
226  ->willReturn( [ $result ] );
227 
228  $ref = new ReflectionObject( $dumpScript );
229  $processedProperty = $ref->getProperty( 'processed' );
230  $processedProperty->setAccessible( true );
231  $processedProperty->setValue( $dumpScript, $preProcessed );
232 
233  $output = fopen( "php://memory", "w+b" );
234  $dbr = wfGetDB( DB_REPLICA );
236  $dumpScript->initialize();
237  $dumpScript->getRdf();
238  $dumpScript->$handler( $dbr, $output );
239 
240  rewind( $output );
241  $sparql = stream_get_contents( $output );
242  $this->assertFileContains( $testFileName, $sparql );
243 
244  $processed = $processedProperty->getValue( $dumpScript );
245  $expectedProcessed = array_keys( $preProcessed );
246  foreach ( $result as $row ) {
247  if ( isset( $row->_processed ) ) {
248  $this->assertArrayHasKey( $row->_processed, $processed,
249  "ID {$row->_processed} was not processed!" );
250  $expectedProcessed[] = $row->_processed;
251  }
252  }
253  $this->assertSame( $expectedProcessed, array_keys( $processed ),
254  'Processed array has wrong items' );
255  }
256 
257  public function testUpdateTs() {
258  $dumpScript = new CategoryChangesAsRdf();
259  $dumpScript->initialize();
260  $update = $dumpScript->updateTS( 1503620949 );
261  $outFile = __DIR__ . '/../data/categoriesrdf/updatets.txt';
262  $this->assertFileContains( $outFile, $update );
263  }
264 
265 }
Maintenance script to provide RDF representation of the recent changes in category tree...
const NS_MAIN
Definition: Defines.php:60
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
testSparqlUpdate( $testFileName, $iterator, $handler, $result, array $preProcessed=[])
provideCategoryData
Tests for CategoryChangesAsRdf recent changes exporter.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2217
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1980
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1982
$res
Definition: database.txt:21
getCategoryLinksIterator( $dbr, array $ids)
Mock category links iterator.
const NS_CATEGORY
Definition: Defines.php:74
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
assertFileContains( $fileName, $actualData, $createIfMissing=false, $msg='')
Check whether file contains given data.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
const DB_REPLICA
Definition: defines.php:25