MediaWiki  1.33.0
PagePropsTest.php
Go to the documentation of this file.
1 <?php
2 
13 
17  private $title1;
18 
22  private $title2;
23 
27  private $the_properties;
28 
29  protected function setUp() {
30  parent::setUp();
31 
32  $this->setMwGlobals( [
33  'wgExtraNamespaces' => [
34  12312 => 'Dummy',
35  12313 => 'Dummy_talk',
36  ],
37  'wgNamespaceContentModels' => [ 12312 => 'DUMMY' ],
38  ] );
39 
41  'wgContentHandlers',
42  [ 'DUMMY' => 'DummyContentHandlerForTesting' ]
43  );
44 
45  if ( !$this->the_properties ) {
46  $this->the_properties = [
47  "property1" => "value1",
48  "property2" => "value2",
49  "property3" => "value3",
50  "property4" => "value4"
51  ];
52  }
53 
54  if ( !$this->title1 ) {
55  $page = $this->createPage(
56  'PagePropsTest_page_1',
57  "just a dummy page",
59  );
60  $this->title1 = $page->getTitle();
61  $page1ID = $this->title1->getArticleID();
62  $this->setProperties( $page1ID, $this->the_properties );
63  }
64 
65  if ( !$this->title2 ) {
66  $page = $this->createPage(
67  'PagePropsTest_page_2',
68  "just a dummy page",
70  );
71  $this->title2 = $page->getTitle();
72  $page2ID = $this->title2->getArticleID();
73  $this->setProperties( $page2ID, $this->the_properties );
74  }
75  }
76 
81  public function testGetSingleProperty() {
82  $pageProps = PageProps::getInstance();
83  $page1ID = $this->title1->getArticleID();
84  $result = $pageProps->getProperties( $this->title1, "property1" );
85  $this->assertArrayHasKey( $page1ID, $result, "Found property" );
86  $this->assertEquals( $result[$page1ID], "value1", "Get property" );
87  }
88 
94  $pageProps = PageProps::getInstance();
95  $page1ID = $this->title1->getArticleID();
96  $page2ID = $this->title2->getArticleID();
97  $titles = [
100  ];
101  $result = $pageProps->getProperties( $titles, "property1" );
102  $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
103  $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
104  $this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
105  $this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
106  }
107 
113  $pageProps = PageProps::getInstance();
114  $page1ID = $this->title1->getArticleID();
115  $page2ID = $this->title2->getArticleID();
116  $titles = [
119  ];
120  $properties = [
121  "property1",
122  "property2"
123  ];
124  $result = $pageProps->getProperties( $titles, $properties );
125  $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
126  $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
127  $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
128  $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
129  $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
130  $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
131  $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
132  $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
133  $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
134  $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
135  }
136 
148  public function testGetAllProperties() {
149  $pageProps = PageProps::getInstance();
150  $page1ID = $this->title1->getArticleID();
151  $result = $pageProps->getAllProperties( $this->title1 );
152  $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
153  $properties = $result[$page1ID];
154  $patched = array_replace_recursive( $properties, $this->the_properties );
155  $this->assertEquals( $patched, $properties, "Get all properties" );
156  }
157 
163  $pageProps = PageProps::getInstance();
164  $page1ID = $this->title1->getArticleID();
165  $page2ID = $this->title2->getArticleID();
166  $titles = [
169  ];
170  $result = $pageProps->getAllProperties( $titles );
171  $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
172  $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
173  $properties1 = $result[$page1ID];
174  $patched = array_replace_recursive( $properties1, $this->the_properties );
175  $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
176  $properties2 = $result[$page2ID];
177  $patched = array_replace_recursive( $properties2, $this->the_properties );
178  $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
179  }
180 
187  public function testSingleCache() {
188  $pageProps = PageProps::getInstance();
189  $page1ID = $this->title1->getArticleID();
190  $value1 = $pageProps->getProperties( $this->title1, "property1" );
191  $this->setProperty( $page1ID, "property1", "another value" );
192  $value2 = $pageProps->getProperties( $this->title1, "property1" );
193  $this->assertEquals( $value1, $value2, "Single cache" );
194  }
195 
202  public function testMultiCache() {
203  $pageProps = PageProps::getInstance();
204  $page1ID = $this->title1->getArticleID();
205  $properties1 = $pageProps->getAllProperties( $this->title1 );
206  $this->setProperty( $page1ID, "property1", "another value" );
207  $properties2 = $pageProps->getAllProperties( $this->title1 );
208  $this->assertEquals( $properties1, $properties2, "Multi Cache" );
209  }
210 
219  public function testClearCache() {
220  $pageProps = PageProps::getInstance();
221  $page1ID = $this->title1->getArticleID();
222  $pageProps->getProperties( $this->title1, "property1" );
223  $new_value = "another value";
224  $this->setProperty( $page1ID, "property1", $new_value );
225  $pageProps->getAllProperties( $this->title1 );
226  $result = $pageProps->getProperties( $this->title1, "property1" );
227  $this->assertArrayHasKey( $page1ID, $result, "Found property" );
228  $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
229  }
230 
231  protected function createPage( $page, $text, $model = null ) {
232  if ( is_string( $page ) ) {
233  if ( !preg_match( '/:/', $page ) &&
234  ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
235  ) {
236  $ns = $this->getDefaultWikitextNS();
237  $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
238  }
239 
240  $page = Title::newFromText( $page );
241  }
242 
243  if ( $page instanceof Title ) {
244  $page = new WikiPage( $page );
245  }
246 
247  if ( $page->exists() ) {
248  $page->doDeleteArticle( "done" );
249  }
250 
251  $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
252  $page->doEditContent( $content, "testing", EDIT_NEW );
253 
254  return $page;
255  }
256 
257  protected function setProperties( $pageID, $properties ) {
258  $rows = [];
259 
260  foreach ( $properties as $propertyName => $propertyValue ) {
261  $row = [
262  'pp_page' => $pageID,
263  'pp_propname' => $propertyName,
264  'pp_value' => $propertyValue
265  ];
266 
267  $rows[] = $row;
268  }
269 
270  $dbw = wfGetDB( DB_MASTER );
271  $dbw->replace(
272  'page_props',
273  [
274  [
275  'pp_page',
276  'pp_propname'
277  ]
278  ],
279  $rows,
280  __METHOD__
281  );
282  }
283 
284  protected function setProperty( $pageID, $propertyName, $propertyValue ) {
285  $properties = [];
286  $properties[$propertyName] = $propertyValue;
287 
288  $this->setProperties( $pageID, $properties );
289  }
290 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:904
PagePropsTest\testGetAllProperties
testGetAllProperties()
Test getting all properties from a single page.
Definition: PagePropsTest.php:148
PagePropsTest\$title1
$title1
Definition: PagePropsTest.php:17
PagePropsTest\testClearCache
testClearCache()
Test that getting all properties clears the single properties that have been cached by getting a prop...
Definition: PagePropsTest.php:219
$result
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:1983
PagePropsTest\testSingleCache
testSingleCache()
Test caching when retrieving single properties by getting a property, saving a new value for the prop...
Definition: PagePropsTest.php:187
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:45
PagePropsTest\$the_properties
$the_properties
Definition: PagePropsTest.php:27
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
PagePropsTest\testGetSinglePropertyMultiplePages
testGetSinglePropertyMultiplePages()
Test getting a single property from multiple pages.
Definition: PagePropsTest.php:93
PagePropsTest\setProperty
setProperty( $pageID, $propertyName, $propertyValue)
Definition: PagePropsTest.php:284
php
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
PagePropsTest\setUp
setUp()
Definition: PagePropsTest.php:29
$titles
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition: linkcache.txt:17
PagePropsTest\testGetMultiplePropertiesMultiplePages
testGetMultiplePropertiesMultiplePages()
Test getting multiple properties from multiple pages.
Definition: PagePropsTest.php:112
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
PageProps\getInstance
static getInstance()
Definition: PageProps.php:66
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
PagePropsTest\testGetAllPropertiesMultiplePages
testGetAllPropertiesMultiplePages()
Test getting all properties from multiple pages.
Definition: PagePropsTest.php:162
PagePropsTest
PageProps.
Definition: PagePropsTest.php:12
MediaWikiTestCase\getDefaultWikitextNS
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Definition: MediaWikiTestCase.php:2211
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:133
PagePropsTest\setProperties
setProperties( $pageID, $properties)
Definition: PagePropsTest.php:257
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
PagePropsTest\$title2
$title2
Definition: PagePropsTest.php:22
EDIT_NEW
const EDIT_NEW
Definition: Defines.php:152
Title
Represents a title within MediaWiki.
Definition: Title.php:40
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2636
as
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
$content
$content
Definition: pageupdater.txt:72
PagePropsTest\testMultiCache
testMultiCache()
Test caching when retrieving all properties by getting all properties, saving a new value for a prope...
Definition: PagePropsTest.php:202
MWNamespace\getCanonicalName
static getCanonicalName( $index)
Returns the canonical (English) name for a given index.
Definition: MWNamespace.php:256
PagePropsTest\createPage
createPage( $page, $text, $model=null)
Definition: PagePropsTest.php:231
PagePropsTest\testGetSingleProperty
testGetSingleProperty()
Test getting a single property from a single page.
Definition: PagePropsTest.php:81