/** * A naive wrapper for \WikiPage::doEdit(). */ public function edit() { $responseData = new \StdClass(); $responseData->success = false; $responseData->title = null; $responseData->text = null; $responseData->summary = null; $responseData->user_id = 0; $responseData->user_name = null; $this->response->setFormat('json'); $this->response->setCacheValidity(\WikiaResponse::CACHE_DISABLED); if ($this->getVal('secret') != $this->wg->TheSchwartzSecretToken || !$this->request->wasPosted()) { $this->response->setVal('data', $responseData); return; } $titleText = $this->getVal('title'); $responseData->title = $titleText; $title = \Title::newFromText($titleText); \Wikia\Util\Assert::true($title instanceof \Title); $article = new \Article($title); \Wikia\Util\Assert::true($article instanceof \Article); $text = $this->getVal('text'); $responseData->text = $text; $summary = $this->getVal('summary'); $responseData->summary = $summary; if ($this->wg->User->isLoggedIn()) { $responseData->user_id = $this->wg->User->getId(); $responseData->user_name = $this->wg->User->getName(); $responseData->success = $article->doEdit($text, $summary)->isOK(); } $this->response->setVal('data', $responseData); }
/** * The general method for handling the communication with the service. */ public function request($resourceName, $getParams = [], $postData = [], $extraRequestOptions = []) { // Crash if we cannot make HTTP requests. \Wikia\Util\Assert::true(\MWHttpRequest::canMakeRequests()); // Add client_id and client_secret to the GET data. $getParams['client_id'] = $this->clientId; $getParams['client_secret'] = $this->clientSecret; // Request URI pre-processing. $uri = "{$this->baseUri}{$resourceName}?" . http_build_query($getParams); // Request options pre-processing. $options = ['method' => 'GET', 'timeout' => 5, 'postData' => $postData, 'noProxy' => true, 'followRedirects' => false, 'returnInstance' => true, 'internalRequest' => true]; $options = array_merge($options, $extraRequestOptions); /* * MediaWiki's MWHttpRequest class heavily relies on Messaging API * (wfMessage()) which happens to rely on the value of $wgLang. * $wgLang is set after $wgUser. On per-request authentication with * an access token we use MWHttpRequest before wgUser is created so * we need $wgLang to be present. With GlobalStateWrapper we can set * the global variable in the local, function's scope, so it is the * same as the already existing $wgContLang. */ global $wgContLang; $wrapper = new GlobalStateWrapper(['wgLang' => $wgContLang]); // Request execution. /** @var \MWHttpRequest $request */ $request = $wrapper->wrap(function () use($options, $uri) { return \Http::request($options['method'], $uri, $options); }); $this->status = $request->status; $output = json_decode($request->getContent()); if (!$output) { throw new ClientException('Invalid response.'); } return $output; }
/** * Check if the given upload directory name is available for use. * * @access public * @author MichaĆ Roszka <*****@*****.**> * * @param $sDirectoryName the path to check */ public static function wgUploadDirectoryExists($sDirectoryName) { wfProfileIn(__METHOD__); $iVarId = WikiFactory::getVarIdByName('wgUploadDirectory'); // Crash immediately if $iVarId is not a positive integer! \Wikia\Util\Assert::true($iVarId); $aCityIds = WikiFactory::getCityIDsFromVarValue($iVarId, $sDirectoryName, '='); wfProfileOut(__METHOD__); return !empty($aCityIds); }
/** * assumes $db has a method named "query", like mysqli or PDO * * @param $db * @param BreakDown $breakDown * @param bool $autoIterate whether we should wrap the logic of iterating through db results for the callback * @param callable $callback * @throws \InvalidArgumentException * @return array|mixed query results */ protected function query($db, Breakdown $breakDown, $autoIterate, callable $callback = null) { if (!method_exists($db, 'query')) { throw new \InvalidArgumentException(); } $sql = $this->injectParams($db, $breakDown); $result = $db->query($sql); if ($callback == null) { return $result; } if ($autoIterate) { \Wikia\Util\Assert::true($result instanceof \ResultWrapper, 'FluentSQL::query - did not get ResultWrapper - instead got ' . get_class($result)); $data = $this->autoIterate($result, $callback); } else { $data = $callback($result); } return $data; }
/** * @expectedException Exception */ public function testFalse() { Assert::true(2 > 10); }
/** * @param mysqli_result $res * @param int $n * @return string */ protected function mysqlFieldName($res, $n) { Assert::true($res instanceof mysqli_result, __METHOD__); $field = $res->fetch_field_direct($n); return $field->name; }