public function build() { $article = $this->Agent->getArticle(); $text = $article->getContent(true); # Strip comments and <nowiki> $text = preg_replace("/<!--.*?-->/s", "", $text); $text = preg_replace("@<nowiki>.*?</nowiki>@s", "", $text); # change template usage to substitution; note that this is WRONG #$tchars = Title::legalChars(); #$text = preg_replace("/(?<!{){{([$tchars]+)(\|.*?)?}}(?!})/", "{{subst:$1$2}}", $text); $parser = new Parser(); # so the magic variables work out right $parser->mOptions = new ParserOptions(); $parser->mTitle = $this->Agent->getTitle(); $parser->mOutputType = OT_WIKI; $parser->initialiseVariables(); $parser->clearState(); $text = $parser->replaceVariables($text); preg_match_all("@<rdf>(.*?)</rdf>@s", $text, $matches, PREG_PATTERN_ORDER); $content = $matches[1]; $rdf = implode(' ', array_values($content)); $model = MwRdf::Model(); if (strlen($rdf) > 0) { $parser->mOutputType = OT_HTML; $rdf = $parser->replaceVariables($rdf); $turtle_parser = MwRdf::Parser('turtle'); $base_uri = $this->Agent->getTitle()->getFullUrl(); $prelude = MwRdf::getNamespacePrelude(); $model->loadStatementsFromString($turtle_parser, $prelude . $rdf); } return $model; }
public function retrieve() { $sm = MwRdf::StoredModel(); $model = MwRdf::Model(); $context = $this->Agent->getContextNode($this->getName()); $stream = librdf_model_context_as_stream($sm->getModel(), $context->getNode()); librdf_model_add_statements($model->getModel(), $stream); librdf_free_stream($stream); $sm->rewind(); return $model; }
public function retrieve() { $base_uri = $this->Agent->getTitle()->getFullUrl(); $query = MwRdf::Query($this->getQueryString(), $base_uri, 'sparql'); $store = MwRdf::StoredModel(); $res = $query->execute($store); $model = MwRdf::Model(); foreach ($res as $statement) { $model->addStatement($statement); } return $model; }
public function testInPageModelTurnsUpInStoredModel() { $this->agent->storeModel('inpage'); $store = MwRdf::StoredModel(); # strip contexts (expensive for larger stores, fine here) $smodel = MwRdf::Model(); foreach ($store as $s) { $smodel->addStatement($s); } $store->rewind(); $model = $this->agent->buildModel('inpage'); $this->assertModelContainsModel($smodel, $model, "The inpage RDF model has not been successfully stored and retrieved."); }
public function build() { $dc = MwRdf::Vocabulary('dc'); $model = MwRdf::Model(); $ar = $this->Agent->titleResource(); $categories = $this->Agent->getTitle()->getParentCategories(); if (is_array($categories)) { foreach (array_keys($categories) as $category) { $catmf = MwRdf::ModelingAgent(Title::newFromText($category)); $model->addStatement(MwRdf::Statement($ar, $dc->subject, $catmf->titleResource())); } } return $model; }
public function build() { $dcterms = MwRdf::Vocabulary('dcterms'); $model = MwRdf::Model(); $tr = $this->Agent->titleResource(); $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select('pagelinks', array('pl_namespace', 'pl_title'), array('pl_from = ' . $this->Agent->getTitle()->getArticleID()), 'MwRdfLinksFrom', array('ORDER BY' => 'pl_namespace, pl_title')); while ($res && ($row = $dbr->fetchObject($res))) { $lt = Title::makeTitle($row->pl_namespace, $row->pl_title); $ltmf = MwRdf::ModelingAgent($lt); $model->addStatement(MwRdf::Statement($tr, $dcterms->references, $ltmf->titleResource())); } $dbr->freeResult($res); return $model; }
public function build() { global $wgRightsUrl; $rdf = MwRdf::Vocabulary('rdf'); $cc = MwRdf::Vocabulary('cc'); $model = MwRdf::Model(); $tr = $this->Agent->titleResource(); $model->addStatement(MwRdf::Statement($tr, $rdf->a, $cc->Work)); if (!isset($wgRightsUrl)) { return $model; } $lr = MwRdf::UriNode($wgRightsUrl); $model->addStatement(MwRdf::Statement($tr, $cc->license, $lr)); $model->addStatement(MwRdf::Statement($lr, $rdf->a, $cc->License)); $terms = MwRdf::GetCcTerms($wgRightsUrl); if (!isset($terms)) { return $model; } foreach ($terms as $term) { switch ($term) { case 're': $model->addStatement(MwRdf::Statement($lr, $cc->permits, $cc->Reproduction)); break; case 'di': $model->addStatement(MwRdf::Statement($lr, $cc->permits, $cc->Distribution)); break; case 'de': $model->addStatement(MwRdf::Statement($lr, $cc->permits, $cc->DerivativeWorks)); break; case 'nc': $model->addStatement(MwRdf::Statement($lr, $cc->prohibits, $cc->CommercialUse)); break; case 'no': $model->addStatement(MwRdf::Statement($lr, $cc->requires, $cc->Notice)); break; case 'by': $model->addStatement(MwRdf::Statement($lr, $cc->requires, $cc->Attribution)); break; case 'sa': $model->addStatement(MwRdf::Statement($lr, $cc->requires, $cc->ShareAlike)); break; case 'sc': $model->addStatement(MwRdf::Statement($lr, $cc->requires, $cc->SourceCode)); break; } } return $model; }
public function build() { global $wgContLang; $dc = MwRdf::Vocabulary('dc'); $dcterms = MwRdf::Vocabulary('dcterms'); $rdfs = MwRdf::Vocabulary('rdfs'); $model = MwRdf::Model(); $tr = $this->Agent->titleResource(); $article = $this->Agent->getArticle(); $text = $article->getContent(true); $parser = new Parser(); $parser->mOptions = new ParserOptions(); $parser->mTitle = $this; $parser->initialiseVariables(); $parser->clearState(); $tags = array('nowiki'); $m = array(); $text = $parser->extractTagsAndParams($tags, $text, $m); # XXX: maybe it would actually be better to do this at another # stage after the parser has already identified interwiki and # lang links # Find prefixed links preg_match_all("/\\[\\[([^|\\]]+:[^|\\]]+)(\\|.*)?\\]\\]/", $text, $m); if (!isset($m[0])) { return $model; } // nothing found so nevermind foreach ($m[1] as $linktext) { $iwlink = Title::newFromText($linktext); if (isset($iwlink)) { $pfx = $iwlink->getInterwiki(); if (strlen($pfx) > 0) { $iwlinkmf = MwRdf::ModelingAgent($iwlink); $iwr = $iwlinkmf->titleResource(); # XXX: Wikitravel uses some 4+ prefixes for sister site links if ($wgContLang->getLanguageName($pfx) && strlen($pfx) < 4) { $model->addStatement(MwRdf::Statement($tr, $dcterms->hasVersion, $iwr)); $model->addStatement(MwRdf::Statement($iwr, $dc->language, MwRdf::Language($pfx))); } else { # XXX: Express the "sister site" relationship better $model->addStatement(MwRdf::Statement($tr, $rdfs->seeAlso, $iwr)); } } } } return $model; }
public function build() { global $wgServer; $dc = MwRdf::Vocabulary('dc'); $dcterms = MwRdf::Vocabulary('dcterms'); $dctype = MwRdf::Vocabulary('dctype'); $model = MwRdf::Model(); $tr = $this->Agent->titleResource(); $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select('imagelinks', array('il_to'), array('il_from = ' . $this->Agent->getTitle()->getArticleID()), 'MwRdfImage'); while ($res && ($row = $dbr->fetchObject($res))) { $img = wfFindFile($row->il_to); if (!$img->exists()) { continue; } $iuri = $img->getURL(); if ($iuri[0] == '/') { $iuri = $wgServer . $iuri; } $ir = MwRdf::UriNode($iuri); $model->addStatement(MwRdf::Statement($tr, $dcterms->hasPart, $ir)); $model->addStatement(MwRdf::Statement($ir, $dc->type, $dctype->Image)); $mt = $img->getMimeType(); if (isset($mt)) { $model->addStatement(MwRdf::Statement($ir, $dc->format, MwRdf::MediaType($mt))); } $hist_line = $img->nextHistoryLine(); if (!isset($hist_line)) { continue; } $creator = MwRdf::PersonToResource($hist_line->img_user, $hist_line->img_user_text, User::whoIsReal($hist_line->img_user)); $model->addStatement(MwRdf::Statement($ir, $dc->creator, $creator)); $model->addStatement(MwRdf::Statement($ir, $dc->date, MwRdf::TimestampResource($hist_line->img_timestamp))); $seen = array($hist_line->img_user => true); while ($hist_line = $img->nextHistoryLine()) { if (isset($seen[$hist_line->img_user])) { continue; } $contributor = MwRdf::PersonToResource($hist_line->img_user, $hist_line->img_user_text, User::whoIsReal($hist_line->img_user)); $model->addStatement(MwRdf::Statement($ir, $dc->contributor, $contributor)); $seen[$hist_line->img_user] = true; } } $dbr->freeResult($res); return $model; }
public function build() { global $wgSitename, $wgContLanguageCode; $article = $this->Agent->getArticle(); $model = MwRdf::Model(); $rdf = MwRdf::Vocabulary('rdf'); $dc = MwRdf::Vocabulary('dc'); $dctype = MwRdf::Vocabulary('dctype'); $artres = $this->Agent->titleResource(); $model->addStatement(MwRdf::Statement($artres, $dc->title, MwRdf::LiteralNode($this->Agent->getTitle()->getText()))); $model->addStatement(MwRdf::Statement($artres, $dc->publisher, MwRdf::PageOrString(wfMsg('aboutpage'), $wgSitename))); $model->addStatement(MwRdf::Statement($artres, $dc->language, MwRdf::Language($wgContLanguageCode))); $model->addStatement(MwRdf::Statement($artres, $dc->type, $dctype->Text)); $model->addStatement(MwRdf::Statement($artres, $dc->format, MwRdf::MediaType('text/html'))); if ($this->Agent->getTimestampResource()) { $model->addStatement(MwRdf::Statement($artres, $dc->date, $this->Agent->getTimestampResource())); } if (MWNamespace::isTalk($this->Agent->getTitle()->getNamespace())) { $model->addStatement(MwRdf::Statement($artres, $dc->subject, $this->Agent->subjectResource())); } else { $talk = MwRdf::ModelingAgent($this->Agent->getTitle()->getTalkPage()); $model->addStatement(MwRdf::Statement($talk->titleResource(), $dc->subject, $artres)); } # 'Creator' is responsible for this version $creator = MwRdf::PersonToResource($article->getUser()); $model->addStatement(MwRdf::Statement($artres, $dc->creator, $creator)); # 'Contributors' are all other version authors $contributors = $article->getContributors(); foreach ($contributors as $user_parts) { $contributor = MwRdf::PersonToResource($user_parts[0], $user_parts[1], $user_parts[2]); $model->addStatement(MwRdf::Statement($artres, $dc->contributor, $contributor)); } # Rights notification global $wgRightsPage, $wgRightsUrl, $wgRightsText; $rights = MwRdf::RightsResource(); if ($rights) { $model->addStatement(MwRdf::Statement($artres, $dc->rights, $rights)); } return $model; }
public function build() { global $wgContLanguageCode; $dc = MwRdf::Vocabulary('dc'); $dcterms = MwRdf::Vocabulary('dcterms'); $model = MwRdf::Model(); $tr = $this->Agent->titleResource(); $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select(array('page', 'revision'), array('rev_id', 'rev_timestamp', 'rev_user', 'rev_user_text'), array('page_namespace = ' . $this->Agent->getTitle()->getNamespace(), 'page_title = ' . $dbr->addQuotes($this->Agent->getTitle()->getDBkey()), 'rev_page = page_id', 'rev_id != page_latest'), 'MwRdfHistory', array('ORDER BY' => 'rev_timestamp DESC')); while ($res && ($row = $dbr->fetchObject($res))) { $url = $this->Agent->getTitle()->getFullURL('oldid=' . $row->rev_id); $ur = MwRdf::UriNode($url); $model->addStatement(MwRdf::Statement($tr, $dcterms->hasVersion, $ur)); $model->addStatement(MwRdf::Statement($ur, $dcterms->isVersionOf, $tr)); $model->addStatement(MwRdf::Statement($ur, $dc->language, MwRdf::Language($wgContLanguageCode))); $realname = $row->rev_user == 0 ? null : User::whoIsReal($row->rev_user); $pr = MwRdf::PersonToResource($row->rev_user, $row->rev_user_text, $realname); $model->addStatement(MwRdf::Statement($ur, $dc->creator, $pr)); $model->addStatement(MwRdf::Statement($ur, $dc->date, MwRdf::TimestampResource($row->rev_timestamp))); } $dbr->freeResult($res); return $model; }
public function testSpecialPageProducesCorrectHistoryModel() { $this->format = 'turtle'; $parser = MwRdf::Parser('turtle'); $this->prepareRequest(null, array('history')); $text = wfRdfSpecialPage(null); $model = MwRdf::Model(); $model->loadStatementsFromString($parser, $text); $expect = MwRdfTest::createExpectedHistoryModel(); $this->assertModelEqualsModel($expect, $model); }
public function retrieveModel($par) { if (gettype($par) == 'string') { $par = array($par); } if (!gettype($par) == 'array') { throw new Exception('retrieveModel takes either a model ' . 'name string or an array of modelnames'); } $model = MwRdf::Model(); foreach ($par as $name) { $mm = $this->ModelMakers[$name]; ### TODO Retrieve from Cache if it's there $sub_model = $mm->retrieve($name); ### TODO Cache the sub_model $stream = librdf_model_as_stream($sub_model->getModel()); librdf_model_add_statements($model->getModel(), $stream); librdf_free_stream($stream); $sub_model->rewind(); } return $model; }
public function testModel() { $m = MwRdf::Model(); $this->assertTrue($m instanceof LibRDF_Model); }