/** * @dataProvider provideHandlers * @param ContentHandler $handler */ public function testMakeEmptyContent(ContentHandler $handler) { $content = $handler->makeEmptyContent(); $this->assertInstanceOf(Content::class, $content); if ($handler instanceof TextContentHandler) { // TextContentHandler::getContentClass() is protected, so bypass // that restriction $testingWrapper = TestingAccessWrapper::newFromObject($handler); $this->assertInstanceOf($testingWrapper->getContentClass(), $content); } $handlerClass = get_class($handler); $contentClass = get_class($content); $this->assertTrue($content->isValid(), "{$handlerClass}::makeEmptyContent() did not return a valid content ({$contentClass}::isValid())"); }
public function getDefinitions() { $groups = MessageGroups::getAllGroups(); $keys = array(); /** * @var $g MessageGroup */ foreach ($groups as $g) { $states = $g->getMessageGroupStates()->getStates(); foreach (array_keys($states) as $state) { $keys["Translate-workflow-state-{$state}"] = $state; } } $defs = TranslateUtils::getContents(array_keys($keys), $this->getNamespace()); foreach ($keys as $key => $state) { if (!isset($defs[$key])) { // @todo Use jobqueue $title = Title::makeTitleSafe($this->getNamespace(), $key); $page = new WikiPage($title); $content = ContentHandler::makeContent($state, $title); $page->doEditContent($content, wfMessage('translate-workflow-autocreated-summary', $state)->inContentLanguage()->text(), 0, false, FuzzyBot::getUser()); } else { // Use the wiki translation as definition if available. // getContents returns array( content, last author ) list($content, ) = $defs[$key]; $keys[$key] = $content; } } return $keys; }
/** * Updates $title with the provided $text * @param Title title * @param string $text */ public static function updatePage($title, $text) { $user = new User(); $page = WikiPage::factory($title); $content = ContentHandler::makeContent($text, $page->getTitle()); $page->doEditContent($content, "summary", 0, false, $user); }
function run() { // Initialization $title = $this->title; list(, $code) = TranslateUtils::figureMessage($title->getPrefixedText()); // Return the actual translation page... $page = TranslatablePage::isTranslationPage($title); if (!$page) { var_dump($this->params); var_dump($title); throw new MWException("Oops, this should not happen!"); } $group = $page->getMessageGroup(); $collection = $group->initCollection($code); $text = $page->getParse()->getTranslationPageText($collection); // Other stuff $user = $this->getUser(); $summary = $this->getSummary(); $flags = $this->getFlags(); $page = WikiPage::factory($title); // @todo FuzzyBot hack PageTranslationHooks::$allowTargetEdit = true; $content = ContentHandler::makeContent($text, $page->getTitle()); $page->doEditContent($content, $summary, $flags, false, $user); PageTranslationHooks::$allowTargetEdit = false; return true; }
/** * @dataProvider dataGetAutosummary * @covers WikitextContentHandler::getAutosummary */ public function testGetAutosummary($old, $new, $flags, $expected) { $oldContent = is_null($old) ? null : new WikitextContent($old); $newContent = is_null($new) ? null : new WikitextContent($new); $summary = $this->handler->getAutosummary($oldContent, $newContent, $flags); $this->assertTrue((bool) preg_match($expected, $summary), "Autosummary didn't match expected pattern {$expected}: {$summary}"); }
public function execute() { global $wgUser; $username = $this->getOption('username', false); if ($username === false) { $user = User::newSystemUser('ScriptImporter', ['steal' => true]); } else { $user = User::newFromName($username); } $wgUser = $user; $baseUrl = $this->getArg(1); $pageList = $this->fetchScriptList(); $this->output('Importing ' . count($pageList) . " pages\n"); foreach ($pageList as $page) { $title = Title::makeTitleSafe(NS_MEDIAWIKI, $page); if (!$title) { $this->error("{$page} is an invalid title; it will not be imported\n"); continue; } $this->output("Importing {$page}\n"); $url = wfAppendQuery($baseUrl, ['action' => 'raw', 'title' => "MediaWiki:{$page}"]); $text = Http::get($url, [], __METHOD__); $wikiPage = WikiPage::factory($title); $content = ContentHandler::makeContent($text, $wikiPage->getTitle()); $wikiPage->doEditContent($content, "Importing from {$url}", 0, false, $user); } }
public function testParsing() { $title = Title::newFromText('MediaWiki:Ugakey/nl'); $page = WikiPage::factory($title); $content = ContentHandler::makeContent('$1 van $2', $title); $status = $page->doEditContent($content, __METHOD__); $value = $status->getValue(); /** * @var Revision $rev */ $rev = $value['revision']; $revision = $rev->getId(); $dbw = wfGetDB(DB_MASTER); $conds = array('rt_page' => $title->getArticleID(), 'rt_type' => RevTag::getType('fuzzy'), 'rt_revision' => $revision); $index = array_keys($conds); $dbw->replace('revtag', array($index), $conds, __METHOD__); $handle = new MessageHandle($title); $this->assertTrue($handle->isValid(), 'Message is known'); $this->assertTrue($handle->isFuzzy(), 'Message is fuzzy after database fuzzying'); // Update the translation without the fuzzy string $content = ContentHandler::makeContent('$1 van $2', $title); $page->doEditContent($content, __METHOD__); $this->assertFalse($handle->isFuzzy(), 'Message is unfuzzy after edit'); $content = ContentHandler::makeContent('!!FUZZY!!$1 van $2', $title); $page->doEditContent($content, __METHOD__); $this->assertTrue($handle->isFuzzy(), 'Message is fuzzy after manual fuzzying'); // Update the translation without the fuzzy string $content = ContentHandler::makeContent('$1 van $2', $title); $page->doEditContent($content, __METHOD__); $this->assertFalse($handle->isFuzzy(), 'Message is unfuzzy after edit'); }
public function testMessage() { $user = new MockSuperUser(); $user->setId(123); $title = Title::newFromText('MediaWiki:translated/fi'); $page = WikiPage::factory($title); $content = ContentHandler::makeContent('pupuliini', $title); $status = $page->doEditContent($content, __METHOD__, 0, false, $user); $value = $status->getValue(); $rev = $value['revision']; $revision = $rev->getId(); $group = MessageGroups::getGroup('test-group'); $collection = $group->initCollection('fi'); $collection->loadTranslations(); /** @var TMessage $translated */ $translated = $collection['translated']; $this->assertInstanceof('TMessage', $translated); $this->assertEquals('translated', $translated->key()); $this->assertEquals('bunny', $translated->definition()); $this->assertEquals('pupuliini', $translated->translation()); $this->assertEquals('SuperUser', $translated->getProperty('last-translator-text')); $this->assertEquals(123, $translated->getProperty('last-translator-id')); $this->assertEquals('translated', $translated->getProperty('status'), 'message status is translated'); $this->assertEquals($revision, $translated->getProperty('revision')); /** @var TMessage $untranslated */ $untranslated = $collection['untranslated']; $this->assertInstanceof('TMessage', $untranslated); $this->assertEquals(null, $untranslated->translation(), 'no translation is null'); $this->assertEquals(false, $untranslated->getProperty('last-translator-text')); $this->assertEquals(false, $untranslated->getProperty('last-translator-id')); $this->assertEquals('untranslated', $untranslated->getProperty('status'), 'message status is untranslated'); $this->assertEquals(false, $untranslated->getProperty('revision')); }
protected function getMessageParameters() { $lang = $this->context->getLanguage(); $params = parent::getMessageParameters(); $params[3] = ContentHandler::getLocalizedName($params[3], $lang); $params[4] = ContentHandler::getLocalizedName($params[4], $lang); return $params; }
function updatePageContent($article, $rev, $baseID, $user) { global $wgHuijiPrefix, $wgSitename; $title = $article->getId() == 1 ? $wgSitename : $article->getTitle()->getText(); $post_data = array('timestamp' => $rev->getTimestamp(), 'content' => ContentHandler::getContentText($rev->getContent(Revision::RAW)), 'sitePrefix' => $wgHuijiPrefix, 'siteName' => $wgSitename, 'id' => $article->getId(), 'title' => $title); $post_data_string = json_encode($post_data); curl_post_json('upsert', $post_data_string); }
/** * @param string $text new page text * * @return int|null */ private function editPageText($text) { $page = WikiPage::factory($this->title); $editResult = $page->doEditContent(ContentHandler::makeContent($text, $this->title), __METHOD__); /** @var Revision $revision */ $revision = $editResult->value['revision']; $this->runJobs(); return $revision->getId(); }
public function isSupportedFormat($format) { // Necessary for backwards-compatability where // the format "json" was used if ($format === 'json') { $format = CONTENT_FORMAT_JSON; } return parent::isSupportedFormat($format); }
/** *Test bug 25702 *Prefixes of API search requests are not handled with case sensitivity and may result *in wrong search results */ public function testPrefixNormalizationSearchBug() { $title = Title::newFromText('Category:Template:xyz'); $page = WikiPage::factory($title); $page->doEditContent(ContentHandler::makeContent('Some text', $page->getTitle()), 'inserting content'); $result = $this->doApiRequest(['action' => 'query', 'list' => 'allpages', 'apnamespace' => NS_CATEGORY, 'apprefix' => 'Template:x']); $this->assertArrayHasKey('query', $result[0]); $this->assertArrayHasKey('allpages', $result[0]['query']); $this->assertNotEquals(0, count($result[0]['query']['allpages']), 'allpages list does not contain page Category:Template:xyz'); }
private function doPatrolledPageEdit(User $user, LinkTarget $target, $content, $summary, User $patrollingUser) { $title = Title::newFromLinkTarget($target); $page = WikiPage::factory($title); $status = $page->doEditContent(ContentHandler::makeContent($content, $title), $summary, 0, false, $user); /** @var Revision $rev */ $rev = $status->value['revision']; $rc = $rev->getRecentChange(); $rc->doMarkPatrolled($patrollingUser, false, []); }
/** * Add or update message contents */ function update($translation, $user) { $savePage = function ($title, $text) { $wikiPage = new WikiPage($title); $content = ContentHandler::makeContent($text, $title); $result = $wikiPage->doEditContent($content, '/* PR admin */', EDIT_FORCE_BOT); return $wikiPage; }; $savePage($this->getTitle(), $translation); }
/** * Checks, if the given title supports the use of SpecialMobileHistory. * * @param Title $title The title to check * @return boolean True, if SpecialMobileHistory can be used, false otherwise */ public static function shouldUseSpecialHistory(Title $title) { $contentHandler = ContentHandler::getForTitle($title); $actionOverrides = $contentHandler->getActionOverrides(); // if history is overwritten, assume, that SpecialMobileHistory can't handle them if (isset($actionOverrides['history'])) { // and return false return false; } return true; }
/** * @return int[] Revision ids */ protected function doEdits() { $title = $this->getTitle(); $page = WikiPage::factory($title); $strings = array("it is a kitten", "two kittens", "three kittens", "four kittens"); $revisions = array(); foreach ($strings as $string) { $content = ContentHandler::makeContent($string, $title); $page->doEditContent($content, 'edit page'); $revisions[] = $page->getLatest(); } return $revisions; }
public function execute() { global $wgUser, $wgTitle; $userName = $this->getOption( 'user', 'Maintenance script' ); $summary = $this->getOption( 'summary', '' ); $minor = $this->hasOption( 'minor' ); $bot = $this->hasOption( 'bot' ); $autoSummary = $this->hasOption( 'autosummary' ); $noRC = $this->hasOption( 'no-rc' ); $wgUser = User::newFromName( $userName ); $context = RequestContext::getMain(); $context->setUser( $wgUser ); if ( !$wgUser ) { $this->error( "Invalid username", true ); } if ( $wgUser->isAnon() ) { $wgUser->addToDatabase(); } $wgTitle = Title::newFromText( $this->getArg() ); if ( !$wgTitle ) { $this->error( "Invalid title", true ); } $context->setTitle( $wgTitle ); $page = WikiPage::factory( $wgTitle ); # Read the text $text = $this->getStdin( Maintenance::STDIN_ALL ); $content = ContentHandler::makeContent( $text, $wgTitle ); # Do the edit $this->output( "Saving... " ); $status = $page->doEditContent( $content, $summary, ( $minor ? EDIT_MINOR : 0 ) | ( $bot ? EDIT_FORCE_BOT : 0 ) | ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) | ( $noRC ? EDIT_SUPPRESS_RC : 0 ) ); if ( $status->isOK() ) { $this->output( "done\n" ); $exit = 0; } else { $this->output( "failed\n" ); $exit = 1; } if ( !$status->isGood() ) { $this->output( $status->getWikiText() . "\n" ); } exit( $exit ); }
/** * Adds a revision to a page, while returning the resuting revision's id * * @param Page $page Page to add the revision to * @param string $text Revisions text * @param string $summary Revisions summary * @param string $model The model ID (defaults to wikitext) * * @throws MWException * @return array */ protected function addRevision(Page $page, $text, $summary, $model = CONTENT_MODEL_WIKITEXT) { $status = $page->doEditContent(ContentHandler::makeContent($text, $page->getTitle(), $model), $summary); if ($status->isGood()) { $value = $status->getValue(); $revision = $value['revision']; $revision_id = $revision->getId(); $text_id = $revision->getTextId(); if ($revision_id > 0 && $text_id > 0) { return [$revision_id, $text_id]; } } throw new MWException("Could not determine revision id (" . $status->getWikiText(false, false, 'en') . ")"); }
/** * Helper function for addDBData -- adds a simple page to the database * * @param string $title Title of page to be created * @param string $lang Language and content of the created page * @param string|null $content Content of the created page, or null for a generic string */ protected function makePage($title, $lang, $content = null) { global $wgContLang; if ($content === null) { $content = $lang; } if ($lang !== $wgContLang->getCode()) { $title = "{$title}/{$lang}"; } $title = Title::newFromText($title, NS_MEDIAWIKI); $wikiPage = new WikiPage($title); $contentHandler = ContentHandler::makeContent($content, $title); $wikiPage->doEditContent($contentHandler, "{$lang} translation test case"); }
/** * Adds a revision to a page, while returning the resuting revision's id * * @param $page WikiPage: page to add the revision to * @param $text string: revisions text * @param $text string: revisions summare * * @throws MWExcepion */ protected function addRevision(Page $page, $text, $summary) { $status = $page->doEditContent(ContentHandler::makeContent($text, $page->getTitle()), $summary); if ($status->isGood()) { $value = $status->getValue(); $revision = $value['revision']; $revision_id = $revision->getId(); $text_id = $revision->getTextId(); if ($revision_id > 0 && $text_id > 0) { return array($revision_id, $text_id); } } throw new MWException("Could not determine revision id (" . $status->getWikiText() . ")"); }
public function execute() { global $wgUser; $userName = $this->getOption('user', false); $summary = $this->getOption('summary', ''); $minor = $this->hasOption('minor'); $bot = $this->hasOption('bot'); $autoSummary = $this->hasOption('autosummary'); $noRC = $this->hasOption('no-rc'); if ($userName === false) { $wgUser = User::newSystemUser('Maintenance script', array('steal' => true)); } else { $wgUser = User::newFromName($userName); } if (!$wgUser) { $this->error("Invalid username", true); } if ($wgUser->isAnon()) { $wgUser->addToDatabase(); } $title = Title::newFromText($this->getArg()); if (!$title) { $this->error("Invalid title", true); } if ($this->hasOption('nocreate') && !$title->exists()) { $this->error("Page does not exist", true); } elseif ($this->hasOption('createonly') && $title->exists()) { $this->error("Page already exists", true); } $page = WikiPage::factory($title); # Read the text $text = $this->getStdin(Maintenance::STDIN_ALL); $content = ContentHandler::makeContent($text, $title); # Do the edit $this->output("Saving... "); $status = $page->doEditContent($content, $summary, ($minor ? EDIT_MINOR : 0) | ($bot ? EDIT_FORCE_BOT : 0) | ($autoSummary ? EDIT_AUTOSUMMARY : 0) | ($noRC ? EDIT_SUPPRESS_RC : 0)); if ($status->isOK()) { $this->output("done\n"); $exit = 0; } else { $this->output("failed\n"); $exit = 1; } if (!$status->isGood()) { $this->output($status->getWikiText() . "\n"); } exit($exit); }
/** * @param Page $page * @param ParserOptions $parserOptions ParserOptions to use for the parse * @param int $revid ID of the revision being parsed. * @param bool $useParserCache Whether to use the parser cache. * operation. * @param Content|string $content Content to parse or null to load it; may * also be given as a wikitext string, for BC. */ public function __construct(Page $page, ParserOptions $parserOptions, $revid, $useParserCache, $content = null) { if (is_string($content)) { // BC: old style call $modelId = $page->getRevision()->getContentModel(); $format = $page->getRevision()->getContentFormat(); $content = ContentHandler::makeContent($content, $page->getTitle(), $modelId, $format); } $this->page = $page; $this->revid = $revid; $this->cacheable = $useParserCache; $this->parserOptions = $parserOptions; $this->content = $content; $this->cacheKey = ParserCache::singleton()->getKey($page, $parserOptions); parent::__construct('ArticleView', $this->cacheKey . ':revid:' . $revid); }
public function execute() { $out = $this->mSpecial->getOutput(); $dbr = wfGetDB(DB_SLAVE); $row = $dbr->selectRow('moderation', array('mod_namespace AS namespace', 'mod_title AS title', 'mod_text AS text'), array('mod_id' => $this->id), __METHOD__); if (!$row) { throw new ModerationError('moderation-edit-not-found'); } $title = Title::makeTitle($row->namespace, $row->title); $popts = $out->parserOptions(); $popts->setEditSection(false); $content = ContentHandler::makeContent($row->text, null, $title->getContentModel()); $pout = $content->getParserOutput($title, 0, $popts, true); $out->setPageTitle(wfMessage('moderation-preview-title', $title->getPrefixedText())); $out->addParserOutput($pout); }
/** * @group medium */ public function testContentComesWithContentModelAndFormat() { $pageName = 'Help:' . __METHOD__; $title = Title::newFromText($pageName); $page = WikiPage::factory($title); $page->doEditContent(ContentHandler::makeContent('Some text', $page->getTitle()), 'inserting content'); $apiResult = $this->doApiRequest(['action' => 'query', 'prop' => 'revisions', 'titles' => $pageName, 'rvprop' => 'content']); $this->assertArrayHasKey('query', $apiResult[0]); $this->assertArrayHasKey('pages', $apiResult[0]['query']); foreach ($apiResult[0]['query']['pages'] as $page) { $this->assertArrayHasKey('revisions', $page); foreach ($page['revisions'] as $revision) { $this->assertArrayHasKey('contentformat', $revision, 'contentformat should be included when asking content so client knows how to interpret it'); $this->assertArrayHasKey('contentmodel', $revision, 'contentmodel should be included when asking content so client knows how to interpret it'); } } }
static function newFromTitle($title) { // see if we already have this as our main instance... if (self::$instance && self::$instance->mTitle == $title) { return self::$instance; } $wikiPage = new WikiPage($title); if (!$wikiPage) { return null; } $whow = new WikihowArticleEditor(); $whow->mTitleObj = $wikiPage->getTitle(); $whow->mWikiPage = $wikiPage; $whow->mTitle = $wikiPage->getTitle()->getText(); $text = ContentHandler::getContentText($wikiPage->getContent(Revision::RAW)); $whow->loadFromText($text); return $whow; }
public function testTranslationPageRestrictions() { $superUser = new MockSuperUser(); $title = Title::newFromText('Translatable page'); $page = WikiPage::factory($title); $content = ContentHandler::makeContent('<translate>Hello</translate>', $title); $status = $page->doEditContent($content, 'New page', 0, false, $superUser); $revision = $status->value['revision']->getId(); $translatablePage = TranslatablePage::newFromRevision($title, $revision); $translatablePage->addMarkedTag($revision); MessageGroups::singleton()->recache(); $translationPage = Title::newFromText('Translatable page/fi'); TranslateRenderJob::newJob($translationPage)->run(); $this->assertTrue($translationPage->userCan('read', $superUser), 'Users can read existing translation pages'); $this->assertFalse($translationPage->userCan('edit', $superUser), 'Users can not edit existing translation pages'); $translationPage = Title::newFromText('Translatable page/ab'); $this->assertTrue($translationPage->userCan('read', $superUser), 'Users can read non-existing translation pages'); $this->assertFalse($translationPage->userCan('edit', $superUser), 'Users can not edit non-existing translation pages'); }
public function testPreventCategorization() { $user = new MockSuperUser(); $title = Title::makeTitle(NS_MEDIAWIKI, 'ugakey1/fi'); $wikipage = WikiPage::factory($title); $content = ContentHandler::makeContent('[[Category:Shouldnotbe]]', $title); $wikipage->doEditContent($content, __METHOD__, 0, false, $user); $this->assertEquals(array(), $title->getParentCategories(), 'translation of known message'); $title = Title::makeTitle(NS_MEDIAWIKI, 'ugakey2/qqq'); $wikipage = WikiPage::factory($title); $content = ContentHandler::makeContent('[[Category:Shouldbe]]', $title); $wikipage->doEditContent($content, __METHOD__, 0, false, $user); $this->assertEquals(array('Category:Shouldbe' => 'MediaWiki:ugakey2/qqq'), $title->getParentCategories(), 'message docs'); $title = Title::makeTitle(NS_MEDIAWIKI, 'ugakey3/no'); $wikipage = WikiPage::factory($title); $content = ContentHandler::makeContent('[[Category:Shouldbealso]]', $title); $wikipage->doEditContent($content, __METHOD__, 0, false, $user); $this->assertEquals(array(), $title->getParentCategories(), 'unknown message'); }
/** @brief Intercept image uploads and queue them for moderation. */ public static function onUploadVerifyFile($upload, $mime, &$status) { global $wgRequest, $wgUser, $wgOut; if (ModerationCanSkip::canSkip($wgUser)) { return; } $result = $upload->validateName(); if ($result !== true) { $status = array($upload->getVerificationErrorCode($result['status'])); return; } $special = new ModerationSpecialUpload($wgRequest); $special->publicLoadRequest(); $title = $upload->getTitle(); $model = $title->getContentModel(); try { $file = $upload->stashFile($wgUser); } catch (MWException $e) { $status = array("api-error-stashfailed"); return; } $key = $file->getFileKey(); $pageText = ''; if (!$special->mForReUpload) { $pageText = $special->getInitialPageText($special->mComment, $special->mLicense, $special->mCopyrightStatus, $special->mCopyrightSource); } $content = ContentHandler::makeContent($pageText, null, $model); /* Step 1. Create a page in File namespace (it will be queued for moderation) */ $page = new WikiPage($title); $status = $page->doEditContent($content, $special->mComment, 0, $title->getLatestRevID(), $wgUser); $wgOut->redirect(''); # Disable redirection after doEditContent() /* Step 2. Populate mod_stash_key field in newly inserted row of the moderation table (to indicate that this is an upload, not just editing the text on image page) */ $dbw = wfGetDB(DB_MASTER); $dbw->update('moderation', array('mod_stash_key' => $key), array('mod_id' => ModerationEditHooks::$LastInsertId), __METHOD__); $status = array("moderation-image-queued"); }
public function execute() { $user = User::newFromName($this->getOption('user')); if (!$user->getId()) { $this->error("No such user exists.", 1); } $count = $this->getOption('count'); $namespace = (int) $this->getOption('namespace', 0); for ($i = 0; $i < $count; ++$i) { $title = Title::makeTitleSafe($namespace, "Page " . wfRandomString(2)); $page = WikiPage::factory($title); $content = ContentHandler::makeContent(wfRandomString(), $title); $summary = "Change " . wfRandomString(6); $page->doEditContent($content, $summary, 0, false, $user); $this->output("Edited {$title}\n"); if ($i && $i % $this->mBatchSize == 0) { wfWaitForSlaves(); } } $this->output("Done\n"); }