public static function updateImageLink($tag, $oldTitle, $newTitle, $caption, &$text, &$textChanged) { // TODO if you allow renaming images you must find a way to preserve caption and primary attributes here $old = "<image id=\"I(\\d+)\" filename=\"" . StructuredData::protectRegexSearch(StructuredData::escapeXml($oldTitle)) . "\".*?/>\n"; $matches = array(); $id = 0; $oldFound = false; if (preg_match('$' . $old . '$', $text, $matches)) { $id = (int) $matches[1]; $oldFound = true; } else { $old = "</{$tag}>"; } if ($newTitle) { $newTitle = StructuredData::escapeXml($newTitle); // get the last image number in the text if ($id == 0) { $id = ESINHandler::getLastImageId($text) + 1; } if ($caption) { $caption = " caption=\"{$caption}\""; } $new = "<image id=\"I{$id}\" filename=\"{$newTitle}\"{$caption}/>\n"; } else { $new = ''; } if (!$oldFound) { $new .= $old; } $result = preg_replace('$' . $old . '$', StructuredData::protectRegexReplace($new), $text); if ($result != $text) { $text = $result; $textChanged = true; } }
public static function updatePersonLink($tag, $oldTitle, $newTitle, &$pd, $familyTag, &$text, &$textChanged) { if ($newTitle) { $new = Person::getPropagatedElement($tag, $newTitle, $pd, $familyTag); } else { $new = ''; } $old = "<{$tag}[^>]*? title=\"" . StructuredData::protectRegexSearch(StructuredData::escapeXml($oldTitle)) . "\".*?/>\n"; $found = false; if (preg_match('$' . $old . '$', $text)) { $found = true; } else { if ($tag == 'husband' || $tag == 'wife') { // check the other tag just in case $old = '<' . ($tag == 'husband' ? 'wife' : 'husband') . '[^>]*? title="' . StructuredData::protectRegexSearch(StructuredData::escapeXml($oldTitle)) . "\".*?/>\n"; if (preg_match('$' . $old . '$', $text)) { $found = true; } } } if (!$found) { $old = ESINHandler::findRelationshipInsertionPointTag($tag, $text); $new .= $old; } // limit to 1 replacement, sine there could be multiple <child tags and we don't want to insert the new child more than once $result = preg_replace('$' . $old . '$', StructuredData::protectRegexReplace($new), $text, 1); // keep children sorted if ($tag == 'child') { $childBegin = mb_strpos($result, '<child '); if ($childBegin !== false) { $childEnd = $childBegin; while (mb_substr($result, $childEnd, strlen('<child ')) == '<child ') { $childEnd = mb_strpos($result, "/>\n", $childEnd) + strlen("/>\n"); } $children = mb_substr($result, $childBegin, $childEnd - $childBegin); ESINHandler::sortChildren($children); $result = mb_substr($result, 0, $childBegin) . $children . mb_substr($result, $childEnd); } } if ($result != $text) { $text = $result; $textChanged = true; } }
public static function removeDuplicateLinks($tags, &$text) { $seenLinks = array(); $found = false; if (preg_match_all("\$<({$tags})[^>]*? title=\"(.+?)\"[^>]*>\n\$", $text, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $key = $match[1] . '|' . $match[2]; if (@$seenLinks[$key]) { $seenLinks[$key]++; $found = true; } else { $seenLinks[$key] = 1; } } } if ($found) { foreach ($seenLinks as $key => $count) { if ($count > 1) { $fields = explode('|', $key); $text = preg_replace("\$<{$fields[0]}[^>]*? title=\"" . StructuredData::protectRegexSearch($fields[1]) . "\"[^>]*>\n\$", '', $text, $count - 1); } } } return $found; }
public static function updateFamilyLink($tag, $oldTitle, $newTitle, &$text, &$textChanged) { if ($newTitle) { $new = Family::getPropagatedElement($tag, $newTitle, $pd); } else { $new = ''; } $old = "<{$tag} title=\"" . StructuredData::protectRegexSearch(StructuredData::escapeXml($oldTitle)) . "\"/>\n"; if (!preg_match('$' . $old . '$', $text)) { $old = ESINHandler::findRelationshipInsertionPointTag($tag, $text); $new .= $old; } $result = preg_replace('$' . $old . '$', StructuredData::protectRegexReplace($new), $text, 1); if ($result != $text) { $text = $result; $textChanged = true; } }
/** * Run a refreshLinks job * @return boolean success */ function run() { global $wgTitle, $wgUser, $wgLang; $wgTitle = $this->title; // FakeTitle (the default) generates errors when accessed, and sometimes I log wgTitle, so set it to something else $wgUser = User::newFromName('WeRelate agent'); // set the user $oldTitle = Title::newFromText($this->params['old_title'], NS_PLACE); $newTitle = Title::newFromText($this->params['new_title'], NS_PLACE); $oldPlace = '<place>' . StructuredData::protectRegexSearch(StructuredData::escapeXml($oldTitle->getText())) . '((\\|[^<]*)?)</place>'; $newPlace = '<place>' . StructuredData::protectRegexReplace(StructuredData::escapeXml($newTitle->getText())) . '\\1</place>'; // get wlh pages $dbr =& wfGetDB(DB_SLAVE); $plConds = array('page_id=pl_from', 'pl_namespace' => $oldTitle->getNamespace(), 'pl_title' => $oldTitle->getDBkey(), 'page_is_redirect' => 0); $fields = array('page_namespace', 'page_title'); $plRes = $dbr->select(array('pagelinks', 'page'), $fields, $plConds, 'PlaceRedirectJob'); // for each wlh page in a target namespace while ($row = $dbr->fetchObject($plRes)) { if ($row->page_namespace == NS_SOURCE) { // update place title in page contents $article = StructuredData::getArticle(Title::newFromText($row->page_title, $row->page_namespace), true); $content =& $article->fetchContent(); $updatedContent = preg_replace('$' . $oldPlace . '$', $newPlace, $content); // save page if ($content != $updatedContent) { $article->doEdit($updatedContent, 'Redirect place', PROPAGATE_EDIT_FLAGS); } } } $dbr->freeResult($plRes); return true; }
/** * Create edit fields from xml property */ protected function toEditFields(&$textbox1) { global $wgStylePath, $wgScriptPath, $wgOut, $wgUser, $wgRequest; if (!$this->title->exists()) { return '<p><font color=red>Image does not exist. You must</font> <a href="/wiki/Special:Upload"><b>upload it.</b></a></p>'; } // add javascript functions $wgOut->addScript("<script type='text/javascript' src=\"{$wgScriptPath}/autocomplete.10.js\"></script>"); if ($wgRequest->getVal('action') != 'submit') { $wgOut->addScript("<script type='text/javascript' src=\"{$wgScriptPath}/fnclientwiki.yui.1.js\"></script>"); } $wgOut->addScript("<script type='text/javascript' src=\"{$wgScriptPath}/image.1.js\"></script>"); $wgOut->addScript("<script type='text/javascript' src=\"{$wgStylePath}/common/upload.2.js\"></script>"); $date = ''; $place = ''; $copyright = ''; $license = ''; $notes = ''; $invalidStyle = ' style="background-color:#fdd;"'; $licenseStyle = ''; $people = array(); $families = array(); if (!isset($this->xml)) { $this->addWhatLinksHere($people, $families); } else { $date = htmlspecialchars((string) $this->xml->date); $place = htmlspecialchars((string) $this->xml->place); $people = StructuredData::getTitlesAsArray($this->xml->person); $families = StructuredData::getTitlesAsArray($this->xml->family); $copyright = htmlspecialchars((string) $this->xml->copyright_holder); $license = (string) $this->xml->license; if (isset($this->xml->note)) { foreach ($this->xml->note as $note) { $title = htmlspecialchars((string) $note['title']); $content = htmlspecialchars((string) $note); $notes .= '<note left="' . (string) $note['left'] . '" top="' . (string) $note['top'] . '" right="' . (string) $note['right'] . '" bottom="' . (string) $note['bottom'] . '" title="' . $title . "\">{$content}</note>\n"; } } } // get license options $licenses = new Licenses(); $licenseLabel = wfMsgHtml('license'); $sk = $wgUser->getSkin(); $licenseHelpUrl = $sk->makeInternalOrExternalUrl(wfMsgForContent('licensehelppage')); $licenseHelp = '<a target="helpwindow" href="' . $licenseHelpUrl . '">' . htmlspecialchars(wfMsg('licensehelp')) . '</a>'; $nolicense = wfMsgHtml('nolicense'); $licenseshtml = $licenses->getHTML(); if ($license) { $licenseshtml = preg_replace('$value="' . StructuredData::protectRegexSearch($license) . '"$', 'value="' . StructuredData::protectRegexReplace($license) . '" selected="selected"', $licenseshtml); } $result = ''; // add fotonotes $fn = new Fotonotes($this->title); $result .= $fn->renderEditableImage(isset($this->xml) ? $this->xml->note : null) . '<br>'; // error messages if (!SDImage::isValidLicense($license)) { $licenseStyle = $invalidStyle; $result .= "<p><font color=red>You must select a license</font></p>"; } // add fields $personTbl = $this->addPageInput($people, 'person'); $familyTbl = $this->addPageInput($families, 'family'); $notes = htmlspecialchars($notes); $result .= <<<END <input type="hidden" id="notesField" name="notes" value="{$notes}"/> <table border=0> <tr><td> </td><td><b>License and copyright</b></td></tr> <tr><td align='right'><label for='wpLicense'>{$licenseLabel} ( {$licenseHelp} ):</label></td> \t<td align='left'><select name='license' id='wpLicense' tabindex='1' onchange='licenseSelectorCheck()'> \t\t<option value=''>{$nolicense}</option>{$licenseshtml}</select></td></tr> <tr><td align='right'><label for="copyright_holder">Name of copyright holder:</label></td><td align='left'><input tabindex="1" name="copyright_holder" value="{$copyright}" size="30"/></td></tr> <tr><td> </td><td> </td></tr> <tr><td> </td><td><b>Time place and people</td></tr> <tr><td align='right'><label for="date">Image date:</label></td><td align='left'><input tabindex="1" name="date" value="{$date}" size="15"/></td></tr> <tr><td align='right'><label for="place">Place:</label></td><td align='left'><input class="place_input" tabindex="1" name="place" value="{$place}" size="30"/></td></tr> <tr><td align='right' valign='top'>Person page:</td><td align='left'>{$personTbl}</td></tr> <tr><td> </td><td align='left'><a id='person_link' href='javascript:void(0)' onClick='addImagePage("person"); return preventDefaultAction(event);'>Add another person</a></td></tr> <tr><td align='right' valign='top'>Family page:</td><td align='left'>{$familyTbl}</td></tr> <tr><td> </td><td align='left'><a id='family_link' href='javascript:void(0)' onClick='addImagePage("family"); return preventDefaultAction(event);'>Add another family</a></td></tr> </table><h2>Text</h2> END; return $result; }